Skip to content

Instantly share code, notes, and snippets.

@eklitzke
Last active March 21, 2020 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eklitzke/2fbceb3aaf88f3e26f4feee66ed8cb88 to your computer and use it in GitHub Desktop.
Save eklitzke/2fbceb3aaf88f3e26f4feee66ed8cb88 to your computer and use it in GitHub Desktop.
#include <gtest/gtest.h>
#include "lisp/lexer.h"
// Assert that the next token has the given type (with no value).
#define TOKTYPE(t) \
ASSERT_EQ(lexer.NextToken(), lisp::Token(lisp::TokenType::t));
// Assert that the next token has the given type and value.
#define TOKVAL(t, val) \
ASSERT_EQ(lexer.NextToken(), lisp::Token(lisp::TokenType::t, val));
TEST(lexer, simple) {
lisp::Lexer lexer("(+ 1 23 -8 3.14) (foo bar) ,@()");
// check the first sexp
TOKTYPE(Open);
TOKVAL(Sym, "+");
TOKVAL(Int, "1");
TOKVAL(Int, "23");
TOKVAL(Int, "-8");
TOKVAL(Float, "3.14");
TOKTYPE(Close);
// check the second sexp
TOKTYPE(Open);
TOKVAL(Sym, "foo");
TOKVAL(Sym, "bar");
TOKTYPE(Close);
// check the final sexp
TOKVAL(Punct, ",");
TOKVAL(Punct, "@");
TOKTYPE(Open);
TOKTYPE(Close);
}
TEST(lexer, punct) {
lisp::Lexer lexer(",@'`~#");
TOKVAL(Punct, ",");
TOKVAL(Punct, "@");
TOKVAL(Punct, "'");
TOKVAL(Punct, "`");
TOKVAL(Punct, "~");
TOKVAL(Punct, "#");
}
TEST(lexer, weirdsyms) {
lisp::Lexer lexer("foo-bar foo~bar foo:bar foo+bar foo?!");
TOKVAL(Sym, "foo-bar");
TOKVAL(Sym, "foo~bar");
TOKVAL(Sym, "foo:bar");
TOKVAL(Sym, "foo+bar");
TOKVAL(Sym, "foo?!");
}
TEST(lexer, nil) {
lisp::Lexer lexer("(nil)");
TOKTYPE(Open);
TOKTYPE(Nil);
TOKTYPE(Close);
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment