| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/xml/XPathFunctions.h" |
| 6 |
| 7 #include "core/dom/Document.h" |
| 8 #include "core/xml/XPathExpressionNode.h" // EvaluationContext |
| 9 #include "core/xml/XPathPredicate.h" // Number, StringExpression |
| 10 #include "core/xml/XPathValue.h" |
| 11 #include "platform/heap/Handle.h" // HeapVector, Member, etc. |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "wtf/Allocator.h" |
| 14 |
| 15 #include <cmath> |
| 16 #include <limits> |
| 17 |
| 18 namespace blink { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class XPathContext { |
| 23 STACK_ALLOCATED(); |
| 24 |
| 25 public: |
| 26 XPathContext() : m_document(Document::create()), m_context(*m_document) {} |
| 27 |
| 28 XPath::EvaluationContext& context() { return m_context; } |
| 29 Document& document() { return *m_document; } |
| 30 |
| 31 private: |
| 32 const Member<Document> m_document; |
| 33 XPath::EvaluationContext m_context; |
| 34 }; |
| 35 |
| 36 using XPathArguments = HeapVector<Member<XPath::Expression>>; |
| 37 |
| 38 static String substring(XPathArguments& args) { |
| 39 XPathContext xpath; |
| 40 XPath::Expression* call = XPath::createFunction("substring", args); |
| 41 XPath::Value result = call->evaluate(xpath.context()); |
| 42 return result.toString(); |
| 43 } |
| 44 |
| 45 static String substring(const char* string, double pos) { |
| 46 XPathArguments args; |
| 47 args.append(new XPath::StringExpression(string)); |
| 48 args.append(new XPath::Number(pos)); |
| 49 return substring(args); |
| 50 } |
| 51 |
| 52 static String substring(const char* string, double pos, double len) { |
| 53 XPathArguments args; |
| 54 args.append(new XPath::StringExpression(string)); |
| 55 args.append(new XPath::Number(pos)); |
| 56 args.append(new XPath::Number(len)); |
| 57 return substring(args); |
| 58 } |
| 59 |
| 60 } // namespace |
| 61 |
| 62 TEST(XPathFunctionsTest, substring_specExamples) { |
| 63 EXPECT_EQ(" car", substring("motor car", 6.0)) |
| 64 << "should select characters staring at position 6 to the end"; |
| 65 EXPECT_EQ("ada", substring("metadata", 4.0, 3.0)) |
| 66 << "should select characters at 4 <= position < 7"; |
| 67 EXPECT_EQ("234", substring("123456", 1.5, 2.6)) |
| 68 << "should select characters at 2 <= position < 5"; |
| 69 EXPECT_EQ("12", substring("12345", 0.0, 3.0)) |
| 70 << "should select characters at 0 <= position < 3; note the first " |
| 71 "position is 1 so this is characters in position 1 and 2"; |
| 72 EXPECT_EQ("", substring("12345", 5.0, -3.0)) |
| 73 << "no characters should have 5 <= position < 2"; |
| 74 EXPECT_EQ("1", substring("12345", -3.0, 5.0)) |
| 75 << "should select characters at -3 <= position < 2; since the first " |
| 76 "position is 1, this is the character at position 1"; |
| 77 EXPECT_EQ("", substring("12345", NAN, 3.0)) |
| 78 << "should select no characters since NaN <= position is always false"; |
| 79 EXPECT_EQ("", substring("12345", 1.0, NAN)) |
| 80 << "should select no characters since position < 1. + NaN is always " |
| 81 "false"; |
| 82 EXPECT_EQ("12345", |
| 83 substring("12345", -42, std::numeric_limits<double>::infinity())) |
| 84 << "should select characters at -42 <= position < Infinity, which is all " |
| 85 "of them"; |
| 86 EXPECT_EQ("", substring("12345", -std::numeric_limits<double>::infinity(), |
| 87 std::numeric_limits<double>::infinity())) |
| 88 << "since -Inf+Inf is NaN, should select no characters since position " |
| 89 "< NaN is always false"; |
| 90 } |
| 91 |
| 92 TEST(XPathFunctionsTest, substring_emptyString) { |
| 93 EXPECT_EQ("", substring("", 0.0, 1.0)) |
| 94 << "substring of an empty string should be the empty string"; |
| 95 } |
| 96 |
| 97 TEST(XPathFunctionsTest, substring) { |
| 98 EXPECT_EQ("hello", substring("well hello there", 6.0, 5.0)); |
| 99 } |
| 100 |
| 101 TEST(XPathFunctionsTest, substring_negativePosition) { |
| 102 EXPECT_EQ("hello", substring("hello, world!", -4.0, 10.0)) |
| 103 << "negative start positions should impinge on the result length"; |
| 104 // Try to underflow the length adjustment for negative positions. |
| 105 EXPECT_EQ("", substring("hello", std::numeric_limits<long>::min() + 1, 1.0)); |
| 106 } |
| 107 |
| 108 TEST(XPathFunctionsTest, substring_negativeLength) { |
| 109 EXPECT_EQ("", substring("hello, world!", 1.0, -3.0)) |
| 110 << "negative lengths should result in an empty string"; |
| 111 |
| 112 EXPECT_EQ("", substring("foo", std::numeric_limits<long>::min(), 1.0)) |
| 113 << "large (but long representable) negative position should result in " |
| 114 << "an empty string"; |
| 115 } |
| 116 |
| 117 TEST(XPathFunctionsTest, substring_extremePositionLength) { |
| 118 EXPECT_EQ("", substring("no way", 1e100, 7.0)) |
| 119 << "extremely large positions should result in the empty string"; |
| 120 |
| 121 EXPECT_EQ("no way", substring("no way", -1e200, 1e300)) |
| 122 << "although these indices are not representable as long, this should " |
| 123 << "produce the string because indices are computed as doubles"; |
| 124 } |
| 125 |
| 126 } // namespace blink |
| OLD | NEW |