m9t 0.3.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,195 @@
1
+ require "m9t/direction"
2
+
3
+ describe M9t::Direction do
4
+ before do
5
+ I18n.locale = :en
6
+ end
7
+
8
+ describe ".measurement_name" do
9
+ it "is 'direction'" do
10
+ expect(M9t::Direction.measurement_name).to eq("direction")
11
+ end
12
+ end
13
+
14
+ describe ".options" do
15
+ it "is set" do
16
+ expect(M9t::Direction.options).not_to be_nil
17
+ end
18
+
19
+ context "abbreviated" do
20
+ it "is false" do
21
+ expect(M9t::Direction.options[:abbreviated]).to be_falsey
22
+ end
23
+ end
24
+
25
+ context "units" do
26
+ it "is degrees" do
27
+ expect(M9t::Direction.options[:units]).to eq(:degrees)
28
+ end
29
+ end
30
+ end
31
+
32
+ describe ".normalize" do
33
+ it "reduces number greater than 360" do
34
+ expect(M9t::Direction.normalize(725)).to eq(5)
35
+ end
36
+
37
+ it "reduces 360" do
38
+ expect(M9t::Direction.normalize(360)).to eq(0)
39
+ end
40
+
41
+ it "increases numbers less than zero" do
42
+ expect(M9t::Direction.normalize(-355)).to eq(5)
43
+ end
44
+
45
+ it "handles values with decimals" do
46
+ expect(M9t::Direction.normalize(360.5)).to eq(0.5)
47
+ end
48
+
49
+ it "handles large values" do
50
+ M9t::Direction.normalize(1000000000)
51
+ end
52
+ end
53
+
54
+ context "conversion class methods" do
55
+ describe ".degrees_to_degrees" do
56
+ it "returns the identity" do
57
+ expect(M9t::Direction.degrees_to_degrees(45)).to eq(45)
58
+ end
59
+ end
60
+
61
+ describe ".degrees_to_compass" do
62
+ before do
63
+ I18n.locale = :en
64
+ end
65
+
66
+ context "exact" do
67
+ [
68
+ "N", "NNE", "NE", "ENE",
69
+ "E", "ESE", "SE", "SSE",
70
+ "S", "SSW", "SW", "WSW",
71
+ "W", "WNW", "NW", "NNW"
72
+ ].each.with_index do |result, i|
73
+ it "recognizes #{result}" do
74
+ expect(M9t::Direction.degrees_to_compass(i * 22.5)).to eq(result)
75
+ end
76
+ end
77
+ end
78
+
79
+ context "rounding" do
80
+ specify "up" do
81
+ expect(M9t::Direction.degrees_to_compass(42)).to eq("NE")
82
+ end
83
+
84
+ specify "down" do
85
+ expect(M9t::Direction.degrees_to_compass(93)).to eq("E")
86
+ end
87
+ end
88
+
89
+ context "i18n" do
90
+ before do
91
+ I18n.locale = :it
92
+ end
93
+
94
+ it "translates" do
95
+ expect(M9t::Direction.degrees_to_compass(270)).to eq("O")
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "compass_to_degrees" do
101
+ it "converts correctly" do
102
+ expect(M9t::Direction.compass_to_degrees("WSW")).to eq(247.5)
103
+ end
104
+ end
105
+ end
106
+
107
+ describe ".new" do
108
+ context "strings" do
109
+ it "works" do
110
+ expect(M9t::Direction.new("35").value).to eq(35)
111
+ end
112
+
113
+ it "handles leading zeroes" do
114
+ expect(M9t::Direction.new("010").value).to eq(10)
115
+ end
116
+ end
117
+ end
118
+
119
+ describe ".compass" do
120
+ it "converts cardinals" do
121
+ expect(M9t::Direction.compass("N").value).to eq(0)
122
+ end
123
+
124
+ it "handles 16ths" do
125
+ expect(M9t::Direction.compass("WSW").value).to eq(247.5)
126
+ end
127
+ end
128
+
129
+ describe "#value" do
130
+ let(:degrees) { M9t::Direction.new(45) }
131
+
132
+ it "returns the supplied value" do
133
+ expect(degrees.value).to eq(45)
134
+ end
135
+ end
136
+
137
+ describe "#to_s" do
138
+ context "not abbreviated" do
139
+ context "singular" do
140
+ subject { M9t::Direction.new(1) }
141
+
142
+ it "returns the full unit name" do
143
+ expect(subject.to_s).to eq("1 degree")
144
+ end
145
+
146
+ it "translates" do
147
+ I18n.locale = :it
148
+ expect(subject.to_s).to eq("1 grado")
149
+ end
150
+ end
151
+
152
+ context "plural" do
153
+ subject { M9t::Direction.new(135) }
154
+
155
+ it "returns the full unit name" do
156
+ expect(subject.to_s).to eq("135 degrees")
157
+ end
158
+
159
+ it "translates" do
160
+ I18n.locale = :it
161
+ expect(subject.to_s).to eq("135 gradi")
162
+ end
163
+ end
164
+ end
165
+
166
+ context "abbreviated" do
167
+ subject { M9t::Direction.new(135) }
168
+
169
+ it "uses the symbol" do
170
+ expect(subject.to_s(abbreviated: true)).to eq("135°")
171
+ end
172
+ end
173
+
174
+ context "compass units" do
175
+ subject { M9t::Direction.new(225) }
176
+
177
+ it "works" do
178
+ expect(subject.to_s(units: :compass)).to eq("SW")
179
+ end
180
+
181
+ it "translates" do
182
+ I18n.locale = :it
183
+ expect(subject.to_s(units: :compass)).to eq("SO")
184
+ end
185
+ end
186
+ end
187
+
188
+ describe "#to_compass" do
189
+ subject { M9t::Direction.new(0) }
190
+
191
+ it "is correct" do
192
+ expect(subject.to_compass).to eq("N")
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,127 @@
1
+ require "m9t/distance"
2
+
3
+ describe M9t::Distance do
4
+ context "class methods" do
5
+ it "has a measurement name" do
6
+ expect(M9t::Distance.measurement_name).to eq("distance")
7
+ end
8
+
9
+ context "default options" do
10
+ it "has options" do
11
+ expect(M9t::Distance.options).not_to be_nil
12
+ end
13
+
14
+ it "not abbreviated" do
15
+ expect(M9t::Distance.options[:abbreviated]).to be_falsey
16
+ end
17
+
18
+ it "units: meters" do
19
+ expect(M9t::Distance.options[:units]).to be(:meters)
20
+ end
21
+
22
+ it "precision: 5" do
23
+ expect(M9t::Distance.options[:precision]).to eq(5)
24
+ end
25
+ end
26
+
27
+ it "constructs from meters" do
28
+ expect(M9t::Distance.new(0.3).to_f).to eq(0.3)
29
+ end
30
+
31
+ context "units factories" do
32
+ [
33
+ [:meters, 0.3, 0.3],
34
+ [:kilometers, 0.3, 300.0],
35
+ [:miles, 26.21875, 42194.988]
36
+ ].each do |unit, input, result|
37
+ it "#{unit} constructs correctly" do
38
+ distance = M9t::Distance.send(unit, input)
39
+ expect(distance.to_f).to be_within(0.0001).of(result)
40
+ end
41
+ end
42
+ end
43
+
44
+ context "conversions" do
45
+ [
46
+ ["meters", "kilometers", 0.3, 0.0003],
47
+ ["miles", "kilometers", 26.21875, 42.194988]
48
+ ].each do |from, to, input, expected|
49
+ method = :"#{from}_to_#{to}"
50
+ specify method do
51
+ expect(M9t::Distance.send(method, input)).to eq(expected)
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ context ".conversions" do
58
+ specify "#to_meters" do
59
+ expect(M9t::Distance.new(0.3).to_meters).to eq(0.3)
60
+ end
61
+
62
+ specify "#to_kilometers" do
63
+ expect(M9t::Distance.new(0.3).to_kilometers).to eq(0.0003)
64
+ end
65
+
66
+ specify "#to_feet" do
67
+ expect(M9t::Distance.new(0.3).to_feet).to be_within(0.00001).of(0.98425)
68
+ end
69
+ end
70
+
71
+ context "#to_s" do
72
+ before do
73
+ I18n.locale = :en
74
+ end
75
+
76
+ it "handles singluar" do
77
+ distance = M9t::Distance.new(1)
78
+ expect(distance.to_s(precision: 0)).to eq("1 meter")
79
+ end
80
+
81
+ it "handles plural" do
82
+ distance = M9t::Distance.new(10)
83
+ expect(distance.to_s(precision: 0)).to eq("10 meters")
84
+ end
85
+
86
+ it "handles abbreviation" do
87
+ distance = M9t::Distance.new(0.3)
88
+ expect(distance.to_s(abbreviated: true)).to eq("0.30000m")
89
+ end
90
+
91
+ specify "units: kilometers" do
92
+ distance = M9t::Distance.new( 156003 )
93
+ expect(distance.to_s(precision: 1, units: :kilometers)).to eq("156.0 kilometers")
94
+ end
95
+
96
+ specify "units: miles, singular" do
97
+ marathon = M9t::Distance.miles(26.21875)
98
+ expect(marathon.to_s(units: :miles, precision: 0)).to eq("26 miles")
99
+ end
100
+
101
+ specify "units: miles, plural" do
102
+ ten_km = M9t::Distance.new(10000)
103
+ expect(ten_km.to_s(units: :miles, precision: 1)).to eq("6.2 miles")
104
+ end
105
+
106
+ context "i18n" do
107
+ before do
108
+ I18n.locale = :it
109
+ end
110
+
111
+ specify "precision: 0" do
112
+ distance = M9t::Distance.new(10)
113
+ expect(distance.to_s(precision: 0)).to eq("10 metri")
114
+ end
115
+
116
+ specify "units: miles" do
117
+ marathon = M9t::Distance.miles(26.21875)
118
+ expect(marathon.to_s(units: :miles, precision: 0)).to eq("26 miglia")
119
+ end
120
+
121
+ specify "units: miles, precision: 1" do
122
+ ten_km = M9t::Distance.new(10000)
123
+ expect(ten_km.to_s(units: :miles, precision: 1)).to eq("6,2 miglia")
124
+ end
125
+ end
126
+ end
127
+ end
data/spec/i18n_spec.rb ADDED
@@ -0,0 +1,54 @@
1
+ require "m9t/i18n"
2
+
3
+ describe I18n do
4
+ before do
5
+ @old_locale = I18n.locale
6
+ end
7
+
8
+ after do
9
+ I18n.locale = @old_locale
10
+ end
11
+
12
+ context "languages" do
13
+ [
14
+ [:en, "mile"],
15
+ [:it, "miglio"],
16
+ [:de, "Meile"],
17
+ ].each do |locale, expected|
18
+ it "has #{locale}" do
19
+ I18n.locale = locale
20
+ expect(I18n.t("units.distance.miles.full.one")).to eq(expected)
21
+ end
22
+ end
23
+ end
24
+
25
+ context "decimal separator" do
26
+ [
27
+ [:en, "."],
28
+ [:it, ","],
29
+ ].each do |locale, separator|
30
+ it "has #{separator} for #{locale}" do
31
+ I18n.locale = locale
32
+ expect(I18n.t("numbers.decimal_separator")).to eq(separator)
33
+ end
34
+ end
35
+ end
36
+
37
+ context ".localize_float" do
38
+ [
39
+ [:en, "."],
40
+ [:it, ","],
41
+ ].each do |locale, separator|
42
+ it "uses the #{separator} separator in #{locale}" do
43
+ I18n.locale = locale
44
+ expected = "1#{separator}500000"
45
+ expect(I18n.localize_float(1.5)).to eq(expected)
46
+ end
47
+ end
48
+
49
+ it "accepts a format indicator" do
50
+ I18n.locale = :en
51
+ expect(I18n.localize_float(1.5, {format: "%0.1f"})).to eq("1.5")
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,22 @@
1
+ require "m9t/pressure"
2
+
3
+ describe M9t::Pressure do
4
+ context "class methods" do
5
+ context ".new" do
6
+ it "returns the identity" do
7
+ expect(M9t::Pressure.new(1.0).value).to eq(1.0)
8
+ end
9
+ end
10
+
11
+ context "other units" do
12
+ [
13
+ [:hectopascals, 0.001],
14
+ [:inches_of_mercury, 0.03386],
15
+ ].each do |unit, expected|
16
+ it "handles #{unit}" do
17
+ expect(M9t::Pressure.send(unit, 1.0).value).to be_within(expected / 1000.0).of(expected)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ require "codeclimate-test-reporter"
2
+ require "rspec"
3
+
4
+ CodeClimate::TestReporter.start
5
+
6
+ if RUBY_PLATFORM != "java"
7
+ require "simplecov"
8
+ SimpleCov.start do
9
+ add_filter "/spec/"
10
+ end
11
+ end
12
+
13
+ require "m9t"
14
+
15
+ RSpec.configure do |config|
16
+ config.run_all_when_everything_filtered = true
17
+ config.filter_run :focus
18
+
19
+ config.order = "random"
20
+ end
@@ -0,0 +1,102 @@
1
+ require "m9t/speed"
2
+
3
+ describe M9t::Speed do
4
+ context "conversion constants" do
5
+ it "has knots" do
6
+ expect(M9t::Speed::KNOTS).to be_within(0.0001).of(1.9438)
7
+ end
8
+ end
9
+
10
+ context "known units" do
11
+ it "gives an error for unknown units" do
12
+ speed = M9t::Speed.new(10)
13
+ expect {
14
+ speed.to_s(units: :foos)
15
+ }.to raise_error(M9t::UnitError)
16
+ end
17
+ end
18
+
19
+ context "class methods" do
20
+ context ".new" do
21
+ it "handles identity" do
22
+ expect(M9t::Speed.new(45).value).to eq(45)
23
+ end
24
+ end
25
+
26
+ context ".measurement_name" do
27
+ it "is 'speed'" do
28
+ expect(M9t::Speed.measurement_name).to eq("speed")
29
+ end
30
+ end
31
+
32
+ context "conversion factories" do
33
+ [
34
+ [:kilometers_per_hour, 0.2778],
35
+ [:miles_per_hour, 0.447],
36
+ [:knots, 0.5144],
37
+ ].each do |unit, expected|
38
+ specify unit do
39
+ expect(M9t::Speed.send(unit, 1.0).value).to be_within(0.0001).of(expected)
40
+ end
41
+ end
42
+ end
43
+
44
+ context "conversions" do
45
+ [
46
+ [:meters_per_second, :miles_per_hour, 45.0, 100.6621],
47
+ ].each do |from, to, input, expected|
48
+ method = :"#{from}_to_#{to}"
49
+ specify method do
50
+ expect(M9t::Speed.send(method, input)).to be_within(0.0001).of(expected)
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ context "conversions" do
57
+ subject { M9t::Speed.new(45) }
58
+
59
+ [
60
+ [:kilometers_per_hour, 162.0],
61
+ [:miles_per_hour, 100.6621],
62
+ ].each do |unit, expected|
63
+ method = :"to_#{unit}"
64
+ specify method do
65
+ expect(subject.send(method)).to be_within(expected / 1000.0).of(expected)
66
+ end
67
+ end
68
+ end
69
+
70
+ context "#to_s" do
71
+ subject { M9t::Speed.new(135.0) }
72
+
73
+ specify "full en" do
74
+ I18n.locale = :en
75
+ expect(subject.to_s).to eq("135.00000 meters per second")
76
+ end
77
+
78
+ specify "full it" do
79
+ I18n.locale = :it
80
+ expect(subject.to_s).to eq("135,00000 metri al second")
81
+ end
82
+
83
+ specify "precision" do
84
+ I18n.locale = :en
85
+ expect(subject.to_s(precision: 0)).to eq("135 meters per second")
86
+ end
87
+
88
+ specify "abbreviated" do
89
+ expect(subject.to_s(abbreviated: true, precision: 0)).to eq("135m/s")
90
+ end
91
+
92
+ specify "units: knots, en" do
93
+ I18n.locale = :en
94
+ expect(subject.to_s(units: :knots, precision: 0)).to eq("262 knots")
95
+ end
96
+
97
+ specify "units: knots, it" do
98
+ I18n.locale = :it
99
+ expect(subject.to_s(units: :knots, precision: 0)).to eq("262 nodi")
100
+ end
101
+ end
102
+ end