xembly 0.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,66 @@
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/add'
24
+ require 'xembly/attr'
25
+ require 'xembly/xpath'
26
+
27
+ module Xembly
28
+ # Directives
29
+ class Directives
30
+ # Ctor.
31
+ # +text+:: Directives in text
32
+ def initialize(text)
33
+ @array = text
34
+ .strip
35
+ .split(/\s*;\s*/)
36
+ .reject(&:empty?)
37
+ .map { |t| Directives.map(t) }
38
+ end
39
+
40
+ def each(&block)
41
+ @array.each(&block)
42
+ end
43
+
44
+ def length
45
+ @array.length
46
+ end
47
+
48
+ def self.map(text)
49
+ cmd, tail = text.strip.split(/\s+/, 2)
50
+ args = tail.strip
51
+ .scan(/"([^"]+)"/)
52
+ .flatten
53
+ .map { |a| a.tr('"', '') }
54
+ case cmd.upcase
55
+ when 'ADD'
56
+ Add.new(args[0])
57
+ when 'ATTR'
58
+ Attr.new(args[0], args[1])
59
+ when 'XPATH'
60
+ Xpath.new(args[0])
61
+ else
62
+ fail "Unknown command \"#{cmd}\""
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,29 @@
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
+ # Xembly main module.
24
+ # Author:: Yegor Bugayenko (yegor@teamed.io)
25
+ # Copyright:: Copyright (c) 2016 Yegor Bugayenko
26
+ # License:: MIT
27
+ module Xembly
28
+ VERSION = '0.1'
29
+ end
@@ -0,0 +1,45 @@
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
+ # Xembler
27
+ class Xembler
28
+ # Ctor.
29
+ # +dirs+:: Directives
30
+ def initialize(dirs)
31
+ @dirs = dirs
32
+ end
33
+
34
+ # Apply them to the XML.
35
+ def apply(xml)
36
+ dom = Nokogiri::XML(xml)
37
+ cursor = [dom]
38
+ @dirs.each do |dir|
39
+ cursor = dir.exec(dom, cursor)
40
+ end
41
+ Xembly.log.info "#{@dirs.length} directive(s) applied"
42
+ dom
43
+ end
44
+ end
45
+ end
@@ -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 'nokogiri'
24
+
25
+ module Xembly
26
+ # XPATH directive
27
+ class Xpath
28
+ # Ctor.
29
+ # +path+:: Path
30
+ def initialize(path)
31
+ @path = path
32
+ end
33
+
34
+ def exec(dom, _)
35
+ dom.xpath(@path)
36
+ end
37
+ end
38
+ end
data/lib/xembly.rb ADDED
@@ -0,0 +1,81 @@
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/version'
24
+ require 'xembly/xembler'
25
+ require 'xembly/directives'
26
+ require 'nokogiri'
27
+ require 'logger'
28
+ require 'time'
29
+
30
+ # Xembly main module.
31
+ # Author:: Yegor Bugayenko (yegor@teamed.io)
32
+ # Copyright:: Copyright (c) 2016 Yegor Bugayenko
33
+ # License:: MIT
34
+ module Xembly
35
+ # Get logger.
36
+ def self.log
37
+ unless @logger
38
+ @logger = Logger.new(STDOUT)
39
+ @logger.formatter = proc { |severity, _, _, msg|
40
+ "#{severity}: #{msg.dump}\n"
41
+ }
42
+ @logger.level = Logger::ERROR
43
+ end
44
+ @logger
45
+ end
46
+
47
+ class << self
48
+ attr_writer :logger
49
+ end
50
+
51
+ # Code base abstraction
52
+ class Base
53
+ # Ctor.
54
+ # +opts+:: Options
55
+ def initialize(opts)
56
+ @opts = opts
57
+ Xembly.log.level = Logger::INFO if @opts.verbose?
58
+ Xembly.log.info "my version is #{Xembly::VERSION}"
59
+ Xembly.log.info "Ruby version is #{RUBY_VERSION} at #{RUBY_PLATFORM}"
60
+ end
61
+
62
+ # Generate XML.
63
+ def xml
64
+ if @opts.xml?
65
+ xml = File.read(@opts[:xml])
66
+ Xembly.log.info "reading #{@opts[:xml]}"
67
+ else
68
+ xml = STDIN.read
69
+ Xembly.log.info 'reading STDIN'
70
+ end
71
+ if @opts.dirs?
72
+ Xembly.log.info "reading directives from #{@opts[:dirs]}"
73
+ dirs = File.read(@opts[:dirs])
74
+ else
75
+ Xembly.log.info "#{@opts.arguments.length} directives in command line"
76
+ dirs = @opts.arguments.join(';')
77
+ end
78
+ Xembler.new(Directives.new(dirs)).apply(xml).to_xml
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,39 @@
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 'simplecov'
24
+ require 'nokogiri'
25
+ require 'xembly'
26
+ require 'minitest/autorun'
27
+
28
+ # Xembly test, parent class.
29
+ # Author:: Yegor Bugayenko (yegor@teamed.io)
30
+ # Copyright:: Copyright (c) 2016 Yegor Bugayenko
31
+ # License:: MIT
32
+ class XeTest < Minitest::Test
33
+ def matches(xml, xpaths)
34
+ xpaths.each do |xpath|
35
+ fail "doesn't match '#{xpath}': #{xml}" \
36
+ unless Nokogiri::XML(xml).xpath(xpath).size == 1
37
+ end
38
+ end
39
+ end
data/test/test_add.rb ADDED
@@ -0,0 +1,53 @@
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/add'
24
+ require 'test__helper'
25
+
26
+ # Xembly::Add tests.
27
+ # Author:: Yegor Bugayenko (yegor@teamed.io)
28
+ # Copyright:: Copyright (c) 2016 Yegor Bugayenko
29
+ # License:: MIT
30
+ class TestAdd < XeTest
31
+ def test_adds_nodes
32
+ dom = Nokogiri::XML('<books/>')
33
+ Xembly::Add.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_to_empty_dom
44
+ dom = Nokogiri::XML('')
45
+ Xembly::Add.new('dude').exec(dom, [dom])
46
+ matches(
47
+ dom.to_xml,
48
+ [
49
+ '/dude'
50
+ ]
51
+ )
52
+ end
53
+ end
data/test/test_attr.rb ADDED
@@ -0,0 +1,41 @@
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/attr'
24
+ require 'test__helper'
25
+
26
+ # Xembly::Attr tests.
27
+ # Author:: Yegor Bugayenko (yegor@teamed.io)
28
+ # Copyright:: Copyright (c) 2016 Yegor Bugayenko
29
+ # License:: MIT
30
+ class TestAttr < XeTest
31
+ def test_sets_attributes
32
+ dom = Nokogiri::XML('<book/>')
33
+ Xembly::Attr.new('id', '4').exec(dom, [dom.xpath('/*').first])
34
+ matches(
35
+ dom.to_xml,
36
+ [
37
+ '/book[@id=4]'
38
+ ]
39
+ )
40
+ end
41
+ end
@@ -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 'minitest/autorun'
24
+ require 'nokogiri'
25
+ require 'xembly/directives'
26
+
27
+ # Xembly::Directives module tests.
28
+ # Author:: Yegor Bugayenko (yegor@teamed.io)
29
+ # Copyright:: Copyright (c) 2016 Yegor Bugayenko
30
+ # License:: MIT
31
+ class TestDirectives < Minitest::Test
32
+ def test_parses_directives
33
+ dirs = Xembly::Directives.new(
34
+ " ADD \"book\" ; ATTR 'a1', 'works, for\nme!'; "
35
+ )
36
+ assert dirs.length == 2, 'two directives must be there'
37
+ end
38
+ end
@@ -0,0 +1,46 @@
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 'minitest/autorun'
24
+ require 'nokogiri'
25
+ require 'xembly/xembler'
26
+ require 'xembly/directives'
27
+ require 'test__helper'
28
+
29
+ # Xembly::Xembler module tests.
30
+ # Author:: Yegor Bugayenko (yegor@teamed.io)
31
+ # Copyright:: Copyright (c) 2016 Yegor Bugayenko
32
+ # License:: MIT
33
+ class TestXembler < XeTest
34
+ def test_modifies_xml
35
+ xembler = Xembly::Xembler.new(
36
+ Xembly::Directives.new('XPATH "/books"; ADD "book"; ADD "test";')
37
+ )
38
+ matches(
39
+ xembler.apply('<books/>').to_xml,
40
+ [
41
+ '/*',
42
+ '/books[count(book)=1]'
43
+ ]
44
+ )
45
+ end
46
+ end
@@ -0,0 +1,73 @@
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 'minitest/autorun'
24
+ require 'nokogiri'
25
+ require 'xembly'
26
+ require 'tmpdir'
27
+ require 'slop'
28
+ require 'test__helper'
29
+
30
+ # Xembly main module test.
31
+ # Author:: Yegor Bugayenko (yegor@teamed.io)
32
+ # Copyright:: Copyright (c) 2016 Yegor Bugayenko
33
+ # License:: MIT
34
+ class TestXembly < XeTest
35
+ def test_basic
36
+ opts = opts(['-x', '/dev/null', 'ADD "books"', 'ADD "book"'])
37
+ matches(
38
+ Xembly::Base.new(opts).xml,
39
+ [
40
+ '/books',
41
+ '/books[count(book)=1]'
42
+ ]
43
+ )
44
+ end
45
+
46
+ def test_reading_from_file
47
+ Dir.mktmpdir 'test' do |dir|
48
+ xml = File.join(dir, 'input.xml')
49
+ File.write(xml, '<books/>')
50
+ dirs = File.join(dir, 'dirs.txt')
51
+ File.write(dirs, 'XPATH "/books"; ADD "book"; ATTR "id", "123";')
52
+ opts = opts(['--xml', xml, '--dirs', dirs])
53
+ matches(
54
+ Xembly::Base.new(opts).xml,
55
+ [
56
+ '/books',
57
+ '/books[count(book)=1]',
58
+ '/books/book[@id=123]'
59
+ ]
60
+ )
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def opts(args)
67
+ Slop.parse args do |o|
68
+ o.on '-v', '--verbose'
69
+ o.string '-x', '--xml', argument: :required
70
+ o.string '-d', '--dirs', argument: :required
71
+ end
72
+ end
73
+ end
data/xembly.gemspec ADDED
@@ -0,0 +1,57 @@
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 NONINFRINGEMENT. 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
+ lib = File.expand_path('../lib', __FILE__)
24
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
25
+ require 'xembly/version'
26
+
27
+ Gem::Specification.new do |s|
28
+ s.specification_version = 2 if s.respond_to? :specification_version=
29
+ if s.respond_to? :required_rubygems_version=
30
+ s.required_rubygems_version = Gem::Requirement.new('>= 0')
31
+ end
32
+ s.rubygems_version = '2.2.2'
33
+ s.required_ruby_version = '>= 2.0.0'
34
+ s.name = 'xembly'
35
+ s.version = Xembly::VERSION
36
+ s.license = 'MIT'
37
+ s.summary = 'Xembly Gem'
38
+ s.description = 'Command Line XML Manipulator'
39
+ s.authors = ['Yegor Bugayenko']
40
+ s.email = 'yegor@teamed.io'
41
+ s.homepage = 'http://github.com/yegor256/xembly-gem'
42
+ s.files = `git ls-files`.split($RS)
43
+ s.executables = s.files.grep(/^bin\//) { |f| File.basename(f) }
44
+ s.test_files = s.files.grep(/^(test|spec|features)\//)
45
+ s.rdoc_options = ['--charset=UTF-8']
46
+ s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
47
+ s.add_runtime_dependency 'nokogiri', '1.6.7.2'
48
+ s.add_runtime_dependency 'slop', '4.3.0'
49
+ s.add_runtime_dependency 'rake', '10.5.0'
50
+ s.add_development_dependency 'coveralls', '0.7.2'
51
+ s.add_development_dependency 'rdoc', '4.2.0'
52
+ s.add_development_dependency 'cucumber', '1.3.17'
53
+ s.add_development_dependency 'minitest', '5.5.0'
54
+ s.add_development_dependency 'rubocop', '0.24.1'
55
+ s.add_development_dependency 'rubocop-rspec', '1.2.1'
56
+ s.add_development_dependency 'rspec-rails', '3.1.0'
57
+ end