calcpace 1.2.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f95d90f68ffe230e0f84e3d16b4221de5ffe61004e76ad2f2d2a18260ad9c30
4
- data.tar.gz: fe206da9ac7d89e6df053fa0b6c6aff02227d06da7e81f48e6955cba183a720d
3
+ metadata.gz: 5f044557de907efbc86496d835e45575a5302a965de92cfcb38f52eb64f0e861
4
+ data.tar.gz: 6c676c4d91b34b38593a1f696697d70c8da4025cb3fe34492f98bdd554f54c3c
5
5
  SHA512:
6
- metadata.gz: 8e93948a4751a908473f92cf526c09b6c8dc084978cb9c99a3e2a1977bd2ae7da7a14548631c689da95a0e10dcc361a512d1967f610f60cf38297e51fb7cdad6
7
- data.tar.gz: bbf563ced661d193b79d5c90dd77ce63262488ceaf42fba06033b0154cebb0139a2f63f1749d1ed400985e854b1d716e54872a5a7b83756fabd2be16e081f7c8
6
+ metadata.gz: 8d1878a80782cec3056945bd66aae3b185dd6889fd0de6d330fbf0de990f35f2779196b7032d2dd53364df2fcfef8ee4313132d045a9521fbc72e2134ba9d15d
7
+ data.tar.gz: c578de2a7229f1b2be9fd01e5b0f825b8a03ae6b54b0ca9bacf78923916a28ebd779a50e77811710f4845456a573d65dc30c943ed372597506b915219701bb8d
@@ -1,42 +1,71 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bigdecimal'
4
-
5
- module Calculator
6
- def pace(time, distance)
7
- pace_in_seconds = pace_seconds(time, distance)
8
- convert_to_clocktime(pace_in_seconds)
9
- end
10
-
11
- def pace_seconds(time, distance)
12
- check_time(time)
13
- check_distance(distance)
14
- seconds = convert_to_seconds(time)
15
- bigdecimal ? seconds / BigDecimal(distance.to_s) : seconds / distance
16
- end
17
-
18
- def total_time(pace, distance)
19
- total_time_in_seconds = total_time_seconds(pace, distance)
20
- convert_to_clocktime(total_time_in_seconds)
21
- end
22
-
23
- def total_time_seconds(pace, distance)
24
- check_time(pace)
25
- check_distance(distance)
26
- pace_seconds = convert_to_seconds(pace)
27
- @bigdecimal ? pace_seconds * BigDecimal(distance.to_s) : pace_seconds * distance
28
- end
29
-
30
- def distance(time, pace)
31
- check_time(time)
32
- check_time(pace)
33
- if bigdecimal
34
- time_seconds = BigDecimal(convert_to_seconds(time).to_s)
35
- pace_seconds = BigDecimal(convert_to_seconds(pace).to_s)
36
- else
37
- time_seconds = convert_to_seconds(time)
38
- pace_seconds = convert_to_seconds(pace)
39
- end
40
- time_seconds / pace_seconds
41
- end
42
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+
5
+ # Module to calculate time, distance, pace and velocity
6
+ module Calculator
7
+ def velocity(time, distance)
8
+ check_positive(distance)
9
+ check_positive(time)
10
+ distance.to_f / time
11
+ end
12
+
13
+ def checked_velocity(time, distance)
14
+ check_time(time)
15
+ seconds = convert_to_seconds(time)
16
+ velocity(seconds, distance)
17
+ end
18
+
19
+ def clock_velocity(time, distance)
20
+ velocity_in_seconds = checked_velocity(time, distance)
21
+ convert_to_clocktime(velocity_in_seconds)
22
+ end
23
+
24
+ def pace(time, distance)
25
+ check_positive(distance)
26
+ check_positive(time)
27
+ time.to_f / distance
28
+ end
29
+
30
+ def checked_pace(time, distance)
31
+ check_time(time)
32
+ seconds = convert_to_seconds(time)
33
+ pace(seconds, distance)
34
+ end
35
+
36
+ def clock_pace(time, distance)
37
+ velocity_in_seconds = checked_pace(time, distance)
38
+ convert_to_clocktime(velocity_in_seconds)
39
+ end
40
+
41
+ def time(velocity, distance)
42
+ check_positive(distance)
43
+ check_positive(velocity)
44
+ velocity * distance
45
+ end
46
+
47
+ def checked_time(velocity, distance)
48
+ check_time(velocity)
49
+ velocity_seconds = convert_to_seconds(velocity)
50
+ time(velocity_seconds, distance)
51
+ end
52
+
53
+ def clock_time(velocity, distance)
54
+ total_time_in_seconds = checked_time(velocity, distance)
55
+ convert_to_clocktime(total_time_in_seconds)
56
+ end
57
+
58
+ def distance(time, velocity)
59
+ check_positive(time)
60
+ check_positive(velocity)
61
+ time.to_f / velocity
62
+ end
63
+
64
+ def checked_distance(time, velocity)
65
+ check_time(time)
66
+ check_time(velocity)
67
+ time_seconds = convert_to_seconds(time)
68
+ velocity_seconds = convert_to_seconds(velocity)
69
+ distance(time_seconds, velocity_seconds)
70
+ end
71
+ end
@@ -1,19 +1,12 @@
1
- # frozen_string_literal: true
2
-
3
- module Checker
4
- def check_distance(distance)
5
- raise 'It must be a X.X positive number' unless distance.positive?
6
- end
7
-
8
- def check_time(time_string)
9
- raise 'It must be a XX:XX:XX time' unless time_string =~ /\d{0,2}(:)*?\d{1,2}(:)\d{1,2}/
10
- end
11
-
12
- def check_integer(second)
13
- raise 'It must be a positive number' unless second.is_a?(Integer) && second.positive?
14
- end
15
-
16
- def check_unit(unit)
17
- raise 'It must be km or mi' unless %w[km mi].include?(unit)
18
- end
19
- end
1
+ # frozen_string_literal: true
2
+
3
+ # Module to check if the input is valid or of the correct type
4
+ module Checker
5
+ def check_positive(number)
6
+ raise 'It must be a X.X positive number' unless number.positive?
7
+ end
8
+
9
+ def check_time(time_string)
10
+ raise 'It must be a XX:XX:XX time' unless time_string =~ /\d{0,2}(:)*?\d{1,2}(:)\d{1,2}/
11
+ end
12
+ end
@@ -1,55 +1,58 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bigdecimal'
4
-
5
- module Converter
6
- KM_TO_MI_BIGDECIMAL = BigDecimal('0.621371')
7
- KM_TO_MI = 0.621371
8
- MI_TO_KM_BIGDECIMAL = BigDecimal('1.60934')
9
- MI_TO_KM = 1.60934
10
-
11
- def to_seconds(time)
12
- check_time(time)
13
- convert_to_seconds(time)
14
- end
15
-
16
- def to_clocktime(seconds)
17
- check_integer(seconds)
18
- convert_to_clocktime(seconds)
19
- end
20
-
21
- def convert_to_miles(distance)
22
- convert(distance, 'mi')
23
- end
24
-
25
- def convert_to_km(distance)
26
- convert(distance, 'km')
27
- end
28
-
29
- def convert(distance, unit)
30
- check_distance(distance)
31
- bigdecimal ? distance_to_convert = BigDecimal(distance.to_s) : distance_to_convert = distance
32
- convert_the_distance(distance_to_convert, unit)
33
- end
34
-
35
- def convert_to_seconds(time)
36
- hour, minute, seconds = time.split(':').map(&:to_i)
37
- (hour * 3600) + (minute * 60) + seconds
38
- end
39
-
40
- def convert_to_clocktime(seconds)
41
- format = seconds >= 86_400 ? '%d %H:%M:%S' : '%H:%M:%S'
42
- Time.at(seconds.to_i).utc.strftime(format)
43
- end
44
-
45
- def convert_the_distance(distance, unit)
46
- case unit
47
- when 'mi'
48
- bigdecimal ? km_to_mi = KM_TO_MI_BIGDECIMAL : km_to_mi = KM_TO_MI
49
- (distance * km_to_mi)
50
- when 'km'
51
- bigdecimal ? mi_to_km = MI_TO_KM_BIGDECIMAL : mi_to_km = MI_TO_KM
52
- (distance * mi_to_km)
53
- end
54
- end
55
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+
5
+ # Module to convert units
6
+ module Converter
7
+ KM_TO_MI = 0.621371
8
+ MI_TO_KM = 1.60934
9
+ NAUTICAL_MI_TO_KM = 1.852
10
+ KM_TO_NAUTICAL_MI = 0.539957
11
+ METERS_TO_KM = 0.001
12
+ KM_TO_METERS = 1000
13
+ METERS_TO_MI = 0.000621371
14
+ MI_TO_METERS = 1609.34
15
+ METERS_TO_FEET = 3.28084
16
+ FEET_TO_METERS = 0.3048
17
+ METERS_TO_YARDS = 1.09361
18
+ YARDS_TO_METERS = 0.9144
19
+ METERS_TO_INCHES = 39.3701
20
+ INCHES_TO_METERS = 0.0254
21
+
22
+ M_S_TO_KM_H = 3.6
23
+ KM_H_TO_M_S = 0.277778
24
+ M_S_TO_MI_H = 2.23694
25
+ MI_H_TO_M_S = 0.44704
26
+ M_S_TO_NAUTICAL_MI_H = 1.94384
27
+ NAUTICAL_MI_H_TO_M_S = 0.514444
28
+ M_S_TO_FEET_S = 3.28084
29
+ FEET_S_TO_M_S = 0.3048
30
+ M_S_TO_KNOTS = 1.94384
31
+ KNOTS_TO_M_S = 0.514444
32
+ KM_H_TO_MI_H = 0.621371
33
+ MI_H_TO_KM_H = 1.60934
34
+
35
+ def convert(value, unit)
36
+ check_positive(value)
37
+ unit_constant = constant(unit)
38
+ value * unit_constant
39
+ end
40
+
41
+ def convert_to_seconds(time)
42
+ hour, minute, seconds = time.split(':').map(&:to_i)
43
+ (hour * 3600) + (minute * 60) + seconds
44
+ end
45
+
46
+ def convert_to_clocktime(seconds)
47
+ format = seconds >= 86_400 ? '%d %H:%M:%S' : '%H:%M:%S'
48
+ Time.at(seconds.to_i).utc.strftime(format)
49
+ end
50
+
51
+ def constant(symbol)
52
+ Converter.const_get(symbol.to_s.upcase)
53
+ end
54
+
55
+ def list_constants
56
+ Converter.constants.map { |c| c.downcase.to_sym }
57
+ end
58
+ end
data/lib/calcpace.rb CHANGED
@@ -1,17 +1,16 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'calcpace/calculator'
4
- require_relative 'calcpace/checker'
5
- require_relative 'calcpace/converter'
6
-
7
- class Calcpace
8
- include Calculator
9
- include Checker
10
- include Converter
11
-
12
- attr_reader :bigdecimal
13
-
14
- def initialize(bigdecimal = false)
15
- @bigdecimal = bigdecimal
16
- end
17
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'calcpace/calculator'
4
+ require_relative 'calcpace/checker'
5
+ require_relative 'calcpace/converter'
6
+
7
+ # Main class to calculate velocity, pace, time, distance and velocity
8
+ class Calcpace
9
+ include Calculator
10
+ include Checker
11
+ include Converter
12
+
13
+ attr_reader :bigdecimal
14
+
15
+ def initialize; end
16
+ end
@@ -1,73 +1,118 @@
1
- # frozen_string_literal: true
2
-
3
- require 'minitest/autorun'
4
- require 'bigdecimal'
5
- require_relative '../../lib/calcpace'
6
-
7
- class TestCalculator < Minitest::Test
8
- def setup
9
- @checker = Calcpace.new
10
- @checker_bigdecimal = Calcpace.new(bigdecimal = true)
11
- end
12
-
13
- def test_pace
14
- assert_raises(RuntimeError) { @checker.pace('', 10) }
15
- assert_raises(RuntimeError) { @checker.pace('invalid', 10) }
16
- assert_raises(RuntimeError) { @checker.pace('00:00:00', 0) }
17
- assert_raises(RuntimeError) { @checker.pace('00:00:00', -1) }
18
- assert_equal '00:06:00', @checker.pace('01:00:00', 10)
19
- assert_equal '00:07:54', @checker.pace('01:37:21', 12.3)
20
- end
21
-
22
- def test_pace_with_bigdecimal_precision
23
- assert_equal '00:07:54', @checker_bigdecimal.pace('01:37:21', 12.3)
24
- end
25
-
26
- def test_pace_without_bigdecimal_precision
27
- assert_equal '00:07:54', @checker.pace('01:37:21', 12.3)
28
- end
29
-
30
- def test_pace_seconds
31
- assert_raises(RuntimeError) { @checker.pace_seconds('', 10) }
32
- assert_raises(RuntimeError) { @checker.pace_seconds('invalid', 10) }
33
- assert_raises(RuntimeError) { @checker.pace_seconds('00:00:00', 0) }
34
- assert_raises(RuntimeError) { @checker.pace_seconds('00:00:00', -1) }
35
- assert_equal 474.8780487804878, @checker.pace_seconds('01:37:21', 12.3)
36
- end
37
-
38
- def test_pace_seconds_with_bigdecimal_precision
39
- assert_equal BigDecimal('0.474878048780487804878048780487804878049e3'), @checker_bigdecimal.pace_seconds('01:37:21', 12.3)
40
- end
41
-
42
- def test_total_time
43
- assert_raises(RuntimeError) { @checker.total_time('', 10) }
44
- assert_raises(RuntimeError) { @checker.total_time('invalid', 10) }
45
- assert_raises(RuntimeError) { @checker.total_time('00:00:00', 0) }
46
- assert_raises(RuntimeError) { @checker.total_time('00:00:00', -1) }
47
- assert_equal '01:00:00', @checker.total_time('00:05:00', 12)
48
- end
49
-
50
- def test_total_time_seconds
51
- assert_raises(RuntimeError) { @checker.total_time_seconds('', 10) }
52
- assert_raises(RuntimeError) { @checker.total_time_seconds('invalid', 10) }
53
- assert_raises(RuntimeError) { @checker.total_time_seconds('00:00:00', 0) }
54
- assert_raises(RuntimeError) { @checker.total_time_seconds('00:00:00', -1) }
55
- assert_equal 3600, @checker.total_time_seconds('00:05:00', 12)
56
- assert_equal 71_844.3, @checker.total_time_seconds('01:37:21', 12.3)
57
- end
58
-
59
- def test_total_time_seconds_with_bigdecimal_precision
60
- assert_equal BigDecimal('0.718443e5'), @checker_bigdecimal.total_time_seconds('01:37:21', 12.3)
61
- end
62
-
63
- def test_distance
64
- assert_raises(RuntimeError) { @checker.distance('', '00:05:00') }
65
- assert_raises(RuntimeError) { @checker.distance('01:00:00', '') }
66
- assert_equal 18.0, @checker.distance('01:30:00', '00:05:00')
67
- assert_equal 15.0, @checker.distance('01:37:21', '00:06:17')
68
- end
69
-
70
- def test_distance_with_bigdecimal_precision
71
- assert_equal BigDecimal('0.15493368700265251989389920424403183024e2'), @checker_bigdecimal.distance('01:37:21', '00:06:17')
72
- end
73
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitest/autorun'
4
+ require 'bigdecimal'
5
+ require_relative '../../lib/calcpace'
6
+
7
+ class TestCalculator < Minitest::Test
8
+ def setup
9
+ @checker = Calcpace.new
10
+ end
11
+
12
+ def test_velocity
13
+ assert_equal 3.333, @checker.velocity(3600, 12_000).round(3)
14
+ assert_equal 12.3, @checker.velocity(5841, 71_844.3)
15
+ assert_equal 3.6, @checker.velocity(10_000, 36_000.0)
16
+ assert_raises(RuntimeError) { @checker.velocity(0, 10) }
17
+ assert_raises(RuntimeError) { @checker.velocity(10, -1) }
18
+ end
19
+
20
+ def test_checked_velocity
21
+ assert_raises(RuntimeError) { @checker.checked_velocity('', 10) }
22
+ assert_raises(RuntimeError) { @checker.checked_velocity('invalid', 10) }
23
+ assert_raises(RuntimeError) { @checker.checked_velocity('00:00:00', 0) }
24
+ assert_raises(RuntimeError) { @checker.checked_velocity('00:00:00', -1) }
25
+ assert_equal 2.778, @checker.checked_velocity('01:00:00', 10_000).round(3)
26
+ assert_equal 10, @checker.checked_velocity('00:00:01', 10)
27
+ assert_equal 12.3, @checker.checked_velocity('01:37:21', 71_844.3)
28
+ end
29
+
30
+ def test_clock_velocity
31
+ assert_raises(RuntimeError) { @checker.clock_velocity('', 10) }
32
+ assert_raises(RuntimeError) { @checker.clock_velocity('invalid', 10) }
33
+ assert_raises(RuntimeError) { @checker.clock_velocity('00:00:00', 0) }
34
+ assert_raises(RuntimeError) { @checker.clock_velocity('00:00:00', -1) }
35
+ assert_equal '00:00:02', @checker.clock_velocity('01:00:00', 10_000)
36
+ assert_equal '00:00:12', @checker.clock_velocity('01:37:21', 71_844.3)
37
+ end
38
+
39
+ def test_pace
40
+ assert_equal 300, @checker.pace(3600, 12)
41
+ assert_equal 122.81076923076924, @checker.pace(71_844.3, 585.0)
42
+ assert_raises(RuntimeError) { @checker.pace(0, 10) }
43
+ assert_raises(RuntimeError) { @checker.pace(10, -1) }
44
+ end
45
+
46
+ def test_checked_pace
47
+ assert_raises(RuntimeError) { @checker.checked_pace('', 10) }
48
+ assert_raises(RuntimeError) { @checker.checked_pace('invalid', 10) }
49
+ assert_raises(RuntimeError) { @checker.checked_pace('00:00:00', 0) }
50
+ assert_raises(RuntimeError) { @checker.checked_pace('00:00:00', -1) }
51
+ assert_equal 360, @checker.checked_pace('01:00:00', 10)
52
+ assert_equal 474.8780487804878, @checker.checked_pace('01:37:21', 12.3)
53
+ end
54
+
55
+ def test_clock_pace
56
+ assert_raises(RuntimeError) { @checker.clock_pace('', 10) }
57
+ assert_raises(RuntimeError) { @checker.clock_pace('invalid', 10) }
58
+ assert_raises(RuntimeError) { @checker.clock_pace('00:00:00', 0) }
59
+ assert_raises(RuntimeError) { @checker.clock_pace('00:00:00', -1) }
60
+ assert_equal '00:06:00', @checker.clock_pace('01:00:00', 10)
61
+ assert_equal '00:07:54', @checker.clock_pace('01:37:21', 12.3)
62
+ end
63
+
64
+ def test_time
65
+ assert_equal 43_200, @checker.time(3600, 12)
66
+ assert_equal 5841.0, @checker.time(12.3, 474.8780487804878)
67
+ assert_raises(RuntimeError) { @checker.time(0, 10) }
68
+ assert_raises(RuntimeError) { @checker.time(10, -1) }
69
+ end
70
+
71
+ def test_checked_time
72
+ assert_raises(RuntimeError) { @checker.checked_time('', 10) }
73
+ assert_raises(RuntimeError) { @checker.checked_time('invalid', 10) }
74
+ assert_raises(RuntimeError) { @checker.checked_time('00:00:00', 0) }
75
+ assert_raises(RuntimeError) { @checker.checked_time('00:00:00', -1) }
76
+ assert_equal 3600, @checker.checked_time('00:05:00', 12)
77
+ assert_equal 71_844.3, @checker.checked_time('01:37:21', 12.3)
78
+ end
79
+
80
+ def test_clock_time
81
+ assert_raises(RuntimeError) { @checker.clock_time('', 10) }
82
+ assert_raises(RuntimeError) { @checker.clock_time('invalid', 10) }
83
+ assert_raises(RuntimeError) { @checker.clock_time('00:00:00', 0) }
84
+ assert_raises(RuntimeError) { @checker.clock_time('00:00:00', -1) }
85
+ assert_equal '01:00:00', @checker.clock_time('00:05:00', 12)
86
+ end
87
+
88
+ def test_distance
89
+ assert_equal 30, @checker.distance(3600, 120)
90
+ assert_equal 12.3, @checker.distance(5841.0, 474.8780487804878)
91
+ assert_raises(RuntimeError) { @checker.distance(0, 10) }
92
+ assert_raises(RuntimeError) { @checker.distance(10, -1) }
93
+ end
94
+
95
+ def test_checked_distance
96
+ assert_raises(RuntimeError) { @checker.checked_distance('', '00:05:00') }
97
+ assert_raises(RuntimeError) { @checker.checked_distance('01:00:00', '') }
98
+ assert_equal 18.0, @checker.checked_distance('01:30:00', '00:05:00')
99
+ assert_equal 15.493, @checker.checked_distance('01:37:21', '00:06:17').round(3)
100
+ end
101
+
102
+ def test_readme_examples_one
103
+ assert_equal 3.386206896551724, @checker.velocity(3625, 12_275)
104
+ assert_equal 305.4166666666667, @checker.pace(3665, 12)
105
+ assert_equal 2520.0, @checker.time(210, 12)
106
+ assert_equal 80.5, @checker.distance(9660, 120)
107
+ end
108
+
109
+ def test_readme_examples_two
110
+ assert_equal 2.8658333333333332, @checker.checked_velocity('01:00:00', 10_317)
111
+ assert_equal '00:00:02', @checker.clock_velocity('01:00:00', 10_317)
112
+ assert_equal 489.2, @checker.checked_pace('01:21:32', 10)
113
+ assert_equal '00:08:09', @checker.clock_pace('01:21:32', 10)
114
+ assert_equal 4170.599999999999, @checker.checked_time('00:05:31', 12.6)
115
+ assert_equal '01:09:30', @checker.clock_time('00:05:31', 12.6)
116
+ assert_equal 12.640826873385013, @checker.checked_distance('01:21:32', '00:06:27')
117
+ end
118
+ end
@@ -1,27 +1,21 @@
1
- # frozen_string_literal: true
2
-
3
- require 'minitest/autorun'
4
- require_relative '../../lib/calcpace'
5
-
6
- class TestChecker < Minitest::Test
7
- def setup
8
- @checker = Calcpace.new
9
- end
10
-
11
- def test_check_distance
12
- assert_raises(RuntimeError) { @checker.check_distance(-1) }
13
- assert_raises(RuntimeError) { @checker.check_distance(0) }
14
- assert_nil @checker.check_distance(1)
15
- end
16
-
17
- def test_check_time
18
- assert_raises(RuntimeError) { @checker.check_time('') }
19
- assert_nil @checker.check_time('00:00:00')
20
- end
21
-
22
- def test_check_integer
23
- assert_raises(RuntimeError) { @checker.check_integer(-1) }
24
- assert_raises(RuntimeError) { @checker.check_integer(0) }
25
- assert_nil @checker.check_integer(1)
26
- end
27
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitest/autorun'
4
+ require_relative '../../lib/calcpace'
5
+
6
+ class TestChecker < Minitest::Test
7
+ def setup
8
+ @checker = Calcpace.new
9
+ end
10
+
11
+ def test_check_positive
12
+ assert_raises(RuntimeError) { @checker.check_positive(-1) }
13
+ assert_raises(RuntimeError) { @checker.check_positive(0) }
14
+ assert_nil @checker.check_positive(1)
15
+ end
16
+
17
+ def test_check_time
18
+ assert_raises(RuntimeError) { @checker.check_time('') }
19
+ assert_nil @checker.check_time('00:00:00')
20
+ end
21
+ end
@@ -1,58 +1,67 @@
1
- # frozen_string_literal: true
2
-
3
- require 'minitest/autorun'
4
- require_relative '../../lib/calcpace'
5
-
6
- class TestConverter < Minitest::Test
7
- def setup
8
- @checker = Calcpace.new
9
- @checker_bigdecimal = Calcpace.new(bigdecimal = true)
10
- end
11
-
12
- def test_convert_to_seconds
13
- assert_equal 4262, @checker.convert_to_seconds('01:11:02')
14
- end
15
-
16
- def test_to_seconds
17
- assert_equal 3600, @checker.to_seconds('01:00:00')
18
- assert_raises(RuntimeError) { @checker.to_seconds('invalid') }
19
- end
20
-
21
- def test_convert_to_clocktime
22
- assert_equal '01:11:02', @checker.convert_to_clocktime(4262)
23
- end
24
-
25
- def test_to_clocktime
26
- assert_equal '01:00:00', @checker.to_clocktime(3600)
27
- assert_equal '02 01:00:00', @checker.to_clocktime(90_000)
28
- assert_raises(RuntimeError) { @checker.to_clocktime(-1) }
29
- assert_raises(RuntimeError) { @checker.to_clocktime(0) }
30
- assert_raises(RuntimeError) { @checker.to_clocktime('invalid') }
31
- end
32
-
33
- def test_convert_the_distance
34
- assert_equal 6.21, @checker.convert_the_distance(10, 'mi').round(2)
35
- assert_equal 6.2, @checker.convert_the_distance(10, 'mi').round(1)
36
- assert_equal 6.214, @checker.convert_the_distance(10, 'mi').round(3)
37
- assert_equal 16.09, @checker.convert_the_distance(10, 'km').round(2)
38
- end
39
-
40
- def test_convert_the_distance_with_bigdecimal_precision
41
- assert_equal BigDecimal('6.21371'), @checker_bigdecimal.convert_the_distance(10, 'mi')
42
- end
43
-
44
- def test_convert
45
- assert_equal 6.21, @checker.convert(10, 'mi').round(2)
46
- assert_equal 16.09, @checker.convert(10, 'km').round(2)
47
- assert_equal 6.2, @checker.convert(10, 'mi').round(1)
48
- assert_raises(RuntimeError) { @checker.convert(-1, 'km') }
49
- end
50
-
51
- def test_convert_to_miles
52
- assert_equal 6.21, @checker.convert_to_miles(10).round(2)
53
- end
54
-
55
- def test_convert_to_km
56
- assert_equal 16.09, @checker.convert_to_km(10).round(2)
57
- end
58
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitest/autorun'
4
+ require_relative '../../lib/calcpace'
5
+
6
+ class TestConverter < Minitest::Test
7
+ def setup
8
+ @checker = Calcpace.new
9
+ end
10
+
11
+ def test_convert_to_seconds
12
+ assert_equal 4262, @checker.convert_to_seconds('01:11:02')
13
+ end
14
+
15
+ def test_convert_to_clocktime
16
+ assert_equal '01:11:02', @checker.convert_to_clocktime(4262)
17
+ end
18
+
19
+ def test_convert_distance_one
20
+ assert_equal 0.621371, @checker.convert(1, :km_to_mi)
21
+ assert_equal 1.60934, @checker.convert(1, :mi_to_km)
22
+ assert_equal 1.852, @checker.convert(1, :nautical_mi_to_km)
23
+ assert_equal 0.539957, @checker.convert(1, :km_to_nautical_mi)
24
+ assert_equal 0.001, @checker.convert(1, :meters_to_km)
25
+ assert_equal 1000, @checker.convert(1, :km_to_meters)
26
+ end
27
+
28
+ def test_convert_distance_two
29
+ assert_equal 0.000621371, @checker.convert(1, :meters_to_mi)
30
+ assert_equal 1609.34, @checker.convert(1, :mi_to_meters)
31
+ assert_equal 3.28084, @checker.convert(1, :meters_to_feet)
32
+ assert_equal 0.3048, @checker.convert(1, :feet_to_meters)
33
+ assert_equal 1.09361, @checker.convert(1, :meters_to_yards)
34
+ end
35
+
36
+ def test_convert_distance_three
37
+ assert_equal 0.9144, @checker.convert(1, :yards_to_meters)
38
+ assert_equal 39.3701, @checker.convert(1, :meters_to_inches)
39
+ assert_equal 0.0254, @checker.convert(1, :inches_to_meters)
40
+ end
41
+
42
+ def test_convert_velocity_one
43
+ assert_equal 3.60, @checker.convert(1, :m_s_to_km_h)
44
+ assert_equal 0.277778, @checker.convert(1, :km_h_to_m_s)
45
+ assert_equal 2.23694, @checker.convert(1, :m_s_to_mi_h)
46
+ assert_equal 0.44704, @checker.convert(1, :mi_h_to_m_s)
47
+ assert_equal 1.94384, @checker.convert(1, :m_s_to_nautical_mi_h)
48
+ end
49
+
50
+ def test_convert_velocity_two
51
+ assert_equal 0.514444, @checker.convert(1, :nautical_mi_h_to_m_s)
52
+ assert_equal 0.621371, @checker.convert(1, :km_h_to_mi_h)
53
+ assert_equal 1.60934, @checker.convert(1, :mi_h_to_km_h)
54
+ assert_equal 1.94384, @checker.convert(1, :m_s_to_knots)
55
+ assert_equal 0.514444, @checker.convert(1, :knots_to_m_s)
56
+ end
57
+
58
+ def test_constant
59
+ assert_equal 0.621371, @checker.constant(:km_to_mi)
60
+ assert_equal 1.60934, @checker.constant(:mi_to_km)
61
+ assert_equal 1.852, @checker.constant(:nautical_mi_to_km)
62
+ end
63
+
64
+ def test_list_constants
65
+ assert_equal 26, @checker.list_constants.size
66
+ end
67
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: calcpace
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joao Gilberto Saraiva
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-10 00:00:00.000000000 Z
11
+ date: 2024-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -72,34 +72,17 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0.79'
75
+ version: '1.66'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0.79'
83
- - !ruby/object:Gem::Dependency
84
- name: rubocop-minitest
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '0.11'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '0.11'
97
- description: Calcpace is a Ruby gem that helps with calculations related to running/cycling
98
- activities or general purposes involving distance and time. It can calculate pace,
99
- total time, and distance. It also converts distances between miles and kilometers
100
- and check formats of time and distance. The results are provided in a readable format,
101
- with times in HH:MM:SS or seconds and distances in X.X format. If you need, the
102
- gem supports BigDecimal to handle the calculations with more precision.
82
+ version: '1.66'
83
+ description: Calcpace is designed for calculations and conversions related to distance
84
+ and time. It can calculate velocity, pace, total time, and distance and it supports
85
+ conversion to 26 different units, including kilometers, miles, meters, and feet.
103
86
  email: joaogilberto@tuta.io
104
87
  executables: []
105
88
  extensions: []
@@ -132,11 +115,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
115
  - !ruby/object:Gem::Version
133
116
  version: '0'
134
117
  requirements: []
135
- rubygems_version: 3.5.16
136
- signing_key:
118
+ rubygems_version: 3.4.6
119
+ signing_key:
137
120
  specification_version: 4
138
- summary: 'Calcpace: calculate time, distance, pace, velocity and convert distances
139
- in an easy and precise way.'
121
+ summary: 'Calcpace: calculate total, distance, velocity and convert distances in an
122
+ easy way.'
140
123
  test_files:
141
124
  - test/calcpace/test_calculator.rb
142
125
  - test/calcpace/test_checker.rb