unitwise 2.0.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,7 +31,7 @@ describe Unitwise::Atom do
31
31
  let(:joule) { Unitwise::Atom.find("J")}
32
32
  describe "#scale" do
33
33
  it "must be nil for base atoms" do
34
- second.scale.must_equal nil
34
+ second.scale.must_be_nil
35
35
  end
36
36
  it "sould be a Scale object for derived atoms" do
37
37
  yard.scale.must_be_instance_of Unitwise::Scale
@@ -126,4 +126,40 @@ describe Unitwise::Atom do
126
126
  second.frozen?.must_equal true
127
127
  end
128
128
  end
129
- end
129
+
130
+ describe "validate!" do
131
+ it "returns true for a valid atom" do
132
+ atom = Unitwise::Atom.new(
133
+ primary_code: "warp",
134
+ secondary_code: "[warp]",
135
+ names: ["Warp", "Warp Factor"],
136
+ scale: {
137
+ value: 1,
138
+ unit_code: "[c]"
139
+ }
140
+ )
141
+
142
+ atom.validate!.must_equal true
143
+ end
144
+
145
+ it "returns an error for an atom with missing properties" do
146
+ atom = Unitwise::Atom.new(names: "hot dog")
147
+
148
+ assert_raises { atom.validate! }
149
+ end
150
+
151
+ it "returns an error for an atom that doesn't resolve" do
152
+ atom = Unitwise::Atom.new(
153
+ primary_code: "feels",
154
+ secondary_code: "FEELS",
155
+ names: ["feels"],
156
+ scale: {
157
+ value: 1,
158
+ unit_code: "hearts"
159
+ }
160
+ )
161
+
162
+ assert_raises { atom.validate! }
163
+ end
164
+ end
165
+ end
@@ -15,8 +15,8 @@ describe Unitwise::Expression::Matcher do
15
15
  it "must be an Alternative list of names" do
16
16
  subject.must_be_instance_of Parslet::Atoms::Alternative
17
17
  end
18
- it "must parse 'Joule'" do
19
- subject.parse('Joule').must_equal('Joule')
18
+ it "must parse 'joule'" do
19
+ subject.parse('joule').must_equal('joule')
20
20
  end
21
21
  end
22
22
 
@@ -39,4 +39,4 @@ describe Unitwise::Expression::Matcher do
39
39
  subject.parse('h').must_equal('h')
40
40
  end
41
41
  end
42
- end
42
+ end
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  describe Unitwise::Functional do
4
4
  subject { Unitwise::Functional }
5
- %w{cel degf hpX hpC tan100 ph ld ln lg 2lg}.each do |function|
5
+ %w{cel degf degre hpX hpC tan100 ph ld ln lg 2lg}.each do |function|
6
6
  describe function do
7
7
  it 'should convert back and forth' do
8
8
  number = rand(1000) / 1000.0
@@ -37,6 +37,13 @@ describe Unitwise::Measurement do
37
37
  it "must convert to a unit of another measurement" do
38
38
  mph.convert_to(kmh).value.must_almost_equal(96.56064)
39
39
  end
40
+ it "must convert to and from another unit without losing precision" do
41
+ circle = Unitwise::Measurement.new(1, "circle")
42
+ circle.to_degree.to_circle.must_equal circle
43
+
44
+ meter = Unitwise::Measurement.new(10, "meter")
45
+ meter.to_mile.to_meter.must_equal meter
46
+ end
40
47
  end
41
48
 
42
49
  describe "#*" do
@@ -238,4 +245,4 @@ describe Unitwise::Measurement do
238
245
  end
239
246
  end
240
247
 
241
- end
248
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ describe Unitwise::Number do
4
+ describe ".simplify" do
5
+ it "simplifies to an Integer" do
6
+ [2, 2.0, BigDecimal.new(2), Rational(2, 1), '2', '2.0'].each do |n|
7
+ assert_equal Unitwise::Number.simplify(n), 2
8
+ end
9
+ end
10
+
11
+ it "simplifies to a Float" do
12
+ [2.25, BigDecimal.new('2.25'), Rational(9, 4), '2.25'].each do |n|
13
+ assert_equal Unitwise::Number.simplify(n), 2.25
14
+ end
15
+ end
16
+
17
+ it "doesn't simplify complex BigDecimals" do
18
+ s = "1234567890.0123456789"
19
+ d = BigDecimal.new(s)
20
+ [s, d].each do |n|
21
+ assert_equal Unitwise::Number.simplify(n), d
22
+ end
23
+ end
24
+
25
+ it "doesn't simplify complex Rationals" do
26
+ r = Rational(22, 7)
27
+ assert_equal Unitwise::Number.simplify(r), r
28
+ end
29
+ end
30
+
31
+ describe ".coefficify" do
32
+ it "converts integer values to Integers" do
33
+ [1, 1.0, "1.0", "1"].each do |n|
34
+ assert Unitwise::Number.coefficify(n).eql?(1)
35
+ end
36
+ end
37
+
38
+ it "converts decimal values to BigDecimals" do
39
+ [1.5, "1.5", "1.50", "0.15e1"].each do |n|
40
+ assert Unitwise::Number.coefficify(n).eql?(BigDecimal.new("1.5"))
41
+ end
42
+ end
43
+ end
44
+
45
+ describe ".rationalize" do
46
+ it "coerces numbers to rationals" do
47
+ [1.5, "1.5", BigDecimal.new("1.5")].each do |n|
48
+ assert_equal Unitwise::Number.rationalize(n), Rational(3, 2)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -28,8 +28,28 @@ describe Unitwise do
28
28
  end
29
29
  end
30
30
 
31
+ describe 'register' do
32
+ it 'should allow custom units to be registered' do
33
+ josh = {
34
+ names: ["Josh W Lewis", "joshwlewis"],
35
+ symbol: "JWL",
36
+ primary_code: "jwl",
37
+ secondary_code: "jwl",
38
+ scale: {
39
+ value: 71.875,
40
+ unit_code: "[in_i]"
41
+ }
42
+ }
43
+
44
+ Unitwise.register(josh)
45
+
46
+ joshes = Unitwise(1, 'mile').to_jwl
47
+
48
+ joshes.to_i.must_equal(881)
49
+ end
50
+ end
51
+
31
52
  it "should have a path" do
32
53
  Unitwise.path.must_match(/unitwise$/)
33
54
  end
34
-
35
55
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unitwise
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Lewis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-13 00:00:00.000000000 Z
11
+ date: 2017-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: liner
@@ -167,6 +167,7 @@ files:
167
167
  - lib/unitwise/expression/transformer.rb
168
168
  - lib/unitwise/functional.rb
169
169
  - lib/unitwise/measurement.rb
170
+ - lib/unitwise/number.rb
170
171
  - lib/unitwise/prefix.rb
171
172
  - lib/unitwise/scale.rb
172
173
  - lib/unitwise/search.rb
@@ -190,6 +191,7 @@ files:
190
191
  - test/unitwise/expression/parser_test.rb
191
192
  - test/unitwise/functional_test.rb
192
193
  - test/unitwise/measurement_test.rb
194
+ - test/unitwise/number_test.rb
193
195
  - test/unitwise/prefix_test.rb
194
196
  - test/unitwise/scale_test.rb
195
197
  - test/unitwise/search_test.rb
@@ -217,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
219
  version: '0'
218
220
  requirements: []
219
221
  rubyforge_project:
220
- rubygems_version: 2.4.5
222
+ rubygems_version: 2.6.11
221
223
  signing_key:
222
224
  specification_version: 4
223
225
  summary: Convert between and perform mathematical operations on physical quantities
@@ -232,6 +234,7 @@ test_files:
232
234
  - test/unitwise/expression/parser_test.rb
233
235
  - test/unitwise/functional_test.rb
234
236
  - test/unitwise/measurement_test.rb
237
+ - test/unitwise/number_test.rb
235
238
  - test/unitwise/prefix_test.rb
236
239
  - test/unitwise/scale_test.rb
237
240
  - test/unitwise/search_test.rb