Skip to content

Commit dc7ce4d

Browse files
committed
add tests
1 parent 6c331de commit dc7ce4d

File tree

7 files changed

+121
-20
lines changed

7 files changed

+121
-20
lines changed

.github/workflows/ruby.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7+
8+
name: Ruby
9+
10+
on:
11+
push:
12+
branches: [ "main" ]
13+
pull_request:
14+
branches: [ "main" ]
15+
16+
permissions:
17+
contents: read
18+
19+
jobs:
20+
test:
21+
22+
runs-on: ubuntu-latest
23+
strategy:
24+
matrix:
25+
ruby-version: ['3.3.4']
26+
27+
steps:
28+
- uses: actions/checkout@v4
29+
- name: Set up Ruby
30+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
31+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
32+
# uses: ruby/setup-ruby@v1
33+
uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
34+
with:
35+
ruby-version: ${{ matrix.ruby-version }}
36+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
37+
- name: Run standard
38+
run: bundle exec standardrb
39+
- name: Run tests
40+
run: bundle exec rake

Gemfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ruby "3.3.4"
44

55
gem "sinatra"
66
gem "puma"
7+
gem "rake"
78
gem "standard"
89

910
group :development, :test do
@@ -12,5 +13,5 @@ end
1213

1314
group :test do
1415
gem "rack-test"
15-
gem "rspec"
16+
gem "minitest"
1617
end

Gemfile.lock

+4-15
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ GEM
44
ast (2.4.2)
55
base64 (0.2.0)
66
coderay (1.1.3)
7-
diff-lcs (1.5.1)
87
json (2.7.2)
98
language_server-protocol (3.17.0.3)
109
lint_roller (1.1.0)
1110
method_source (1.1.0)
11+
minitest (5.24.1)
1212
mustermann (3.0.1)
1313
ruby2_keywords (~> 0.0.1)
1414
nio4r (2.7.3)
@@ -31,22 +31,10 @@ GEM
3131
rack-test (2.1.0)
3232
rack (>= 1.3)
3333
rainbow (3.1.1)
34+
rake (13.2.1)
3435
regexp_parser (2.9.2)
3536
rexml (3.3.4)
3637
strscan
37-
rspec (3.13.0)
38-
rspec-core (~> 3.13.0)
39-
rspec-expectations (~> 3.13.0)
40-
rspec-mocks (~> 3.13.0)
41-
rspec-core (3.13.0)
42-
rspec-support (~> 3.13.0)
43-
rspec-expectations (3.13.1)
44-
diff-lcs (>= 1.2.0, < 2.0)
45-
rspec-support (~> 3.13.0)
46-
rspec-mocks (3.13.1)
47-
diff-lcs (>= 1.2.0, < 2.0)
48-
rspec-support (~> 3.13.0)
49-
rspec-support (3.13.1)
5038
rubocop (1.64.1)
5139
json (~> 2.3)
5240
language_server-protocol (>= 3.17.0)
@@ -92,10 +80,11 @@ PLATFORMS
9280
ruby
9381

9482
DEPENDENCIES
83+
minitest
9584
pry
9685
puma
9786
rack-test
98-
rspec
87+
rake
9988
sinatra
10089
standard
10190

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ bin/setup
1818

1919
### Tests
2020

21-
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.
21+
There are some tests. And there are some skipped tests due to bugs! I welcome contributions.
2222

2323
### How to contribute
2424

Rakefile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require "minitest/test_task"
2+
3+
Minitest::TestTask.create # named test, sensible defaults
4+
5+
# or more explicitly:
6+
7+
Minitest::TestTask.create(:test) do |t|
8+
t.libs << "test"
9+
t.libs << "lib"
10+
t.warning = false
11+
t.test_globs = ["test/**/*_test.rb"]
12+
end
13+
14+
task default: :test

app/pace.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ def initialize(type:, activity:, distance: nil, pace_length: nil,
3535
end
3636

3737
def print_pace
38-
"To #{@activity} #{@distance} #{@pace_length} in " \
38+
"To #{activity} #{@distance} #{@pace_length} in " \
3939
"#{time(@time_hours, @time_minutes, @time_seconds)}, " \
4040
"you need a pace of #{pace(@pace_minutes, @pace_seconds, @pace_length)}"
4141
end
4242

4343
def print_time
44-
"If you #{@activity} #{@distance} #{@distance_length} with " \
44+
"If you #{activity} #{@distance} #{@distance_length} with " \
4545
"a pace of #{pace(@pace_minutes, @pace_seconds, @pace_length)}, " \
4646
"you'll finish in #{time(@time_hours, @time_minutes, @time_seconds)}"
4747
end
@@ -50,11 +50,16 @@ def print_distance
5050
"If you #{activity} a pace of of " \
5151
"#{pace(@pace_minutes, @pace_seconds, @pace_length)}" \
5252
" for #{time(@time_hours, @time_minutes, @time_seconds)}, " \
53-
"you will go #{@distance} #{@distance_length}."
53+
"you will go #{pluralize(distance, distance_length)}."
5454
end
5555

5656
private
5757

58+
def pluralize(number, text)
59+
text = (number == 1) ? text : text + "s"
60+
"#{number} #{text}"
61+
end
62+
5863
def calculate_pace
5964
distance_in_pace_length = ConverterService.convert_length(@distance,
6065
@distance_length,

test/pace_test.rb

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require "minitest/autorun"
2+
require "./app/pace"
3+
require "./app/converter_service"
4+
class TestPace < Minitest::Test
5+
def test_calculates_pace_run
6+
pace = Pace.new(type: "pace", activity: "run", distance: 5, pace_length: "mile", distance_length: "mile", time_hours: 0, time_minutes: 30, time_seconds: 0)
7+
assert_equal "To run 5.0 mile in 0:30:00, you need a pace of 6:00/mile", pace.print_pace
8+
end
9+
10+
def test_calculates_time_run
11+
pace = Pace.new(type: "time", activity: "run", distance: 5, pace_length: "mile", distance_length: "mile", pace_minutes: 6, pace_seconds: 0)
12+
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
13+
end
14+
15+
def test_calculates_distance_run
16+
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)
17+
assert_equal "If you run a pace of of 6:00/mile for 0:30:00, you will go 5 miles.", pace.print_distance
18+
end
19+
20+
def test_calculates_pace_swim
21+
skip "this has a bug"
22+
pace = Pace.new(type: "pace", activity: "swim", distance: 500, pace_length: "50yd", distance_length: "meter", time_hours: 0, time_minutes: 10, time_seconds: 0)
23+
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
24+
end
25+
26+
def test_calculates_time_swim
27+
pace = Pace.new(type: "time", activity: "swim", distance: 500, pace_length: "25yd", distance_length: "meter", pace_minutes: 2, pace_seconds: 0)
28+
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
29+
end
30+
31+
def test_calculates_distance_swim
32+
skip "this has a bug"
33+
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)
34+
assert_equal "If you swim a pace of of 2:00/50m for 0:10:00, you will go 500 meters.", pace.print_distance
35+
end
36+
37+
def test_calculates_pace_row
38+
skip "this has a bug"
39+
pace = Pace.new(type: "pace", activity: "row", distance: 2000, pace_length: "500m", distance_length: "meter", time_hours: 0, time_minutes: 10, time_seconds: 0)
40+
assert_equal "To row 2000 m in 0:10:00, you need a pace of 2:30/500m", pace.print_pace
41+
end
42+
43+
def test_calculates_time_row
44+
pace = Pace.new(type: "time", activity: "row", distance: 2, pace_length: "500meter", distance_length: "mile", pace_minutes: 2, pace_seconds: 30)
45+
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
46+
end
47+
48+
def test_calculates_distance_row
49+
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)
50+
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
51+
end
52+
end

0 commit comments

Comments
 (0)