-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
183 lines (147 loc) · 5.21 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <gtest/gtest.h>
#include <sstream>
#include "xos.h"
namespace {
constexpr char kHelloWorldStr[] =
"main( () => () )\n"
" out \"Hello World\"";
TEST(Lexer, HelloWorld) {
std::stringstream ss(kHelloWorldStr);
xos::Lexer lexer(ss);
ASSERT_EQ(lexer.Lex().get(),
xos::Token(xos::Token::identifier, 1, 1, "main"));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::lparen, 1, 5));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::lparen, 1, 7));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::rparen, 1, 8));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::arrow, 1, 10));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::lparen, 1, 13));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::rparen, 1, 14));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::rparen, 1, 16));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::out, 2, 3));
ASSERT_EQ(lexer.Lex().get(),
xos::Token(xos::Token::string, 2, 7, "Hello World"));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::eof, 2, 20));
}
TEST(Lexer, Peek) {
std::stringstream ss(kHelloWorldStr);
xos::Lexer lexer(ss);
xos::Token expected(xos::Token::identifier, 1, 1, "main");
// We can peek as much as we want and see the same token.
ASSERT_EQ(lexer.Peek().get(), expected);
ASSERT_EQ(lexer.Peek().get(), expected);
ASSERT_EQ(lexer.Peek().get(), expected);
ASSERT_EQ(lexer.Lex().get(), expected);
xos::Token lparen(xos::Token::lparen, 1, 5);
ASSERT_EQ(lexer.Peek().get(), lparen);
ASSERT_EQ(lexer.Peek().get(), lparen);
ASSERT_EQ(lexer.Peek().get(), lparen);
}
TEST(Lexer, UnfinishedArrowToken) {
std::stringstream ss("=");
xos::Lexer lexer(ss);
auto result = lexer.Lex();
ASSERT_TRUE(result.hasError());
ASSERT_EQ(result.getError(), "Expected '=>' at row 1, col 1");
}
TEST(Lexer, UnhandledCharacter) {
std::stringstream ss("%");
xos::Lexer lexer(ss);
auto result = lexer.Lex();
ASSERT_TRUE(result.hasError());
ASSERT_EQ(result.getError(), "Unhandled character '%'");
}
TEST(Parser, ParseStr) {
std::stringstream ss("\"Hello World\"");
xos::Lexer lexer(ss);
xos::Parser parser(lexer);
auto result = parser.parseStr();
ASSERT_FALSE(result.hasError());
ASSERT_EQ(result.get().getStr(), "Hello World");
}
TEST(Parser, ParseStrError) {
std::stringstream ss("Hello World");
xos::Lexer lexer(ss);
xos::Parser parser(lexer);
auto result = parser.parseStr();
ASSERT_TRUE(result.hasError());
ASSERT_EQ(result.getError(), "Expected string on row 1, col 1");
}
TEST(Parser, ParseOut) {
std::stringstream ss("out \"Hello World\"");
xos::Lexer lexer(ss);
xos::Parser parser(lexer);
auto result = parser.parseOut();
ASSERT_FALSE(result.hasError());
ASSERT_EQ(result.get().getExpr(), xos::ast::Str("Hello World"));
}
TEST(Parser, ParseFunc) {
std::stringstream ss("main( () => () ) out \"Hello World\"");
xos::Lexer lexer(ss);
xos::Parser parser(lexer);
auto result = parser.parseFunc();
ASSERT_FALSE(result.hasError()) << result.getError();
xos::ast::Prototype proto("main", std::vector<std::string>(), "");
std::unique_ptr<xos::ast::Str> hw(new xos::ast::Str("Hello World"));
std::unique_ptr<xos::ast::Out> out(
std::make_unique<xos::ast::Out>(std::move(hw)));
xos::ast::Func f(std::make_unique<xos::ast::Prototype>(proto),
std::move(out));
ASSERT_EQ(result.get(), f);
}
TEST(Result, ErrorBuilder) {
std::string msg("msg");
xos::Result<int> res = xos::Result<int>::BuildError()
<< "Test error " << 1 << " " << msg;
ASSERT_TRUE(res.hasError());
ASSERT_EQ(res.getError(), "Test error 1 msg");
}
TEST(Result, IndependentStorageSize) {
constexpr size_t kLargeSize = 1024;
using large_t = char[kLargeSize];
auto err = xos::Result<large_t>::Error("err");
static_assert(
sizeof(err) < sizeof(large_t),
"The size of Result<T> should be independent from the size of T.");
}
TEST(Result, ResultDoesntRequireDefaultCtor) {
struct S {
S() = delete;
int x, y;
};
[[maybe_unused]] auto err = xos::Result<S>::Error("err");
}
TEST(Result, OperatorOverloads) {
struct S {
int x;
S(int x) : x(x) {}
bool operator==(const S &other) const { return x == other.x; }
};
xos::Result<S> good_res(4);
ASSERT_TRUE(good_res);
ASSERT_EQ(*good_res, S{4});
ASSERT_EQ(good_res->x, 4);
auto bad_res = xos::Result<S>::Error("err msg");
ASSERT_FALSE(bad_res);
}
TEST(Result, ErrornessIndependentOfValue) {
// A result should be independent of the underlying value of the object. That
// is, if the underlying value can have some "falseness", the Result of that
// object should be treated as not an error unless it was constructed as an
// error.
xos::Result<int> int_res(0);
xos::Result<int *> int_ptr_res(nullptr);
struct S {
operator bool() const { return false; }
};
xos::Result<S> s_res(S{});
ASSERT_FALSE(int_res.hasError());
ASSERT_FALSE(int_ptr_res.hasError());
ASSERT_FALSE(s_res.hasError());
auto int_err = xos::Result<int>::Error("");
auto int_ptr_err = xos::Result<int *>::Error("");
auto s_err = xos::Result<S>::Error("");
ASSERT_TRUE(int_err.hasError());
ASSERT_TRUE(int_ptr_err.hasError());
ASSERT_TRUE(s_err.hasError());
}
} // namespace