diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml new file mode 100644 index 0000000..d887c13 --- /dev/null +++ b/.github/workflows/ruby.yml @@ -0,0 +1,40 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake +# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby + +name: Ruby + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + test: + + runs-on: ubuntu-latest + strategy: + matrix: + ruby-version: ['3.3.4'] + + steps: + - uses: actions/checkout@v4 + - name: Set up Ruby + # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby, + # change this to (see https://github.com/ruby/setup-ruby#versioning): + # uses: ruby/setup-ruby@v1 + uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0 + with: + ruby-version: ${{ matrix.ruby-version }} + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + - name: Run standard + run: bundle exec standardrb + - name: Run tests + run: bundle exec rake diff --git a/Gemfile b/Gemfile index cf18c8b..8b25f4d 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ ruby "3.3.4" gem "sinatra" gem "puma" +gem "rake" gem "standard" group :development, :test do @@ -12,5 +13,5 @@ end group :test do gem "rack-test" - gem "rspec" + gem "minitest" end diff --git a/Gemfile.lock b/Gemfile.lock index 02361c8..313b045 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,11 +4,11 @@ GEM ast (2.4.2) base64 (0.2.0) coderay (1.1.3) - diff-lcs (1.5.1) json (2.7.2) language_server-protocol (3.17.0.3) lint_roller (1.1.0) method_source (1.1.0) + minitest (5.24.1) mustermann (3.0.1) ruby2_keywords (~> 0.0.1) nio4r (2.7.3) @@ -31,22 +31,10 @@ GEM rack-test (2.1.0) rack (>= 1.3) rainbow (3.1.1) + rake (13.2.1) regexp_parser (2.9.2) rexml (3.3.4) strscan - rspec (3.13.0) - rspec-core (~> 3.13.0) - rspec-expectations (~> 3.13.0) - rspec-mocks (~> 3.13.0) - rspec-core (3.13.0) - rspec-support (~> 3.13.0) - rspec-expectations (3.13.1) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.13.0) - rspec-mocks (3.13.1) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.13.0) - rspec-support (3.13.1) rubocop (1.64.1) json (~> 2.3) language_server-protocol (>= 3.17.0) @@ -92,10 +80,11 @@ PLATFORMS ruby DEPENDENCIES + minitest pry puma rack-test - rspec + rake sinatra standard diff --git a/README.md b/README.md index 14ebfbc..249058f 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ bin/setup ### Tests -There should definitely be tests! I will be using Rspec, but I spiked this for fun over Christmas and haven't written tests yet 😳. If you want to contribute, it would be appreciated. +There are some tests. And there are some skipped tests due to bugs! I welcome contributions. ### How to contribute diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..608529f --- /dev/null +++ b/Rakefile @@ -0,0 +1,14 @@ +require "minitest/test_task" + +Minitest::TestTask.create # named test, sensible defaults + +# or more explicitly: + +Minitest::TestTask.create(:test) do |t| + t.libs << "test" + t.libs << "lib" + t.warning = false + t.test_globs = ["test/**/*_test.rb"] +end + +task default: :test diff --git a/app/pace.rb b/app/pace.rb index fa6b8a7..76aa90d 100644 --- a/app/pace.rb +++ b/app/pace.rb @@ -35,13 +35,13 @@ def initialize(type:, activity:, distance: nil, pace_length: nil, end def print_pace - "To #{@activity} #{@distance} #{@pace_length} in " \ + "To #{activity} #{@distance} #{@pace_length} in " \ "#{time(@time_hours, @time_minutes, @time_seconds)}, " \ "you need a pace of #{pace(@pace_minutes, @pace_seconds, @pace_length)}" end def print_time - "If you #{@activity} #{@distance} #{@distance_length} with " \ + "If you #{activity} #{@distance} #{@distance_length} with " \ "a pace of #{pace(@pace_minutes, @pace_seconds, @pace_length)}, " \ "you'll finish in #{time(@time_hours, @time_minutes, @time_seconds)}" end @@ -50,11 +50,16 @@ def print_distance "If you #{activity} a pace of of " \ "#{pace(@pace_minutes, @pace_seconds, @pace_length)}" \ " for #{time(@time_hours, @time_minutes, @time_seconds)}, " \ - "you will go #{@distance} #{@distance_length}." + "you will go #{pluralize(distance, distance_length)}." end private + def pluralize(number, text) + text = (number == 1) ? text : text + "s" + "#{number} #{text}" + end + def calculate_pace distance_in_pace_length = ConverterService.convert_length(@distance, @distance_length, diff --git a/test/pace_test.rb b/test/pace_test.rb new file mode 100644 index 0000000..1e720d8 --- /dev/null +++ b/test/pace_test.rb @@ -0,0 +1,52 @@ +require "minitest/autorun" +require "./app/pace" +require "./app/converter_service" +class TestPace < Minitest::Test + def test_calculates_pace_run + pace = Pace.new(type: "pace", activity: "run", distance: 5, pace_length: "mile", distance_length: "mile", time_hours: 0, time_minutes: 30, time_seconds: 0) + assert_equal "To run 5.0 mile in 0:30:00, you need a pace of 6:00/mile", pace.print_pace + end + + def test_calculates_time_run + pace = Pace.new(type: "time", activity: "run", distance: 5, pace_length: "mile", distance_length: "mile", pace_minutes: 6, pace_seconds: 0) + assert_equal "If you run 5.0 mile with a pace of 6:00/mile, you'll finish in 0:30:00", pace.print_time + end + + def test_calculates_distance_run + pace = Pace.new(type: "distance", activity: "run", pace_length: "mile", distance_length: "mile", pace_minutes: 6, pace_seconds: 0, time_hours: 0, time_minutes: 30, time_seconds: 0) + assert_equal "If you run a pace of of 6:00/mile for 0:30:00, you will go 5 miles.", pace.print_distance + end + + def test_calculates_pace_swim + skip "this has a bug" + pace = Pace.new(type: "pace", activity: "swim", distance: 500, pace_length: "50yd", distance_length: "meter", time_hours: 0, time_minutes: 10, time_seconds: 0) + assert_equal "If you swim 500.0 meter with a pace of 2:00/50yd, you'll finish in 0:43:44", pace.print_pace + end + + def test_calculates_time_swim + pace = Pace.new(type: "time", activity: "swim", distance: 500, pace_length: "25yd", distance_length: "meter", pace_minutes: 2, pace_seconds: 0) + assert_equal "If you swim 500.0 meter with a pace of 2:00/25yd, you'll finish in 0:43:44", pace.print_time + end + + def test_calculates_distance_swim + skip "this has a bug" + pace = Pace.new(type: "distance", activity: "swim", pace_length: "50m", distance_length: "meter", pace_minutes: 2, pace_seconds: 0, time_hours: 0, time_minutes: 10, time_seconds: 0) + assert_equal "If you swim a pace of of 2:00/50m for 0:10:00, you will go 500 meters.", pace.print_distance + end + + def test_calculates_pace_row + skip "this has a bug" + pace = Pace.new(type: "pace", activity: "row", distance: 2000, pace_length: "500m", distance_length: "meter", time_hours: 0, time_minutes: 10, time_seconds: 0) + assert_equal "To row 2000 m in 0:10:00, you need a pace of 2:30/500m", pace.print_pace + end + + def test_calculates_time_row + pace = Pace.new(type: "time", activity: "row", distance: 2, pace_length: "500meter", distance_length: "mile", pace_minutes: 2, pace_seconds: 30) + assert_equal "If you row 2.0 mile with a pace of 2:30/500meter, you'll finish in 0:08:02", pace.print_time + end + + def test_calculates_distance_row + pace = Pace.new(type: "distance", activity: "row", pace_length: "500meter", distance_length: "mile", pace_minutes: 2, pace_seconds: 30, time_hours: 0, time_minutes: 10, time_seconds: 0) + assert_equal "If you row a pace of of 2:30/500meter for 0:10:00, you will go 6.44 miles.", pace.print_distance + end +end