Bug 1362330 - Part 2: Add testcases for C++ XPathGenerator's functions. r=mikedeboer,r=smaug

MozReview-Commit-ID: HhFE20QgoZb
This commit is contained in:
Beekill95 2017-06-15 08:51:41 +07:00
Родитель 463ad1bdff
Коммит 6749a72a90
4 изменённых файлов: 209 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,140 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "XPathGenerator.h"
#include "nsString.h"
TEST(TestXPathGenerator, TestQuoteArgumentWithoutQuote)
{
nsAutoString arg;
arg.Assign(NS_LITERAL_STRING("testing"));
nsAutoString expectedResult;
expectedResult.Assign(NS_LITERAL_STRING("\'testing\'"));
nsAutoString result;
XPathGenerator::QuoteArgument(arg, result);
ASSERT_TRUE(expectedResult.Equals(result));
}
TEST(TestXPathGenerator, TestQuoteArgumentWithSingleQuote)
{
nsAutoString arg;
arg.Assign(NS_LITERAL_STRING("\'testing\'"));
nsAutoString expectedResult;
expectedResult.Assign(NS_LITERAL_STRING("\"\'testing\'\""));
nsAutoString result;
XPathGenerator::QuoteArgument(arg, result);
ASSERT_TRUE(expectedResult.Equals(result));
}
TEST(TestXPathGenerator, TestQuoteArgumentWithDoubleQuote)
{
nsAutoString arg;
arg.Assign(NS_LITERAL_STRING("\"testing\""));
nsAutoString expectedResult;
expectedResult.Assign(NS_LITERAL_STRING("\'\"testing\"\'"));
nsAutoString result;
XPathGenerator::QuoteArgument(arg, result);
ASSERT_TRUE(expectedResult.Equals(result));
}
TEST(TestXPathGenerator, TestQuoteArgumentWithSingleAndDoubleQuote)
{
nsAutoString arg;
arg.Assign(NS_LITERAL_STRING("\'testing\""));
nsAutoString expectedResult;
expectedResult.Assign(NS_LITERAL_STRING("concat(\'\',\"\'\",\'testing\"\')"));
nsAutoString result;
XPathGenerator::QuoteArgument(arg, result);
printf("Result: %s\nExpected: %s\n", NS_ConvertUTF16toUTF8(result).get(), NS_ConvertUTF16toUTF8(expectedResult).get());
ASSERT_TRUE(expectedResult.Equals(result));
}
TEST(TestXPathGenerator, TestQuoteArgumentWithDoubleQuoteAndASequenceOfSingleQuote)
{
nsAutoString arg;
arg.Assign(NS_LITERAL_STRING("\'\'\'\'testing\""));
nsAutoString expectedResult;
expectedResult.Assign(NS_LITERAL_STRING("concat(\'\',\"\'\'\'\'\",\'testing\"\')"));
nsAutoString result;
XPathGenerator::QuoteArgument(arg, result);
printf("Result: %s\nExpected: %s\n", NS_ConvertUTF16toUTF8(result).get(), NS_ConvertUTF16toUTF8(expectedResult).get());
ASSERT_TRUE(expectedResult.Equals(result));
}
TEST(TestXPathGenerator, TestQuoteArgumentWithDoubleQuoteAndTwoSequencesOfSingleQuote)
{
nsAutoString arg;
arg.Assign(NS_LITERAL_STRING("\'\'\'\'testing\'\'\'\'\'\'\""));
nsAutoString expectedResult;
expectedResult.Assign(NS_LITERAL_STRING("concat(\'\',\"\'\'\'\'\",\'testing\',\"\'\'\'\'\'\'\",\'\"\')"));
nsAutoString result;
XPathGenerator::QuoteArgument(arg, result);
printf("Result: %s\nExpected: %s\n", NS_ConvertUTF16toUTF8(result).get(), NS_ConvertUTF16toUTF8(expectedResult).get());
ASSERT_TRUE(expectedResult.Equals(result));
}
TEST(TestXPathGenerator, TestQuoteArgumentWithDoubleQuoteAndTwoSequencesOfSingleQuoteInMiddle)
{
nsAutoString arg;
arg.Assign(NS_LITERAL_STRING("t\'\'\'\'estin\'\'\'\'\'\'\"g"));
nsAutoString expectedResult;
expectedResult.Assign(NS_LITERAL_STRING("concat(\'t\',\"\'\'\'\'\",\'estin\',\"\'\'\'\'\'\'\",\'\"g\')"));
nsAutoString result;
XPathGenerator::QuoteArgument(arg, result);
printf("Result: %s\nExpected: %s\n", NS_ConvertUTF16toUTF8(result).get(), NS_ConvertUTF16toUTF8(expectedResult).get());
ASSERT_TRUE(expectedResult.Equals(result));
}
TEST(TestXPathGenerator, TestEscapeNameWithNormalCharacters)
{
nsAutoString arg;
arg.Assign(NS_LITERAL_STRING("testing"));
nsAutoString expectedResult;
expectedResult.Assign(NS_LITERAL_STRING("testing"));
nsAutoString result;
XPathGenerator::EscapeName(arg, result);
ASSERT_TRUE(expectedResult.Equals(result));
}
TEST(TestXPathGenerator, TestEscapeNameWithSpecialCharacters)
{
nsAutoString arg;
arg.Assign(NS_LITERAL_STRING("^testing!"));
nsAutoString expectedResult;
expectedResult.Assign(NS_LITERAL_STRING("*[local-name()=\'^testing!\']"));
nsAutoString result;
XPathGenerator::EscapeName(arg, result);
printf("Result: %s\nExpected: %s\n", NS_ConvertUTF16toUTF8(result).get(), NS_ConvertUTF16toUTF8(expectedResult).get());
ASSERT_TRUE(expectedResult.Equals(result));
}

Просмотреть файл

@ -8,6 +8,7 @@ UNIFIED_SOURCES += [
'TestNativeXMLHttpRequest.cpp',
'TestParserDialogOptions.cpp',
'TestPlainTextSerializer.cpp',
'TestXPathGenerator.cpp',
]
LOCAL_INCLUDES += [

Просмотреть файл

@ -0,0 +1,65 @@
function run_test()
{
test_generate_xpath();
}
// TEST CODE
function test_generate_xpath()
{
let docString = `
<html>
<body>
<label><input type="checkbox" id="input1" />Input 1</label>
<label><input type="checkbox" id="input2'" />Input 2</label>
<label><input type="checkbox" id='"input3"' />Input 3</label>
<label><input type="checkbox"/>Input 4</label>
<label><input type="checkbox" />Input 5</label>
</body>
</html>
`;
let doc = DOMParser().parseFromString(docString, "text/html");
// Test generate xpath for body.
do_print("Test generate xpath for body node");
let body = doc.getElementsByTagName("body")[0];
let bodyXPath = body.generateXPath();
let bodyExpXPath = "/xhtml:html/xhtml:body";
equal(bodyExpXPath, bodyXPath, " xpath generated for body");
// Test generate xpath for input with id.
do_print("Test generate xpath for input with id");
let inputWithId = doc.getElementById("input1");
let inputWithIdXPath = inputWithId.generateXPath();
let inputWithIdExpXPath = "//xhtml:input[@id='input1']";
equal(inputWithIdExpXPath, inputWithIdXPath, " xpath generated for input with id");
// Test generate xpath for input with id has single quote.
do_print("Test generate xpath for input with id has single quote");
let inputWithIdSingleQuote = doc.getElementsByTagName("input")[1];
let inputWithIdXPathSingleQuote = inputWithIdSingleQuote.generateXPath();
let inputWithIdExpXPathSingleQuote = '//xhtml:input[@id="input2\'"]';
equal(inputWithIdExpXPathSingleQuote, inputWithIdXPathSingleQuote, " xpath generated for input with id");
// Test generate xpath for input with id has double quote.
do_print("Test generate xpath for input with id has double quote");
let inputWithIdDoubleQuote = doc.getElementsByTagName("input")[2];
let inputWithIdXPathDoubleQuote = inputWithIdDoubleQuote.generateXPath();
let inputWithIdExpXPathDoubleQuote = "//xhtml:input[@id='\"input3\"']";
equal(inputWithIdExpXPathDoubleQuote, inputWithIdXPathDoubleQuote, " xpath generated for input with id");
// Test generate xpath for input with id has both single and double quote.
do_print("Test generate xpath for input with id has single and double quote");
let inputWithIdSingleDoubleQuote = doc.getElementsByTagName("input")[3];
inputWithIdSingleDoubleQuote.setAttribute("id", "\"input'4");
let inputWithIdXPathSingleDoubleQuote = inputWithIdSingleDoubleQuote.generateXPath();
let inputWithIdExpXPathSingleDoubleQuote = "//xhtml:input[@id=concat('\"input',\"'\",'4')]";
equal(inputWithIdExpXPathSingleDoubleQuote, inputWithIdXPathSingleDoubleQuote, " xpath generated for input with id");
// Test generate xpath for input without id.
do_print("Test generate xpath for input without id");
let inputNoId = doc.getElementsByTagName("input")[4];
let inputNoIdXPath = inputNoId.generateXPath();
let inputNoIdExpXPath = "/xhtml:html/xhtml:body/xhtml:label[5]/xhtml:input";
equal(inputNoIdExpXPath, inputNoIdXPath, " xpath generated for input without id");
}

Просмотреть файл

@ -52,3 +52,6 @@ head = head_xml.js
[test_xmlserializer.js]
[test_cancelPrefetch.js]
[test_chromeutils_base64.js]
[test_generate_xpath.js]
head = head_xml.js