unitwise 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
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.1.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: 2017-04-28 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.6.8
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