-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
114 lines (92 loc) · 3.14 KB
/
main.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
#include "common.h"
#include "test_runner_p.h"
inline std::ostream& operator<<(std::ostream& output, Position pos) {
return output << "(" << pos.row << ", " << pos.col << ")";
}
inline Position operator"" _pos(const char* str, std::size_t) {
return Position::FromString(str);
}
inline std::ostream& operator<<(std::ostream& output, Size size) {
return output << "(" << size.rows << ", " << size.cols << ")";
}
inline std::ostream& operator<<(std::ostream& output, const CellInterface::Value& value) {
std::visit(
[&](const auto& x) {
output << x;
},
value);
return output;
}
namespace {
void TestEmpty() {
auto sheet = CreateSheet();
ASSERT_EQUAL(sheet->GetPrintableSize(), (Size{0, 0}));
}
void TestInvalidPosition() {
auto sheet = CreateSheet();
try {
sheet->SetCell(Position{-1, 0}, "");
} catch (const InvalidPositionException&) {
}
try {
sheet->GetCell(Position{0, -2});
} catch (const InvalidPositionException&) {
}
try {
sheet->ClearCell(Position{Position::MAX_ROWS, 0});
} catch (const InvalidPositionException&) {
}
}
void TestSetCellPlainText() {
auto sheet = CreateSheet();
auto checkCell = [&](Position pos, std::string text) {
sheet->SetCell(pos, text);
CellInterface* cell = sheet->GetCell(pos);
ASSERT(cell != nullptr);
ASSERT_EQUAL(cell->GetText(), text);
ASSERT_EQUAL(std::get<std::string>(cell->GetValue()), text);
};
checkCell("A1"_pos, "Hello");
checkCell("A1"_pos, "World");
checkCell("B2"_pos, "Purr");
checkCell("A3"_pos, "Meow");
const SheetInterface& constSheet = *sheet;
ASSERT_EQUAL(constSheet.GetCell("B2"_pos)->GetText(), "Purr");
sheet->SetCell("A3"_pos, "'=escaped");
CellInterface* cell = sheet->GetCell("A3"_pos);
ASSERT_EQUAL(cell->GetText(), "'=escaped");
ASSERT_EQUAL(std::get<std::string>(cell->GetValue()), "=escaped");
}
void TestClearCell() {
auto sheet = CreateSheet();
sheet->SetCell("C2"_pos, "Me gusta");
sheet->ClearCell("C2"_pos);
ASSERT(sheet->GetCell("C2"_pos) == nullptr);
sheet->ClearCell("A1"_pos);
sheet->ClearCell("J10"_pos);
}
void TestPrint() {
auto sheet = CreateSheet();
sheet->SetCell("A2"_pos, "meow");
sheet->SetCell("B2"_pos, "=1+2");
sheet->SetCell("A1"_pos, "=1/0");
ASSERT_EQUAL(sheet->GetPrintableSize(), (Size{2, 2}));
std::ostringstream texts;
sheet->PrintTexts(texts);
ASSERT_EQUAL(texts.str(), "=1/0\t\nmeow\t=1+2\n");
std::ostringstream values;
sheet->PrintValues(values);
ASSERT_EQUAL(values.str(), "#DIV/0!\t\nmeow\t3\n");
sheet->ClearCell("B2"_pos);
ASSERT_EQUAL(sheet->GetPrintableSize(), (Size{2, 1}));
}
} // namespace
int main() {
TestRunner tr;
RUN_TEST(tr, TestEmpty);
RUN_TEST(tr, TestInvalidPosition);
RUN_TEST(tr, TestSetCellPlainText);
RUN_TEST(tr, TestClearCell);
RUN_TEST(tr, TestPrint);
return 0;
}