Chromium Code Reviews| 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 namespace blink { | |
| 16 | |
| 17 class XPathContext { | |
|
yosin_UTC9
2016/10/17 03:56:01
Too avoid name crash, it is better to enclose XPat
| |
| 18 STACK_ALLOCATED(); | |
| 19 | |
| 20 public: | |
| 21 XPathContext() : m_document(Document::create()), m_context(*m_document) {} | |
| 22 | |
| 23 XPath::EvaluationContext& context() { return m_context; } | |
| 24 Document& document() { return *m_document; } | |
| 25 | |
| 26 private: | |
| 27 Member<Document> m_document; | |
|
yosin_UTC9
2016/10/17 03:56:01
nit: s/Member<Document>/const Member<Document>/
| |
| 28 XPath::EvaluationContext m_context; | |
| 29 }; | |
| 30 | |
| 31 static String substring(const char* string, double pos, double len) { | |
| 32 XPathContext xpath; | |
| 33 HeapVector<Member<XPath::Expression>> args; | |
| 34 args.append(new XPath::StringExpression(string)); | |
| 35 args.append(new XPath::Number(pos)); | |
| 36 args.append(new XPath::Number(len)); | |
| 37 XPath::Expression* call = XPath::createFunction("substring", args); | |
| 38 XPath::Value result = call->evaluate(xpath.context()); | |
| 39 return result.toString(); | |
| 40 } | |
| 41 | |
| 42 TEST(XPathFunctionsTest, substring_emptyString) { | |
| 43 EXPECT_EQ("", substring("", 0., 1.)) | |
| 44 << "substring of an empty string should be the empty string"; | |
| 45 } | |
| 46 | |
| 47 TEST(XPathFunctionsTest, substring) { | |
| 48 EXPECT_EQ("hello", substring("well hello there", 6., 5.)); | |
| 49 } | |
| 50 | |
| 51 TEST(XPathFunctionsTest, substring_negativePosition) { | |
| 52 EXPECT_EQ("hello", substring("hello, world!", -4., 10.)) | |
| 53 << "negative start positions should impinge on the result length"; | |
| 54 // Try to underflow the length adjustment for negative positions. | |
| 55 EXPECT_EQ("", substring("hello", std::numeric_limits<long>::min() + 1, 1.)); | |
| 56 } | |
| 57 | |
| 58 TEST(XPathFunctionsTest, substring_negativeLength) { | |
| 59 EXPECT_EQ("", substring("hello, world!", 1., -3.)) | |
| 60 << "negative lengths should result in an empty string"; | |
| 61 | |
| 62 EXPECT_EQ("", substring("foo", std::numeric_limits<long>::min(), 1.)) | |
| 63 << "large (but long representable) negative position should result in " | |
| 64 << "an empty string"; | |
| 65 } | |
| 66 | |
| 67 TEST(XPathFunctionsTest, substring_extremePositionLength) { | |
|
yosin_UTC9
2016/10/17 03:56:01
We want to have test cases for NaN and +Inifity/-I
| |
| 68 EXPECT_EQ("", substring("no way", 1e100, 7.)) | |
| 69 << "extremely large positions should result in the empty string"; | |
| 70 | |
| 71 EXPECT_EQ("no way", substring("no way", -1e200, 1e300)) | |
| 72 << "although these indices are not representable as long, this should " | |
| 73 << "produce the string because indices are computed as doubles"; | |
| 74 } | |
| 75 | |
| 76 } // namespace blink | |
| OLD | NEW |