samjones-calculator 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 450579aba0c0d50d4f55a692db441e15b278abd8
4
- data.tar.gz: 9b7b92648664b1e93463eedc984330dd17641f08
3
+ metadata.gz: 8496db06a4c79a55168cfdb0355a03c85a3f83e5
4
+ data.tar.gz: 198891de801f830dcfc595546b2daa693535e00c
5
5
  SHA512:
6
- metadata.gz: f7cc190c53a504282bfbd468f517bc7c849ba5163924baa0e58dca4a52cfee89f19390da33d439e1bd509bdc4d6cfab13c5c1ec10679918a65648625723d1086
7
- data.tar.gz: d0e8262c13b58257e4282f0541ce47f745da9c1dad0eedcde022f606d40ac6dd27f2db529ec58c561b5164ba2e99195db34372ecb38715bcd98d042b773ae820
6
+ metadata.gz: 9b10f9ab5b17660bbc280f01cc3e9d94146db21c3e15810edbae24a801b55b1e7b0e5f2044c803b512f990fb5df01e02617c42426742bfb14327fa2ed8d51a20
7
+ data.tar.gz: fe8d3c61d4a48877c0560f000cd013955e1dfd176c0d7def246c836ce75925580b87fff26a48e2966506acf06a2db40c8ec8b8986ebc600d6ccc2de08724d9ee
@@ -1,7 +1,9 @@
1
+ require 'tokenizer/tokenizer'
2
+
1
3
  class StringCalculator
2
- def initialize(value = 0, tokenizer = Tokenizer.new)
4
+ include Tokenizer
5
+ def initialize(value = 0)
3
6
  @value = value
4
- @tokenizer = tokenizer
5
7
  end
6
8
 
7
9
  def value
@@ -9,14 +11,13 @@ class StringCalculator
9
11
  end
10
12
 
11
13
  def add(string)
14
+ check_negatives(string)
12
15
  @value += sum_ints(string)
13
16
  self
14
17
  end
15
18
 
16
19
  def sum_ints(string)
17
- ints = @tokenizer.ints(string)
18
- @negs = ints.select { |i| i<0 }
19
- ints.inject(:+)
20
+ ints(string).inject(:+)
20
21
  end
21
22
 
22
23
 
@@ -25,8 +26,9 @@ class StringCalculator
25
26
  self
26
27
  end
27
28
 
28
- def check_negatives
29
- raise "Negatives not allowed! #{@negs.join(', ')}" if @negs.any?
29
+ def check_negatives(string)
30
+ negs = ints(string).select { |i| i<0 }
31
+ raise "Negatives not allowed! #{negs.join(', ')}" if negs.any?
30
32
  self
31
33
  end
32
34
  end
@@ -1,4 +1,4 @@
1
- class Tokenizer
1
+ module Tokenizer
2
2
  def initialize
3
3
  @regex_or = '|'
4
4
 
@@ -1,116 +1,165 @@
1
1
  require 'calculator/string_calculator'
2
- require 'tokenizer/tokenizer'
3
2
 
4
- def tokenizer(test)
5
- tokenizer = instance_double('Tokenizer')
6
- allow(tokenizer).to receive(:ints) { test[:ints] }
7
- expect(tokenizer).to receive(:ints).with(test[:string])
8
- tokenizer
9
- end
10
3
 
11
4
  describe 'StringCalculator' do
12
5
 
13
- it 'should have a default value' do
14
- expect(StringCalculator.new.value).to eq(0)
15
- expect(StringCalculator.new(0).value).to eq(0)
16
- expect(StringCalculator.new(10).value).to eq(10)
17
- end
18
-
19
- context 'add' do
20
- it 'should add' do
21
- [{
22
- :initial_value => 0,
23
- :string => '10',
24
- :ints => [10],
25
- :result => 10
26
- },
27
- {
28
- :initial_value => 10,
29
- :string => '10',
30
- :ints => [10],
31
- :result => 20
32
- },
33
- {
34
- :initial_value => 10,
35
- :string => '0',
36
- :ints => [0],
37
- :result => 10
38
- }
39
- ].each { |test|
40
- expect(StringCalculator.new(test[:initial_value], tokenizer(test)).add(test[:string]).value).to eq(test[:result])
41
- }
6
+ context 'initial values' do
7
+ it 'should have a default value' do
8
+ expect(StringCalculator.new.value).to eq(0)
42
9
  end
43
10
 
44
- it 'should add with no negatives when checking for negatives' do
45
- [{
46
- :initial_value => 0,
47
- :string => '10',
48
- :ints => [10],
49
- :result => 10
50
- }
51
- ].each { |test|
52
- expect(StringCalculator.new(test[:initial_value], tokenizer(test)).add(test[:string]).check_negatives.value).to eq(test[:result])
53
- }
11
+
12
+ [
13
+ {
14
+ :initial_value => 0,
15
+ :value => 0
16
+ },
17
+ {
18
+ :initial_value => 10,
19
+ :value => 10
20
+ }
21
+ ].each do |test_case|
22
+ it 'should have a value' do
23
+ expect(StringCalculator.new(test_case[:initial_value]).value).to eq(test_case[:value])
24
+ end
54
25
  end
26
+ end
27
+
55
28
 
56
- it 'should not add with negatives when checking for negatives' do
57
- [{
58
- :initial_value => 0,
59
- :string => '-10',
60
- :ints => [-10],
61
- :result => 10
62
- }
63
- ].each { |test|
64
- expect { StringCalculator.new(test[:initial_value], tokenizer(test)).add(test[:string]).check_negatives }.to raise_error('Negatives not allowed! -10')
65
- }
29
+ context 'sum' do
30
+ [
31
+ {
32
+ :initial_value => 0,
33
+ :string => '10,20',
34
+ :ints => [10, 20],
35
+ :sum => 30
36
+ },
37
+ {
38
+ :initial_value => 10,
39
+ :string => '10,20',
40
+ :ints => [10, 20],
41
+ :sum => 30
42
+ },
43
+ {
44
+ :initial_value => 0,
45
+ :string => '10,-20',
46
+ :ints => [10, -20],
47
+ :sum => -10
48
+ },
49
+ {
50
+ :initial_value => 10,
51
+ :string => '10,-20',
52
+ :ints => [10, -20],
53
+ :sum => -10
54
+ }
55
+ ].each do |test_case|
56
+ it 'should sum ints' do
57
+ calculator = StringCalculator.new(test_case[:initial_value])
58
+ allow(calculator).to receive(:ints).and_return(test_case[:ints])
59
+ expect(calculator.sum_ints(test_case[:string])).to eq(test_case[:sum])
60
+ end
66
61
  end
67
62
  end
68
63
 
69
- context 'subtract' do
70
- it 'should subtract' do
71
- [{
72
- :initial_value => 0,
73
- :string => '10',
74
- :ints => [10],
75
- :result => -10
76
- },
77
- {
78
- :initial_value => 10,
79
- :string => '10',
80
- :ints => [10],
81
- :result => 0
82
- }
83
- ].each { |test|
84
- expect(StringCalculator.new(test[:initial_value], tokenizer(test)).subtract(test[:string]).value).to eq(test[:result])
85
- }
64
+
65
+ context 'check negatives' do
66
+ [
67
+ {
68
+ :initial_value => 0,
69
+ :string => '10,20',
70
+ :ints => [10, 20],
71
+ :raise => false
72
+ },
73
+ {
74
+ :initial_value => 0,
75
+ :string => '10,-20',
76
+ :ints => [10, -20],
77
+ :raise => true,
78
+ :error => 'Negatives not allowed! -20'
79
+ },
80
+ {
81
+ :initial_value => 0,
82
+ :string => '10,-20,-30',
83
+ :ints => [10, -20, -30],
84
+ :raise => true,
85
+ :error => 'Negatives not allowed! -20, -30'
86
+ }
87
+ ].each do |test_case|
88
+ it 'should check negatives' do
89
+ calculator = StringCalculator.new(test_case[:initial_value])
90
+ allow(calculator).to receive(:ints).and_return(test_case[:ints])
91
+ expect { calculator.check_negatives(test_case[:string]) }.to raise_error(test_case[:error]) if test_case[:raise]
92
+ expect(calculator.check_negatives(test_case[:string])).to eq(calculator) unless test_case[:raise]
93
+ end
86
94
  end
95
+ end
96
+
87
97
 
88
- it 'should subtract with no negatives when checking for negatives' do
89
- [{
90
- :initial_value => 0,
91
- :string => '10',
92
- :ints => [10],
93
- :result => -10
94
- }
95
- ].each { |test|
96
- expect(StringCalculator.new(test[:initial_value], tokenizer(test)).subtract(test[:string]).check_negatives.value).to eq(test[:result])
97
- }
98
+ context 'add' do
99
+ [
100
+ {
101
+ :initial_value => 0,
102
+ :string => '10,20',
103
+ :ints => [10, 20],
104
+ :value => 30,
105
+ :raise => false
106
+ },
107
+ {
108
+ :initial_value => 10,
109
+ :string => '10,20',
110
+ :ints => [10, 20],
111
+ :value => 40,
112
+ :raise => false
113
+ },
114
+ {
115
+ :initial_value => 0,
116
+ :string => '10,20',
117
+ :ints => [10, 20],
118
+ :raise => true
119
+ }
120
+ ].each do |test_case|
121
+ it 'should add' do
122
+ calculator = StringCalculator.new(test_case[:initial_value])
123
+ allow(calculator).to receive(:ints).and_return(test_case[:ints])
124
+ allow(calculator).to receive(:check_negatives) { raise 'Error' if test_case[:raise] }
125
+ expect { calculator.add(test_case[:string]) }.to raise_error('Error') if test_case[:raise]
126
+ expect(calculator.add(test_case[:string]).value).to eq(test_case[:value]) unless test_case[:raise]
127
+ end
98
128
  end
129
+ end
99
130
 
100
- it 'should not subtract with negatives when checking for negatives' do
101
- [{
102
- :initial_value => 0,
103
- :string => '-10',
104
- :ints => [-10],
105
- :result => 10
106
- }
107
- ].each { |test|
108
- expect { StringCalculator.new(test[:initial_value], tokenizer(test)).subtract(test[:string]).check_negatives }.to raise_error('Negatives not allowed! -10')
109
- }
131
+
132
+ context 'subtract' do
133
+ [
134
+ {
135
+ :initial_value => 0,
136
+ :string => '10,20',
137
+ :ints => [10, 20],
138
+ :value => -30,
139
+ },
140
+ {
141
+ :initial_value => 50,
142
+ :string => '10,20',
143
+ :ints => [10, 20],
144
+ :value => 20,
145
+ }
146
+ ].each do |test_case|
147
+ it 'should subtract' do
148
+ calculator = StringCalculator.new(test_case[:initial_value])
149
+ allow(calculator).to receive(:ints).and_return(test_case[:ints])
150
+ expect(calculator.subtract(test_case[:string]).value).to eq(test_case[:value])
151
+ end
110
152
  end
111
153
  end
112
154
 
113
- it 'should do both' do
114
- expect(StringCalculator.new.add('10').subtract('5').add('15,10').subtract('5,2').value).to eq(23)
155
+
156
+ context 'preserve value' do
157
+ it 'should preserve value calls' do
158
+ calculator = StringCalculator.new(0)
159
+ allow(calculator).to receive(:ints).and_return([10, 20])
160
+ expect(calculator.subtract('10,20').value).to eq(-30)
161
+ allow(calculator).to receive(:ints).and_return([30, 40])
162
+ expect(calculator.add('30,40').value).to eq(40)
163
+ end
115
164
  end
116
165
  end
@@ -1,44 +1,56 @@
1
1
  require 'tokenizer/tokenizer.rb'
2
2
 
3
- describe 'Tokenizer' do
4
- it "should not blow up" do
5
- expect(Tokenizer.new.ints).to eq([])
6
- expect(Tokenizer.new.ints('')).to eq([])
3
+ describe Tokenizer do
4
+
5
+ let(:tokenizer) { Class.new { include Tokenizer }.new }
6
+
7
+
8
+ it 'should not blow up' do
9
+ expect(tokenizer.ints).to eq([])
10
+ expect(tokenizer.ints('')).to eq([])
7
11
  end
8
12
 
9
- it "should tokenize" do
10
- expect(Tokenizer.new.tokens('abc')).to eq(['abc'])
11
- expect(Tokenizer.new.tokens('abc,123')).to eq(['abc', '123'])
13
+
14
+ it 'should tokenize' do
15
+ expect(tokenizer.tokens('abc')).to eq(['abc'])
16
+ expect(tokenizer.tokens('abc,123')).to eq(['abc', '123'])
12
17
  end
13
18
 
14
- it "should handle multiple delimiters" do
15
- expect(Tokenizer.new.tokens("1,2\n3")).to eq(['1', '2', '3'])
19
+
20
+ it 'should handle multiple delimiters' do
21
+ expect(tokenizer.tokens("1,2\n3")).to eq(['1', '2', '3'])
16
22
  end
17
23
 
18
- it "should handle setting delimiter" do
19
- expect(Tokenizer.new.tokens("//'\n1'2")).to eq(['1', '2'])
24
+
25
+ it 'should handle setting delimiter' do
26
+ expect(tokenizer.tokens("//'\n1'2")).to eq(['1', '2'])
20
27
  end
21
28
 
22
- it "should handle setting multiple delimiters" do
23
- expect(Tokenizer.new.tokens("//['][#]\n1'2#3")).to eq(['1', '2', '3'])
29
+
30
+ it 'should handle setting multiple delimiters' do
31
+ expect(tokenizer.tokens("//['][#]\n1'2#3")).to eq(['1', '2', '3'])
24
32
  end
25
33
 
26
- it "should handle complex delimiters" do
27
- expect(Tokenizer.new.tokens("//['#]\n1'#2")).to eq(['1', '2'])
34
+
35
+ it 'should handle complex delimiters' do
36
+ expect(tokenizer.tokens("//['#]\n1'#2")).to eq(['1', '2'])
28
37
  end
29
38
 
30
- it "should test for int" do
31
- expect(Tokenizer.new.is_i?("123")).to eq(true)
32
- expect(Tokenizer.new.is_i?("abc")).to eq(false)
39
+
40
+ it 'should test for int' do
41
+ expect(tokenizer.is_i?("123")).to eq(true)
42
+ expect(tokenizer.is_i?("abc")).to eq(false)
33
43
  end
34
44
 
35
- it "should return ints" do
36
- expect(Tokenizer.new.ints('1')).to eq([1])
37
- expect(Tokenizer.new.ints('1,2')).to eq([1, 2])
45
+
46
+ it 'should return ints' do
47
+ expect(tokenizer.ints('1')).to eq([1])
48
+ expect(tokenizer.ints('1,2')).to eq([1, 2])
38
49
  end
39
50
 
40
- it "should filter non ints" do
41
- expect(Tokenizer.new.ints('abc,123')).to eq([123])
51
+
52
+ it 'should filter non ints' do
53
+ expect(tokenizer.ints('abc,123')).to eq([123])
42
54
  end
43
55
 
44
56
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samjones-calculator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Jones
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-11 00:00:00.000000000 Z
11
+ date: 2016-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -115,8 +115,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  version: '0'
116
116
  requirements: []
117
117
  rubyforge_project:
118
- rubygems_version: 2.4.6
118
+ rubygems_version: 2.6.4
119
119
  signing_key:
120
120
  specification_version: 4
121
121
  summary: String Calculator
122
122
  test_files: []
123
+ has_rdoc: