diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..deb52f2 --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = true + t.test_files = FileList['specs/*_spec.rb'] +end + +task default: :test diff --git a/clock.rb b/clock.rb deleted file mode 100644 index 01e8856..0000000 --- a/clock.rb +++ /dev/null @@ -1 +0,0 @@ -# clock.rb diff --git a/clock_spec.rb b/clock_spec.rb deleted file mode 100644 index f5b114b..0000000 --- a/clock_spec.rb +++ /dev/null @@ -1 +0,0 @@ -# clock_spec.rb diff --git a/lib/clock.rb b/lib/clock.rb new file mode 100644 index 0000000..165a2e5 --- /dev/null +++ b/lib/clock.rb @@ -0,0 +1,36 @@ +# clock.rb + +def clock(hours, minutes, seconds) + inputs = [hours, min, sec] + max_inputs = [ 23, 59, 59] + 3.times do |index| + if inputs[index] > max_inputs[index] + raise ArgumentError, "#{inputs[index]} is too large" + elsif inputs[index] < 0 + raise ArgumentError, "#{inputs[index]} is too small" + elsif inputs[index] < 10 + inputs[index] = "0#{inputs[index]}" + end + +end + +return "#{inputs[0]}:#{inputs[1]}:#{inputs[2]}" + +end + +# if hours < 10 +# hours = "0#{hours}" +# if hours > 23 || hours < 0 +# raise ArgumentError, 'Hours must be between 0-23' +# end +# # if minutes < 10 +# # minutes = "0#{minutes}" +# if minutes > 59 || minutes < 0 +# raise ArgumentError, 'Minutes must be between 0-59' +# end +# if seconds < 10 +# seconds = "0#{seconds}" +# end +# return "#{hours}:#{minutes}:#{seconds}" +# end +# # What edge case does this cover? diff --git a/specs/clock_spec.rb b/specs/clock_spec.rb new file mode 100644 index 0000000..86a19c4 --- /dev/null +++ b/specs/clock_spec.rb @@ -0,0 +1,99 @@ +# clock_spec.rb + +# run spec files and minitest reporters +# to get colors +require 'minitest/autorun' +require 'minitest/reporters' +require_relative '../lib/clock' + +Minitest::Reporters.use! + +# This is to describe my test (Each block represents one tets) + +describe 'Clock' dov # Describe test contains several tests + it 'will return a string' do # this will return a string + + # Arrange the situation (Something is up) + hours = 9 + minutes = 25 + seconds = 46 + + # Act - Perform the action we are trying to test (do the thing) + time = clock(hours, minutes, seconds) + + # Assert what I am expecting to be true - step (no matter what is does it needs to give us string) + # Check that you have done the thing I ask you to do... + expect(time).must_be_instance_of String # this is an expectation that the result will be an instanc of string +# These are assertions and matches. It needs to satisfy this test above. + end + + it 'will return a string formatted in hh:mm:ss format when hours is a single digit' do + #Arrange + hours = 9 + minutes = 25 + seconds = 46 + + #Act + time = clock(hours, minutes, seconds) + + #Assert + expect(time).must_equal '09:25:46' + end + + it ' will return a string formated in hh:mm:ss even when the minutes are single digits ' do + #Arrange + hours = 9 + minutes = 3 + seconds = 7 + + #Act + time = clock(hours, minutes, seconds) + + #Assert + expect(time).must_equal '09:03:07' +end + +it 'will raise an error for an invalid hour/min/sec' do + #Arrange + hour = 23 + min = 0 + sec = 0 + + #Act + time = clock(hour, min, sec) + + #Assert + expect(time).must_equal '23:00:00' + + # re-Arrange + hour = 24 + + #Act-Assert (this keeps the program from crashing) + expect { + time = clock(hour, min, sec) + }.must_raise ArgumentError +end + +it 'will raise an error for an invalid hour/min/sec' do + #Arrange + hour = 0 + min = 59 + sec = 0 + + #Act + time = clock(hour, min, sec) + + #Assert + expect(time).must_equal '00:59:00' + + # re-Arrange + min = 60 + + #Act-Assert (this keeps the program from crashing) + expect { + time = clock(hour, min, sec) + }.must_raise ArgumentError + +end + +end