From e1909fef6a21a094aa3f92afc411093ac88bfcaa Mon Sep 17 00:00:00 2001 From: Victor Payno Date: Sat, 28 Oct 2023 14:20:15 -0700 Subject: [PATCH] ruby/clock: download exercise --- ruby/clock/.exercism/config.json | 35 ++++ ruby/clock/.exercism/metadata.json | 1 + ruby/clock/HELP.md | 54 +++++ ruby/clock/README.md | 39 ++++ ruby/clock/clock.rb | 7 + ruby/clock/clock_test.rb | 312 +++++++++++++++++++++++++++++ 6 files changed, 448 insertions(+) create mode 100644 ruby/clock/.exercism/config.json create mode 100644 ruby/clock/.exercism/metadata.json create mode 100644 ruby/clock/HELP.md create mode 100644 ruby/clock/README.md create mode 100644 ruby/clock/clock.rb create mode 100644 ruby/clock/clock_test.rb diff --git a/ruby/clock/.exercism/config.json b/ruby/clock/.exercism/config.json new file mode 100644 index 00000000..46575c2e --- /dev/null +++ b/ruby/clock/.exercism/config.json @@ -0,0 +1,35 @@ +{ + "authors": [ + "kytrinyx" + ], + "contributors": [ + "alxndr", + "budmc29", + "cadwallion", + "guygastineau", + "henrik", + "hilary", + "iHiD", + "Insti", + "jpotts244", + "kotp", + "pendletons", + "pgaspar", + "tryantwit", + "wvmitchell" + ], + "files": { + "solution": [ + "clock.rb" + ], + "test": [ + "clock_test.rb" + ], + "example": [ + ".meta/example.rb" + ] + }, + "blurb": "Implement a clock that handles times without dates.", + "source": "Pairing session with Erin Drummond", + "source_url": "https://twitter.com/ebdrummond" +} diff --git a/ruby/clock/.exercism/metadata.json b/ruby/clock/.exercism/metadata.json new file mode 100644 index 00000000..c72a5332 --- /dev/null +++ b/ruby/clock/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"ruby","exercise":"clock","id":"e860d5afc008457a9fc134f073882c5c","url":"https://exercism.org/tracks/ruby/exercises/clock","handle":"vpayno","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/ruby/clock/HELP.md b/ruby/clock/HELP.md new file mode 100644 index 00000000..1375a226 --- /dev/null +++ b/ruby/clock/HELP.md @@ -0,0 +1,54 @@ +# Help + +## Running the tests + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + +``` +gem install minitest +``` + + +Run the tests from the exercise directory using the following command: + +``` +ruby _test.rb +``` + +Please replace `` with your exercise name in snake_case. + +## Color output + +You can `require 'minitest/pride'` or run the following command to get colored output: + +``` +ruby -r minitest/pride _test.rb +``` + +## Submitting your solution + +You can submit your solution using the `exercism submit clock.rb` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Ruby track's documentation](https://exercism.org/docs/tracks/ruby) +- The [Ruby track's programming category on the forum](https://forum.exercism.org/c/programming/ruby) +- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +To get help if you're having trouble, you can use one of the following resources: + +- [Ruby Documentation](http://ruby-doc.org/) +- [StackOverflow](http://stackoverflow.com/questions/tagged/ruby) +- [/r/ruby](https://www.reddit.com/r/ruby) is the Ruby subreddit. \ No newline at end of file diff --git a/ruby/clock/README.md b/ruby/clock/README.md new file mode 100644 index 00000000..9486a084 --- /dev/null +++ b/ruby/clock/README.md @@ -0,0 +1,39 @@ +# Clock + +Welcome to Clock on Exercism's Ruby Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Implement a clock that handles times without dates. + +You should be able to add and subtract minutes to it. + +Two clocks that represent the same time should be equal to each other. + +## Source + +### Created by + +- @kytrinyx + +### Contributed to by + +- @alxndr +- @budmc29 +- @cadwallion +- @guygastineau +- @henrik +- @hilary +- @iHiD +- @Insti +- @jpotts244 +- @kotp +- @pendletons +- @pgaspar +- @tryantwit +- @wvmitchell + +### Based on + +Pairing session with Erin Drummond - https://twitter.com/ebdrummond \ No newline at end of file diff --git a/ruby/clock/clock.rb b/ruby/clock/clock.rb new file mode 100644 index 00000000..d3b9d132 --- /dev/null +++ b/ruby/clock/clock.rb @@ -0,0 +1,7 @@ +=begin +Write your code for the 'Clock' exercise in this file. Make the tests in +`clock_test.rb` pass. + +To get started with TDD, see the `README.md` file in your +`ruby/clock` directory. +=end diff --git a/ruby/clock/clock_test.rb b/ruby/clock/clock_test.rb new file mode 100644 index 00000000..9659d6e4 --- /dev/null +++ b/ruby/clock/clock_test.rb @@ -0,0 +1,312 @@ +require 'minitest/autorun' +require_relative 'clock' + +class ClockTest < Minitest::Test + def test_on_the_hour + # skip + assert_equal "08:00", Clock.new(hour: 8).to_s + end + + def test_past_the_hour + skip + assert_equal "11:09", Clock.new(hour: 11, minute: 9).to_s + end + + def test_midnight_is_zero_hours + skip + assert_equal "00:00", Clock.new(hour: 24).to_s + end + + def test_hour_rolls_over + skip + assert_equal "01:00", Clock.new(hour: 25).to_s + end + + def test_hour_rolls_over_continuously + skip + assert_equal "04:00", Clock.new(hour: 100).to_s + end + + def test_sixty_minutes_is_next_hour + skip + assert_equal "02:00", Clock.new(hour: 1, minute: 60).to_s + end + + def test_minutes_roll_over + skip + assert_equal "02:40", Clock.new(minute: 160).to_s + end + + def test_minutes_roll_over_continuously + skip + assert_equal "04:43", Clock.new(minute: 1723).to_s + end + + def test_hour_and_minutes_roll_over + skip + assert_equal "03:40", Clock.new(hour: 25, minute: 160).to_s + end + + def test_hour_and_minutes_roll_over_continuously + skip + assert_equal "11:01", Clock.new(hour: 201, minute: 3001).to_s + end + + def test_hour_and_minutes_roll_over_to_exactly_midnight + skip + assert_equal "00:00", Clock.new(hour: 72, minute: 8640).to_s + end + + def test_negative_hour + skip + assert_equal "23:15", Clock.new(hour: -1, minute: 15).to_s + end + + def test_negative_hour_rolls_over + skip + assert_equal "23:00", Clock.new(hour: -25).to_s + end + + def test_negative_hour_rolls_over_continuously + skip + assert_equal "05:00", Clock.new(hour: -91).to_s + end + + def test_negative_minutes + skip + assert_equal "00:20", Clock.new(hour: 1, minute: -40).to_s + end + + def test_negative_minutes_roll_over + skip + assert_equal "22:20", Clock.new(hour: 1, minute: -160).to_s + end + + def test_negative_minutes_roll_over_continuously + skip + assert_equal "16:40", Clock.new(hour: 1, minute: -4820).to_s + end + + def test_negative_sixty_minutes_is_previous_hour + skip + assert_equal "01:00", Clock.new(hour: 2, minute: -60).to_s + end + + def test_negative_hour_and_minutes_both_roll_over + skip + assert_equal "20:20", Clock.new(hour: -25, minute: -160).to_s + end + + def test_negative_hour_and_minutes_both_roll_over_continuously + skip + assert_equal "22:10", Clock.new(hour: -121, minute: -5810).to_s + end + + def test_add_minutes + skip + clock = Clock.new(hour: 10, minute: 0) + assert_equal "10:03", (clock + Clock.new(minute: 3)).to_s + end + + def test_add_no_minutes + skip + clock = Clock.new(hour: 6, minute: 41) + assert_equal "06:41", (clock + Clock.new(minute: 0)).to_s + end + + def test_add_to_next_hour + skip + clock = Clock.new(hour: 0, minute: 45) + assert_equal "01:25", (clock + Clock.new(minute: 40)).to_s + end + + def test_add_more_than_one_hour + skip + clock = Clock.new(hour: 10, minute: 0) + assert_equal "11:01", (clock + Clock.new(minute: 61)).to_s + end + + def test_add_more_than_two_hours_with_carry + skip + clock = Clock.new(hour: 0, minute: 45) + assert_equal "03:25", (clock + Clock.new(minute: 160)).to_s + end + + def test_add_across_midnight + skip + clock = Clock.new(hour: 23, minute: 59) + assert_equal "00:01", (clock + Clock.new(minute: 2)).to_s + end + + def test_add_more_than_one_day + skip + clock = Clock.new(hour: 5, minute: 32) + assert_equal "06:32", (clock + Clock.new(minute: 1500)).to_s + end + + def test_add_more_than_two_days + skip + clock = Clock.new(hour: 1, minute: 1) + assert_equal "11:21", (clock + Clock.new(minute: 3500)).to_s + end + + def test_subtract_minutes + skip + clock = Clock.new(hour: 10, minute: 3) + assert_equal "10:00", (clock - Clock.new(minute: 3)).to_s + end + + def test_subtract_to_previous_hour + skip + clock = Clock.new(hour: 10, minute: 3) + assert_equal "09:33", (clock - Clock.new(minute: 30)).to_s + end + + def test_subtract_more_than_an_hour + skip + clock = Clock.new(hour: 10, minute: 3) + assert_equal "08:53", (clock - Clock.new(minute: 70)).to_s + end + + def test_subtract_across_midnight + skip + clock = Clock.new(hour: 0, minute: 3) + assert_equal "23:59", (clock - Clock.new(minute: 4)).to_s + end + + def test_subtract_more_than_two_hours + skip + clock = Clock.new(hour: 0, minute: 0) + assert_equal "21:20", (clock - Clock.new(minute: 160)).to_s + end + + def test_subtract_more_than_two_hours_with_borrow + skip + clock = Clock.new(hour: 6, minute: 15) + assert_equal "03:35", (clock - Clock.new(minute: 160)).to_s + end + + def test_subtract_more_than_one_day + skip + clock = Clock.new(hour: 5, minute: 32) + assert_equal "04:32", (clock - Clock.new(minute: 1500)).to_s + end + + def test_subtract_more_than_two_days + skip + clock = Clock.new(hour: 2, minute: 20) + assert_equal "00:20", (clock - Clock.new(minute: 3000)).to_s + end + + def test_clocks_with_same_time + skip + clock1 = Clock.new(hour: 15, minute: 37) + clock2 = Clock.new(hour: 15, minute: 37) + assert_equal clock1, clock2 + end + + def test_clocks_a_minute_apart + skip + clock1 = Clock.new(hour: 15, minute: 36) + clock2 = Clock.new(hour: 15, minute: 37) + refute clock1 == clock2 + end + + def test_clocks_an_hour_apart + skip + clock1 = Clock.new(hour: 14, minute: 37) + clock2 = Clock.new(hour: 15, minute: 37) + refute clock1 == clock2 + end + + def test_clocks_with_hour_overflow + skip + clock1 = Clock.new(hour: 10, minute: 37) + clock2 = Clock.new(hour: 34, minute: 37) + assert_equal clock1, clock2 + end + + def test_clocks_with_hour_overflow_by_several_days + skip + clock1 = Clock.new(hour: 3, minute: 11) + clock2 = Clock.new(hour: 99, minute: 11) + assert_equal clock1, clock2 + end + + def test_clocks_with_negative_hour + skip + clock1 = Clock.new(hour: 22, minute: 40) + clock2 = Clock.new(hour: -2, minute: 40) + assert_equal clock1, clock2 + end + + def test_clocks_with_negative_hour_that_wraps + skip + clock1 = Clock.new(hour: 17, minute: 3) + clock2 = Clock.new(hour: -31, minute: 3) + assert_equal clock1, clock2 + end + + def test_clocks_with_negative_hour_that_wraps_multiple_times + skip + clock1 = Clock.new(hour: 13, minute: 49) + clock2 = Clock.new(hour: -83, minute: 49) + assert_equal clock1, clock2 + end + + def test_clocks_with_minute_overflow + skip + clock1 = Clock.new(hour: 0, minute: 1) + clock2 = Clock.new(hour: 0, minute: 1441) + assert_equal clock1, clock2 + end + + def test_clocks_with_minute_overflow_by_several_days + skip + clock1 = Clock.new(hour: 2, minute: 2) + clock2 = Clock.new(hour: 2, minute: 4322) + assert_equal clock1, clock2 + end + + def test_clocks_with_negative_minute + skip + clock1 = Clock.new(hour: 2, minute: 40) + clock2 = Clock.new(hour: 3, minute: -20) + assert_equal clock1, clock2 + end + + def test_clocks_with_negative_minute_that_wraps + skip + clock1 = Clock.new(hour: 4, minute: 10) + clock2 = Clock.new(hour: 5, minute: -1490) + assert_equal clock1, clock2 + end + + def test_clocks_with_negative_minute_that_wraps_multiple_times + skip + clock1 = Clock.new(hour: 6, minute: 15) + clock2 = Clock.new(hour: 6, minute: -4305) + assert_equal clock1, clock2 + end + + def test_clocks_with_negative_hours_and_minutes + skip + clock1 = Clock.new(hour: 7, minute: 32) + clock2 = Clock.new(hour: -12, minute: -268) + assert_equal clock1, clock2 + end + + def test_clocks_with_negative_hours_and_minutes_that_wrap + skip + clock1 = Clock.new(hour: 18, minute: 7) + clock2 = Clock.new(hour: -54, minute: -11_513) + assert_equal clock1, clock2 + end + + def test_full_clockand_zeroed_clock + skip + clock1 = Clock.new(hour: 24, minute: 0) + clock2 = Clock.new(hour: 0, minute: 0) + assert_equal clock1, clock2 + end +end