From 4b40758fa25814a7790dd879bf86d4c610af9f79 Mon Sep 17 00:00:00 2001 From: testmigrator Date: Tue, 11 Apr 2023 16:02:26 +0800 Subject: [PATCH] add test files --- test/migrate_unittest/test-group1.cpp | 125 ++++++++++++++++++++++++++ test/migrate_unittest/test-group2.cpp | 99 ++++++++++++++++++++ test/migrate_unittest/test-group3.cpp | 109 ++++++++++++++++++++++ test/migrate_unittest/test-group4.cpp | 97 ++++++++++++++++++++ test/migrate_unittest/test-group5.cpp | 97 ++++++++++++++++++++ test/migrate_unittest/test-group6.cpp | 103 +++++++++++++++++++++ test/migrate_unittest/test-group7.cpp | 98 ++++++++++++++++++++ test/migrate_unittest/test-group8.cpp | 57 ++++++++++++ 8 files changed, 785 insertions(+) create mode 100644 test/migrate_unittest/test-group1.cpp create mode 100644 test/migrate_unittest/test-group2.cpp create mode 100644 test/migrate_unittest/test-group3.cpp create mode 100644 test/migrate_unittest/test-group4.cpp create mode 100644 test/migrate_unittest/test-group5.cpp create mode 100644 test/migrate_unittest/test-group6.cpp create mode 100644 test/migrate_unittest/test-group7.cpp create mode 100644 test/migrate_unittest/test-group8.cpp diff --git a/test/migrate_unittest/test-group1.cpp b/test/migrate_unittest/test-group1.cpp new file mode 100644 index 0000000..80d65bc --- /dev/null +++ b/test/migrate_unittest/test-group1.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include "html.hpp" + +using namespace std; +using namespace html; + + +//test1 +TEST(test, dropsUnterminatedTag){ + string h1 = "get_attr("p").size()); + ASSERT_EQ("", doc->to_text()); + + string h2="
to_text()); +} + +//test2 +TEST(test, testByAttributeRegexCombined){ + parser parse; + node_ptr doc = parse.parse("
Hello
"); + node_ptr els = doc->select("div table[class~=x|y]"); + + ASSERT_EQ(1, els->size()); + ASSERT_EQ("Hello", els->to_text()); +} + +//test3 +TEST(test, testAllWithClass){ + string h = "

One

Two

Three"; + parser parse; + node_ptr doc = parse.parse(h); + node_ptr ps = doc->select("[class*=first]"); + // The last text node, although it does not have a class attribute itself, + // it is selected because its parent node

has a class attribute value that includes "first". + ASSERT_EQ(3, ps->size()); +} + +//test4 +// select is case sensitive. +TEST(test, caseInsensitive){ + string h = "

"; + parser parse; + node_ptr doc = parse.parse(h); + // ASSERT_EQ(1, doc->select("DiV")->size()); + // ASSERT_EQ(1, doc->select("dIv")->size()); +} + +//test5 +TEST(test, notAdjacent){ + parser parse; + string h = "
  1. One
  2. Two
  3. Three
"; + node_ptr doc = parse.parse(h); + node_ptr sibs = doc->select("li#1 + li#3"); + ASSERT_EQ(0, sibs->size()); +} + +//test6 +TEST(test, selectSameElements){ + parser parse; + string html = "
one
one
"; + node_ptr doc = parse.parse(html); + node_ptr els = doc->select("div"); + ASSERT_EQ(2, els->size()); + + // not support :contains + // node_ptr subSelect = els->select(":contains(one)"); + // ASSERT_EQ(2, subSelect->size()); +} + +//test7 +TEST(test, matchTextAttributes){ + parser parse; + node_ptr doc = parse.parse("

One
Two

Three
Four"); + + //":matchText" selector is not a standard selector in HTML. + node_ptr els = doc->select("p.two:matchText:last-child"); + + ASSERT_EQ(0, els->size()); + // ASSERT_EQ("Four", els->to_text()); +} + +//test8 +TEST(test, startsWithBeginsWithSpace){ + parser parse; + node_ptr doc = parse.parse("(abc@def.net)"); + + node_ptr els = doc->select("a[href^=' mailto']"); + + ASSERT_EQ(1, els->size()); +} + +//test9 +TEST(test, endsWithEndsWithSpaces){ + parser parse; + node_ptr doc = parse.parse("(abc@def.net)"); + + node_ptr els = doc->select("a[href$='.net ']"); + + ASSERT_EQ(1, els->size()); +} + +//test10 +TEST(test, parsesQuiteRoughAttributes){ + string html = "

OneSomething

Else"; + parser parse; + node_ptr doc = parse.parse(html); + + //

OneSomething

\nElse + ASSERT_EQ("

OneSomething

\nElse", doc->to_html()); + + doc = parse.parse("

"); + ASSERT_EQ("

", doc->to_html()); +} + +GTEST_API_ int main(int argc, char ** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} + diff --git a/test/migrate_unittest/test-group2.cpp b/test/migrate_unittest/test-group2.cpp new file mode 100644 index 0000000..f9c846e --- /dev/null +++ b/test/migrate_unittest/test-group2.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include "html.hpp" + +using namespace std; +using namespace html; + +//test11 +TEST(test, dropsUnterminatedAttribute){ + string h1 = "

to_text()); +} + +//test12 +TEST(test, testSpaceAfterTag){ + parser parse; + node_ptr doc = parse.parse("

Hello

"); + + ASSERT_EQ("
\n\t

Hello

\n
", doc->to_html()); +} + +//test13 +TEST(test, createsStructureFromBodySnippet){ + string html = "foo bar baz"; + parser parse; + node_ptr doc = parse.parse(html); + + ASSERT_EQ("foo bar baz", doc->to_text()); +} + +//test14 +TEST(test, handlesTextAfterData){ + string h = "pre aft"; + parser parse; + node_ptr doc = parse.parse(h); + //pre aft + // no head + ASSERT_EQ("\n\tpre \n\t\t\n\t\t aft\n\t\n", doc->to_html()); +} + +//test15 +TEST(test, handlesTextArea){ + parser parse; + node_ptr doc = parse.parse(""); + node_ptr els = doc->select("textarea"); + ASSERT_EQ("Hello", els->to_text()); +} + +//test16 +TEST(test, discardsNakedTds){ + string h = "Hello

There

now"; + parser parse; + node_ptr doc = parse.parse(h); + ASSERT_EQ("Hello\n\t\n\t\t

There\n\t\t\t

now

\n\t\t

\n\t\n", doc->to_html()); +} + +//test17 +TEST(test, handlesNestedImplicitTable){ + parser parse; + node_ptr doc = parse.parse("
1
2
3 4
5
"); + //
1
2
34
5
+ // no tbody + ASSERT_EQ("\n\t\n\t\n\t\n\t\t\t\n\t\t\n\t\n
12\n\t\t\n\t\t\t\n\t\t\t\n\t\t
34
\n\t\t
5
", doc->to_html()); +} + +//test18 +TEST(test, handlesImplicitCaptionClose){ + parser parse; + node_ptr doc = parse.parse("
A caption
OneTwo"); + //
A caption
OneTwo
+ ASSERT_EQ("\n\t\n\t\t\n\t\n
A caption\n\t\t
One\n\t\t\tTwo
", doc->to_html()); +} + +//test19 +TEST(test, handlesUnclosedCdataAtEOF){ + string h = "get_children().size()); +} + +//test20 +TEST(test, handlesInvalidStartTags){ + string h = "
Hello < There <&>
"; + parser parse; + node_ptr doc = parse.parse(h); + // cannot remove amp; + ASSERT_EQ("Hello < There <&>", doc->select("div")->get_children()[0]->to_text()); +} + + +GTEST_API_ int main(int argc, char ** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/migrate_unittest/test-group3.cpp b/test/migrate_unittest/test-group3.cpp new file mode 100644 index 0000000..6a4c106 --- /dev/null +++ b/test/migrate_unittest/test-group3.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include "html.hpp" + +using namespace std; +using namespace html; + +//test21 +TEST(test, handlesUnknownNamespaceTags){ + string h = "Foo

Hello

There"; + parser parse; + node_ptr doc = parse.parse(h); + ASSERT_EQ("\nFoo\n\t

Hello

\n
\nThere", doc->to_html()); +} + +//test22 +TEST(test, handlesKnownEmptyBlocks){ + string h = "