UNPKG

2.15 kBtext/x-cView Raw
1#include <assert.h>
2#include <sstream>
3
4#include "node.hpp"
5#include "parser.hpp"
6
7
8#define STATIC_ARRAY_SIZE(array) (sizeof((array))/sizeof((array[0])))
9
10
11namespace Sass {
12
13 Context ctx = Context::Data();
14
15 const char* const ROUNDTRIP_TESTS[] = {
16 NULL,
17 "~",
18 "CMPD",
19 "~ CMPD",
20 "CMPD >",
21 "> > CMPD",
22 "CMPD ~ ~",
23 "> + CMPD1.CMPD2 > ~",
24 "> + CMPD1.CMPD2 CMPD3.CMPD4 > ~",
25 "+ CMPD1 CMPD2 ~ CMPD3 + CMPD4 > CMPD5 > ~"
26 };
27
28
29
30 static Complex_Selector* createComplexSelector(std::string src) {
31 std::string temp(src);
32 temp += ";";
33 return (*Parser::from_c_str(temp.c_str(), ctx, "", Position()).parse_selector_list())[0];
34 }
35
36
37 void roundtripTest(const char* toTest) {
38
39 // Create the initial selector
40
41 Complex_Selector* pOrigSelector = NULL;
42 if (toTest) {
43 pOrigSelector = createComplexSelector(toTest);
44 }
45
46 std::string expected(pOrigSelector ? pOrigSelector->to_string() : "NULL");
47
48
49 // Roundtrip the selector into a node and back
50
51 Node node = complexSelectorToNode(pOrigSelector, ctx);
52
53 std::stringstream nodeStringStream;
54 nodeStringStream << node;
55 std::string nodeString = nodeStringStream.str();
56 cout << "ASNODE: " << node << endl;
57
58 Complex_Selector* pNewSelector = nodeToComplexSelector(node, ctx);
59
60 // Show the result
61
62 std::string result(pNewSelector ? pNewSelector->to_string() : "NULL");
63
64 cout << "SELECTOR: " << expected << endl;
65 cout << "NEW SELECTOR: " << result << endl;
66
67
68 // Test that they are equal using the equality operator
69
70 assert( (!pOrigSelector && !pNewSelector ) || (pOrigSelector && pNewSelector) );
71 if (pOrigSelector) {
72 assert( *pOrigSelector == *pNewSelector );
73 }
74
75
76 // Test that they are equal by comparing the string versions of the selectors
77
78 assert(expected == result);
79
80 }
81
82
83 int main() {
84 for (int index = 0; index < STATIC_ARRAY_SIZE(ROUNDTRIP_TESTS); index++) {
85 const char* const toTest = ROUNDTRIP_TESTS[index];
86 cout << "\nINPUT STRING: " << (toTest ? toTest : "NULL") << endl;
87 roundtripTest(toTest);
88 }
89
90 cout << "\nTesting Done.\n";
91 }
92
93
94}