From 392f7ebb1dcc1bc490633928b2430def39480bc8 Mon Sep 17 00:00:00 2001 From: spencerwi Date: Tue, 11 Dec 2018 10:50:48 -0500 Subject: [PATCH] Add specs to day1 as a test for allowing TDD --- day1/day1.cr | 8 +++++--- day1/spec/day1_spec.cr | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 day1/spec/day1_spec.cr diff --git a/day1/day1.cr b/day1/day1.cr index 1958370..d209971 100644 --- a/day1/day1.cr +++ b/day1/day1.cr @@ -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 diff --git a/day1/spec/day1_spec.cr b/day1/spec/day1_spec.cr new file mode 100644 index 0000000..ad9acf8 --- /dev/null +++ b/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