gamera-symbolmatrix 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,133 @@
1
+ require 'complete_features_helper'
2
+
3
+ describe SymbolMatrix::Serialization do
4
+ describe '.parse' do
5
+ it 'parses an empty string' do
6
+ sm = SymbolMatrix::Serialization.parse ""
7
+ expect(sm.keys).to be_empty
8
+ end
9
+
10
+ it 'parses a simple string aja:hola' do
11
+ sm = SymbolMatrix::Serialization.parse 'aja:hola'
12
+ expect(sm.aja).to eq 'hola'
13
+ end
14
+
15
+ it 'parses another simple string esto:distinto' do
16
+ expect(
17
+ SymbolMatrix::Serialization
18
+ .parse('esto:distinto')
19
+ .esto
20
+ ).to eq "distinto"
21
+ end
22
+
23
+ it 'parses to a number when is a number' do
24
+ expect(
25
+ SymbolMatrix::Serialization
26
+ .parse("esto:3442")
27
+ .esto
28
+ ).to eq 3442
29
+ end
30
+
31
+ it 'parses a more complex string' do
32
+ sm = SymbolMatrix::Serialization.parse 'hola:eso que:pensas'
33
+ expect(sm.hola).to eq 'eso'
34
+ expect(sm.que).to eq 'pensas'
35
+ end
36
+
37
+ it 'parses another complex string' do
38
+ sm = SymbolMatrix::Serialization
39
+ .parse("esto:es bastante:original gracias:bdd")
40
+ expect(sm.esto).to eq "es"
41
+ expect(sm.bastante).to eq "original"
42
+ expect(sm.gracias).to eq "bdd"
43
+ end
44
+
45
+ it 'parses a string with a single dot' do
46
+ expect(
47
+ SymbolMatrix::Serialization
48
+ .parse("just.one:dot")
49
+ .just.one
50
+ ).to eq "dot"
51
+ end
52
+
53
+ it 'parses another string with single dot' do
54
+ expect(
55
+ SymbolMatrix::Serialization
56
+ .parse("just.one:point")
57
+ .just.one
58
+ ).to eq "point"
59
+ end
60
+
61
+ it 'parses another one, other var name' do
62
+ expect(
63
+ SymbolMatrix::Serialization
64
+ .parse("just.another:data")
65
+ .just.another
66
+ ).to eq "data"
67
+ end
68
+
69
+ it 'parses a string with dots' do
70
+ expect(
71
+ SymbolMatrix::Serialization
72
+ .parse("the.one.with:dots")
73
+ .the.one.with
74
+ ).to eq "dots"
75
+ end
76
+
77
+ it 'parses a string with dots and spaces' do
78
+ sm = SymbolMatrix::Serialization
79
+ .parse("in.one.with:dots the.other.with:dots")
80
+ expect(sm.the.other.with).to eq "dots"
81
+ expect(sm.in.one.with).to eq "dots"
82
+ end
83
+
84
+ it 'merges recursively when needed' do
85
+ sm = SymbolMatrix::Serialization
86
+ .parse("client.host:localhost client.path:hola")
87
+
88
+ expect(sm.client.host).to eq "localhost"
89
+ expect(sm.client.path).to eq "hola"
90
+ end
91
+ end
92
+ end
93
+
94
+ describe SymbolMatrix do
95
+ describe "#initialize" do
96
+ context "a valid path to a file is provided" do
97
+ before do
98
+ Fast.file.write "temp/data.yaml", "a: { nested: { data: with, very: much }, content: to find }"
99
+ end
100
+
101
+ it "loads the data into self" do
102
+ f = SymbolMatrix.new "temp/data.yaml"
103
+ expect(f.a.nested.data).to eq "with"
104
+ end
105
+
106
+ after do
107
+ Fast.dir.remove! :temp
108
+ end
109
+ end
110
+
111
+ context "a YAML string is provided" do
112
+ it "loads the data into self" do
113
+ e = SymbolMatrix.new "beta: { nano: { data: with, very: much }, content: to find }"
114
+ expect(e.beta.nano[:very]).to eq "much"
115
+ end
116
+ end
117
+
118
+ context "a SymbolMatrix serialization is provided" do
119
+ it 'loads the data into self' do
120
+ a = SymbolMatrix.new "those.pesky:attempts of.making.it:work"
121
+ expect(a.those.pesky).to eq "attempts"
122
+ expect(a.of.making.it).to eq "work"
123
+ end
124
+ end
125
+
126
+ context "an empty string is provided" do
127
+ it 'loads nothing into the SymbolMatrix' do
128
+ a = SymbolMatrix ""
129
+ expect(a.keys).to be_empty
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,275 @@
1
+ require "plain_symbolmatrix_helper"
2
+
3
+ describe SymbolMatrix do
4
+ describe "#validate_key" do
5
+ context "an not convertible to Symbol key is passed" do
6
+ it "raises a SymbolMatrix::InvalidKeyException" do
7
+ m = SymbolMatrix.new
8
+ o = Object.new
9
+ expect { m.validate_key o
10
+ }.to raise_error SymbolMatrix::InvalidKeyException, "The key '#{o}' does not respond to #to_sym, so is not a valid key for SymbolMatrix"
11
+ end
12
+ end
13
+ end
14
+
15
+ describe "#store" do
16
+ context "a key is stored using a symbol" do
17
+ it "is foundable with #[<symbol]" do
18
+ a = SymbolMatrix.new
19
+ a.store :a, 2
20
+ expect(a[:a]).to eq 2
21
+ end
22
+ end
23
+
24
+ context "the passed value is a Hash" do
25
+ it "is converted into a SymbolTable" do
26
+ a = SymbolMatrix.new
27
+ a.store :b, { :c => 3 }
28
+ expect(a[:b]).to be_a SymbolMatrix
29
+ end
30
+ end
31
+ end
32
+
33
+ shared_examples_for "any merging operation" do
34
+ it "calls :store for every item in the passed Hash" do
35
+ m = SymbolMatrix.new
36
+ expect(m).to receive(:store).exactly(3).times
37
+ m.send @method, { :a => 1, :b => 3, :c => 4 }
38
+ end
39
+ end
40
+
41
+ describe "#merge!" do
42
+ before { @method = :merge! }
43
+ it_behaves_like "any merging operation"
44
+ end
45
+
46
+ describe "#update" do
47
+ before { @method = :update }
48
+ it_behaves_like "any merging operation"
49
+ end
50
+
51
+ describe "#merge" do
52
+ it "calls #validate_key for each passed item" do
53
+ m = SymbolMatrix.new
54
+ expect(m).to receive(:validate_key).exactly(3).times.and_return(true)
55
+ m.merge :a => 2, :b => 3, :c => 4
56
+ end
57
+ end
58
+
59
+ describe "#[]" do
60
+ context "the matrix is empty" do
61
+ it "raises a SymbolMatrix::KeyNotDefinedException" do
62
+ m = SymbolMatrix.new
63
+ expect { m['t']
64
+ }.to raise_error SymbolMatrix::KeyNotDefinedException, "The key :t is not defined"
65
+ end
66
+ end
67
+
68
+ context "the matrix has a key defined using a symbol" do
69
+ it "returns the same value when called with a string" do
70
+ m = SymbolMatrix.new
71
+ m[:s] = 3
72
+ expect(m["s"]).to eq 3
73
+ end
74
+ end
75
+ end
76
+
77
+ describe "#to_hash" do
78
+ it "shows a deprecation notice" do
79
+ expect(Kernel).to receive(:warn).with "[DEPRECATION]: #to_hash is deprecated, please use #to.hash instead"
80
+ SymbolMatrix.new.to_hash
81
+ end
82
+ end
83
+
84
+ describe ".new" do
85
+ context "a Hash is passed as argument" do
86
+ it "accepts it" do
87
+ m = SymbolMatrix.new :a => 1
88
+ expect(m["a"]).to eq 1
89
+ expect(m[:a]).to eq 1
90
+ end
91
+ end
92
+ end
93
+
94
+ describe "method_missing" do
95
+ it "stores in a key named after the method without the '=' sign" do
96
+ m = SymbolMatrix.new
97
+ m.a = 4
98
+ expect(m[:a]).to eq 4
99
+ end
100
+
101
+ it "returns the same as the symbol representation of the method" do
102
+ m = SymbolMatrix.new
103
+ m.a = 3
104
+ expect(m[:a]).to eq 3
105
+ expect(m["a"]).to eq 3
106
+ expect(m.a).to eq 3
107
+ end
108
+
109
+ it "preserves the class of the argument" do
110
+ class A < SymbolMatrix; end
111
+ class B < SymbolMatrix; end
112
+
113
+ a = A.new
114
+ b = B.new
115
+
116
+ a.a = b
117
+
118
+ expect(a.a).to be_instance_of B
119
+ end
120
+ end
121
+
122
+ describe "#recursive_merge" do
123
+ it 'raises a relevant error when the two matrices collide' do
124
+ sm = SymbolMatrix a: "hola"
125
+ expect { sm.recursive_merge SymbolMatrix a: "hey"
126
+ }.to raise_error SymbolMatrix::MergeError,
127
+ "The value of the :a key is already defined. Run recursive merge with flag true if you want it to override"
128
+ end
129
+
130
+ it 'overrides on collide if the override flag is on' do
131
+ sm = SymbolMatrix a: "hola"
132
+ result = sm.recursive_merge SymbolMatrix(a: "hey"), true
133
+ expect(result.a).to eq "hey"
134
+ end
135
+
136
+ it 'sends the override directive into the recursion' do
137
+ sm = SymbolMatrix a: { b: "hola" }
138
+ result = sm.recursive_merge SymbolMatrix( a: { b: "hey" } ), true
139
+ expect(result.a.b).to eq "hey"
140
+ end
141
+
142
+ it 'merges two symbolmatrices' do
143
+ sm = SymbolMatrix.new a: "hola"
144
+ result = sm.recursive_merge SymbolMatrix.new b: "chau"
145
+ expect(result.a).to eq "hola"
146
+ expect(result.b).to eq "chau"
147
+ end
148
+
149
+ it 'merges two symbolmatrices (new values)' do
150
+ sm = SymbolMatrix.new a: "hey"
151
+ result = sm.recursive_merge SymbolMatrix.new b: "bye"
152
+ expect(result.a).to eq "hey"
153
+ expect(result.b).to eq "bye"
154
+ end
155
+
156
+ it 'merges two symbolmatrices (new keys)' do
157
+ sm = SymbolMatrix.new y: "allo"
158
+ result = sm.recursive_merge SymbolMatrix.new z: "ciao"
159
+ expect(result.y).to eq "allo"
160
+ expect(result.z).to eq "ciao"
161
+ end
162
+
163
+ it 'recursively merges this with that (simple)' do
164
+ sm = SymbolMatrix.new another: { b: "aa" }
165
+ result = sm.recursive_merge SymbolMatrix.new another: { c: "ee" }
166
+ expect(result.another.b).to eq "aa"
167
+ expect(result.another.c).to eq "ee"
168
+ end
169
+
170
+ it 'recursively merges this with that (simple)' do
171
+ sm = SymbolMatrix.new distinct: { b: "rr" }
172
+ result = sm.recursive_merge SymbolMatrix.new distinct: { c: "gg" }
173
+ expect(result.distinct.b).to eq "rr"
174
+ expect(result.distinct.c).to eq "gg"
175
+ end
176
+
177
+ it 'recursively merges this with that v2 (simple)' do
178
+ sm = SymbolMatrix.new a: { z: "ee" }
179
+ result = sm.recursive_merge SymbolMatrix.new a: { g: "oo" }
180
+ expect(result.a.z).to eq "ee"
181
+ expect(result.a.g).to eq "oo"
182
+ end
183
+
184
+ it 'recursively merges this with the argument hash' do
185
+ sm = SymbolMatrix.new a: { b: { c: "hola" } }
186
+ result = sm.recursive_merge a: { b: { d: "aaa" } }
187
+ expect(result.a.b.c).to eq "hola"
188
+ expect(result.a.b.d).to eq "aaa"
189
+ end
190
+ end
191
+
192
+ describe '#recursive_merge!' do
193
+ it 'raises a relevant error when the two matrices collide' do
194
+ sm = SymbolMatrix a: "hola"
195
+ expect { sm.recursive_merge! SymbolMatrix a: "hey"
196
+ }.to raise_error SymbolMatrix::MergeError,
197
+ "The value of the :a key is already defined. Run recursive merge with flag true if you want it to override"
198
+ end
199
+
200
+ it 'overrides on collide if the override flag is on' do
201
+ sm = SymbolMatrix a: "hola"
202
+ sm.recursive_merge! SymbolMatrix(a: "hey"), true
203
+ expect(sm.a).to eq "hey"
204
+ end
205
+
206
+ it 'sends the override directive into the recursion' do
207
+ sm = SymbolMatrix a: { b: "hola" }
208
+ sm.recursive_merge! SymbolMatrix( a: { b: "hey" } ), true
209
+ expect(sm.a.b).to eq "hey"
210
+ end
211
+
212
+ it 'merges a symbolmatrix into this' do
213
+ sm = SymbolMatrix.new a: "hola"
214
+ sm.recursive_merge! SymbolMatrix.new b: "chau"
215
+ expect(sm.a).to eq "hola"
216
+ expect(sm.b).to eq "chau"
217
+ end
218
+
219
+ it 'merges two symbolmatrices (new values)' do
220
+ sm = SymbolMatrix.new a: "hey"
221
+ sm.recursive_merge! SymbolMatrix.new b: "bye"
222
+ expect(sm.a).to eq "hey"
223
+ expect(sm.b).to eq "bye"
224
+ end
225
+
226
+ it 'merges two symbolmatrices (new keys)' do
227
+ sm = SymbolMatrix.new y: "allo"
228
+ sm.recursive_merge! SymbolMatrix.new z: "ciao"
229
+ expect(sm.y).to eq "allo"
230
+ expect(sm.z).to eq "ciao"
231
+ end
232
+
233
+ it 'recursively merges this with that (simple)' do
234
+ sm = SymbolMatrix.new another: { b: "aa" }
235
+ sm.recursive_merge! SymbolMatrix.new another: { c: "ee" }
236
+ expect(sm.another.b).to eq "aa"
237
+ expect(sm.another.c).to eq "ee"
238
+ end
239
+
240
+ it 'recursively merges this with that (simple)' do
241
+ sm = SymbolMatrix.new distinct: { b: "rr" }
242
+ sm.recursive_merge! SymbolMatrix.new distinct: { c: "gg" }
243
+ expect(sm.distinct.b).to eq "rr"
244
+ expect(sm.distinct.c).to eq "gg"
245
+ end
246
+
247
+ it 'recursively merges this with that v2 (simple)' do
248
+ sm = SymbolMatrix.new a: { z: "ee" }
249
+ sm.recursive_merge! SymbolMatrix.new a: { g: "oo" }
250
+ expect(sm.a.z).to eq "ee"
251
+ expect(sm.a.g).to eq "oo"
252
+ end
253
+
254
+ it 'recursively merges this with the argument hash' do
255
+ sm = SymbolMatrix.new a: { b: { c: "hola" } }
256
+ sm.recursive_merge! a: { b: { d: "aaa" } }
257
+ expect(sm.a.b.c).to eq "hola"
258
+ expect(sm.a.b.d).to eq "aaa"
259
+ end
260
+ end
261
+
262
+ it 'is a method that calls SymbolMatrix.new with its arguments' do
263
+ argument = double 'argument'
264
+ expect(SymbolMatrix).to receive(:new).with argument
265
+ SymbolMatrix argument
266
+ end
267
+ end
268
+
269
+ describe SymbolMatrix::KeyNotDefinedException do
270
+ it 'is a subclass of NoMethodError' do
271
+ expect(
272
+ SymbolMatrix::KeyNotDefinedException.superclass
273
+ ).to eq NoMethodError
274
+ end
275
+ end
@@ -0,0 +1,204 @@
1
+ require 'complete_features_helper'
2
+
3
+ describe Writer::SymbolMatrix do
4
+ describe '#initialize' do
5
+ it 'sets the argument as the source' do
6
+ source = double 'source'
7
+ writer = Writer::SymbolMatrix.new source
8
+ expect(writer.source).to eq source
9
+ end
10
+ end
11
+
12
+ describe '#hash' do
13
+ it "returns an instance of Hash" do
14
+ m = SymbolMatrix[:b, 1]
15
+ writer = Writer::SymbolMatrix.new m
16
+ expect(writer.hash).to be_instance_of Hash
17
+ end
18
+
19
+ it "has the same keys" do
20
+ m = SymbolMatrix[:a, 1]
21
+ writer = Writer::SymbolMatrix.new m
22
+ expect(writer.hash[:a]).to eq 1
23
+ end
24
+
25
+ it "has the same keys (more keys)" do
26
+ m = SymbolMatrix[:a, 2]
27
+ writer = Writer::SymbolMatrix.new m
28
+ expect(writer.hash[:a]).to eq 2
29
+ end
30
+
31
+ context "there is some SymbolMatrix within this SymbolMatrix" do
32
+ it "recursively calls #to.hash in it" do
33
+ in_writer = double 'inside writer'
34
+
35
+ inside = SymbolMatrix.new
36
+ expect(inside).to receive(:to).and_return in_writer
37
+ expect(in_writer).to receive(:hash)
38
+
39
+ m = SymbolMatrix[:a, inside]
40
+ writer = Writer::SymbolMatrix.new m
41
+ writer.hash
42
+ end
43
+ end
44
+ end
45
+
46
+ shared_examples_for 'any writer serialization' do
47
+ it 'serializes "try: this" into "try:this"' do
48
+ s = SymbolMatrix try: "this"
49
+ writer = Writer::SymbolMatrix.new s
50
+ expect(writer.send(@method)).to eq "try:this"
51
+ end
52
+
53
+ it 'serializes "try: that" into "try:that"' do
54
+ s = SymbolMatrix try: "that"
55
+ expect(
56
+ Writer::SymbolMatrix
57
+ .new(s).send(@method)
58
+ ).to eq "try:that"
59
+ end
60
+
61
+ it 'serializes "attempt: this" into "attempt:this"' do
62
+ expect(
63
+ Writer::SymbolMatrix
64
+ .new(SymbolMatrix(attempt: "this"))
65
+ .send(@method)
66
+ ).to eq "attempt:this"
67
+ end
68
+
69
+ it 'serializes "attempt: that" into "attempt:that"' do
70
+ expect(
71
+ Writer::SymbolMatrix
72
+ .new(SymbolMatrix(attempt: "that"))
73
+ .send(@method)
74
+ ).to eq "attempt:that"
75
+ end
76
+
77
+ it 'serializes a more complex hash' do
78
+ expect(
79
+ Writer::SymbolMatrix
80
+ .new(SymbolMatrix(a: "b", c: "d"))
81
+ .send(@method)
82
+ ).to eq "a:b c:d"
83
+ end
84
+
85
+ it 'serializes a more complex hash with different values' do
86
+ expect(
87
+ Writer::SymbolMatrix
88
+ .new(SymbolMatrix(g: "e", r: "t"))
89
+ .send(@method)
90
+ ).to eq "g:e r:t"
91
+ end
92
+
93
+ it 'serializes a multidimentional hash' do
94
+ expect(
95
+ Writer::SymbolMatrix
96
+ .new(SymbolMatrix(g: {e: "s"}))
97
+ .send(@method)
98
+ ).to eq "g.e:s"
99
+ end
100
+
101
+ it 'serializes a different multidimentional hash' do
102
+ expect(
103
+ Writer::SymbolMatrix
104
+ .new(SymbolMatrix(r: {e: "s"}))
105
+ .send(@method)
106
+ ).to eq "r.e:s"
107
+ end
108
+
109
+ it 'serializes a yet different multidimentional hash' do
110
+ expect(
111
+ Writer::SymbolMatrix
112
+ .new(SymbolMatrix(r: {a: "s"}))
113
+ .send(@method)
114
+ ).to eq "r.a:s"
115
+ end
116
+
117
+ it 'serializes a more complex multidimentional hash' do
118
+ expect(
119
+ Writer::SymbolMatrix
120
+ .new(SymbolMatrix(r: {a: "s", f: "o"}))
121
+ .send(@method)
122
+ ).to eq "r.a:s r.f:o"
123
+ end
124
+
125
+ it 'serializes a different more complex multidimentional hash' do
126
+ expect(
127
+ Writer::SymbolMatrix
128
+ .new(SymbolMatrix(r: {ar: "s", fe: "o"}))
129
+ .send(@method)
130
+ ).to eq "r.ar:s r.fe:o"
131
+ end
132
+
133
+ it 'serializes a yet different more complex multidimentional hash' do
134
+ expect(
135
+ Writer::SymbolMatrix
136
+ .new(SymbolMatrix(r: {ar: "s", fe: "o", wh: "at"}))
137
+ .send(@method)
138
+ ).to eq "r.ar:s r.fe:o r.wh:at"
139
+ end
140
+
141
+ it 'serializes a more complex multidimentional hash' do
142
+ expect(
143
+ Writer::SymbolMatrix
144
+ .new(SymbolMatrix(r: {a: { f: "o", s: "h", y: "u"}}))
145
+ .send(@method)
146
+ ).to eq "r.a.f:o r.a.s:h r.a.y:u"
147
+ end
148
+
149
+ it 'transforms the multidimentional hash into a simple dot and colons serialization' do
150
+ multidimentional = SymbolMatrix.new hola: {
151
+ the: "start",
152
+ asdfdf: 8989,
153
+ of: {
154
+ some: "multidimentional"
155
+ }
156
+ },
157
+ stuff: "oops"
158
+
159
+ writer = Writer::SymbolMatrix.new multidimentional
160
+ expect(writer.send(@method)).to eq "hola.the:start hola.asdfdf:8989 hola.of.some:multidimentional stuff:oops"
161
+ end
162
+ end
163
+
164
+ describe '#serialization' do
165
+ before { @method = :serialization }
166
+ it_behaves_like 'any writer serialization'
167
+ end
168
+
169
+ describe '#smas' do
170
+ before { @method = :smas }
171
+ it_behaves_like 'any writer serialization'
172
+ end
173
+
174
+ describe '#json' do
175
+ it 'returns a json serialization' do
176
+ sm = SymbolMatrix alpha: { beta: "gamma" }
177
+ writer = Writer::SymbolMatrix.new sm
178
+ expect(writer.json).to eq '{"alpha":{"beta":"gamma"}}'
179
+ end
180
+ end
181
+
182
+ describe '#yaml' do
183
+ it 'returns a yaml serialization' do
184
+ sm = SymbolMatrix alpha: { beta: "gamma" }
185
+ writer = Writer::SymbolMatrix.new sm
186
+ expect(writer.yaml).to include "alpha:\n beta: gamma"
187
+ end
188
+ end
189
+
190
+ describe '#string_key_hash' do
191
+ it 'converts a SymbolMatrix to a multidimentional hash with all string keys' do
192
+ sm = SymbolMatrix alpha: { beta: "gamma" }
193
+ writer = Writer::SymbolMatrix.new sm
194
+ expect(writer.string_key_hash)
195
+ .to eq({ "alpha" => { "beta" => "gamma"} })
196
+ end
197
+ end
198
+ end
199
+
200
+ describe SymbolMatrix do
201
+ it 'includes the Discoverer for Writer' do
202
+ expect(SymbolMatrix.ancestors).to include Discoverer::Writer
203
+ end
204
+ end