forked from mermaid-js/mermaid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create separate test case for
CommonTokenBuilder
- Loading branch information
1 parent
875c4fe
commit 51896f3
Showing
2 changed files
with
64 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { createLangiumGrammarServices, createServicesForGrammar } from 'langium/grammar'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { CommonTokenBuilder } from '../src/index.js'; | ||
import type { TokenType } from 'chevrotain'; | ||
import { EmptyFileSystem } from 'langium'; | ||
|
||
const grammarServices = createLangiumGrammarServices(EmptyFileSystem).grammar; | ||
|
||
async function createServicesFromGrammar(grammar: string) { | ||
const services = await createServicesForGrammar({ | ||
grammar, | ||
module: { | ||
parser: { | ||
TokenBuilder: () => new CommonTokenBuilder([], grammarServices), | ||
}, | ||
}, | ||
}); | ||
|
||
return { | ||
grammar: services.Grammar, | ||
tokenBuilder: services.parser.TokenBuilder, | ||
}; | ||
} | ||
|
||
describe('CommonTokenBuilder', async () => { | ||
it('should handle grammar with one greedy rule', async () => { | ||
const grammar = ` | ||
grammar TestGrammar | ||
entry Rule: | ||
'rule' value=(LessGreedy | Greedy); | ||
hidden terminal WS: /\\s+/; | ||
/** @greedy */ | ||
terminal Greedy: /[\\w\\d]+/; | ||
terminal LessGreedy: /[\\w]+/; | ||
`; | ||
const services = await createServicesFromGrammar(grammar); | ||
const tokens = services.tokenBuilder.buildTokens(services.grammar) as TokenType[]; | ||
|
||
expect(tokens[2].name).toBe('LessGreedy'); | ||
expect(tokens[3].name).toBe('Greedy'); | ||
}); | ||
|
||
it('should handle grammar with more than one greedy rule', async () => { | ||
const grammar = ` | ||
grammar TestGrammar | ||
entry Rule: | ||
'rule' value=(LessGreedy | Greedy); | ||
hidden terminal WS: /\\s+/; | ||
/** @greedy */ | ||
terminal LessGreedy: /[\\w]+/; | ||
/** @greedy */ | ||
terminal Greedy: /[\\w\\d]+/; | ||
`; | ||
const services = await createServicesFromGrammar(grammar); | ||
const tokens = services.tokenBuilder.buildTokens(services.grammar) as TokenType[]; | ||
|
||
expect(tokens[2].name).toBe('LessGreedy'); | ||
expect(tokens[3].name).toBe('Greedy'); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.