m9t 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,129 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe M9t::Distance do
5
+ context 'class methods' do
6
+ it 'has a measurement name' do
7
+ expect(M9t::Distance.measurement_name).to eq('distance')
8
+ end
9
+
10
+ context 'default options' do
11
+ it 'has options' do
12
+ expect(M9t::Distance.options).not_to be_nil
13
+ end
14
+
15
+ it 'not abbreviated' do
16
+ expect(M9t::Distance.options[:abbreviated]).to be_false
17
+ end
18
+
19
+ it 'units: meters' do
20
+ expect(M9t::Distance.options[:units]).to be(:meters)
21
+ end
22
+
23
+ it 'precision: 5' do
24
+ expect(M9t::Distance.options[:precision]).to eq(5)
25
+ end
26
+ end
27
+
28
+ it 'constructs from meters' do
29
+ expect(M9t::Distance.new(0.3).to_f).to eq(0.3)
30
+ end
31
+
32
+ context 'units factories' do
33
+ [
34
+ [:meters, 0.3, 0.3],
35
+ [:kilometers, 0.3, 300.0],
36
+ [:miles, 26.21875, 42194.988]
37
+ ].each do |unit, input, result|
38
+ it "#{unit} constructs correctly" do
39
+ distance = M9t::Distance.send(unit, input)
40
+ expect(distance.to_f).to be_within(0.0001).of(result)
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'conversions' do
46
+ [
47
+ ['meters', 'kilometers', 0.3, 0.0003],
48
+ ['miles', 'kilometers', 26.21875, 42.194988]
49
+ ].each do |from, to, input, expected|
50
+ method = :"#{from}_to_#{to}"
51
+ specify method do
52
+ expect(M9t::Distance.send(method, input)).to eq(expected)
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ context '.conversions' do
59
+ specify '#to_meters' do
60
+ expect(M9t::Distance.new(0.3).to_meters).to eq(0.3)
61
+ end
62
+
63
+ specify '#to_kilometers' do
64
+ expect(M9t::Distance.new(0.3).to_kilometers).to eq(0.0003)
65
+ end
66
+
67
+ specify '#to_feet' do
68
+ expect(M9t::Distance.new(0.3).to_feet).to be_within(0.00001).of(0.98425)
69
+ end
70
+ end
71
+
72
+ context '#to_s' do
73
+ before do
74
+ I18n.locale = :en
75
+ end
76
+
77
+ it 'handles singluar' do
78
+ distance = M9t::Distance.new(1)
79
+ expect(distance.to_s(:precision => 0)).to eq('1 meter')
80
+ end
81
+
82
+ it 'handles plural' do
83
+ distance = M9t::Distance.new(10)
84
+ expect(distance.to_s(:precision => 0)).to eq('10 meters')
85
+ end
86
+
87
+ it 'handles abbreviation' do
88
+ distance = M9t::Distance.new(0.3)
89
+ expect(distance.to_s(:abbreviated => true)).to eq('0.30000m')
90
+ end
91
+
92
+ specify 'units: kilometers' do
93
+ distance = M9t::Distance.new( 156003 )
94
+ expect(distance.to_s(:precision => 1, :units => :kilometers)).to eq('156.0 kilometers')
95
+ end
96
+
97
+ specify 'units: miles, singular' do
98
+ marathon = M9t::Distance.miles(26.21875)
99
+ expect(marathon.to_s(:units => :miles, :precision => 0)).to eq('26 miles')
100
+ end
101
+
102
+ specify 'units: miles, plural' do
103
+ ten_km = M9t::Distance.new(10000)
104
+ expect(ten_km.to_s(:units => :miles, :precision => 1)).to eq('6.2 miles')
105
+ end
106
+
107
+ context 'i18n' do
108
+ before do
109
+ I18n.locale = :it
110
+ end
111
+
112
+ specify 'precision: 0' do
113
+ distance = M9t::Distance.new(10)
114
+ expect(distance.to_s(:precision => 0)).to eq('10 metri')
115
+ end
116
+
117
+ specify 'units: miles' do
118
+ marathon = M9t::Distance.miles(26.21875)
119
+ expect(marathon.to_s(:units => :miles, :precision => 0)).to eq('26 miglia')
120
+ end
121
+
122
+ specify 'units: miles, precision: 1' do
123
+ ten_km = M9t::Distance.new(10000)
124
+ expect(ten_km.to_s(:units => :miles, :precision => 1)).to eq('6,2 miglia')
125
+ end
126
+ end
127
+ end
128
+ end
129
+
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe I18n do
5
+ before do
6
+ @old_locale = I18n.locale
7
+ end
8
+
9
+ after do
10
+ I18n.locale = @old_locale
11
+ end
12
+
13
+ context 'languages' do
14
+ [
15
+ [:en, 'mile'],
16
+ [:it, 'miglio'],
17
+ [:de, 'Meile'],
18
+ ].each do |locale, expected|
19
+ it "has #{locale}" do
20
+ I18n.locale = locale
21
+ expect(I18n.t('units.distance.miles.full.one')).to eq(expected)
22
+ end
23
+ end
24
+ end
25
+
26
+ context 'decimal separator' do
27
+ [
28
+ [:en, '.'],
29
+ [:it, ','],
30
+ ].each do |locale, separator|
31
+ it "has #{separator} for #{locale}" do
32
+ I18n.locale = locale
33
+ expect(I18n.t('numbers.decimal_separator')).to eq(separator)
34
+ end
35
+ end
36
+ end
37
+
38
+ context '.localize_float' do
39
+ [
40
+ [:en, '.'],
41
+ [:it, ','],
42
+ ].each do |locale, separator|
43
+ it "uses the #{separator} separator in #{locale}" do
44
+ I18n.locale = locale
45
+ expected = "1#{separator}500000"
46
+ expect(I18n.localize_float(1.5)).to eq(expected)
47
+ end
48
+ end
49
+
50
+ it 'accepts a format indicator' do
51
+ I18n.locale = :en
52
+ expect(I18n.localize_float(1.5, {:format => '%0.1f'})).to eq('1.5')
53
+ end
54
+ end
55
+ end
56
+
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe M9t::Pressure do
5
+ context 'class methods' do
6
+ context '.new' do
7
+ it 'returns the identity' do
8
+ expect(M9t::Pressure.new(1.0).value).to eq(1.0)
9
+ end
10
+ end
11
+
12
+ context 'other units' do
13
+ [
14
+ [:hectopascals, 0.001],
15
+ [:inches_of_mercury, 0.03386],
16
+ ].each do |unit, expected|
17
+ it "handles #{unit}" do
18
+ expect(M9t::Pressure.send(unit, 1.0).value).to be_within(expected / 1000.0).of(expected)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,104 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe M9t::Speed do
5
+ context 'conversion constants' do
6
+ it 'has knots' do
7
+ expect(M9t::Speed::KNOTS).to be_within(0.0001).of(1.9438)
8
+ end
9
+ end
10
+
11
+ context 'known units' do
12
+ it 'gives an error for unknown units' do
13
+ speed = M9t::Speed.new(10)
14
+ expect {
15
+ speed.to_s( :units => :foos )
16
+ }.to raise_error(M9t::UnitError)
17
+ end
18
+ end
19
+
20
+ context 'class methods' do
21
+ context '.new' do
22
+ it 'handles identity' do
23
+ expect(M9t::Speed.new(45).value).to eq(45)
24
+ end
25
+ end
26
+
27
+ context '.measurement_name' do
28
+ it "is 'speed'" do
29
+ expect(M9t::Speed.measurement_name).to eq('speed')
30
+ end
31
+ end
32
+
33
+ context 'conversion factories' do
34
+ [
35
+ [:kilometers_per_hour, 0.2778],
36
+ [:miles_per_hour, 0.447],
37
+ [:knots, 0.5144],
38
+ ].each do |unit, expected|
39
+ specify unit do
40
+ expect(M9t::Speed.send(unit, 1.0).value).to be_within(0.0001).of(expected)
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'conversions' do
46
+ [
47
+ [:meters_per_second, :miles_per_hour, 45.0, 100.6621],
48
+ ].each do |from, to, input, expected|
49
+ method = :"#{from}_to_#{to}"
50
+ specify method do
51
+ expect(M9t::Speed.send(method, input)).to be_within(0.0001).of(expected)
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ context 'conversions' do
58
+ subject { M9t::Speed.new(45) }
59
+
60
+ [
61
+ [:kilometers_per_hour, 162.0],
62
+ [:miles_per_hour, 100.6621],
63
+ ].each do |unit, expected|
64
+ method = :"to_#{unit}"
65
+ specify method do
66
+ expect(subject.send(method)).to be_within(expected / 1000.0).of(expected)
67
+ end
68
+ end
69
+ end
70
+
71
+ context '#to_s' do
72
+ subject { M9t::Speed.new(135.0) }
73
+
74
+ specify 'full en' do
75
+ I18n.locale = :en
76
+ expect(subject.to_s).to eq('135.00000 meters per second')
77
+ end
78
+
79
+ specify 'full it' do
80
+ I18n.locale = :it
81
+ expect(subject.to_s).to eq('135,00000 metri al second')
82
+ end
83
+
84
+ specify 'precision' do
85
+ I18n.locale = :en
86
+ expect(subject.to_s(:precision => 0)).to eq('135 meters per second')
87
+ end
88
+
89
+ specify 'abbreviated' do
90
+ expect(subject.to_s(:abbreviated => true, :precision => 0)).to eq('135m/s')
91
+ end
92
+
93
+ specify 'units: knots, en' do
94
+ I18n.locale = :en
95
+ expect(subject.to_s(:units => :knots, :precision => 0)).to eq('262 knots')
96
+ end
97
+
98
+ specify 'units: knots, it' do
99
+ I18n.locale = :it
100
+ expect(subject.to_s(:units => :knots, :precision => 0)).to eq('262 nodi')
101
+ end
102
+ end
103
+ end
104
+
@@ -0,0 +1,111 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe M9t::Temperature do
5
+ context 'constants' do
6
+ specify 'absolute zero' do
7
+ expect(M9t::Temperature::ABSOLUTE_ZERO).to be_within(0.0001).of(-273.15)
8
+ end
9
+ end
10
+
11
+ context 'default options' do
12
+ specify 'units: meters' do
13
+ expect(M9t::Temperature.options[:units]).to eq(:degrees)
14
+ end
15
+
16
+ specify 'precision: 2' do
17
+ expect(M9t::Temperature.options[:precision]).to eq(5)
18
+ end
19
+ end
20
+
21
+ context 'class methods' do
22
+ context '.new' do
23
+ it 'handles identity' do
24
+ expect(M9t::Temperature.new(45.0).value).to eq(45.0)
25
+ end
26
+ end
27
+
28
+ context '.measurement_name' do
29
+ it "is 'temperature'" do
30
+ expect(M9t::Temperature.measurement_name).to eq('temperature')
31
+ end
32
+ end
33
+
34
+ context 'conversion factories' do
35
+ [
36
+ [:kelvin, -273.15],
37
+ [:fahrenheit, -17.77777778],
38
+ ].each do |unit, expected|
39
+ specify unit do
40
+ expect(M9t::Temperature.send(unit, 0.0).value).to be_within(expected.abs / 1000.0).of(expected)
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'conversions' do
46
+ [
47
+ [:kelvin, 273.15],
48
+ [:fahrenheit, 32.0],
49
+ ].each do |unit, expected|
50
+ method = :"to_#{unit}"
51
+ specify method do
52
+ expect(M9t::Temperature.send(method, 0)).to eq(expected)
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ context 'conversions' do
59
+ subject { M9t::Temperature.new(100) }
60
+
61
+ [
62
+ [:kelvin, 373.15],
63
+ [:fahrenheit, 212.0],
64
+ ].each do |unit, expected|
65
+ method = :"to_#{unit}"
66
+ specify method do
67
+ expect(subject.send(method)).to be_within(expected.abs / 1000.0).of(expected)
68
+ end
69
+ end
70
+ end
71
+
72
+ context '#to_s' do
73
+ subject { M9t::Temperature.new(135) }
74
+
75
+ specify 'en' do
76
+ I18n.locale = :en
77
+ expect(subject.to_s).to eq('135.00000 degrees')
78
+ end
79
+
80
+ specify 'it' do
81
+ I18n.locale = :it
82
+ expect(subject.to_s).to eq('135,00000 gradi')
83
+ end
84
+
85
+ specify 'precision' do
86
+ I18n.locale = :en
87
+ expect(subject.to_s(:precision => 0)).to eq('135 degrees')
88
+ end
89
+
90
+ specify 'abbreviated, en' do
91
+ I18n.locale = :en
92
+ expect(subject.to_s(:abbreviated => true, :precision => 0)).to eq('135°C')
93
+ end
94
+
95
+ specify 'kelvin, en' do
96
+ I18n.locale = :en
97
+ expect(subject.to_s(:units => :kelvin, :precision => 2)).to eq('408.15 kelvin')
98
+ end
99
+
100
+ specify 'kelvin, it' do
101
+ I18n.locale = :it
102
+ expect(subject.to_s(:units => :kelvin, :precision => 2)).to eq('408,15 kelvin')
103
+ end
104
+
105
+ specify 'abbreviated, kelvin, it' do
106
+ I18n.locale = :it
107
+ expect(subject.to_s(:units => :kelvin, :abbreviated => true, :precision => 2)).to eq('408,15K')
108
+ end
109
+ end
110
+ end
111
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: m9t
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-14 00:00:00.000000000 Z
12
+ date: 2013-05-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 0.3.5
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: simplecov
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -84,14 +100,14 @@ files:
84
100
  - locales/en.yml
85
101
  - locales/it.yml
86
102
  - m9t.gemspec
87
- - test/base_test.rb
88
- - test/direction_test.rb
89
- - test/distance_test.rb
90
- - test/i18n_test.rb
91
- - test/pressure_test.rb
92
- - test/speed_test.rb
93
- - test/temperature_test.rb
94
- - test/test_helper.rb
103
+ - spec/spec_helper.rb
104
+ - spec/unit/base_spec.rb
105
+ - spec/unit/direction_spec.rb
106
+ - spec/unit/distance_spec.rb
107
+ - spec/unit/i18n_spec.rb
108
+ - spec/unit/pressure_spec.rb
109
+ - spec/unit/speed_spec.rb
110
+ - spec/unit/temperature_spec.rb
95
111
  homepage: https://github.com/joeyates/m9t
96
112
  licenses: []
97
113
  post_install_message:
@@ -106,7 +122,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
122
  version: '0'
107
123
  segments:
108
124
  - 0
109
- hash: 2381801537073680275
125
+ hash: -2793566875705204263
110
126
  required_rubygems_version: !ruby/object:Gem::Requirement
111
127
  none: false
112
128
  requirements:
@@ -115,18 +131,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
131
  version: '0'
116
132
  segments:
117
133
  - 0
118
- hash: 2381801537073680275
134
+ hash: -2793566875705204263
119
135
  requirements: []
120
136
  rubyforge_project: nowarning
121
137
  rubygems_version: 1.8.23
122
138
  signing_key:
123
139
  specification_version: 3
124
140
  summary: Measurements and conversions library for Ruby
125
- test_files:
126
- - test/base_test.rb
127
- - test/direction_test.rb
128
- - test/distance_test.rb
129
- - test/i18n_test.rb
130
- - test/pressure_test.rb
131
- - test/speed_test.rb
132
- - test/temperature_test.rb
141
+ test_files: []