yadtfp 1.0.2
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.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.yardopts +9 -0
- data/CONTRIBUTING.md +9 -0
- data/Gemfile +4 -0
- data/Guardfile +16 -0
- data/MIT-LICENSE.txt +20 -0
- data/README.md +285 -0
- data/Rakefile +2 -0
- data/bin/yadtfp +3 -0
- data/lib/yadtfp.rb +21 -0
- data/lib/yadtfp/cli.rb +55 -0
- data/lib/yadtfp/configuration.rb +82 -0
- data/lib/yadtfp/outputters.rb +3 -0
- data/lib/yadtfp/outputters/diffable.rb +74 -0
- data/lib/yadtfp/outputters/pretty.rb +128 -0
- data/lib/yadtfp/outputters_factory.rb +26 -0
- data/lib/yadtfp/parsers.rb +1 -0
- data/lib/yadtfp/parsers/ox.rb +328 -0
- data/lib/yadtfp/parsers_factory.rb +24 -0
- data/lib/yadtfp/version.rb +3 -0
- data/spec/cli_spec.rb +44 -0
- data/spec/configuration_spec.rb +115 -0
- data/spec/outputters/diffable_spec.rb +119 -0
- data/spec/outputters/pretty_spec.rb +260 -0
- data/spec/outputters_factory_spec.rb +20 -0
- data/spec/parsers/ox_spec.rb +966 -0
- data/spec/parsers_factory_spec.rb +18 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/support/helpers.rb +22 -0
- data/spec/yadtfp_spec.rb +26 -0
- data/yadtfp.gemspec +34 -0
- metadata +206 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'yadtfp/parsers/ox'
|
2
|
+
|
3
|
+
|
4
|
+
module Yadtfp
|
5
|
+
|
6
|
+
class ParsersFactory
|
7
|
+
|
8
|
+
# Creates and returns parser defined for the supplied parser parameter.
|
9
|
+
#
|
10
|
+
# Note that parameter `parser` needs to be a symbol.
|
11
|
+
# The allowed values for `parser` is:
|
12
|
+
# :ox
|
13
|
+
#
|
14
|
+
# Raises `ArgumentError` if `parser` is not one one of the values specified above.
|
15
|
+
def self.create(parser)
|
16
|
+
|
17
|
+
return Parsers::Ox.new if parser == :ox
|
18
|
+
|
19
|
+
raise ArgumentError, "Invalid parser `#{parser}`"
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
data/spec/cli_spec.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yadtfp/cli'
|
3
|
+
|
4
|
+
|
5
|
+
describe Yadtfp::CLI do
|
6
|
+
|
7
|
+
describe '#diff' do
|
8
|
+
|
9
|
+
let(:cli) { Yadtfp::CLI.new }
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
it 'when default options' do
|
14
|
+
config = instance_double(Yadtfp::Configuration, parser: :ox, outputter: :pretty)
|
15
|
+
|
16
|
+
expect(Yadtfp::Configuration).to receive(:parse_options).with({}).and_return(config)
|
17
|
+
|
18
|
+
parser = instance_double(Yadtfp::Parsers::Ox)
|
19
|
+
expect(Yadtfp::ParsersFactory).to receive(:create).with(:ox).and_return(parser)
|
20
|
+
|
21
|
+
|
22
|
+
file1, file2 = double('file1'), double('file2')
|
23
|
+
left, right = double('left'), double('right')
|
24
|
+
|
25
|
+
|
26
|
+
expect(parser).to receive(:parse).with(file1).and_return(left)
|
27
|
+
expect(parser).to receive(:parse).with(file2).and_return(right)
|
28
|
+
|
29
|
+
|
30
|
+
diff = double('diff')
|
31
|
+
expect(parser).to receive(:diff).with(left, right).and_return(diff)
|
32
|
+
|
33
|
+
|
34
|
+
outputter = instance_double(Yadtfp::Outputters::Pretty)
|
35
|
+
expect(Yadtfp::OutputtersFactory).to receive(:create).with(:pretty, diff).and_return(outputter)
|
36
|
+
expect(outputter).to receive(:print)
|
37
|
+
|
38
|
+
|
39
|
+
cli.diff(file1, file2)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Configuration' do
|
4
|
+
|
5
|
+
let(:config) { Yadtfp::Configuration.instance }
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
describe '#initialize' do
|
10
|
+
|
11
|
+
|
12
|
+
it 'does not allow instantiation of singleton Configuration class' do
|
13
|
+
expect{ Yadtfp::Configuration.new }.to raise_error(NoMethodError)
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
context 'filter' do
|
20
|
+
|
21
|
+
it 'defaults filter to *' do
|
22
|
+
expect(config.filter).to eq '*'
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
context 'parser' do
|
31
|
+
|
32
|
+
it 'defaults parser to :ox' do
|
33
|
+
expect(config.parser).to eq :ox
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
context 'outputter' do
|
42
|
+
|
43
|
+
it 'default outputter' do
|
44
|
+
expect(config.outputter).to eq(:pretty)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
describe '#parser=' do
|
55
|
+
|
56
|
+
it 'when nil' do
|
57
|
+
expect { config.parser = nil }.to raise_error(ArgumentError)
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
it 'when invalid type' do
|
64
|
+
parser = double('foo')
|
65
|
+
allow(parser).to receive(:respond_to?).with(:to_sym).and_return(false)
|
66
|
+
expect { config.parser = parser }.to raise_error(ArgumentError)
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
describe '#outputter=' do
|
75
|
+
|
76
|
+
it 'when nil' do
|
77
|
+
expect { config.outputter = nil }.to raise_error(ArgumentError)
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
it 'when invalid type' do
|
84
|
+
outputter = double('foo')
|
85
|
+
allow(outputter).to receive(:respond_to?).with(:to_sym).and_return(false)
|
86
|
+
expect { config.outputter = outputter }.to raise_error(ArgumentError)
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
describe '.parse_options' do
|
95
|
+
|
96
|
+
it 'when valid hash' do
|
97
|
+
options = { filter: '*', parser: 'ox', outputter: 'pretty' }
|
98
|
+
config = Yadtfp::Configuration.parse_options(options)
|
99
|
+
|
100
|
+
expect(config.filter).to eq '*'
|
101
|
+
expect(config.parser).to eq :ox
|
102
|
+
expect(config.outputter).to eq :pretty
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
it 'when nil' do
|
109
|
+
expect { Yadtfp::Configuration.parse_options(nil) }.to raise_error(ArgumentError)
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'Yadtfp::Outputters::Diffable' do
|
4
|
+
|
5
|
+
|
6
|
+
describe '#initialize' do
|
7
|
+
|
8
|
+
it 'responds to `diff` array' do
|
9
|
+
expect(described_class.new).to respond_to(:diff)
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
it 'when empty array' do
|
16
|
+
described_object = described_class.new([])
|
17
|
+
expect(described_object.instance_variable_get(:@diff)).to eq([])
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
it 'when initialized with nil' do
|
24
|
+
expect{ described_class.new(nil) }.to raise_error(ArgumentError)
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
it 'when initialized without arguments do' do
|
31
|
+
described_object = described_class.new
|
32
|
+
expect{ described_object }.not_to raise_error
|
33
|
+
|
34
|
+
expect(described_object.instance_variable_get(:@diff)).to eq([])
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
describe '#changes' do
|
44
|
+
|
45
|
+
let(:diff) { [{ type: 'c' }] }
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
it 'when changes exist' do
|
50
|
+
obj = described_class.new(diff)
|
51
|
+
expect(obj.changes).to be_a ::Array
|
52
|
+
expect(obj.changes.length).to eq(1)
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
it 'when changes do not exist' do
|
58
|
+
obj = described_class.new
|
59
|
+
expect(obj.changes).to be_a ::Array
|
60
|
+
expect(obj.changes.length).to eq(0)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
describe '#appends' do
|
70
|
+
|
71
|
+
let(:diff) { [{ type: 'a' }] }
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
it 'when appends exist' do
|
76
|
+
obj = described_class.new(diff)
|
77
|
+
expect(obj.appends).to be_a ::Array
|
78
|
+
expect(obj.appends.length).to eq(1)
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
it 'when appends do not exist' do
|
85
|
+
obj = described_class.new
|
86
|
+
expect(obj.appends).to be_a ::Array
|
87
|
+
expect(obj.appends.length).to eq(0)
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
describe '#deletes' do
|
97
|
+
|
98
|
+
let(:diff) { [{ type: 'd' }] }
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
it 'when deletes exist' do
|
103
|
+
obj = described_class.new(diff)
|
104
|
+
expect(obj.deletes).to be_a ::Array
|
105
|
+
expect(obj.deletes.length).to eq(1)
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
it 'when deletes do not exist' do
|
112
|
+
obj = described_class.new
|
113
|
+
expect(obj.deletes).to be_a ::Array
|
114
|
+
expect(obj.deletes.length).to eq(0)
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
@@ -0,0 +1,260 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'outputters/diffable_spec'
|
3
|
+
|
4
|
+
|
5
|
+
describe Yadtfp::Outputters::Pretty do
|
6
|
+
|
7
|
+
it_behaves_like 'Yadtfp::Outputters::Diffable'
|
8
|
+
|
9
|
+
|
10
|
+
describe '#print' do
|
11
|
+
|
12
|
+
it 'prints diff' do
|
13
|
+
pretty = Yadtfp::Outputters::Pretty.new([])
|
14
|
+
|
15
|
+
expect(Yadtfp::Outputters::Pretty::TYPES).to receive(:map).ordered.with(any_args).and_return([])
|
16
|
+
expect(pretty).to receive(:repeatc).with("\xA", 1)
|
17
|
+
expect(pretty).to receive(:summary)
|
18
|
+
|
19
|
+
expect(pretty.print).to be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
describe '#header' do
|
29
|
+
|
30
|
+
|
31
|
+
it "when change 'c'" do
|
32
|
+
diff = [{ type: 'c' }]
|
33
|
+
|
34
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
35
|
+
|
36
|
+
header = "Changes (Replace left value with right value)"
|
37
|
+
header << "\xA#{ "-" * header.length }\xA"
|
38
|
+
|
39
|
+
expect(pretty.header('c')).to eq(header)
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
it "when append 'a'" do
|
46
|
+
diff = [{ type: 'a' }]
|
47
|
+
|
48
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
49
|
+
|
50
|
+
header = "Appends (Add values to left)"
|
51
|
+
header << "\xA#{ "-" * header.length }\xA"
|
52
|
+
|
53
|
+
expect(pretty.header('a')).to eq(header)
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
it "when delete 'd'" do
|
60
|
+
diff = [{ type: 'd' }]
|
61
|
+
|
62
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
63
|
+
|
64
|
+
header = "Deletes (Remove values from left)"
|
65
|
+
header << "\xA#{ "-" * header.length }\xA"
|
66
|
+
|
67
|
+
expect(pretty.header('d')).to eq(header)
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
it 'when invalid type' do
|
74
|
+
pretty = Yadtfp::Outputters::Pretty.new([])
|
75
|
+
|
76
|
+
expect(pretty.header(nil)).to eq('')
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
describe '#body' do
|
86
|
+
|
87
|
+
it "when change is single" do
|
88
|
+
diff = [{ type: 'c', path: '/xml/@id', lvalue: 'root', rvalue: 'root_node' }]
|
89
|
+
|
90
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
91
|
+
|
92
|
+
body = pretty.changes.to_enum.with_index(1).inject("") do |body, (diff, index)|
|
93
|
+
body << "\xA#{index}.\x20Path:\x20/xml/@id"
|
94
|
+
body << "\xA#{ "\x20" * 3 }Left:\x20root"
|
95
|
+
body << "\xA#{ "\x20" * 3 }Right:\x20root_node"
|
96
|
+
body << "\xA"
|
97
|
+
end
|
98
|
+
body << "\xA"
|
99
|
+
|
100
|
+
expect(pretty.body('c')).to eq(body)
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
it "when append is single" do
|
107
|
+
diff = [{ type: 'a', path: '/xml/@id', lvalue: nil, rvalue: 'root' }]
|
108
|
+
|
109
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
110
|
+
|
111
|
+
body = pretty.appends.to_enum.with_index(1).inject("") do |body, (diff, index)|
|
112
|
+
body << "\xA#{index}.\x20Path:\x20/xml/@id"
|
113
|
+
body << "\xA#{ "\x20" * 3 }Left:\x20"
|
114
|
+
body << "\xA#{ "\x20" * 3 }Right:\x20root"
|
115
|
+
body << "\xA"
|
116
|
+
end
|
117
|
+
body << "\xA"
|
118
|
+
|
119
|
+
expect(pretty.body('a')).to eq(body)
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
it 'when append is multiple' do
|
127
|
+
diff = [{ type: 'a', path: '/xml/@id', lvalue: nil, rvalue: [ 'root', 'root_node' ] }]
|
128
|
+
|
129
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
130
|
+
|
131
|
+
body = "#{"\xA"}1.\x20Path:\x20/xml/@id\xA"
|
132
|
+
body << "#{ "\x20" * 3 }Left:\x20\xA"
|
133
|
+
body << "#{ "\x20" * 3 }Right:\x20\xA"
|
134
|
+
body << "#{ "\x20" * 5}1.\x20root\xA"
|
135
|
+
body << "#{ "\x20" * 5}2.\x20root_node\xA"
|
136
|
+
body << "\xA"
|
137
|
+
|
138
|
+
expect(pretty.body('a')).to eq(body)
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
it 'when append contains nil right' do
|
145
|
+
diff = [{ type: 'a', rvalue: nil }]
|
146
|
+
|
147
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
148
|
+
|
149
|
+
expect { pretty.body('a') }.to raise_error(Yadtfp::Outputters::TypeValuesMismatchError)
|
150
|
+
end
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
it "when delete is string type" do
|
157
|
+
diff = [{ type: 'd', path: '/xml/@id', lvalue: 'root', rvalue: nil }]
|
158
|
+
|
159
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
160
|
+
|
161
|
+
body = pretty.deletes.to_enum.with_index(1).inject("") do |body, (diff, index)|
|
162
|
+
body << "\xA#{index}.\x20Path:\x20/xml/@id"
|
163
|
+
body << "\xA#{ "\x20" * 3 }Left:\x20root"
|
164
|
+
body << "\xA#{ "\x20" * 3 }Right:\x20"
|
165
|
+
body << "\xA"
|
166
|
+
end
|
167
|
+
body << "\xA"
|
168
|
+
|
169
|
+
expect(pretty.body('d')).to eq(body)
|
170
|
+
end
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
it 'when delete is multiple' do
|
177
|
+
diff = [{ type: 'd', path: '/xml/@id', lvalue: [ 'root', 'root_node' ], rvalue: nil }]
|
178
|
+
|
179
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
180
|
+
|
181
|
+
body = "#{"\xA"}1.\x20Path:\x20/xml/@id"
|
182
|
+
body << "\xA#{ "\x20" * 3 }Left:\x20\xA"
|
183
|
+
body << "#{ "\x20" * 5}1.\x20root\xA"
|
184
|
+
body << "#{ "\x20" * 5}2.\x20root_node\xA"
|
185
|
+
body << "#{ "\x20" * 3 }Right:\x20\xA"
|
186
|
+
body << "\xA"
|
187
|
+
|
188
|
+
expect(pretty.body('d')).to eq(body)
|
189
|
+
end
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
it 'when delete contains nil left' do
|
196
|
+
diff = [{ type: 'd', lvalue: nil }]
|
197
|
+
|
198
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
199
|
+
|
200
|
+
expect { pretty.body('d') }.to raise_error(Yadtfp::Outputters::TypeValuesMismatchError)
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
it 'when invalid type' do
|
208
|
+
pretty = Yadtfp::Outputters::Pretty.new([])
|
209
|
+
|
210
|
+
expect(pretty.body(nil)).to eq('')
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
describe '#summary' do
|
220
|
+
|
221
|
+
|
222
|
+
it 'when contains changes, appends and deletes' do
|
223
|
+
diff = [
|
224
|
+
{ type: 'c', path: '/xml/@id', lvalue: 'root', rvalue: 'new_root' },
|
225
|
+
{ type: 'a', path: '/xml/@name', lvalue: nil, rvalue: 'Root node' },
|
226
|
+
{ type: 'd', path: '/xml/@class', lvalue: 'root', rvalue: nil }
|
227
|
+
]
|
228
|
+
|
229
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
230
|
+
|
231
|
+
summary = "Summary of differences"
|
232
|
+
summary << "\xA#{ "-" * summary.length }\xA"
|
233
|
+
summary << "Number of differences: 3"
|
234
|
+
summary << "\xA Changes 'c': 1"
|
235
|
+
summary << "\xA Appends 'a': 1"
|
236
|
+
summary << "\xA Deletes 'd': 1"
|
237
|
+
summary << "\xA"
|
238
|
+
|
239
|
+
expect(pretty.summary).to eq(summary)
|
240
|
+
end
|
241
|
+
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
it 'when empty diff' do
|
246
|
+
diff = []
|
247
|
+
|
248
|
+
pretty = Yadtfp::Outputters::Pretty.new(diff)
|
249
|
+
|
250
|
+
summary = "Summary of differences"
|
251
|
+
summary << "\xA#{ "-" * summary.length }\xA"
|
252
|
+
summary << "Number of differences: 0"
|
253
|
+
summary << "\xA"
|
254
|
+
|
255
|
+
expect(pretty.summary).to eq(summary)
|
256
|
+
end
|
257
|
+
|
258
|
+
end
|
259
|
+
|
260
|
+
end
|