Skip to content

Commit

Permalink
test: add parser test
Browse files Browse the repository at this point in the history
  • Loading branch information
KeJunMao committed Jan 21, 2024
1 parent 3644cae commit 3714900
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/core/directives/define.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DefineStatement, DefineToken, defineDirective } from "..";
import { defineDirective } from "../directive";
import { DefineStatement, DefineToken } from "../types";
import { createProgramNode, simpleMatchToken } from "../utils";

export const theDefineDirective = defineDirective<DefineToken, DefineStatement>((context) => ({
Expand Down
3 changes: 2 additions & 1 deletion src/core/directives/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MessageStatement, MessageToken, defineDirective } from "..";
import { defineDirective } from "../directive";
import { MessageToken, MessageStatement } from "../types";
import { createProgramNode, simpleMatchToken } from "../utils";

export const MessageDirective = defineDirective<MessageToken, MessageStatement>((context) => ({
Expand Down
17 changes: 17 additions & 0 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, it } from 'vitest'
import { Parser } from '../src'

describe('Parser', () => {
it('should parse code statements', () => {
const tokens = [{ type: 'code', value: 'console.log("Hello, World!")' }]
const ast = Parser.parse(tokens)
expect(ast.body).toHaveLength(1)
expect(ast.body[0].type).toBe('CodeStatement')
expect(ast.body[0].value).toBe('console.log("Hello, World!")')
})

it('should throw an error for unknown token type', () => {
const tokens = [{ type: 'unknown', value: 'unknown token' }]
expect(() => Parser.parse(tokens)).toThrowError('Parser: Unknown token type: unknown')
})
});

0 comments on commit 3714900

Please sign in to comment.