-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
120 lines (100 loc) · 2.92 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include "api.h"
#include "html.h"
#include "sync.h"
using namespace std;
using namespace barista;
template<typename T>
void Expect(T actual, T expected) {
if (actual != expected) {
cout << "Test failed:\n Expected: " << expected << "\n Was: " << actual << endl;
exit(1);
} else {
cout << "PASSED: " << expected << endl;
}
}
template void Expect<unsigned long>(unsigned long, unsigned long);
template void Expect<unsigned int>(unsigned int, unsigned int);
template void Expect<int>(int, int);
template void Expect<bool>(bool, bool);
template void Expect<string>(string, string);
template<typename E>
void _vectorsDiffer(string message, vector<E> actual, vector<E> expected) {
cout << message << endl;
cout << "Expected: [";
for (auto elem = expected.begin(); elem != expected.end(); elem++) {
cout << *elem;
if (expected.end() - elem > 1) { cout << ", "; }
}
cout << "]" << endl;
cout << "Actual: [";
for (auto elem = actual.begin(); elem != actual.end(); elem++) {
cout << *elem;
if (actual.end() - elem > 1) { cout << ", "; }
}
cout << "]" << endl;
exit(1);
}
template<typename E>
void ExpectVector(vector<E> actual, vector<E> expected) {
if (actual.size() != expected.size()) {
_vectorsDiffer<E>("Vectors have different size", actual, expected);
}
auto aIter = actual.begin();
auto eIter = expected.begin();
while(aIter != actual.end()) {
if (*aIter != *eIter) {
_vectorsDiffer<E>(
"Vectors have different elements at position " + to_string(aIter - actual.begin()),
actual, expected
);
}
aIter++;
eIter++;
}
}
template void ExpectVector<int>(vector<int>, vector<int>);
template void ExpectVector<string>(vector<string>, vector<string>);
void ExpectTreeUpdate(shared_ptr<Tree> tree, TreeUpdate& expected) {
auto diff = tree->RenderFrame(2);
Expect(diff, expected.Render(2));
}
class ChildDiffState : public State {
public:
virtual shared_ptr<Node> Build() {
auto parent = El("parent");
for (auto& childSpec : spec) {
auto child = parent->El(get<0>(childSpec));
auto key = get<1>(childSpec);
if (key != "") {
child->SetKey(key);
}
}
return parent;
}
vector<tuple<string, string>> spec;
};
class ChildDiff : public StatefulWidget {
public:
ChildDiff()
: state(make_shared<ChildDiffState>()) { };
virtual shared_ptr<State> CreateState() {
return state;
}
shared_ptr<ChildDiffState> state;
};
void ExpectChildDiff(
vector<tuple<string, string>> before,
vector<tuple<string, string>> after,
TreeUpdate & expectedUpdate
) {
auto widget = make_shared<ChildDiff>();
// Render before; drop the diff
widget->state->spec = before;
auto tree = make_shared<Tree>(widget);
tree->RenderFrame();
// Render after; compare the diff
widget->state->spec = after;
widget->state->ScheduleUpdate();
ExpectTreeUpdate(tree, expectedUpdate);
}