2024-03-26 23:05:58 +00:00
|
|
|
import { expect, test, describe } from "bun:test";
|
|
|
|
import { lex } from "./lexer";
|
|
|
|
|
|
|
|
describe("Lexer", () => {
|
|
|
|
test("empty program should return no tokens", () => {
|
|
|
|
const code = "";
|
|
|
|
const tokens = lex(code);
|
|
|
|
expect(tokens).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("program with whitespace should return a single token", () => {
|
|
|
|
const code = " ";
|
|
|
|
const tokens = lex(code);
|
2024-03-26 23:29:52 +00:00
|
|
|
expect(tokens).toEqual([{v: " ", token_type: ""}]);
|
2024-03-26 23:05:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test("program with newlines should return a single token", () => {
|
|
|
|
const code = "\n";
|
|
|
|
const tokens = lex(code);
|
2024-03-26 23:29:52 +00:00
|
|
|
expect(tokens).toEqual([{v: "\n", token_type: ""}]);
|
2024-03-26 23:05:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("program with random unicode should return the same unicode", () => {
|
|
|
|
const code = "🍕";
|
|
|
|
const tokens = lex(code);
|
2024-03-26 23:29:52 +00:00
|
|
|
expect(tokens).toEqual([{v: "🍕", token_type: ""}]);
|
2024-03-26 23:05:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("should scan integers", () => {
|
|
|
|
const code = "12345";
|
|
|
|
const tokens = lex(code);
|
2024-03-26 23:29:52 +00:00
|
|
|
expect(tokens).toEqual([{v: "12345", token_type: "number"}]);
|
2024-03-26 23:05:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("should scan integers and whitespace around", () => {
|
|
|
|
const code = " 12345 \n ";
|
|
|
|
const tokens = lex(code);
|
|
|
|
expect(tokens).toEqual([
|
2024-03-26 23:29:52 +00:00
|
|
|
{v: " ", token_type: ""},
|
|
|
|
{v: "12345", token_type: "number"},
|
|
|
|
{v: " \n ", token_type: ""},
|
2024-03-26 23:05:58 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|