srctree

Robin Linden parent a69febc2 0c6b0156
js: Reduce boilerplate in tokenizer tests

inlinesplit
js/tokenizer_test.cpp added: 19, removed: 13, total 6
@@ -10,40 +10,46 @@
 
using namespace js::parse;
 
using etest::expect_eq;
namespace {
 
using Tokens = std::vector<Token>;
void expect_tokens(std::string_view input,
std::vector<Token> const &tokens,
etest::source_location const &loc = etest::source_location::current()) {
etest::expect_eq(tokenize(input), tokens, std::nullopt, loc);
}
 
} // namespace
 
int main() {
etest::test("int literal", [] {
expect_eq(tokenize("13"), Tokens{IntLiteral{13}, Eof{}});
expect_eq(tokenize("0"), Tokens{IntLiteral{0}, Eof{}});
expect_tokens("13", {IntLiteral{13}, Eof{}});
expect_tokens("0", {IntLiteral{0}, Eof{}});
});
 
etest::test("identifier", [] {
expect_eq(tokenize("hello"), Tokens{Identifier{"hello"}, Eof{}}); //
expect_tokens("hello", {Identifier{"hello"}, Eof{}}); //
});
 
etest::test("identifiers w/ whitespace", [] {
expect_eq(tokenize(" lol no "), Tokens{Identifier{"lol"}, Identifier{"no"}, Eof{}}); //
expect_tokens(" lol no ", {Identifier{"lol"}, Identifier{"no"}, Eof{}}); //
});
 
etest::test("function call", [] {
expect_eq(tokenize("func();"), Tokens{Identifier{"func"}, LParen{}, RParen{}, Semicolon{}, Eof{}}); //
expect_tokens("func();", {Identifier{"func"}, LParen{}, RParen{}, Semicolon{}, Eof{}}); //
});
 
etest::test("function call w/ whitespace", [] {
expect_eq(tokenize("func ( ) ;"), Tokens{Identifier{"func"}, LParen{}, RParen{}, Semicolon{}, Eof{}}); //
expect_tokens("func ( ) ;", {Identifier{"func"}, LParen{}, RParen{}, Semicolon{}, Eof{}}); //
});
 
etest::test("function call w/ numeric argument", [] {
expect_eq(tokenize("func(9)"), Tokens{Identifier{"func"}, LParen{}, IntLiteral{9}, RParen{}, Eof{}});
expect_eq(tokenize("func( 9 )"), Tokens{Identifier{"func"}, LParen{}, IntLiteral{9}, RParen{}, Eof{}});
expect_tokens("func(9)", {Identifier{"func"}, LParen{}, IntLiteral{9}, RParen{}, Eof{}});
expect_tokens("func( 9 )", {Identifier{"func"}, LParen{}, IntLiteral{9}, RParen{}, Eof{}});
});
 
etest::test("function call w/ multiple arguments", [] {
expect_eq(tokenize("nh(5, 20)"),
Tokens{Identifier{"nh"}, LParen{}, IntLiteral{5}, Comma{}, IntLiteral{20}, RParen{}, Eof{}});
expect_tokens(
"nh(5, 20)", {Identifier{"nh"}, LParen{}, IntLiteral{5}, Comma{}, IntLiteral{20}, RParen{}, Eof{}});
});
 
return etest::run_all_tests();