Skip to content

Commit

Permalink
Add scanner tests
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg committed Dec 10, 2017
1 parent cd83bd8 commit f9dee72
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/scanner/doubles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const test = require('ava')
const { scan } = require('../../src/scanner')

test('Scanner: One or two character tokens', async (t) => {
const cases = [
['`!`', ['TICK', 'BANG', 'TICK']],
['`!=`', ['TICK', 'BANG_EQUAL', 'TICK']],
['`=`', ['TICK', 'EQUAL', 'TICK']],
['`==`', ['TICK', 'EQUAL_EQUAL', 'TICK']],
['`<`', ['TICK', 'LESS', 'TICK']],
['`<=`', ['TICK', 'LESS_EQUAL', 'TICK']],
['`>`', ['TICK', 'GREATER', 'TICK']],
['`>=`', ['TICK', 'GREATER_EQUAL', 'TICK']]
]
t.plan(cases.length)

for (let [input, expected] of cases) {
const actual = await scan(input)
t.deepEqual(
actual.map((token) => token.type),
expected,
`Expected "${input}" to give a "${expected[1]}" token, got a "${actual[1].type}" token`
)
}
})
22 changes: 22 additions & 0 deletions test/scanner/monologue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const test = require('ava')
const { scan } = require('../../src/scanner')

test('Scanner: MONOLOGUE', async (t) => {
const cases = [
['', []],
['Hello, world', ['MONOLOGUE']],
['Anything goes in a monologue: 🤘\nこんにちは', ['MONOLOGUE']],
['Anything between ticks are not monologues `a`, anything after is', [
'MONOLOGUE', 'TICK', 'IDENTIFIER', 'TICK', 'MONOLOGUE'
]]
]
t.plan(cases.length)

for (let [input, expected] of cases) {
t.deepEqual(
(await scan(input))
.map((token) => token.type),
expected
)
}
})
15 changes: 15 additions & 0 deletions test/scanner/numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const test = require('ava')
const { scan } = require('../../src/scanner')

test('Scanner: Numbers', async (t) => {
t.plan(1)

t.deepEqual(
await scan('`1234567890`'),
[
{ type: 'TICK' },
{ type: 'NUMBER', value: '1234567890' },
{ type: 'TICK' }
]
)
})
26 changes: 26 additions & 0 deletions test/scanner/singles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const test = require('ava')
const { scan } = require('../../src/scanner')

test('Scanner: Single character tokens', async (t) => {
const cases = [
['`(`', ['TICK', 'LEFT_PAREN', 'TICK']],
['`)`', ['TICK', 'RIGHT_PAREN', 'TICK']],
['`,`', ['TICK', 'COMMA', 'TICK']],
['`.`', ['TICK', 'DOT', 'TICK']],
['`:`', ['TICK', 'COLON', 'TICK']],
['`-`', ['TICK', 'MINUS', 'TICK']],
['`+`', ['TICK', 'PLUS', 'TICK']],
['`*`', ['TICK', 'STAR', 'TICK']],
['`/`', ['TICK', 'SLASH', 'TICK']]
]
t.plan(cases.length)

for (let [input, expected] of cases) {
const actual = await scan(input)
t.deepEqual(
actual.map((token) => token.type),
expected,
`Expected "${input}" to give a "${expected[1]}" token, got a "${actual[1].type}" token`
)
}
})
22 changes: 22 additions & 0 deletions test/scanner/whitespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const test = require('ava')
const { scan } = require('../../src/scanner')

test('Scanner: Whitespace', async (t) => {
const cases = [
['` `', ['TICK', 'TICK']],
['`\r`', ['TICK', 'TICK']],
['`\t`', ['TICK', 'TICK']],
['`\n`', ['TICK', 'TICK']],
[' \r\t\n', ['MONOLOGUE']]
]
t.plan(cases.length)

for (let [input, expected] of cases) {
const actual = await scan(input)
t.deepEqual(
actual.map((token) => token.type),
expected,
`Expected whitespace to be ignored except in monologues`
)
}
})

0 comments on commit f9dee72

Please sign in to comment.