Skip to content

Commit

Permalink
add unit tests for Optional parser, rename String --> StringParser
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac committed Dec 29, 2024
1 parent aef21c7 commit 084419d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
11 changes: 6 additions & 5 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@
}

// Terminal parser that matches a String
function String (opts) {
function StringParser (opts) {
return {
name: opts.name,
parse: (txt, pos) => {
Expand Down Expand Up @@ -605,7 +605,7 @@
parsers.discard = Seq({
name: 'discard',
parsers: [
String({ name: 'marker', str: '#_' }),
StringParser({ name: 'marker', str: '#_' }),
Repeat({ parser: '_gap' }),
Named({ name: '.body', parser: '_form' })
]
Expand All @@ -618,8 +618,8 @@
Choice({
parsers: [
Char({ name: '.open', char: '{' }),
String({ name: '.open', str: '#{' }),
String({ name: '.open', str: '#::{' }),
StringParser({ name: '.open', str: '#{' }),
StringParser({ name: '.open', str: '#::{' }),
Regex({ name: '.open', regex: /^#:{1,2}[a-zA-Z][a-zA-Z0-9.-_]*{/ })
]
}),
Expand Down Expand Up @@ -3972,10 +3972,11 @@
API._Char = Char
API._Choice = Choice
API._NotChar = NotChar
API._Optional = Optional
API._Regex = Regex
API._Repeat = Repeat
API._Seq = Seq
API._String = String
API._StringParser = StringParser

API._flattenTree = flattenTree
API._parseJavaPackageWithClass = parseJavaPackageWithClass
Expand Down
52 changes: 41 additions & 11 deletions test/internals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ test('NotChar parser', () => {
})

test('String parser', () => {
const stringTest1 = scsLib._String({ str: 'foo', name: 'string_test_foo' })
const stringTest1 = scsLib._StringParser({ str: 'foo', name: 'string_test_foo' })
expect(stringTest1.parse('foo', 0).name).toBe('string_test_foo')
expect(stringTest1.parse('foo', 0).text).toBe('foo')
expect(stringTest1.parse('foo', 0).startIdx).toBe(0)
Expand Down Expand Up @@ -478,16 +478,7 @@ test('Regex parser', () => {
})

test('Choice parser', () => {
const choiceTest1 = scsLib._Choice({
parsers: [
scsLib._Char({ char: 'a', name: '.a' }),
scsLib._Char({ char: 'b', name: '.b' }),
scsLib._Char({ char: 'c', name: '.c' })]
})
expect(choiceTest1.parse('a', 0).text).toBe('a')
expect(choiceTest1.parse('b', 0).text).toBe('b')
expect(choiceTest1.parse('c', 0).text).toBe('c')
expect(choiceTest1.parse('z', 0)).toBeNull()

})

test('Seq parser', () => {
Expand Down Expand Up @@ -622,6 +613,45 @@ test('Choice parser', () => {
expect(choiceResult6.text).toBe('a')
})

test('Optional parser', () => {
const optTest1 = scsLib._Optional(scsLib._Char({ char: 'a', name: 'optional_a' }))

// Test successful match
const result1 = optTest1.parse('abc', 0)
expect(result1.startIdx).toBe(0)
expect(result1.endIdx).toBe(1)
expect(result1.name).toBe('optional_a')
expect(result1.text).toBe('a')

// Test no match - should return empty node at same position
const result2 = optTest1.parse('xyz', 0)
expect(result2.startIdx).toBe(0)
expect(result2.endIdx).toBe(0)
expect(result2.name).toBeUndefined()

// Test with offset
const result3 = optTest1.parse('xabc', 1)
expect(result3.startIdx).toBe(1)
expect(result3.endIdx).toBe(2)
expect(result3.name).toBe('optional_a')
expect(result3.text).toBe('a')

const optTest2 = scsLib._Optional(scsLib._StringParser({ str: 'foo', name: 'optional_foo' }))

// Test successful match
const result4 = optTest2.parse('foobar', 0)
expect(result4.startIdx).toBe(0)
expect(result4.endIdx).toBe(3)
expect(result4.name).toBe('optional_foo')
expect(result4.text).toBe('foo')

// Test no match
const result5 = optTest2.parse('bar', 0)
expect(result5.startIdx).toBe(0)
expect(result5.endIdx).toBe(0)
expect(result5.name).toBeUndefined()
})

test('parseJavaPackageWithClass', () => {
const example1 = scsLib._parseJavaPackageWithClass('aaa.bbb.ccc.Ddd')
expect(example1.package).toBe('aaa.bbb.ccc')
Expand Down

0 comments on commit 084419d

Please sign in to comment.