generated from norskeld/serpent
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parsers/newline): add
newline
parser
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 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,14 @@ | ||
import { Parser } from '../state' | ||
|
||
import { choice } from '../combinators/choice' | ||
import { error } from '../combinators/error' | ||
import { string } from './string' | ||
|
||
const EOL_UNIX = '\n' | ||
const EOL_NON_UNIX = '\r\n' | ||
|
||
export function newline(): Parser<string> { | ||
return error(choice(string(EOL_UNIX), string(EOL_NON_UNIX)), 'newline') | ||
} | ||
|
||
export { newline as eol } |
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
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,45 @@ | ||
import { sequence } from '@lib/internal/combinators/sequence' | ||
import { newline } from '@lib/internal/parsers/newline' | ||
import { letters } from '@lib/internal/parsers/letter' | ||
|
||
import { run, result, should } from '@tests/@helpers' | ||
|
||
const tcase = `Hello\nWorld\n` | ||
const tcaseLit = `Hello | ||
World | ||
` | ||
|
||
describe(newline, () => { | ||
it('should succeed if given a newline (Unix)', () => { | ||
const actual = run(newline(), '\n') | ||
const expected = result('success', '\n') | ||
|
||
should.matchState(actual, expected) | ||
}) | ||
|
||
it('should succeed if given a newline sequence (non-Unix)', () => { | ||
const actual = run(newline(), '\r\n') | ||
const expected = result('success', '\r\n') | ||
|
||
should.matchState(actual, expected) | ||
}) | ||
|
||
it('should succeed if given a string with a newline at the end', () => { | ||
const parser = sequence(letters(), newline(), letters(), newline()) | ||
|
||
const actualExplicit = run(parser, tcase) | ||
const actualImplicit = run(parser, tcaseLit) | ||
|
||
const expected = result('success', ['Hello', '\n', 'World', '\n']) | ||
|
||
should.matchState(actualExplicit, expected) | ||
should.matchState(actualImplicit, expected) | ||
}) | ||
|
||
it('should fail if given a string without a newline', () => { | ||
const actual = run(sequence(letters(), newline()), 'Hello') | ||
const expected = result('failure', 'newline') | ||
|
||
should.matchState(actual, expected) | ||
}) | ||
}) |
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