Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion clock.rb

This file was deleted.

1 change: 0 additions & 1 deletion clock_spec.rb

This file was deleted.

36 changes: 36 additions & 0 deletions lib/clock.rb
Original file line number Diff line number Diff line change
@@ -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?
99 changes: 99 additions & 0 deletions specs/clock_spec.rb
Original file line number Diff line number Diff line change
@@ -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