Chromium Code Reviews| Index: third_party/WebKit/Source/core/xml/XPathFunctionsTest.cpp |
| diff --git a/third_party/WebKit/Source/core/xml/XPathFunctionsTest.cpp b/third_party/WebKit/Source/core/xml/XPathFunctionsTest.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3947023e8dbd6db3e119c77d611847fa042e43ae |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/xml/XPathFunctionsTest.cpp |
| @@ -0,0 +1,76 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "core/xml/XPathFunctions.h" |
| + |
| +#include "core/dom/Document.h" |
| +#include "core/xml/XPathExpressionNode.h" // EvaluationContext |
| +#include "core/xml/XPathPredicate.h" // Number, StringExpression |
| +#include "core/xml/XPathValue.h" |
| +#include "platform/heap/Handle.h" // HeapVector, Member, etc. |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "wtf/Allocator.h" |
| + |
| +namespace blink { |
| + |
| +class XPathContext { |
|
yosin_UTC9
2016/10/17 03:56:01
Too avoid name crash, it is better to enclose XPat
|
| + STACK_ALLOCATED(); |
| + |
| + public: |
| + XPathContext() : m_document(Document::create()), m_context(*m_document) {} |
| + |
| + XPath::EvaluationContext& context() { return m_context; } |
| + Document& document() { return *m_document; } |
| + |
| + private: |
| + Member<Document> m_document; |
|
yosin_UTC9
2016/10/17 03:56:01
nit: s/Member<Document>/const Member<Document>/
|
| + XPath::EvaluationContext m_context; |
| +}; |
| + |
| +static String substring(const char* string, double pos, double len) { |
| + XPathContext xpath; |
| + HeapVector<Member<XPath::Expression>> args; |
| + args.append(new XPath::StringExpression(string)); |
| + args.append(new XPath::Number(pos)); |
| + args.append(new XPath::Number(len)); |
| + XPath::Expression* call = XPath::createFunction("substring", args); |
| + XPath::Value result = call->evaluate(xpath.context()); |
| + return result.toString(); |
| +} |
| + |
| +TEST(XPathFunctionsTest, substring_emptyString) { |
| + EXPECT_EQ("", substring("", 0., 1.)) |
| + << "substring of an empty string should be the empty string"; |
| +} |
| + |
| +TEST(XPathFunctionsTest, substring) { |
| + EXPECT_EQ("hello", substring("well hello there", 6., 5.)); |
| +} |
| + |
| +TEST(XPathFunctionsTest, substring_negativePosition) { |
| + EXPECT_EQ("hello", substring("hello, world!", -4., 10.)) |
| + << "negative start positions should impinge on the result length"; |
| + // Try to underflow the length adjustment for negative positions. |
| + EXPECT_EQ("", substring("hello", std::numeric_limits<long>::min() + 1, 1.)); |
| +} |
| + |
| +TEST(XPathFunctionsTest, substring_negativeLength) { |
| + EXPECT_EQ("", substring("hello, world!", 1., -3.)) |
| + << "negative lengths should result in an empty string"; |
| + |
| + EXPECT_EQ("", substring("foo", std::numeric_limits<long>::min(), 1.)) |
| + << "large (but long representable) negative position should result in " |
| + << "an empty string"; |
| +} |
| + |
| +TEST(XPathFunctionsTest, substring_extremePositionLength) { |
|
yosin_UTC9
2016/10/17 03:56:01
We want to have test cases for NaN and +Inifity/-I
|
| + EXPECT_EQ("", substring("no way", 1e100, 7.)) |
| + << "extremely large positions should result in the empty string"; |
| + |
| + EXPECT_EQ("no way", substring("no way", -1e200, 1e300)) |
| + << "although these indices are not representable as long, this should " |
| + << "produce the string because indices are computed as doubles"; |
| +} |
| + |
| +} // namespace blink |