m9t 0.3.3 → 0.3.4
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.
- data/Gemfile +2 -1
- data/README.md +25 -10
- data/Rakefile +11 -24
- data/lib/m9t/base.rb +62 -19
- data/lib/m9t/version.rb +1 -1
- data/locales/en.yml +13 -0
- data/m9t.gemspec +9 -2
- data/spec/spec_helper.rb +23 -0
- data/spec/unit/base_spec.rb +53 -0
- data/spec/unit/direction_spec.rb +185 -0
- data/spec/unit/distance_spec.rb +129 -0
- data/spec/unit/i18n_spec.rb +56 -0
- data/spec/unit/pressure_spec.rb +24 -0
- data/spec/unit/speed_spec.rb +104 -0
- data/spec/unit/temperature_spec.rb +111 -0
- metadata +29 -20
- data/test/base_test.rb +0 -55
- data/test/direction_test.rb +0 -106
- data/test/distance_test.rb +0 -161
- data/test/i18n_test.rb +0 -42
- data/test/pressure_test.rb +0 -25
- data/test/speed_test.rb +0 -104
- data/test/temperature_test.rb +0 -111
- data/test/test_helper.rb +0 -17
data/test/base_test.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
4
|
-
|
5
|
-
class SomeMeasurement
|
6
|
-
DEFAULT_OPTIONS = {:units => :foos, :abbreviated => false, :decimals => 1}
|
7
|
-
CONVERSIONS = {:foos => 1.0,
|
8
|
-
:bars => 1 / 42.0}
|
9
|
-
|
10
|
-
include M9t::Base
|
11
|
-
end
|
12
|
-
|
13
|
-
class SomeDerivedMeasurement < SomeMeasurement
|
14
|
-
end
|
15
|
-
|
16
|
-
class TestM9tBase < Test::Unit::TestCase
|
17
|
-
|
18
|
-
def setup
|
19
|
-
I18n.locale = :en
|
20
|
-
SomeMeasurement.reset_options!
|
21
|
-
end
|
22
|
-
|
23
|
-
# class methods
|
24
|
-
|
25
|
-
def test_class_method_missing_for_known_units
|
26
|
-
assert_in_delta(0.0714, SomeMeasurement.foos_to_bars(3.0), 0.001)
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_class_method_missing_fails_on_unknown_units
|
30
|
-
assert_raise(RuntimeError) do
|
31
|
-
SomeMeasurement.bazs_to_bars(3.0)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
# instance methods
|
36
|
-
|
37
|
-
def test_instance_method_missing_for_known_units
|
38
|
-
some = SomeMeasurement.new(3.0)
|
39
|
-
assert_in_delta(0.0714, some.to_bars(3.0), 0.001)
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_instance_method_missing_fails_on_unknown_units
|
43
|
-
some = SomeMeasurement.new(3.0)
|
44
|
-
assert_raise(RuntimeError) do
|
45
|
-
some.to_bazs(3.0)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
# derived classes
|
50
|
-
|
51
|
-
def test_derived_class_gets_options
|
52
|
-
assert_equal(SomeMeasurement.options, SomeDerivedMeasurement.options)
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
data/test/direction_test.rb
DELETED
@@ -1,106 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
4
|
-
|
5
|
-
class TestM9tDirection < Test::Unit::TestCase
|
6
|
-
|
7
|
-
def setup
|
8
|
-
I18n.locale = :en
|
9
|
-
M9t::Direction.reset_options!
|
10
|
-
end
|
11
|
-
|
12
|
-
# Basic use
|
13
|
-
def test_degrees
|
14
|
-
assert_equal(45, M9t::Direction.new(45).value)
|
15
|
-
end
|
16
|
-
|
17
|
-
# Class methods
|
18
|
-
|
19
|
-
# Base class
|
20
|
-
|
21
|
-
def test_measurement_name
|
22
|
-
assert_equal('direction', M9t::Direction.measurement_name)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_normalize
|
26
|
-
assert_equal(5, M9t::Direction.normalize(725))
|
27
|
-
assert_equal(5, M9t::Direction.normalize(-355))
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_default_options_set
|
31
|
-
assert_not_nil(M9t::Direction.options)
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_default_option_abbreviated
|
35
|
-
assert(! M9t::Direction.options[:abbreviated])
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_default_option_units
|
39
|
-
assert_equal(:degrees, M9t::Direction.options[:units])
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_to_degrees_identity
|
43
|
-
assert_equal(45, M9t::Direction.degrees_to_degrees(45))
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_degrees_to_compass
|
47
|
-
assert_equal('N', M9t::Direction.degrees_to_compass(0))
|
48
|
-
assert_equal('NE', M9t::Direction.degrees_to_compass(42)) # Rounding up
|
49
|
-
assert_equal('E', M9t::Direction.degrees_to_compass(93)) # Rounding down
|
50
|
-
assert_equal('ESE', M9t::Direction.degrees_to_compass(113)) # 16ths
|
51
|
-
I18n.locale = :it
|
52
|
-
assert_equal('O', M9t::Direction.degrees_to_compass(270))
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_compass
|
56
|
-
assert_equal(0, M9t::Direction.compass('N').value)
|
57
|
-
assert_equal(247.5, M9t::Direction.compass('WSW').value)
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_compass_to_degrees
|
61
|
-
assert_equal(247.5, M9t::Direction.compass_to_degrees('WSW'))
|
62
|
-
end
|
63
|
-
|
64
|
-
# Instance methods
|
65
|
-
|
66
|
-
def test_to_s_not_abbreviated_singular
|
67
|
-
direction = M9t::Direction.new( 1 )
|
68
|
-
|
69
|
-
assert_equal '1 degree', direction.to_s
|
70
|
-
I18n.locale = :it
|
71
|
-
assert_equal '1 grado', direction.to_s
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_to_s_not_abbreviated_plural
|
75
|
-
direction = M9t::Direction.new( 135 )
|
76
|
-
|
77
|
-
assert_equal '135 degrees', direction.to_s
|
78
|
-
I18n.locale = :it
|
79
|
-
assert_equal '135 gradi', direction.to_s
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_to_s_abbreviated
|
83
|
-
direction = M9t::Direction.new( 135 )
|
84
|
-
|
85
|
-
assert_equal '135°', direction.to_s( :abbreviated => true )
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_compass_units
|
89
|
-
direction = M9t::Direction.new( 225 )
|
90
|
-
|
91
|
-
assert_equal 'SW', direction.to_s( :units => :compass )
|
92
|
-
I18n.locale = :it
|
93
|
-
assert_equal 'SO', direction.to_s( :units => :compass )
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_handles_string_leading_zero
|
97
|
-
assert_equal(10, M9t::Direction.new('010').value)
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_to_compass
|
101
|
-
direction = M9t::Direction.new(0)
|
102
|
-
|
103
|
-
assert_equal('N', direction.to_compass)
|
104
|
-
end
|
105
|
-
|
106
|
-
end
|
data/test/distance_test.rb
DELETED
@@ -1,161 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
4
|
-
|
5
|
-
class TestM9tDistance < Test::Unit::TestCase
|
6
|
-
|
7
|
-
def setup
|
8
|
-
I18n.locale = :en
|
9
|
-
M9t::Distance.reset_options!
|
10
|
-
end
|
11
|
-
|
12
|
-
# Basic use
|
13
|
-
def test_singular
|
14
|
-
distance = M9t::Distance.new( 1 )
|
15
|
-
I18n.locale = :en
|
16
|
-
|
17
|
-
assert_equal('1 meter', distance.to_s( :precision => 0 ) )
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_plural
|
21
|
-
distance = M9t::Distance.new( 10 )
|
22
|
-
|
23
|
-
I18n.locale = :en
|
24
|
-
assert_equal( '10 meters', distance.to_s( :precision => 0 ) )
|
25
|
-
I18n.locale = :it
|
26
|
-
assert_equal( '10 metri', distance.to_s( :precision => 0 ) )
|
27
|
-
end
|
28
|
-
|
29
|
-
# Class methods
|
30
|
-
|
31
|
-
# Base class
|
32
|
-
|
33
|
-
def test_unit_name
|
34
|
-
assert_equal('distance', M9t::Distance.measurement_name)
|
35
|
-
end
|
36
|
-
|
37
|
-
# Construction
|
38
|
-
# ...from meters
|
39
|
-
def test_new
|
40
|
-
assert_equal(0.3, M9t::Distance.new(0.3).to_f)
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_class_meters
|
44
|
-
assert_equal(0.3, M9t::Distance.meters(0.3).to_f)
|
45
|
-
end
|
46
|
-
|
47
|
-
# ...from other units
|
48
|
-
def test_class_kilometers
|
49
|
-
assert_in_delta(300.0, M9t::Distance.kilometers(0.3).to_f, 0.00001)
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_class_miles
|
53
|
-
assert_in_delta(1609.344, M9t::Distance.miles(1).to_f, 0.00001)
|
54
|
-
assert_in_delta(42194.988, M9t::Distance.miles(26.21875).to_f, 0.00001)
|
55
|
-
end
|
56
|
-
|
57
|
-
# Conversion class methods
|
58
|
-
# ...to other units
|
59
|
-
def test_class_meters_to_kilometers
|
60
|
-
assert_equal(0.0003, M9t::Distance.meters_to_kilometers(0.3))
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_class_miles_to_kilometers
|
64
|
-
assert_in_delta(42.194988, M9t::Distance.miles_to_kilometers(26.21875), 0.00001)
|
65
|
-
end
|
66
|
-
|
67
|
-
# Default options
|
68
|
-
|
69
|
-
def test_default_options_set
|
70
|
-
assert_not_nil(M9t::Distance.options)
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_default_option_abbreviated
|
74
|
-
assert(! M9t::Distance.options[:abbreviated])
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_default_option_units
|
78
|
-
assert_equal(:meters, M9t::Distance.options[:units])
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_default_option_decimals
|
82
|
-
assert_equal(5, M9t::Distance.options[:precision])
|
83
|
-
end
|
84
|
-
|
85
|
-
# Instance methods
|
86
|
-
|
87
|
-
# Conversion
|
88
|
-
def test_instance_to_meters
|
89
|
-
assert_equal(0.3, M9t::Distance.new(0.3).to_meters)
|
90
|
-
end
|
91
|
-
|
92
|
-
def test_instance_to_kilometers
|
93
|
-
assert_equal(0.0003, M9t::Distance.new(0.3).to_kilometers)
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_instance_to_kilometers
|
97
|
-
assert_in_delta(0.98425, M9t::Distance.new(0.3).to_feet, 0.00001)
|
98
|
-
end
|
99
|
-
|
100
|
-
# to_s
|
101
|
-
def test_to_s_singular
|
102
|
-
assert_equal('1.00000 meter', M9t::Distance.new(1).to_s)
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_to_s_plural
|
106
|
-
assert_equal('0.30000 meters', M9t::Distance.new(0.3).to_s)
|
107
|
-
end
|
108
|
-
|
109
|
-
# i18n
|
110
|
-
def test_to_s_plural_abbreviated
|
111
|
-
distance = M9t::Distance.new( 10 )
|
112
|
-
I18n.locale = :en
|
113
|
-
assert_equal( '10m', distance.to_s( :abbreviated => true,
|
114
|
-
:precision => 0 ) )
|
115
|
-
I18n.locale = :it
|
116
|
-
assert_equal( '10m', distance.to_s( :abbreviated => true,
|
117
|
-
:precision => 0 ) )
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_to_s_abbreviated
|
121
|
-
distance = M9t::Distance.new( 0.3 )
|
122
|
-
|
123
|
-
assert_equal( '0.30000m', distance.to_s( :abbreviated => true ) )
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_to_s_precision
|
127
|
-
distance = M9t::Distance.new( 0.3 )
|
128
|
-
|
129
|
-
assert_equal( '0.3m', distance.to_s( :precision => 1,
|
130
|
-
:abbreviated => true ) )
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_to_s_kilometers
|
134
|
-
distance = M9t::Distance.new( 156003 )
|
135
|
-
|
136
|
-
assert_equal( '156.0 kilometers', distance.to_s( :precision => 1,
|
137
|
-
:units => :kilometers ) )
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_miles_singular
|
141
|
-
marathon = M9t::Distance.miles( 26.21875 )
|
142
|
-
I18n.locale = :en
|
143
|
-
assert_equal( '26 miles', marathon.to_s( :units => :miles,
|
144
|
-
:precision => 0 ) )
|
145
|
-
I18n.locale = :it
|
146
|
-
assert_equal( '26 miglia', marathon.to_s( :units => :miles,
|
147
|
-
:precision => 0 ) )
|
148
|
-
end
|
149
|
-
|
150
|
-
def test_to_s_miles_plural
|
151
|
-
ten_km = M9t::Distance.new( 10000 )
|
152
|
-
|
153
|
-
I18n.locale = :en
|
154
|
-
assert_equal( '6.2 miles', ten_km.to_s( :units => :miles,
|
155
|
-
:precision => 1 ) )
|
156
|
-
I18n.locale = :it
|
157
|
-
assert_equal( '6,2 miglia', ten_km.to_s( :units => :miles,
|
158
|
-
:precision => 1 ) )
|
159
|
-
end
|
160
|
-
|
161
|
-
end
|
data/test/i18n_test.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
4
|
-
|
5
|
-
class TestI18nMonkeyPatching < Test::Unit::TestCase
|
6
|
-
def setup
|
7
|
-
@old_locale = I18n.locale
|
8
|
-
end
|
9
|
-
|
10
|
-
def teardown
|
11
|
-
I18n.locale = @old_locale
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_english_loaded
|
15
|
-
I18n.locale = :en
|
16
|
-
assert_equal('.', I18n.t('numbers.decimal_separator'))
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_italian_loaded
|
20
|
-
I18n.locale = :it
|
21
|
-
assert_equal(',', I18n.t('numbers.decimal_separator'))
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_german_loaded
|
25
|
-
I18n.locale = :de
|
26
|
-
assert_equal('Meile', I18n.t('units.distance.miles.full.one'))
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_localize_float_default
|
30
|
-
assert_equal('1.500000', I18n.localize_float(1.5))
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_localize_float_formatted
|
34
|
-
assert_equal('1.5', I18n.localize_float(1.5, {:format => '%0.1f'}))
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_localize_float_italian
|
38
|
-
I18n.locale = :it
|
39
|
-
assert_equal('1,5', I18n.localize_float(1.5, {:format => '%0.1f'}))
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
data/test/pressure_test.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
4
|
-
|
5
|
-
class TestM9tDistance < Test::Unit::TestCase
|
6
|
-
|
7
|
-
def setup
|
8
|
-
I18n.locale = :en
|
9
|
-
M9t::Pressure.reset_options!
|
10
|
-
end
|
11
|
-
|
12
|
-
# Basic use
|
13
|
-
def test_bar
|
14
|
-
assert_equal(1.0, M9t::Pressure.new(1.0).value)
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_hectopascals
|
18
|
-
assert_equal(0.001, M9t::Pressure.hectopascals(1.0).value)
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_inches_of_mercury
|
22
|
-
assert_in_delta(0.03386, M9t::Pressure.inches_of_mercury(1.0).value, 0.00001)
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
data/test/speed_test.rb
DELETED
@@ -1,104 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
4
|
-
|
5
|
-
class TestM9tSpeed < Test::Unit::TestCase
|
6
|
-
|
7
|
-
def setup
|
8
|
-
I18n.locale = :en
|
9
|
-
M9t::Speed.reset_options!
|
10
|
-
end
|
11
|
-
|
12
|
-
# Basic use
|
13
|
-
def test_new
|
14
|
-
assert_equal(45, M9t::Speed.new(45).value)
|
15
|
-
end
|
16
|
-
|
17
|
-
# Class methods
|
18
|
-
|
19
|
-
# Base class
|
20
|
-
|
21
|
-
def test_measurement_name
|
22
|
-
assert_equal('speed', M9t::Speed.measurement_name)
|
23
|
-
end
|
24
|
-
|
25
|
-
# conversion constants
|
26
|
-
|
27
|
-
def test_knot_conversion
|
28
|
-
assert_in_delta(1.9438, M9t::Speed::KNOTS, 0.0001)
|
29
|
-
end
|
30
|
-
|
31
|
-
# input conversions
|
32
|
-
|
33
|
-
def test_class_kilometers_per_hour
|
34
|
-
assert_in_delta(0.2778, M9t::Speed.kilometers_per_hour(1).value, 0.0001)
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_class_miles_per_hour
|
38
|
-
assert_in_delta(0.447, M9t::Speed.miles_per_hour(1).value, 0.0001)
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_class_knots
|
42
|
-
assert_in_delta 0.5144, M9t::Speed.knots(1).value, 0.0001
|
43
|
-
end
|
44
|
-
|
45
|
-
# output conversions
|
46
|
-
|
47
|
-
def test_class_to_miles_per_hour
|
48
|
-
assert_in_delta(100.6621, M9t::Speed.meters_per_second_to_miles_per_hour(45), 0.0001)
|
49
|
-
end
|
50
|
-
|
51
|
-
# Instance methods
|
52
|
-
|
53
|
-
# new
|
54
|
-
|
55
|
-
def test_unknown_units
|
56
|
-
speed = M9t::Speed.new( '010' )
|
57
|
-
|
58
|
-
assert_raises( M9t::UnitError ) do
|
59
|
-
speed.to_s( :units => :foos )
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
# output conversions
|
64
|
-
|
65
|
-
def test_kmh
|
66
|
-
assert_equal(162, M9t::Speed.new(45).to_kilometers_per_hour)
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_mph
|
70
|
-
assert_in_delta(100.6621, M9t::Speed.new(45).to_miles_per_hour, 0.0001)
|
71
|
-
end
|
72
|
-
|
73
|
-
# to_s
|
74
|
-
|
75
|
-
def test_to_s
|
76
|
-
assert_equal '135.00000 meters per second', M9t::Speed.new(135).to_s
|
77
|
-
I18n.locale = :it
|
78
|
-
assert_equal '135,00000 metri al second', M9t::Speed.new(135).to_s
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_to_s_precision
|
82
|
-
speed = M9t::Speed.new( 135 )
|
83
|
-
|
84
|
-
assert_equal '135 meters per second', speed.to_s( :precision => 0 )
|
85
|
-
end
|
86
|
-
|
87
|
-
def test_to_s_abbreviated
|
88
|
-
speed = M9t::Speed.new( 135 )
|
89
|
-
|
90
|
-
assert_equal '135m/s', speed.to_s( :abbreviated => true,
|
91
|
-
:precision => 0 )
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_to_s_knots
|
95
|
-
speed = M9t::Speed.new( 135 )
|
96
|
-
|
97
|
-
assert_equal '262 knots', speed.to_s( :units => :knots,
|
98
|
-
:precision => 0 )
|
99
|
-
I18n.locale = :it
|
100
|
-
assert_equal '262 nodi', speed.to_s( :units => :knots,
|
101
|
-
:precision => 0 )
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|