Skip to content

Commit

Permalink
Add specs to day1 as a test for allowing TDD
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwi committed Dec 11, 2018
1 parent 104720e commit 392f7eb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
8 changes: 5 additions & 3 deletions day1/day1.cr
Expand Up @@ -48,6 +48,8 @@ class Day1
end
end

day1 = Day1.new(File.read("input.txt"))
puts "1A: #{day1.part_a}"
puts "1B: #{day1.part_b}"
unless PROGRAM_NAME.includes?("crystal-run-spec")
day1 = Day1.new(File.read("input.txt"))
puts "1A: #{day1.part_a}"
puts "1B: #{day1.part_b}"
end
33 changes: 33 additions & 0 deletions day1/spec/day1_spec.cr
@@ -0,0 +1,33 @@
require "../day1"
require "spec"

describe Day1 do
describe "#part_a" do
it "works correctly for sample input" do
input = <<-SAMPLE
+1
-2
+3
+1
SAMPLE
day1 = Day1.new(input)

day1.part_a.should eq 3
end
end

describe "#part_b" do
it "works correctly for sample input" do
sample_inputs_with_expected_outputs = {
"+1, -1" => 0,
"+3, +3, +4, -2, -4" => 10,
"-6, +3, +8, +5, -6" => 5,
"+7, +7, -2, -7, -4" => 14
}
sample_inputs_with_expected_outputs.each do |input_str, expected_output|
day1 = Day1.new(input_str.gsub(", ", "\n"))
day1.part_b.should eq expected_output
end
end
end
end

0 comments on commit 392f7eb

Please sign in to comment.