xembly 0.2 → 0.3
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 +4 -4
- data/.rubocop.yml +2 -0
- data/README.md +2 -2
- data/bin/xembly +2 -2
- data/features/cli.feature +14 -0
- data/lib/xembly/add.rb +2 -0
- data/lib/xembly/addif.rb +50 -0
- data/lib/xembly/attr.rb +3 -0
- data/lib/xembly/directives.rb +16 -1
- data/lib/xembly/remove.rb +38 -0
- data/lib/xembly/set.rb +42 -0
- data/lib/xembly/strict.rb +40 -0
- data/lib/xembly/up.rb +36 -0
- data/lib/xembly/version.rb +1 -1
- data/lib/xembly/xpath.rb +6 -2
- data/test/test_addif.rb +55 -0
- data/test/test_remove.rb +42 -0
- data/test/test_set.rb +43 -0
- data/test/test_xembler.rb +5 -2
- data/test/test_xembly.rb +6 -2
- metadata +12 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aefd3284a7d99ffa9bc9b2d477263a1b47a220a3
|
4
|
+
data.tar.gz: 1d795786c9081702f40b6aa43668af5d07964795
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f56633f3ed7ebe4f8feea6fe724d7f1a22a14e721a8f12e4665aadfcaf567fd43443f202c0eb91b8ac29dfdb22c7fad44a858d609c0a399b90976b28941aed52
|
7
|
+
data.tar.gz: 033130d913d80d85d0f205ec519dfcd474d7626b1f3577f4dede4a54a283a11591cb830669deef211b2f3ea45f9088509be7f67d7d2348bd39f0f681702082e5
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -36,7 +36,7 @@ in file `doc.xml`:
|
|
36
36
|
```xml
|
37
37
|
<books>
|
38
38
|
<book isbn="0735619654">Object Thinking</book>
|
39
|
-
<book isbn="1519166915">
|
39
|
+
<book isbn="1519166915">Elegant Objects</book>
|
40
40
|
</books>
|
41
41
|
```
|
42
42
|
|
@@ -46,7 +46,7 @@ Now, say, you want to add one more book there:
|
|
46
46
|
$ xembly --xml doc.xml 'XPATH "/books"; ADD "book"; ATTR "isbn", "0201379430"; SET "Object Design";'
|
47
47
|
<books>
|
48
48
|
<book isbn="0735619654">Object Thinking</book>
|
49
|
-
<book isbn="1519166915">
|
49
|
+
<book isbn="1519166915">Elegant Objects</book>
|
50
50
|
<book isbn="0201379430">Object Design</book>
|
51
51
|
</books>
|
52
52
|
```
|
data/bin/xembly
CHANGED
@@ -24,11 +24,11 @@
|
|
24
24
|
STDOUT.sync = true
|
25
25
|
|
26
26
|
require 'xembly'
|
27
|
-
require 'slop'
|
28
27
|
require 'xembly/version'
|
28
|
+
require 'slop'
|
29
29
|
|
30
30
|
opts = Slop.parse(ARGV, strict: true, help: true) do |o|
|
31
|
-
o.banner = "Usage (#{Xembly::VERSION}): xembly [options]"
|
31
|
+
o.banner = "Usage (#{Xembly::VERSION}): xembly [options] [directives]"
|
32
32
|
o.on '-h', '--help', 'Print help' do
|
33
33
|
puts o
|
34
34
|
exit
|
data/features/cli.feature
CHANGED
@@ -17,17 +17,31 @@ Feature: Command Line Processing
|
|
17
17
|
<books>
|
18
18
|
<book id="1"/>
|
19
19
|
<book id="2"/>
|
20
|
+
<garbage/>
|
20
21
|
</books>
|
21
22
|
"""
|
22
23
|
And I have a "dirs.txt" file with content:
|
23
24
|
"""
|
24
25
|
XPATH "/books";
|
25
26
|
ADD "book";
|
27
|
+
ATTR "isbn", "1519166915";
|
28
|
+
SET "Elegant Objects";
|
29
|
+
UP;
|
30
|
+
ADD "author";
|
31
|
+
ADDIF "name";
|
32
|
+
SET "yegor";
|
33
|
+
UP; UP;
|
34
|
+
XPATH "garbage";
|
35
|
+
STRICT "1";
|
36
|
+
REMOVE;
|
26
37
|
"""
|
27
38
|
When I run bin/xembly with "-v -d dirs.txt -f out.xml -x text.xml"
|
28
39
|
Then Exit code is zero
|
29
40
|
And Stdout contains "reading text.xml"
|
30
41
|
And XML file "out.xml" matches "/books[count(book) = 3]"
|
42
|
+
And XML file "out.xml" matches "/books/book[@isbn='1519166915' and .='Elegant Objects']"
|
43
|
+
And XML file "out.xml" matches "/books[author='yegor']"
|
44
|
+
And XML file "out.xml" matches "/books[not(garbage)]"
|
31
45
|
|
32
46
|
Scenario: Rejects unknown options
|
33
47
|
When I run bin/xembly with "--some-unknown-option"
|
data/lib/xembly/add.rb
CHANGED
@@ -20,6 +20,7 @@
|
|
20
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
21
|
# SOFTWARE.
|
22
22
|
|
23
|
+
require 'xembly'
|
23
24
|
require 'nokogiri'
|
24
25
|
|
25
26
|
module Xembly
|
@@ -37,6 +38,7 @@ module Xembly
|
|
37
38
|
child = Nokogiri::XML::Node.new(@name, dom)
|
38
39
|
node.add_child(child)
|
39
40
|
after.push(child)
|
41
|
+
Xembly.log.info "node \"#{@name}\" added to \"#{node.name}\""
|
40
42
|
end
|
41
43
|
after
|
42
44
|
end
|
data/lib/xembly/addif.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Copyright (c) 2016 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require 'xembly'
|
24
|
+
require 'nokogiri'
|
25
|
+
|
26
|
+
module Xembly
|
27
|
+
# ADDIF directive
|
28
|
+
class AddIf
|
29
|
+
# Ctor.
|
30
|
+
# +name+:: Node name to add
|
31
|
+
def initialize(name)
|
32
|
+
@name = name
|
33
|
+
end
|
34
|
+
|
35
|
+
def exec(dom, cursor)
|
36
|
+
after = []
|
37
|
+
cursor.each do |node|
|
38
|
+
if !node.element_children.any? { |e| e.name == @name }
|
39
|
+
child = Nokogiri::XML::Node.new(@name, dom)
|
40
|
+
node.add_child(child)
|
41
|
+
after.push(child)
|
42
|
+
Xembly.log.info "node \"#{@name}\" added to \"#{node.name}\""
|
43
|
+
else
|
44
|
+
after.push(node)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
after
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/xembly/attr.rb
CHANGED
@@ -20,6 +20,8 @@
|
|
20
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
21
|
# SOFTWARE.
|
22
22
|
|
23
|
+
require 'xembly'
|
24
|
+
|
23
25
|
module Xembly
|
24
26
|
# ATTR directive
|
25
27
|
class Attr
|
@@ -34,6 +36,7 @@ module Xembly
|
|
34
36
|
def exec(_, cursor)
|
35
37
|
cursor.each do |node|
|
36
38
|
node[@name] = @value
|
39
|
+
Xembly.log.info "attribute \"#{@name}\" set for node \"#{node.name}\""
|
37
40
|
end
|
38
41
|
cursor
|
39
42
|
end
|
data/lib/xembly/directives.rb
CHANGED
@@ -21,7 +21,12 @@
|
|
21
21
|
# SOFTWARE.
|
22
22
|
|
23
23
|
require 'xembly/add'
|
24
|
+
require 'xembly/addif'
|
24
25
|
require 'xembly/attr'
|
26
|
+
require 'xembly/remove'
|
27
|
+
require 'xembly/set'
|
28
|
+
require 'xembly/strict'
|
29
|
+
require 'xembly/up'
|
25
30
|
require 'xembly/xpath'
|
26
31
|
|
27
32
|
module Xembly
|
@@ -47,15 +52,25 @@ module Xembly
|
|
47
52
|
|
48
53
|
def self.map(text)
|
49
54
|
cmd, tail = text.strip.split(/\s+/, 2)
|
50
|
-
args = tail.strip
|
55
|
+
args = (tail.nil? ? '' : tail).strip
|
51
56
|
.scan(/"([^"]+)"/)
|
52
57
|
.flatten
|
53
58
|
.map { |a| a.tr('"', '') }
|
54
59
|
case cmd.upcase
|
55
60
|
when 'ADD'
|
56
61
|
Add.new(args[0])
|
62
|
+
when 'ADDIF'
|
63
|
+
AddIf.new(args[0])
|
57
64
|
when 'ATTR'
|
58
65
|
Attr.new(args[0], args[1])
|
66
|
+
when 'REMOVE'
|
67
|
+
Remove.new
|
68
|
+
when 'SET'
|
69
|
+
Set.new(args[0])
|
70
|
+
when 'STRICT'
|
71
|
+
Strict.new(args[0])
|
72
|
+
when 'UP'
|
73
|
+
Up.new
|
59
74
|
when 'XPATH'
|
60
75
|
Xpath.new(args[0])
|
61
76
|
else
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Copyright (c) 2016 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require 'xembly'
|
24
|
+
|
25
|
+
module Xembly
|
26
|
+
# REMOVE directive
|
27
|
+
class Remove
|
28
|
+
def exec(_, cursor)
|
29
|
+
after = []
|
30
|
+
cursor.each do |node|
|
31
|
+
Xembly.log.info "node \"#{node.name}\" removed"
|
32
|
+
node.remove
|
33
|
+
after.push(node.parent)
|
34
|
+
end
|
35
|
+
after
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/xembly/set.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Copyright (c) 2016 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require 'xembly'
|
24
|
+
|
25
|
+
module Xembly
|
26
|
+
# SET directive
|
27
|
+
class Set
|
28
|
+
# Ctor.
|
29
|
+
# +value+:: Text value to set
|
30
|
+
def initialize(value)
|
31
|
+
@value = value
|
32
|
+
end
|
33
|
+
|
34
|
+
def exec(_, cursor)
|
35
|
+
cursor.each do |node|
|
36
|
+
node.content = @value
|
37
|
+
Xembly.log.info "node \"#{node.name}\" text content set"
|
38
|
+
end
|
39
|
+
cursor
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Copyright (c) 2016 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require 'nokogiri'
|
24
|
+
|
25
|
+
module Xembly
|
26
|
+
# STRICT directive
|
27
|
+
class Strict
|
28
|
+
# Ctor.
|
29
|
+
# +count+:: How many nodes to expect
|
30
|
+
def initialize(count)
|
31
|
+
@count = count.to_i
|
32
|
+
end
|
33
|
+
|
34
|
+
def exec(_, cursor)
|
35
|
+
fail "there are #{cursor.length} nodes, while #{@count} expected" unless \
|
36
|
+
cursor.length == @count
|
37
|
+
cursor
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/xembly/up.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Copyright (c) 2016 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require 'nokogiri'
|
24
|
+
|
25
|
+
module Xembly
|
26
|
+
# UP directive
|
27
|
+
class Up
|
28
|
+
def exec(_, cursor)
|
29
|
+
after = []
|
30
|
+
cursor.each do |node|
|
31
|
+
after.push(node.parent)
|
32
|
+
end
|
33
|
+
after
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/xembly/version.rb
CHANGED
data/lib/xembly/xpath.rb
CHANGED
data/test/test_addif.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Copyright (c) 2016 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require 'xembly/addif'
|
24
|
+
require 'test__helper'
|
25
|
+
|
26
|
+
# Xembly::AddIf tests.
|
27
|
+
# Author:: Yegor Bugayenko (yegor@teamed.io)
|
28
|
+
# Copyright:: Copyright (c) 2016 Yegor Bugayenko
|
29
|
+
# License:: MIT
|
30
|
+
class TestAddIf < XeTest
|
31
|
+
def test_skips_nodes
|
32
|
+
dom = Nokogiri::XML('<books><book/></books>')
|
33
|
+
Xembly::AddIf.new('book').exec(dom, [dom.xpath('/*').first])
|
34
|
+
matches(
|
35
|
+
dom.to_xml,
|
36
|
+
[
|
37
|
+
'/books',
|
38
|
+
'/books[count(book)=1]'
|
39
|
+
]
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_adds_nodes
|
44
|
+
dom = Nokogiri::XML('<books><book/></books>')
|
45
|
+
Xembly::AddIf.new('book-2').exec(dom, [dom.xpath('/*').first])
|
46
|
+
matches(
|
47
|
+
dom.to_xml,
|
48
|
+
[
|
49
|
+
'/books',
|
50
|
+
'/books[count(book)=1]',
|
51
|
+
'/books[count(book-2)=1]'
|
52
|
+
]
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
data/test/test_remove.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Copyright (c) 2016 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require 'xembly/remove'
|
24
|
+
require 'test__helper'
|
25
|
+
|
26
|
+
# Xembly::Remove tests.
|
27
|
+
# Author:: Yegor Bugayenko (yegor@teamed.io)
|
28
|
+
# Copyright:: Copyright (c) 2016 Yegor Bugayenko
|
29
|
+
# License:: MIT
|
30
|
+
class TestRemove < XeTest
|
31
|
+
def test_adds_nodes
|
32
|
+
dom = Nokogiri::XML('<books><book id="1"/><book id="2"/></books>')
|
33
|
+
Xembly::Remove.new.exec(dom, [dom.xpath('/books/book[@id=1]').first])
|
34
|
+
matches(
|
35
|
+
dom.to_xml,
|
36
|
+
[
|
37
|
+
'/books',
|
38
|
+
'/books[count(book)=1]'
|
39
|
+
]
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
data/test/test_set.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Copyright (c) 2016 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require 'xembly/set'
|
24
|
+
require 'test__helper'
|
25
|
+
|
26
|
+
# Xembly::Set tests.
|
27
|
+
# Author:: Yegor Bugayenko (yegor@teamed.io)
|
28
|
+
# Copyright:: Copyright (c) 2016 Yegor Bugayenko
|
29
|
+
# License:: MIT
|
30
|
+
class TestSet < XeTest
|
31
|
+
def test_sets_values
|
32
|
+
dom = Nokogiri::XML('<books><book/></books>')
|
33
|
+
Xembly::Set.new('hello').exec(dom, [dom.xpath('/books/book').first])
|
34
|
+
matches(
|
35
|
+
dom.to_xml,
|
36
|
+
[
|
37
|
+
'/books',
|
38
|
+
'/books[count(book)=1]',
|
39
|
+
'/books/book[.="hello"]'
|
40
|
+
]
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
data/test/test_xembler.rb
CHANGED
@@ -33,13 +33,16 @@ require 'test__helper'
|
|
33
33
|
class TestXembler < XeTest
|
34
34
|
def test_modifies_xml
|
35
35
|
xembler = Xembly::Xembler.new(
|
36
|
-
Xembly::Directives.new(
|
36
|
+
Xembly::Directives.new(
|
37
|
+
'XPATH "/books"; ADD "book"; ADD "test"; UP; ADD "title"; SET "hi";'
|
38
|
+
)
|
37
39
|
)
|
38
40
|
matches(
|
39
41
|
xembler.apply('<books/>').to_xml,
|
40
42
|
[
|
41
43
|
'/*',
|
42
|
-
'/books[count(book)=1]'
|
44
|
+
'/books[count(book)=1]',
|
45
|
+
'/books/book[test and title]'
|
43
46
|
]
|
44
47
|
)
|
45
48
|
end
|
data/test/test_xembly.rb
CHANGED
@@ -48,14 +48,18 @@ class TestXembly < XeTest
|
|
48
48
|
xml = File.join(dir, 'input.xml')
|
49
49
|
File.write(xml, '<books/>')
|
50
50
|
dirs = File.join(dir, 'dirs.txt')
|
51
|
-
File.write(
|
51
|
+
File.write(
|
52
|
+
dirs,
|
53
|
+
'XPATH "/books"; ADD "book"; ATTR "id", "123"; SET "Elegant Objects";'
|
54
|
+
)
|
52
55
|
opts = opts(['--xml', xml, '--dirs', dirs])
|
53
56
|
matches(
|
54
57
|
Xembly::Base.new(opts).xml,
|
55
58
|
[
|
56
59
|
'/books',
|
57
60
|
'/books[count(book)=1]',
|
58
|
-
'/books/book[@id=123]'
|
61
|
+
'/books/book[@id=123]',
|
62
|
+
'/books/book[@id=123 and .="Elegant Objects"]'
|
59
63
|
]
|
60
64
|
)
|
61
65
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xembly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
@@ -179,15 +179,23 @@ files:
|
|
179
179
|
- features/support/env.rb
|
180
180
|
- lib/xembly.rb
|
181
181
|
- lib/xembly/add.rb
|
182
|
+
- lib/xembly/addif.rb
|
182
183
|
- lib/xembly/attr.rb
|
183
184
|
- lib/xembly/directives.rb
|
185
|
+
- lib/xembly/remove.rb
|
186
|
+
- lib/xembly/set.rb
|
187
|
+
- lib/xembly/strict.rb
|
188
|
+
- lib/xembly/up.rb
|
184
189
|
- lib/xembly/version.rb
|
185
190
|
- lib/xembly/xembler.rb
|
186
191
|
- lib/xembly/xpath.rb
|
187
192
|
- test/test__helper.rb
|
188
193
|
- test/test_add.rb
|
194
|
+
- test/test_addif.rb
|
189
195
|
- test/test_attr.rb
|
190
196
|
- test/test_directives.rb
|
197
|
+
- test/test_remove.rb
|
198
|
+
- test/test_set.rb
|
191
199
|
- test/test_xembler.rb
|
192
200
|
- test/test_xembly.rb
|
193
201
|
- xembly.gemspec
|
@@ -223,7 +231,10 @@ test_files:
|
|
223
231
|
- features/support/env.rb
|
224
232
|
- test/test__helper.rb
|
225
233
|
- test/test_add.rb
|
234
|
+
- test/test_addif.rb
|
226
235
|
- test/test_attr.rb
|
227
236
|
- test/test_directives.rb
|
237
|
+
- test/test_remove.rb
|
238
|
+
- test/test_set.rb
|
228
239
|
- test/test_xembler.rb
|
229
240
|
- test/test_xembly.rb
|