【发布时间】:2013-09-24 00:46:32
【问题描述】:
如果有一个 orderEvents 元素并且该元素有一个不是 orderEvent 或空文本的子节点,我有以下代码应该添加一个错误。测试用例更好地展示了我所追求的。
XPATH 表达式适用于所有情况,除非orderEvents 元素中有空白区域。请参阅下面的测试用例 validVendorPaymentFormat7。这是我的 XPATH 表达式。
"./expectedVendorPaymentTransactions/orderEvents/node()[not(self::orderEvent) or self::text()[normalize-space(.)!='']]"
测试用例:
<TestData>
<TestcaseList>
<Testcase>
<rootIdentifier>validVendorPaymentFormat1</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat2</rootIdentifier>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat3</rootIdentifier>
<expectedVendorPaymentTransactions>
</expectedVendorPaymentTransactions>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat4</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents><orderEvent>SOME_EVENT</orderEvent></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat5</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents><orderEvent>SOME_EVENT</orderEvent></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat6</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents><orderEvent>SOME_EVENT</orderEvent><orderEvent>SOME_EVENT</orderEvent></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat7</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents>
<orderEvent>SOME_EVENT</orderEvent>
</orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
</TestcaseList>
</TestData>
失败案例是validVendorPaymentFormat7
失败消息:
[exec] Failure:
[exec] Order vendor payments format contains elements other than orderEvent for validVendorPaymentFormat7 ["\n ", "\n "].
[exec] <false> is not true.
将失败的情况改为:
<Testcase>
<rootIdentifier>validVendorPaymentFormat7</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents><orderEvent>SOME_EVENT</orderEvent></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
通过。
不幸的是,http://www.freeformatter.com/xpath-tester.html 测试用例 #7 按预期返回一个空列表。
更新 - 添加代码
Ruby 版本:1.9
irb -v = 0.9.6
测试文件:
require 'rexml/document'
xpath = "//expectedVendorPaymentTransactions/orderEvents/node()[not(self::orderEvent) or self::text()[translate(., ' ', '')!='']]"
document = REXML::Document.new <<EOF
<Testcase>
<rootIdentifier>validVendorPaymentFormat7</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents>
<orderEvent>SOME_EVENT</orderEvent>
</orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
EOF
document2 = REXML::Document.new <<EOF
<Testcase>
<rootIdentifier>validVendorPaymentFormat7</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents><orderEvent>SOME_EVENT</orderEvent></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
EOF
puts "1: #{REXML::XPath.match(document, xpath).inspect}"
puts "2: #{REXML::XPath.match(document2, xpath).inspect}"
输出:
irb(main):001:0> load './test/test_rexp.rb'
1: ["\n ", "\n "]
2: []
=> true
Jens 的最新版本:
irb(main):009:0> load './test/test_rexp.rb'
1: ", "
2: ", "
=> true
【问题讨论】: