arithmetic 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -3
- data/lib/arithmetic/nodes.rb +8 -8
- data/lib/arithmetic/version.rb +1 -1
- data/spec/arithmetic_spec.rb +17 -17
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf1c9eed215d822d2720b2aaf0bdd3f91ab9599210dbdca7af02bb87714b7082
|
4
|
+
data.tar.gz: 1790f872ede7478154ab6787e3f07498a286b66d79f78a48b6f46c886e53da6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e919d795a51f1e1272823d711578ac88768356f5e5f163016f7597c385e4cffa6947eb88f822c09f95b58880eb879a0c41c8201a1e1e7e5272005cdba898a091
|
7
|
+
data.tar.gz: 0ae184ee15440e82314e9f477f50f46eb17ed7ec897969ccce4857bc799a404d5d10650752eb02fcc0b2b92047c4b55e569fdbec4b34ec696f17ac6e74653b27
|
data/Gemfile.lock
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
arithmetic (0.1.
|
4
|
+
arithmetic (0.1.6)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
9
|
diff-lcs (1.1.3)
|
10
|
-
rake (
|
10
|
+
rake (13.0.1)
|
11
11
|
rspec (2.11.0)
|
12
12
|
rspec-core (~> 2.11.0)
|
13
13
|
rspec-expectations (~> 2.11.0)
|
@@ -26,4 +26,4 @@ DEPENDENCIES
|
|
26
26
|
rspec
|
27
27
|
|
28
28
|
BUNDLED WITH
|
29
|
-
2.
|
29
|
+
2.1.4
|
data/lib/arithmetic/nodes.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
module Arithmetic
|
1
|
+
module Arithmetic
|
2
2
|
class OperandNode
|
3
3
|
attr_accessor :operand
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(operand)
|
6
6
|
@operand = operand
|
7
7
|
end
|
@@ -9,12 +9,12 @@ module Arithmetic
|
|
9
9
|
def to_s(visitor, na=nil)
|
10
10
|
visitor.call(@operand)
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def eval
|
14
14
|
if has_dangling_decimal_point?
|
15
|
-
BigDecimal
|
15
|
+
BigDecimal(@operand + "0")
|
16
16
|
else
|
17
|
-
BigDecimal
|
17
|
+
BigDecimal(@operand)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -27,12 +27,12 @@ module Arithmetic
|
|
27
27
|
|
28
28
|
class OperatorNode
|
29
29
|
attr_accessor :operator, :operands
|
30
|
-
|
30
|
+
|
31
31
|
def initialize(operator, operands)
|
32
32
|
@operator = operator
|
33
33
|
@operands = operands
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
def to_s(visitor, top=true)
|
37
37
|
strs = @operands.map {|o| o.to_s(visitor, false) }
|
38
38
|
|
@@ -44,7 +44,7 @@ module Arithmetic
|
|
44
44
|
result
|
45
45
|
end
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
def eval
|
49
49
|
@operator.eval(*@operands.map(&:eval))
|
50
50
|
end
|
data/lib/arithmetic/version.rb
CHANGED
data/spec/arithmetic_spec.rb
CHANGED
@@ -2,47 +2,47 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Arithmetic do
|
4
4
|
it "evaluates simple expressions" do
|
5
|
-
test_eval("2").
|
5
|
+
expect(test_eval("2")).to be == 2.0
|
6
6
|
end
|
7
7
|
|
8
8
|
it "evaluates addition" do
|
9
|
-
test_eval("2 + 2").
|
9
|
+
expect(test_eval("2 + 2")).to be == 4.0
|
10
10
|
end
|
11
11
|
|
12
12
|
it "evaluates subtraction" do
|
13
|
-
test_eval("2.1 - 2").
|
13
|
+
expect(test_eval("2.1 - 2")).to be == 0.1
|
14
14
|
end
|
15
15
|
|
16
16
|
it "handles negative numbers" do
|
17
|
-
test_eval("3--2").
|
17
|
+
expect(test_eval("3--2")).to be == 5
|
18
18
|
end
|
19
19
|
|
20
20
|
it "handles negative numbers with parens" do
|
21
|
-
test_eval("-(3+2)").
|
21
|
+
expect(test_eval("-(3+2)")).to be == -5
|
22
22
|
end
|
23
23
|
|
24
24
|
it "handles leading minus signs" do
|
25
|
-
test_eval("-3+2").
|
25
|
+
expect(test_eval("-3+2")).to be == -1
|
26
26
|
end
|
27
27
|
|
28
28
|
it "has unary minus take precedence over multiplication" do
|
29
|
-
test_eval("-3 * -2").
|
29
|
+
expect(test_eval("-3 * -2")).to be == 6
|
30
30
|
end
|
31
31
|
|
32
32
|
it "evaluates division" do
|
33
|
-
test_eval("10.5 / 5").
|
33
|
+
expect(test_eval("10.5 / 5")).to be == 2.1
|
34
34
|
end
|
35
35
|
|
36
36
|
it "evaluates multiplication" do
|
37
|
-
test_eval("2 * 3.1").
|
37
|
+
expect(test_eval("2 * 3.1")).to be == 6.2
|
38
38
|
end
|
39
39
|
|
40
40
|
it "evaluates parens" do
|
41
|
-
test_eval("2 * (2.1 + 1)").
|
41
|
+
expect(test_eval("2 * (2.1 + 1)")).to be == 6.2
|
42
42
|
end
|
43
43
|
|
44
44
|
it "evaluates regardless of whitespace" do
|
45
|
-
test_eval("2*(1+\t1)").
|
45
|
+
expect(test_eval("2*(1+\t1)")).to be == 4
|
46
46
|
end
|
47
47
|
|
48
48
|
it "evaluates order of operations" do
|
@@ -50,27 +50,27 @@ describe Arithmetic do
|
|
50
50
|
end
|
51
51
|
|
52
52
|
it "evaluates multiple levels of parens" do
|
53
|
-
test_eval("2*(1/(1+3))").
|
53
|
+
expect(test_eval("2*(1/(1+3))")).to be == 0.5
|
54
54
|
end
|
55
55
|
|
56
56
|
it "formats the expression" do
|
57
|
-
test_to_s(" -1+\n 2* \t3").
|
57
|
+
expect(test_to_s(" -1+\n 2* \t3")).to be == '-1 + (2 * 3)'
|
58
58
|
end
|
59
59
|
|
60
60
|
it "formats the expression using a custom visitor" do
|
61
|
-
test_to_s("-1 + 2 * 3", ->(n) { "x#{n}x" }).
|
61
|
+
expect(test_to_s("-1 + 2 * 3", ->(n) { "x#{n}x" })).to be == 'x-xx1x x+x x(xx2x x*x x3xx)x'
|
62
62
|
end
|
63
63
|
|
64
64
|
it "handles ridiculous precision" do
|
65
|
-
test_eval("1.111111111111111111111111111111111111111111 + 2").
|
65
|
+
expect(test_eval("1.111111111111111111111111111111111111111111 + 2")).to be == BigDecimal('3.111111111111111111111111111111111111111111')
|
66
66
|
end
|
67
67
|
|
68
68
|
it "handles simple numbers" do
|
69
|
-
test_eval(2).
|
69
|
+
expect(test_eval(2)).to be == 2
|
70
70
|
end
|
71
71
|
|
72
72
|
it "handles dangling decimal points" do
|
73
|
-
test_eval("0.").
|
73
|
+
expect(test_eval("0.")).to be == 0
|
74
74
|
end
|
75
75
|
|
76
76
|
context "invalid expressions" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arithmetic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Kirby
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2020-03-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -80,8 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
80
|
- !ruby/object:Gem::Version
|
81
81
|
version: '0'
|
82
82
|
requirements: []
|
83
|
-
|
84
|
-
rubygems_version: 2.7.6.2
|
83
|
+
rubygems_version: 3.0.6
|
85
84
|
signing_key:
|
86
85
|
specification_version: 4
|
87
86
|
summary: Simple arithmetic calculator for Ruby
|