moxml 0.1.6 → 0.1.8
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/.github/workflows/dependent-repos.json +5 -0
- data/.github/workflows/dependent-tests.yml +20 -0
- data/.github/workflows/docs.yml +59 -0
- data/.github/workflows/rake.yml +12 -4
- data/.github/workflows/release.yml +5 -3
- data/.gitignore +37 -0
- data/.rubocop.yml +15 -7
- data/.rubocop_todo.yml +238 -40
- data/Gemfile +14 -9
- data/LICENSE.md +6 -2
- data/README.adoc +535 -373
- data/Rakefile +53 -0
- data/benchmarks/.gitignore +6 -0
- data/benchmarks/generate_report.rb +550 -0
- data/docs/Gemfile +13 -0
- data/docs/_config.yml +138 -0
- data/docs/_guides/advanced-features.adoc +87 -0
- data/docs/_guides/development-testing.adoc +165 -0
- data/docs/_guides/index.adoc +45 -0
- data/docs/_guides/modifying-xml.adoc +293 -0
- data/docs/_guides/parsing-xml.adoc +231 -0
- data/docs/_guides/sax-parsing.adoc +603 -0
- data/docs/_guides/working-with-documents.adoc +118 -0
- data/docs/_pages/adapter-compatibility.adoc +369 -0
- data/docs/_pages/adapters/headed-ox.adoc +237 -0
- data/docs/_pages/adapters/index.adoc +98 -0
- data/docs/_pages/adapters/libxml.adoc +286 -0
- data/docs/_pages/adapters/nokogiri.adoc +252 -0
- data/docs/_pages/adapters/oga.adoc +292 -0
- data/docs/_pages/adapters/ox.adoc +55 -0
- data/docs/_pages/adapters/rexml.adoc +293 -0
- data/docs/_pages/best-practices.adoc +430 -0
- data/docs/_pages/compatibility.adoc +468 -0
- data/docs/_pages/configuration.adoc +251 -0
- data/docs/_pages/error-handling.adoc +350 -0
- data/docs/_pages/headed-ox-limitations.adoc +558 -0
- data/docs/_pages/headed-ox.adoc +1025 -0
- data/docs/_pages/index.adoc +35 -0
- data/docs/_pages/installation.adoc +141 -0
- data/docs/_pages/node-api-reference.adoc +50 -0
- data/docs/_pages/performance.adoc +36 -0
- data/docs/_pages/quick-start.adoc +244 -0
- data/docs/_pages/thread-safety.adoc +29 -0
- data/docs/_references/document-api.adoc +408 -0
- data/docs/_references/index.adoc +48 -0
- data/docs/_tutorials/basic-usage.adoc +268 -0
- data/docs/_tutorials/builder-pattern.adoc +343 -0
- data/docs/_tutorials/index.adoc +33 -0
- data/docs/_tutorials/namespace-handling.adoc +325 -0
- data/docs/_tutorials/xpath-queries.adoc +359 -0
- data/docs/index.adoc +122 -0
- data/examples/README.md +124 -0
- data/examples/api_client/README.md +424 -0
- data/examples/api_client/api_client.rb +394 -0
- data/examples/api_client/example_response.xml +48 -0
- data/examples/headed_ox_example/README.md +90 -0
- data/examples/headed_ox_example/headed_ox_demo.rb +71 -0
- data/examples/rss_parser/README.md +194 -0
- data/examples/rss_parser/example_feed.xml +93 -0
- data/examples/rss_parser/rss_parser.rb +189 -0
- data/examples/sax_parsing/README.md +50 -0
- data/examples/sax_parsing/data_extractor.rb +75 -0
- data/examples/sax_parsing/example.xml +21 -0
- data/examples/sax_parsing/large_file.rb +78 -0
- data/examples/sax_parsing/simple_parser.rb +55 -0
- data/examples/web_scraper/README.md +352 -0
- data/examples/web_scraper/example_page.html +201 -0
- data/examples/web_scraper/web_scraper.rb +312 -0
- data/lib/moxml/adapter/base.rb +107 -28
- data/lib/moxml/adapter/customized_libxml/cdata.rb +28 -0
- data/lib/moxml/adapter/customized_libxml/comment.rb +24 -0
- data/lib/moxml/adapter/customized_libxml/declaration.rb +85 -0
- data/lib/moxml/adapter/customized_libxml/element.rb +39 -0
- data/lib/moxml/adapter/customized_libxml/node.rb +44 -0
- data/lib/moxml/adapter/customized_libxml/processing_instruction.rb +31 -0
- data/lib/moxml/adapter/customized_libxml/text.rb +27 -0
- data/lib/moxml/adapter/customized_oga/xml_generator.rb +1 -1
- data/lib/moxml/adapter/customized_ox/attribute.rb +28 -3
- data/lib/moxml/adapter/customized_ox/namespace.rb +0 -2
- data/lib/moxml/adapter/customized_ox/text.rb +0 -2
- data/lib/moxml/adapter/customized_rexml/formatter.rb +11 -6
- data/lib/moxml/adapter/headed_ox.rb +161 -0
- data/lib/moxml/adapter/libxml.rb +1548 -0
- data/lib/moxml/adapter/nokogiri.rb +121 -9
- data/lib/moxml/adapter/oga.rb +123 -12
- data/lib/moxml/adapter/ox.rb +283 -27
- data/lib/moxml/adapter/rexml.rb +127 -20
- data/lib/moxml/adapter.rb +21 -4
- data/lib/moxml/attribute.rb +6 -0
- data/lib/moxml/builder.rb +40 -4
- data/lib/moxml/config.rb +8 -3
- data/lib/moxml/context.rb +39 -1
- data/lib/moxml/doctype.rb +13 -1
- data/lib/moxml/document.rb +39 -6
- data/lib/moxml/document_builder.rb +27 -5
- data/lib/moxml/element.rb +71 -2
- data/lib/moxml/error.rb +175 -6
- data/lib/moxml/node.rb +94 -3
- data/lib/moxml/node_set.rb +34 -0
- data/lib/moxml/sax/block_handler.rb +194 -0
- data/lib/moxml/sax/element_handler.rb +124 -0
- data/lib/moxml/sax/handler.rb +113 -0
- data/lib/moxml/sax.rb +31 -0
- data/lib/moxml/version.rb +1 -1
- data/lib/moxml/xml_utils/encoder.rb +4 -4
- data/lib/moxml/xml_utils.rb +7 -4
- data/lib/moxml/xpath/ast/node.rb +159 -0
- data/lib/moxml/xpath/cache.rb +91 -0
- data/lib/moxml/xpath/compiler.rb +1768 -0
- data/lib/moxml/xpath/context.rb +26 -0
- data/lib/moxml/xpath/conversion.rb +124 -0
- data/lib/moxml/xpath/engine.rb +52 -0
- data/lib/moxml/xpath/errors.rb +101 -0
- data/lib/moxml/xpath/lexer.rb +304 -0
- data/lib/moxml/xpath/parser.rb +485 -0
- data/lib/moxml/xpath/ruby/generator.rb +269 -0
- data/lib/moxml/xpath/ruby/node.rb +193 -0
- data/lib/moxml/xpath.rb +37 -0
- data/lib/moxml.rb +5 -2
- data/moxml.gemspec +3 -1
- data/old-specs/moxml/adapter/customized_libxml/.gitkeep +6 -0
- data/spec/consistency/README.md +77 -0
- data/spec/{moxml/examples/adapter_spec.rb → consistency/adapter_parity_spec.rb} +4 -4
- data/spec/examples/README.md +75 -0
- data/spec/{support/shared_examples/examples/attribute.rb → examples/attribute_examples_spec.rb} +1 -1
- data/spec/{support/shared_examples/examples/basic_usage.rb → examples/basic_usage_spec.rb} +2 -2
- data/spec/{support/shared_examples/examples/namespace.rb → examples/namespace_examples_spec.rb} +3 -3
- data/spec/{support/shared_examples/examples/readme_examples.rb → examples/readme_examples_spec.rb} +6 -4
- data/spec/{support/shared_examples/examples/xpath.rb → examples/xpath_examples_spec.rb} +10 -6
- data/spec/integration/README.md +71 -0
- data/spec/{moxml/all_with_adapters_spec.rb → integration/all_adapters_spec.rb} +3 -2
- data/spec/integration/headed_ox_integration_spec.rb +326 -0
- data/spec/{support → integration}/shared_examples/edge_cases.rb +37 -10
- data/spec/integration/shared_examples/high_level/.gitkeep +0 -0
- data/spec/{support/shared_examples/context.rb → integration/shared_examples/high_level/context_behavior.rb} +2 -1
- data/spec/{support/shared_examples/integration.rb → integration/shared_examples/integration_workflows.rb} +23 -6
- data/spec/integration/shared_examples/node_wrappers/.gitkeep +0 -0
- data/spec/{support/shared_examples/cdata.rb → integration/shared_examples/node_wrappers/cdata_behavior.rb} +6 -1
- data/spec/{support/shared_examples/comment.rb → integration/shared_examples/node_wrappers/comment_behavior.rb} +2 -1
- data/spec/{support/shared_examples/declaration.rb → integration/shared_examples/node_wrappers/declaration_behavior.rb} +5 -2
- data/spec/{support/shared_examples/doctype.rb → integration/shared_examples/node_wrappers/doctype_behavior.rb} +2 -2
- data/spec/{support/shared_examples/document.rb → integration/shared_examples/node_wrappers/document_behavior.rb} +1 -1
- data/spec/{support/shared_examples/node.rb → integration/shared_examples/node_wrappers/node_behavior.rb} +9 -2
- data/spec/{support/shared_examples/node_set.rb → integration/shared_examples/node_wrappers/node_set_behavior.rb} +1 -18
- data/spec/{support/shared_examples/processing_instruction.rb → integration/shared_examples/node_wrappers/processing_instruction_behavior.rb} +6 -2
- data/spec/moxml/README.md +41 -0
- data/spec/moxml/adapter/.gitkeep +0 -0
- data/spec/moxml/adapter/README.md +61 -0
- data/spec/moxml/adapter/base_spec.rb +27 -0
- data/spec/moxml/adapter/headed_ox_spec.rb +311 -0
- data/spec/moxml/adapter/libxml_spec.rb +14 -0
- data/spec/moxml/adapter/ox_spec.rb +9 -8
- data/spec/moxml/adapter/shared_examples/.gitkeep +0 -0
- data/spec/{support/shared_examples/xml_adapter.rb → moxml/adapter/shared_examples/adapter_contract.rb} +39 -12
- data/spec/moxml/adapter_spec.rb +16 -0
- data/spec/moxml/attribute_spec.rb +30 -0
- data/spec/moxml/builder_spec.rb +33 -0
- data/spec/moxml/cdata_spec.rb +31 -0
- data/spec/moxml/comment_spec.rb +31 -0
- data/spec/moxml/config_spec.rb +3 -3
- data/spec/moxml/context_spec.rb +28 -0
- data/spec/moxml/declaration_spec.rb +36 -0
- data/spec/moxml/doctype_spec.rb +33 -0
- data/spec/moxml/document_builder_spec.rb +30 -0
- data/spec/moxml/document_spec.rb +105 -0
- data/spec/moxml/element_spec.rb +143 -0
- data/spec/moxml/error_spec.rb +266 -22
- data/spec/{moxml_spec.rb → moxml/moxml_spec.rb} +9 -9
- data/spec/moxml/namespace_spec.rb +32 -0
- data/spec/moxml/node_set_spec.rb +39 -0
- data/spec/moxml/node_spec.rb +37 -0
- data/spec/moxml/processing_instruction_spec.rb +34 -0
- data/spec/moxml/sax_spec.rb +1067 -0
- data/spec/moxml/text_spec.rb +31 -0
- data/spec/moxml/version_spec.rb +14 -0
- data/spec/moxml/xml_utils/.gitkeep +0 -0
- data/spec/moxml/xml_utils/encoder_spec.rb +27 -0
- data/spec/moxml/xml_utils_spec.rb +49 -0
- data/spec/moxml/xpath/ast/node_spec.rb +83 -0
- data/spec/moxml/xpath/axes_spec.rb +296 -0
- data/spec/moxml/xpath/cache_spec.rb +358 -0
- data/spec/moxml/xpath/compiler_spec.rb +406 -0
- data/spec/moxml/xpath/context_spec.rb +210 -0
- data/spec/moxml/xpath/conversion_spec.rb +365 -0
- data/spec/moxml/xpath/fixtures/sample.xml +25 -0
- data/spec/moxml/xpath/functions/boolean_functions_spec.rb +114 -0
- data/spec/moxml/xpath/functions/node_functions_spec.rb +145 -0
- data/spec/moxml/xpath/functions/numeric_functions_spec.rb +164 -0
- data/spec/moxml/xpath/functions/position_functions_spec.rb +93 -0
- data/spec/moxml/xpath/functions/special_functions_spec.rb +89 -0
- data/spec/moxml/xpath/functions/string_functions_spec.rb +381 -0
- data/spec/moxml/xpath/lexer_spec.rb +488 -0
- data/spec/moxml/xpath/parser_integration_spec.rb +210 -0
- data/spec/moxml/xpath/parser_spec.rb +364 -0
- data/spec/moxml/xpath/ruby/generator_spec.rb +421 -0
- data/spec/moxml/xpath/ruby/node_spec.rb +291 -0
- data/spec/moxml/xpath_capabilities_spec.rb +199 -0
- data/spec/moxml/xpath_spec.rb +77 -0
- data/spec/performance/README.md +83 -0
- data/spec/performance/benchmark_spec.rb +64 -0
- data/spec/{support/shared_examples/examples/memory.rb → performance/memory_usage_spec.rb} +3 -1
- data/spec/{support/shared_examples/examples/thread_safety.rb → performance/thread_safety_spec.rb} +3 -1
- data/spec/performance/xpath_benchmark_spec.rb +259 -0
- data/spec/spec_helper.rb +58 -1
- data/spec/support/xml_matchers.rb +1 -1
- metadata +176 -35
- data/lib/ox/node.rb +0 -9
- data/spec/support/shared_examples/examples/benchmark_spec.rb +0 -51
- /data/spec/{support/shared_examples/builder.rb → integration/shared_examples/high_level/builder_behavior.rb} +0 -0
- /data/spec/{support/shared_examples/document_builder.rb → integration/shared_examples/high_level/document_builder_behavior.rb} +0 -0
- /data/spec/{support/shared_examples/attribute.rb → integration/shared_examples/node_wrappers/attribute_behavior.rb} +0 -0
- /data/spec/{support/shared_examples/element.rb → integration/shared_examples/node_wrappers/element_behavior.rb} +0 -0
- /data/spec/{support/shared_examples/namespace.rb → integration/shared_examples/node_wrappers/namespace_behavior.rb} +0 -0
- /data/spec/{support/shared_examples/text.rb → integration/shared_examples/node_wrappers/text_behavior.rb} +0 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe "XPath Numeric Functions" do
|
|
6
|
+
let(:doc) do
|
|
7
|
+
xml = <<~XML
|
|
8
|
+
<root>
|
|
9
|
+
<item price="10">A</item>
|
|
10
|
+
<item price="20">B</item>
|
|
11
|
+
<item price="30">C</item>
|
|
12
|
+
</root>
|
|
13
|
+
XML
|
|
14
|
+
Moxml.new(:nokogiri).parse(xml)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe "number()" do
|
|
18
|
+
it "converts string to number" do
|
|
19
|
+
ast = Moxml::XPath::Parser.parse('number("123.45")')
|
|
20
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
21
|
+
result = proc.call(doc)
|
|
22
|
+
|
|
23
|
+
expect(result).to eq(123.45)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "converts boolean true to number" do
|
|
27
|
+
ast = Moxml::XPath::Parser.parse("number(true())")
|
|
28
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
29
|
+
result = proc.call(doc)
|
|
30
|
+
|
|
31
|
+
expect(result).to eq(1.0)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "converts boolean false to number" do
|
|
35
|
+
ast = Moxml::XPath::Parser.parse("number(false())")
|
|
36
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
37
|
+
result = proc.call(doc)
|
|
38
|
+
|
|
39
|
+
expect(result).to eq(0.0)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
describe "sum()" do
|
|
44
|
+
it "sums node values" do
|
|
45
|
+
ast = Moxml::XPath::Parser.parse("sum(/root/item/@price)")
|
|
46
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
47
|
+
result = proc.call(doc)
|
|
48
|
+
|
|
49
|
+
expect(result).to eq(60.0)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "returns 0 for empty nodeset" do
|
|
53
|
+
ast = Moxml::XPath::Parser.parse("sum(/root/missing)")
|
|
54
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
55
|
+
result = proc.call(doc)
|
|
56
|
+
|
|
57
|
+
expect(result).to eq(0.0)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
describe "count()" do
|
|
62
|
+
it "counts nodes" do
|
|
63
|
+
ast = Moxml::XPath::Parser.parse("count(/root/item)")
|
|
64
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
65
|
+
result = proc.call(doc)
|
|
66
|
+
|
|
67
|
+
expect(result).to eq(3.0)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "returns 0 for empty nodeset" do
|
|
71
|
+
ast = Moxml::XPath::Parser.parse("count(/root/missing)")
|
|
72
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
73
|
+
result = proc.call(doc)
|
|
74
|
+
|
|
75
|
+
expect(result).to eq(0.0)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
describe "floor()" do
|
|
80
|
+
it "rounds down positive number" do
|
|
81
|
+
ast = Moxml::XPath::Parser.parse("floor(3.7)")
|
|
82
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
83
|
+
result = proc.call(doc)
|
|
84
|
+
|
|
85
|
+
expect(result).to eq(3.0)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
it "rounds down negative number" do
|
|
89
|
+
ast = Moxml::XPath::Parser.parse("floor(-3.2)")
|
|
90
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
91
|
+
result = proc.call(doc)
|
|
92
|
+
|
|
93
|
+
expect(result).to eq(-4.0)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it "leaves integer unchanged" do
|
|
97
|
+
ast = Moxml::XPath::Parser.parse("floor(5)")
|
|
98
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
99
|
+
result = proc.call(doc)
|
|
100
|
+
|
|
101
|
+
expect(result).to eq(5.0)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
describe "ceiling()" do
|
|
106
|
+
it "rounds up positive number" do
|
|
107
|
+
ast = Moxml::XPath::Parser.parse("ceiling(3.2)")
|
|
108
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
109
|
+
result = proc.call(doc)
|
|
110
|
+
|
|
111
|
+
expect(result).to eq(4.0)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
it "rounds up negative number" do
|
|
115
|
+
ast = Moxml::XPath::Parser.parse("ceiling(-3.7)")
|
|
116
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
117
|
+
result = proc.call(doc)
|
|
118
|
+
|
|
119
|
+
expect(result).to eq(-3.0)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it "leaves integer unchanged" do
|
|
123
|
+
ast = Moxml::XPath::Parser.parse("ceiling(5)")
|
|
124
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
125
|
+
result = proc.call(doc)
|
|
126
|
+
|
|
127
|
+
expect(result).to eq(5.0)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
describe "round()" do
|
|
132
|
+
it "rounds to nearest (up)" do
|
|
133
|
+
ast = Moxml::XPath::Parser.parse("round(3.5)")
|
|
134
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
135
|
+
result = proc.call(doc)
|
|
136
|
+
|
|
137
|
+
expect(result).to eq(4.0)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
it "rounds to nearest (down)" do
|
|
141
|
+
ast = Moxml::XPath::Parser.parse("round(3.4)")
|
|
142
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
143
|
+
result = proc.call(doc)
|
|
144
|
+
|
|
145
|
+
expect(result).to eq(3.0)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
it "rounds negative numbers" do
|
|
149
|
+
ast = Moxml::XPath::Parser.parse("round(-3.5)")
|
|
150
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
151
|
+
result = proc.call(doc)
|
|
152
|
+
|
|
153
|
+
expect(result).to eq(-4.0)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
it "leaves integer unchanged" do
|
|
157
|
+
ast = Moxml::XPath::Parser.parse("round(5)")
|
|
158
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
159
|
+
result = proc.call(doc)
|
|
160
|
+
|
|
161
|
+
expect(result).to eq(5.0)
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe "XPath Position Functions" do
|
|
6
|
+
let(:context) { Moxml.new(:nokogiri) }
|
|
7
|
+
|
|
8
|
+
let(:doc) do
|
|
9
|
+
xml = <<~XML
|
|
10
|
+
<root>
|
|
11
|
+
<item>first</item>
|
|
12
|
+
<item>second</item>
|
|
13
|
+
<item>third</item>
|
|
14
|
+
</root>
|
|
15
|
+
XML
|
|
16
|
+
context.parse(xml)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe "position()" do
|
|
20
|
+
it "raises error when used outside predicate" do
|
|
21
|
+
ast = Moxml::XPath::Parser.parse("position()")
|
|
22
|
+
|
|
23
|
+
expect do
|
|
24
|
+
Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
25
|
+
end.to raise_error(
|
|
26
|
+
Moxml::XPath::InvalidContextError,
|
|
27
|
+
/position\(\) requires a predicate context/,
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# These tests require predicate support to be implemented
|
|
32
|
+
# They are marked as pending until predicates are working
|
|
33
|
+
xit "returns current position in predicate" do
|
|
34
|
+
ast = Moxml::XPath::Parser.parse("/root/item[position() = 2]")
|
|
35
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
36
|
+
result = proc.call(doc)
|
|
37
|
+
|
|
38
|
+
expect(result.size).to eq(1)
|
|
39
|
+
expect(result.first.text).to eq("second")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
xit "works with position comparison" do
|
|
43
|
+
ast = Moxml::XPath::Parser.parse("/root/item[position() > 1]")
|
|
44
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
45
|
+
result = proc.call(doc)
|
|
46
|
+
|
|
47
|
+
expect(result.size).to eq(2)
|
|
48
|
+
expect(result.map(&:text)).to eq(["second", "third"])
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
describe "last()" do
|
|
53
|
+
it "raises error when used outside predicate" do
|
|
54
|
+
ast = Moxml::XPath::Parser.parse("last()")
|
|
55
|
+
|
|
56
|
+
expect do
|
|
57
|
+
Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
58
|
+
end.to raise_error(
|
|
59
|
+
Moxml::XPath::InvalidContextError,
|
|
60
|
+
/last\(\) requires a predicate context/,
|
|
61
|
+
)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# These tests require predicate support to be implemented
|
|
65
|
+
# They are marked as pending until predicates are working
|
|
66
|
+
xit "returns size of context in predicate" do
|
|
67
|
+
ast = Moxml::XPath::Parser.parse("/root/item[position() = last()]")
|
|
68
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
69
|
+
result = proc.call(doc)
|
|
70
|
+
|
|
71
|
+
expect(result.size).to eq(1)
|
|
72
|
+
expect(result.first.text).to eq("third")
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
xit "works with last() - 1" do
|
|
76
|
+
ast = Moxml::XPath::Parser.parse("/root/item[position() = last() - 1]")
|
|
77
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
78
|
+
result = proc.call(doc)
|
|
79
|
+
|
|
80
|
+
expect(result.size).to eq(1)
|
|
81
|
+
expect(result.first.text).to eq("second")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
xit "works with comparison to last()" do
|
|
85
|
+
ast = Moxml::XPath::Parser.parse("/root/item[position() < last()]")
|
|
86
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
87
|
+
result = proc.call(doc)
|
|
88
|
+
|
|
89
|
+
expect(result.size).to eq(2)
|
|
90
|
+
expect(result.map(&:text)).to eq(["first", "second"])
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe "XPath Special Functions" do
|
|
6
|
+
let(:context) { Moxml.new(:nokogiri) }
|
|
7
|
+
|
|
8
|
+
describe "id()" do
|
|
9
|
+
let(:doc) do
|
|
10
|
+
xml = <<~XML
|
|
11
|
+
<root>
|
|
12
|
+
<item id="a">first</item>
|
|
13
|
+
<item id="b">second</item>
|
|
14
|
+
<item id="c">third</item>
|
|
15
|
+
</root>
|
|
16
|
+
XML
|
|
17
|
+
context.parse(xml)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "finds node by single ID" do
|
|
21
|
+
ast = Moxml::XPath::Parser.parse('id("b")')
|
|
22
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
23
|
+
result = proc.call(doc)
|
|
24
|
+
|
|
25
|
+
expect(result).to be_a(Moxml::NodeSet)
|
|
26
|
+
expect(result.size).to eq(1)
|
|
27
|
+
expect(result.first.text).to eq("second")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "finds multiple nodes by space-separated IDs" do
|
|
31
|
+
ast = Moxml::XPath::Parser.parse('id("a c")')
|
|
32
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
33
|
+
result = proc.call(doc)
|
|
34
|
+
|
|
35
|
+
expect(result).to be_a(Moxml::NodeSet)
|
|
36
|
+
expect(result.size).to eq(2)
|
|
37
|
+
expect(result.map(&:text)).to contain_exactly("first", "third")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "returns empty nodeset for non-existent ID" do
|
|
41
|
+
ast = Moxml::XPath::Parser.parse('id("nonexistent")')
|
|
42
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
43
|
+
result = proc.call(doc)
|
|
44
|
+
|
|
45
|
+
expect(result).to be_a(Moxml::NodeSet)
|
|
46
|
+
expect(result.size).to eq(0)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "handles multiple space-separated IDs" do
|
|
50
|
+
ast = Moxml::XPath::Parser.parse('id("b a")')
|
|
51
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
52
|
+
result = proc.call(doc)
|
|
53
|
+
|
|
54
|
+
expect(result).to be_a(Moxml::NodeSet)
|
|
55
|
+
expect(result.size).to eq(2)
|
|
56
|
+
expect(result.map(&:text)).to contain_exactly("first", "second")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "ignores duplicate IDs" do
|
|
60
|
+
ast = Moxml::XPath::Parser.parse('id("a a b")')
|
|
61
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
62
|
+
result = proc.call(doc)
|
|
63
|
+
|
|
64
|
+
expect(result).to be_a(Moxml::NodeSet)
|
|
65
|
+
expect(result.map(&:text)).to include("first", "second")
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Test with nodeset argument - requires path evaluation
|
|
69
|
+
xit "accepts nodeset argument containing IDs" do
|
|
70
|
+
xml = <<~XML
|
|
71
|
+
<root>
|
|
72
|
+
<item id="a">first</item>
|
|
73
|
+
<item id="b">second</item>
|
|
74
|
+
<ref>a</ref>
|
|
75
|
+
<ref>b</ref>
|
|
76
|
+
</root>
|
|
77
|
+
XML
|
|
78
|
+
doc_with_refs = context.parse(xml)
|
|
79
|
+
|
|
80
|
+
ast = Moxml::XPath::Parser.parse("id(/root/ref)")
|
|
81
|
+
proc = Moxml::XPath::Compiler.compile_with_cache(ast)
|
|
82
|
+
result = proc.call(doc_with_refs)
|
|
83
|
+
|
|
84
|
+
expect(result).to be_a(Moxml::NodeSet)
|
|
85
|
+
expect(result.size).to eq(2)
|
|
86
|
+
expect(result.map(&:text)).to contain_exactly("first", "second")
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|