vcdom 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +33 -0
- data/lib/vcdom/attr.rb +3 -3
- data/lib/vcdom/character_data.rb +8 -1
- data/lib/vcdom/document.rb +27 -11
- data/lib/vcdom/element.rb +87 -1
- data/lib/vcdom/node.rb +29 -1
- data/lib/vcdom/parent.rb +9 -0
- data/lib/vcdom/{xml_parser.rb → xml_ls/xml_parser.rb} +123 -94
- data/lib/vcdom/{xml_serializer.rb → xml_ls/xml_serializer.rb} +1 -1
- data/lib/vcdom/xml_ls.rb +42 -0
- data/lib/vcdom/xpath/evaluative.rb.old +73 -0
- data/lib/vcdom/xpath/internal/command.rb +121 -0
- data/lib/vcdom/xpath/internal/evaluator.rb +307 -0
- data/lib/vcdom/xpath/internal/expr.rb +35 -0
- data/lib/vcdom/xpath/internal/parser.rb +333 -0
- data/lib/vcdom/xpath/internal/value.rb +342 -0
- data/lib/vcdom/xpath/xpath_evaluator_mod.rb +32 -0
- data/lib/vcdom/xpath/xpath_exception.rb +23 -0
- data/lib/vcdom/xpath/xpath_expression.rb +79 -0
- data/lib/vcdom/xpath/xpath_ns_resolver.rb +36 -0
- data/lib/vcdom/xpath/xpath_result.rb +104 -0
- data/lib/vcdom/xpath.rb +15 -0
- data/test/main_test_1.8.rb.old +1 -0
- data/test/main_test_1.9.rb.old +25 -0
- data/test/test_main.rb +6 -0
- data/test/test_parsing.rb +41 -0
- data/test/test_xpath.rb +266 -0
- metadata +34 -25
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding : utf-8
|
2
|
+
|
3
|
+
module VCDOM::XPath
|
4
|
+
|
5
|
+
class XPathException < Exception
|
6
|
+
|
7
|
+
# If the expression has a syntax error or otherwise is not a legal expression
|
8
|
+
# according to the rules of the specific XPathEvaluator or contains specialized
|
9
|
+
# extension functions or variables not supported by this implementation.
|
10
|
+
INVALID_EXPRESSION_ERR = :invalid_expression_err
|
11
|
+
# If the expression cannot be converted to return the specified type.
|
12
|
+
TYPE_ERR = :type_err
|
13
|
+
|
14
|
+
attr_reader :type
|
15
|
+
|
16
|
+
def initialize( type, msg )
|
17
|
+
super msg
|
18
|
+
@type = type
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# coding : utf-8
|
2
|
+
|
3
|
+
module VCDOM::XPath
|
4
|
+
|
5
|
+
# This class implements {the interface XPathExpression of W3C DOM XPath}[http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathExpression].
|
6
|
+
#
|
7
|
+
# == Way to create a XPathExpression object
|
8
|
+
# When you want a XPathExpression object, please use XPathEvaluatorMod#create_expression method.
|
9
|
+
# The class Document implements the module XPathEvaluatorMod when you required "vcdom/xpath", so you can use this method as the following:
|
10
|
+
#
|
11
|
+
# require "vcdom"
|
12
|
+
# require "vcdom/xpath"
|
13
|
+
# # doc is a Document object
|
14
|
+
# expr = doc.create_expression( "/test/abc", nil )
|
15
|
+
#
|
16
|
+
class XPathExpression
|
17
|
+
|
18
|
+
#require "vcdom/xpath/internal/tokenizer"
|
19
|
+
#require "vcdom/xpath/internal/parser"
|
20
|
+
require "vcdom/xpath/xpath_result"
|
21
|
+
require "vcdom/xpath/internal/evaluator"
|
22
|
+
|
23
|
+
def initialize( expr ) # :nodoc:
|
24
|
+
@expr = expr
|
25
|
+
end
|
26
|
+
|
27
|
+
# Evaluates the XPath expression.
|
28
|
+
#
|
29
|
+
# Usage:
|
30
|
+
#
|
31
|
+
# # expr is a XPathExpression object, and elem is an Element object.
|
32
|
+
# result = expr.evaluate( elem, :first_ordered_node_type )
|
33
|
+
# res_node = result.single_node_value
|
34
|
+
def evaluate( context_node, type, result = nil )
|
35
|
+
res = Internal::Evaluator.new( context_node ).evaluate_expr( @expr )
|
36
|
+
case type
|
37
|
+
when :any_type
|
38
|
+
case res.value_type
|
39
|
+
when :number
|
40
|
+
type = :number_type
|
41
|
+
when :string
|
42
|
+
type = :string_type
|
43
|
+
when :boolean
|
44
|
+
type = :boolean_type
|
45
|
+
when :node_set
|
46
|
+
type = :unordered_node_iterator_type
|
47
|
+
else
|
48
|
+
raise "INTERNAL ERROR"
|
49
|
+
end
|
50
|
+
when :number_type
|
51
|
+
res = res.to_number_value if res.value_type != :number
|
52
|
+
xpath_result = XPathResult::ResultNumber.new( res.value )
|
53
|
+
when :string_type
|
54
|
+
res = res.to_string_value if res.value_type != :string
|
55
|
+
xpath_result = XPathResult::ResultString.new( res.value )
|
56
|
+
when :boolean_type
|
57
|
+
res = res.to_boolean_value if res.value_type != :boolean
|
58
|
+
xpath_result = XPathResult::ResultBoolean.new( res.value )
|
59
|
+
when :first_ordered_node_type
|
60
|
+
res.sort
|
61
|
+
xpath_result = XPathResult::ResultSingleNode.new( res.value[0], :first_ordered_node_type )
|
62
|
+
when :any_unordered_node_type
|
63
|
+
xpath_result = XPathResult::ResultSingleNode.new( res.value[0], :any_unordered_node_type )
|
64
|
+
when :ordered_node_snapshot_type
|
65
|
+
res.sort
|
66
|
+
xpath_result = XPathResult::ResultNodesSnapshot.new( res.value, :ordered_node_snapshot_type )
|
67
|
+
when :unordered_node_snapshot_type
|
68
|
+
xpath_result = XPathResult::ResultNodesSnapshot.new( res.value, :unordered_node_snapshot_type )
|
69
|
+
when :ordered_node_iterator_type, :unordered_node_iterator_type
|
70
|
+
raise "not still be supported" # TODO
|
71
|
+
else
|
72
|
+
raise "USER ERROR"
|
73
|
+
end
|
74
|
+
return xpath_result
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding : utf-8
|
2
|
+
|
3
|
+
module VCDOM::XPath # :nodoc:
|
4
|
+
|
5
|
+
# A XPathNSResolver object permit prefix strings in the expression
|
6
|
+
# to be properly bound to namespaceURI strings. XPathEvaluator can
|
7
|
+
# construct an implementation of XPathNSResolver from a node, or
|
8
|
+
# the interface may be implemented by any application.
|
9
|
+
#
|
10
|
+
# == Way to create a XPathNSResolver object
|
11
|
+
#
|
12
|
+
# If you want a XPathNSResolver object, please use XPathEvaluatorMod#create_expression.
|
13
|
+
# The class Document includes the module XPathEvaluatorMod when you required "vcdom/xpath", so you can use this method as the following:
|
14
|
+
#
|
15
|
+
# require "vcdom"
|
16
|
+
# require "vcdom/xpath"
|
17
|
+
# # doc is a Document object
|
18
|
+
# resolver = doc.create_ns_resolver( node )
|
19
|
+
#
|
20
|
+
class XPathNSResolver
|
21
|
+
|
22
|
+
def initialize( ns_resolver ) # :nodoc:
|
23
|
+
@ns_resolver = ns_resolver
|
24
|
+
end
|
25
|
+
|
26
|
+
# Look up the namespace URI associated to the given namespace prefix.
|
27
|
+
# The XPath evaluator must never call this with a null or empty argument,
|
28
|
+
# because the result of doing this is undefined.
|
29
|
+
def lookup_namespace_uri( prefix )
|
30
|
+
# TODO
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# coding : utf-8
|
2
|
+
|
3
|
+
require "vcdom/xpath/xpath_exception"
|
4
|
+
|
5
|
+
module VCDOM::XPath
|
6
|
+
|
7
|
+
class XPathResult
|
8
|
+
|
9
|
+
ANY_TYPE = :any_type
|
10
|
+
NUMBER_TYPE = :number_type
|
11
|
+
STRING_TYPE = :string_type
|
12
|
+
BOOLEAN_TYPE = :boolean_type
|
13
|
+
UNORDERED_NODE_ITERATOR_TYPE = :unordered_node_iterator_type
|
14
|
+
ORDERED_NODE_ITERATOR_TYPE = :ordered_node_iterator_type
|
15
|
+
UNORDERED_NODE_SNAPSHOT_TYPE = :unordered_node_snapshot_type
|
16
|
+
ORDERED_NODE_SNAPSHOT_TYPE = :ordered_node_snapshot_type
|
17
|
+
ANY_UNORDERED_NODE_TYPE = :any_unordered_node_type
|
18
|
+
FIRST_ORDERED_NODE_TYPE = :first_ordered_node_type
|
19
|
+
|
20
|
+
def result_type
|
21
|
+
raise "INTERNAL ERROR"
|
22
|
+
end
|
23
|
+
# Signifies that the iterator has become invalid. True if resultType is UNORDERED_NODE_ITERATOR_TYPE
|
24
|
+
# or ORDERED_NODE_ITERATOR_TYPE and the document has been modified since this result was returned.
|
25
|
+
def invalid_iterator_state
|
26
|
+
false
|
27
|
+
end
|
28
|
+
def snapshot_length
|
29
|
+
raise XPathException.new( XPathException::TYPE_ERR,
|
30
|
+
"result type is not UNORDERED_NODE_SNAPSHOT_TYPE or ORDERED_NODE_SNAPSHOT_TYPE" )
|
31
|
+
end
|
32
|
+
|
33
|
+
# raises(XPathException) on retrieval
|
34
|
+
def number_value
|
35
|
+
raise XPathException.new( XPathException::TYPE_ERR, "result type is not NUMBER_TYPE" )
|
36
|
+
end
|
37
|
+
def string_value
|
38
|
+
raise XPathException.new( XPathException::TYPE_ERR, "result type is not STRING_TYPE" )
|
39
|
+
end
|
40
|
+
def boolean_value
|
41
|
+
raise XPathException.new( XPathException::TYPE_ERR, "result type is not STRING_TYPE" )
|
42
|
+
end
|
43
|
+
def single_node_value
|
44
|
+
raise XPathException.new( XPathException::TYPE_ERR,
|
45
|
+
"result type is not ANY_UNORDERED_NODE_TYPE or FIRST_ORDERED_NODE_TYPE" )
|
46
|
+
end
|
47
|
+
def snapshot_item( index )
|
48
|
+
raise XPathException.new( XPathException::TYPE_ERR,
|
49
|
+
"result type is not UNORDERED_NODE_SNAPSHOT_TYPE or ORDERED_NODE_SNAPSHOT_TYPE" )
|
50
|
+
end
|
51
|
+
def iterate_next()
|
52
|
+
raise XPathException.new( XPathException::TYPE_ERR,
|
53
|
+
"result type is not UNORDERED_NODE_ITERATOR_TYPE or ORDERED_NODE_ITERATOR_TYPE" )
|
54
|
+
end
|
55
|
+
def initialize( value ) # :nodoc:
|
56
|
+
@value = value
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
class VCDOM::XPath::XPathResult
|
64
|
+
|
65
|
+
# This class is subclass of XPathResult.
|
66
|
+
# This exists to represent a number type result of XPath expression.
|
67
|
+
class ResultNumber < self
|
68
|
+
def result_type; NUMBER_TYPE end
|
69
|
+
def number_value; @value end
|
70
|
+
end
|
71
|
+
class ResultString < self
|
72
|
+
def result_type; STRING_TYPE end
|
73
|
+
def string_value; @value end
|
74
|
+
end
|
75
|
+
class ResultBoolean < self
|
76
|
+
def result_type; BOOLEAN_TYPE end
|
77
|
+
def boolean_value; @value end
|
78
|
+
TRUE = self.new( true )
|
79
|
+
FALSE = self.new( false )
|
80
|
+
end
|
81
|
+
class ResultSingleNode < self
|
82
|
+
def initialize( node, type ) # :nodoc:
|
83
|
+
super( node )
|
84
|
+
@type = type
|
85
|
+
end
|
86
|
+
def result_type; @type end
|
87
|
+
def single_node_value; @value end
|
88
|
+
end
|
89
|
+
class ResultNodesSnapshot < self
|
90
|
+
def initialize( nodes, type ) # :nodoc:
|
91
|
+
super( nodes )
|
92
|
+
@type = type
|
93
|
+
end
|
94
|
+
def result_type; @type end
|
95
|
+
def snapshot_length
|
96
|
+
@value.length
|
97
|
+
end
|
98
|
+
def snapshot_item( index )
|
99
|
+
@value[index]
|
100
|
+
end
|
101
|
+
alias :[] :snapshot_item
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
data/lib/vcdom/xpath.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "test/unit"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
require "test/unit"
|
4
|
+
|
5
|
+
require "vcdom/xml_ls"
|
6
|
+
#require "vcdom/xml_parser"
|
7
|
+
#require "vcdom/xml_serializer"
|
8
|
+
|
9
|
+
class TestParsing < Test::Unit::TestCase
|
10
|
+
|
11
|
+
@@ls_impl = VCDOM::XMLLS
|
12
|
+
|
13
|
+
def test_of_one_element_doc_parsing
|
14
|
+
parser = @@ls_impl.create_ls_parser( :mode_asynchronous, nil )
|
15
|
+
input = @@ls_impl.create_ls_input()
|
16
|
+
input.string_data = "<empty-element/>"
|
17
|
+
doc = parser.parse( input )
|
18
|
+
root_elem = doc.first_child
|
19
|
+
assert_equal( "empty-element", root_elem.node_name )
|
20
|
+
assert( root_elem.equal? doc.document_element )
|
21
|
+
assert( root_elem.equal? doc.last_child )
|
22
|
+
assert( root_elem.parent_node.equal? doc )
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/test/test_main.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
require "test/unit"
|
4
|
+
|
5
|
+
require "vcdom/xml_ls"
|
6
|
+
|
7
|
+
class TestParsing < Test::Unit::TestCase
|
8
|
+
|
9
|
+
@@ls_impl = VCDOM::XMLLS
|
10
|
+
|
11
|
+
def test_of_parsing_one_element_xml_string()
|
12
|
+
parser = @@ls_impl.create_ls_parser( :mode_asynchronous, nil )
|
13
|
+
input = @@ls_impl.create_ls_input()
|
14
|
+
input.string_data = "<empty-element/>"
|
15
|
+
doc = parser.parse( input )
|
16
|
+
root_elem = doc.document_element
|
17
|
+
assert_equal( "empty-element", root_elem.node_name )
|
18
|
+
assert( root_elem.equal? doc.first_child )
|
19
|
+
assert( root_elem.equal? doc.last_child )
|
20
|
+
assert( root_elem.parent_node.equal? doc )
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_of_parsing_text_node()
|
24
|
+
parser = @@ls_impl.create_ls_parser( :mode_asynchronous, nil )
|
25
|
+
input = @@ls_impl.create_ls_input()
|
26
|
+
input.string_data = "<element>テキストノードです\n改行</element>"
|
27
|
+
doc = parser.parse( input )
|
28
|
+
root_elem = doc.document_element
|
29
|
+
assert_equal( "テキストノードです\n改行", root_elem.first_child.node_value )
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_of_parsing_text_node_within_predefined_char_ref()
|
33
|
+
parser = @@ls_impl.create_ls_parser( :mode_asynchronous, nil )
|
34
|
+
input = @@ls_impl.create_ls_input()
|
35
|
+
input.string_data = "<element>テキストノードです<うん>私&あなた"'&</element>"
|
36
|
+
doc = parser.parse( input )
|
37
|
+
root_elem = doc.document_element
|
38
|
+
assert_equal( "テキストノードです<うん>私&あなた\"'&", root_elem.first_child.node_value )
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/test/test_xpath.rb
ADDED
@@ -0,0 +1,266 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
require "test/unit"
|
4
|
+
|
5
|
+
require "vcdom/xpath"
|
6
|
+
require "vcdom/xpath/xpath_expression"
|
7
|
+
require "vcdom/xpath/xpath_result"
|
8
|
+
require "vcdom/xpath/internal/value"
|
9
|
+
require "vcdom/xpath/internal/expr"
|
10
|
+
require "vcdom/xpath/internal/command"
|
11
|
+
|
12
|
+
class TestXPath < Test::Unit::TestCase
|
13
|
+
|
14
|
+
@@ls_impl = VCDOM::XMLLS
|
15
|
+
|
16
|
+
require "vcdom/document"
|
17
|
+
@@doc = VCDOM::Document._new()
|
18
|
+
elem = @@doc.append_child @@doc.create_element( "test" )
|
19
|
+
elem << @@doc.create_element( "test-child1" )
|
20
|
+
elem << @@doc.create_element( "test-child2" )
|
21
|
+
elem << @@doc.create_element( "test-child3" )
|
22
|
+
#doc.create_expression(nil,nil)
|
23
|
+
|
24
|
+
def test_creating_xpath_expression()
|
25
|
+
xpath_expr = @@doc.create_expression( "//test-child1/.././test-child1", nil )
|
26
|
+
res = xpath_expr.evaluate( @@doc, :any_unordered_node_type )
|
27
|
+
assert_equal @@doc.document_element.first_child, res.single_node_value
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_creating_xpath_expression_snapshot()
|
31
|
+
res = @@doc.evaluate( "/*/child::*[2]", @@doc, nil, :unordered_node_snapshot_type )
|
32
|
+
assert_equal :unordered_node_snapshot_type, res.result_type
|
33
|
+
assert_equal 1, res.snapshot_length
|
34
|
+
assert_equal @@doc.first_child.first_child.next_sibling, res.snapshot_item( 0 )
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# 関数呼び出しのテスト
|
39
|
+
def test_of_evaluating_function_call()
|
40
|
+
res_type = :number_type
|
41
|
+
res = evaluate_equality_expr( res_type, res_type, 1.0 ) do |expr|
|
42
|
+
expr << VCDOM::XPath::Internal::FunctionCallCommand.new( :position, nil )
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# 数値, リテラル, 真偽値単体を返す式のチェック
|
48
|
+
# 結果は数値として受け取る
|
49
|
+
def test_of_evaluating_expr_having_only_one_value_number()
|
50
|
+
res_type = :number_type
|
51
|
+
res = evaluate_equality_expr( res_type, res_type, 10.0 ) do |expr|
|
52
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 10 )
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# 数値, リテラル, 真偽値単体を返す式のチェック
|
58
|
+
# 結果は文字列として受け取る
|
59
|
+
def test_of_evaluating_expr_having_only_one_value_string()
|
60
|
+
res_type = :string_type
|
61
|
+
res = evaluate_equality_expr( res_type, res_type, "10" ) do |expr|
|
62
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 10 )
|
63
|
+
end
|
64
|
+
res = evaluate_equality_expr( res_type, res_type, "100.5" ) do |expr|
|
65
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 100.5 )
|
66
|
+
end
|
67
|
+
res = evaluate_equality_expr( res_type, res_type, "てすと" ) do |expr|
|
68
|
+
expr << VCDOM::XPath::Internal::StringValue.new( "てすと" )
|
69
|
+
end
|
70
|
+
res = evaluate_equality_expr( res_type, res_type, "true" ) do |expr|
|
71
|
+
expr << VCDOM::XPath::Internal::FunctionCallCommand.new( :true, nil )
|
72
|
+
end
|
73
|
+
res = evaluate_equality_expr( res_type, res_type, "false" ) do |expr|
|
74
|
+
expr << VCDOM::XPath::Internal::FunctionCallCommand.new( :false, nil )
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
##
|
79
|
+
# 数値, リテラル, 真偽値単体を返す式のチェック
|
80
|
+
# 結果は真偽値として受け取る
|
81
|
+
def test_of_evaluating_expr_having_only_one_value_boolean()
|
82
|
+
res_type = :boolean_type
|
83
|
+
res = evaluate_equality_expr( res_type, res_type, true ) do |expr|
|
84
|
+
expr << VCDOM::XPath::Internal::FunctionCallCommand.new( :true, nil )
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# 算術演算のテスト
|
90
|
+
def test_operation_of_numbers()
|
91
|
+
res_type = :number_type
|
92
|
+
res = evaluate_equality_expr( res_type, res_type, 5.0 ) do |expr|
|
93
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 5 )
|
94
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 5 )
|
95
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"+" )
|
96
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 10 )
|
97
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"-@" )
|
98
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"+" )
|
99
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 1 )
|
100
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"-" )
|
101
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 2 )
|
102
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"div" )
|
103
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( -10 )
|
104
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"*" )
|
105
|
+
end
|
106
|
+
end
|
107
|
+
def test_operation_of_numbers_mod()
|
108
|
+
res_type = :number_type
|
109
|
+
res = evaluate_equality_expr( res_type, res_type, 1.0 ) do |expr|
|
110
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 5 )
|
111
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 2 )
|
112
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"mod" )
|
113
|
+
end
|
114
|
+
res = evaluate_equality_expr( res_type, res_type, 1.0 ) do |expr|
|
115
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 5 )
|
116
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( -2 )
|
117
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"mod" )
|
118
|
+
end
|
119
|
+
res = evaluate_equality_expr( res_type, res_type, -1.0 ) do |expr|
|
120
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( -5 )
|
121
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 2 )
|
122
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"mod" )
|
123
|
+
end
|
124
|
+
res = evaluate_equality_expr( res_type, res_type, -1.0 ) do |expr|
|
125
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( -5 )
|
126
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( -2 )
|
127
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"mod" )
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
##
|
132
|
+
# 文字列を数値に変換するテスト
|
133
|
+
def test_conversion_str_to_num()
|
134
|
+
res_type = :number_type
|
135
|
+
res = evaluate_equality_expr( res_type, res_type, 105.0 ) do |expr|
|
136
|
+
expr << VCDOM::XPath::Internal::StringValue.new( "105.0" )
|
137
|
+
end
|
138
|
+
res = evaluate_equality_expr( res_type, res_type, 12.0 ) do |expr|
|
139
|
+
expr << VCDOM::XPath::Internal::StringValue.new( "12." )
|
140
|
+
end
|
141
|
+
res = evaluate_equality_expr( res_type, res_type, -0.5 ) do |expr|
|
142
|
+
expr << VCDOM::XPath::Internal::StringValue.new( "-.500000" )
|
143
|
+
end
|
144
|
+
res = evaluate_equality_expr( res_type, res_type, lambda { |v| v.nan? } ) do |expr|
|
145
|
+
expr << VCDOM::XPath::Internal::StringValue.new( "45aa" )
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
##
|
150
|
+
# Boolean に変換するテスト
|
151
|
+
def test_conversion_num_to_bool()
|
152
|
+
res_type = :boolean_type
|
153
|
+
res = evaluate_equality_expr( res_type, res_type, true ) do |expr|
|
154
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 105.0 )
|
155
|
+
end
|
156
|
+
res = evaluate_equality_expr( res_type, res_type, false ) do |expr|
|
157
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 0.0 )
|
158
|
+
end
|
159
|
+
end
|
160
|
+
def test_conversion_str_to_bool()
|
161
|
+
res_type = :boolean_type
|
162
|
+
res = evaluate_equality_expr( res_type, res_type, true ) do |expr|
|
163
|
+
expr << VCDOM::XPath::Internal::StringValue.new( "aaaa" )
|
164
|
+
end
|
165
|
+
res = evaluate_equality_expr( res_type, res_type, false ) do |expr|
|
166
|
+
expr << VCDOM::XPath::Internal::StringValue.new( "" )
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
##
|
171
|
+
# 比較演算テスト
|
172
|
+
def test_operation_of_relation()
|
173
|
+
res_type = :boolean_type
|
174
|
+
res = evaluate_equality_expr( res_type, res_type, false ) do |expr|
|
175
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 5 )
|
176
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 5 )
|
177
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :">" )
|
178
|
+
end
|
179
|
+
res = evaluate_equality_expr( res_type, res_type, true ) do |expr|
|
180
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( "5" )
|
181
|
+
expr << VCDOM::XPath::Internal::NumberValue.new( 100 )
|
182
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"<" )
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
##
|
187
|
+
# Node を取得するテスト
|
188
|
+
def test_root_node()
|
189
|
+
res_type = :any_unordered_node_type
|
190
|
+
res = evaluate_equality_expr( res_type, res_type, @@doc ) do |expr|
|
191
|
+
expr << VCDOM::XPath::Internal::RootNodeCommand.new()
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
##
|
196
|
+
# Node を選択するテスト
|
197
|
+
def test_node_selection()
|
198
|
+
res_type = :any_unordered_node_type
|
199
|
+
res = evaluate_equality_expr( res_type, res_type, @@doc.document_element ) do |expr|
|
200
|
+
expr << VCDOM::XPath::Internal::RootNodeCommand.new()
|
201
|
+
expr << VCDOM::XPath::Internal::NodeSelectionCommand.new( :child, :named_node, nil, nil, nil )
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
##
|
206
|
+
# union 演算子のテスト
|
207
|
+
def test_operator_union()
|
208
|
+
res_type = :any_unordered_node_type
|
209
|
+
res = evaluate_equality_expr( res_type, res_type, @@doc ) do |expr|
|
210
|
+
expr << VCDOM::XPath::Internal::RootNodeCommand.new()
|
211
|
+
expr << VCDOM::XPath::Internal::RootNodeCommand.new()
|
212
|
+
expr << VCDOM::XPath::Internal.get_operation_command( :"|" )
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
##
|
217
|
+
# Predicate テスト
|
218
|
+
def test_predicate()
|
219
|
+
res_type = :any_unordered_node_type
|
220
|
+
res = evaluate_equality_expr( res_type, res_type, @@doc ) do |expr|
|
221
|
+
expr << VCDOM::XPath::Internal::RootNodeCommand.new()
|
222
|
+
expr << VCDOM::XPath::Internal::PredsEvalCommand.new( [
|
223
|
+
VCDOM::XPath::Internal::EqualityExpr.new(
|
224
|
+
VCDOM::XPath::Internal::NumberValue.new( 1.0 )
|
225
|
+
)
|
226
|
+
] )
|
227
|
+
end
|
228
|
+
res_type = :any_unordered_node_type
|
229
|
+
res = evaluate_equality_expr( res_type, res_type, nil ) do |expr|
|
230
|
+
expr << VCDOM::XPath::Internal::RootNodeCommand.new()
|
231
|
+
expr << VCDOM::XPath::Internal::PredsEvalCommand.new( [
|
232
|
+
VCDOM::XPath::Internal::EqualityExpr.new(
|
233
|
+
VCDOM::XPath::Internal::NumberValue.new( 2.0 )
|
234
|
+
)
|
235
|
+
] )
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
##
|
240
|
+
# equality expr のテストを補助するための関数
|
241
|
+
def evaluate_equality_expr( res_type, expected_type, expected_val )
|
242
|
+
internal_expr = VCDOM::XPath::Internal::EqualityExpr.new()
|
243
|
+
yield( internal_expr )
|
244
|
+
expr = VCDOM::XPath::XPathExpression.new( internal_expr )
|
245
|
+
res = expr.evaluate( @@doc, res_type, nil )
|
246
|
+
assert_equal( expected_type, res.result_type )
|
247
|
+
case expected_type
|
248
|
+
when :number_type
|
249
|
+
val = res.number_value
|
250
|
+
when :string_type
|
251
|
+
val = res.string_value
|
252
|
+
when :boolean_type
|
253
|
+
val = res.boolean_value
|
254
|
+
when :any_unordered_node_type, :first_ordered_node_type
|
255
|
+
val = res.single_node_value
|
256
|
+
else
|
257
|
+
raise "ERROR"
|
258
|
+
end
|
259
|
+
if expected_val.is_a? Proc then
|
260
|
+
assert expected_val.call( val )
|
261
|
+
else
|
262
|
+
assert_equal( expected_val, val )
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
end
|