Skip to content

Commit

Permalink
Up coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Apr 24, 2024
1 parent 8e9558f commit c1dd466
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/abnf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import * as fs from "fs";
import { parse } from "./abnfp.js";

export function parseString(str, grammarSource = "unknown") {
const text = str;
try {
return parse(str + "\r\n", {
return parse(text, {
grammarSource,
});
} catch (er) {
er.grammarSource = grammarSource;
er.grammarText = str;
if (typeof er.format === "function") {
er.message = er.format([{ source: grammarSource, text: str }]);
er.message = er.format([{ source: grammarSource, text }]);
}
throw er;
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@
"peggy": "^4.0.2"
},
"devDependencies": {
"c8": "9.1.0",
"@peggyjs/coverage": "1.0.0",
"@peggyjs/eslint-config": "^3.2.4",
"@typescript-eslint/eslint-plugin": "^7.7.0",
"@typescript-eslint/parser": "^7.7.0",
"ava": "6.1.2",
"c8": "9.1.0",
"eslint": "^8.57.0",
"typescript": "^5.4.5"
},
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions test/ast.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,27 @@ test("bad base class types", t => {
t.throws(() => new ast.Base());
t.throws(() => new ast.Range("", 0, 1));
});

test("nested alternates", t => {
const loc = {
source: "source",
start: {
offset: 0,
line: 1,
column: 1,
},
end: {
offset: 1,
line: 1,
column: 2,
},
};
const alts = [
new ast.Range(16, 0x7f, 0xd7ff, loc),
new ast.Alternation([
new ast.Range(16, 0xe000, 0xffff, loc),
], loc),
];
const a = new ast.Alternation(alts, loc);
t.truthy(a);
});
49 changes: 49 additions & 0 deletions test/parse.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as abnf from "../lib/abnf.js";
import { removeLoc, visit } from "../lib/utils.js";
import { parse } from "../lib/abnfp.js";
import test from "ava";
import { testPeggy } from "@peggyjs/coverage";

test("newlines", t => {
let rules = abnf.parseString("foo = %x20\n");
Expand Down Expand Up @@ -88,6 +89,9 @@ test("failures", t => {
t.throws(() => abnf.parseString("foo = %x1-\x80"));
t.throws(() => abnf.parseString("foo = %x1-10\x80"));
t.throws(() => abnf.parseString("foo"));
t.throws(() => abnf.parseString("foo = %x20 / /"));
t.throws(() => abnf.parseString("foo = %x20 /"));
t.throws(() => abnf.parseString("foo = %x20 / %x20 / "));
});

test("prose", t => {
Expand Down Expand Up @@ -162,3 +166,48 @@ prose = <Some prose>
}
});
});

test("testPeggy", async t => {
const starts = [
{
validInput: "foo = %x20\n",
validResult(rules) {
const res = removeLoc(rules);
t.snapshot(res);
return res;
},
peg$maxFailPos: 11,
},
{
invalidInput: "",
},
{
invalidInput: "foo",
options: {
peg$silentFails: -1,
},
},
{
validInput: " ",
invalidInput: "a",
options: {
peg$startRuleFunction: "peg$parseSP",
},
},
{
validInput: "\t",
invalidInput: "a",
options: {
peg$startRuleFunction: "peg$parseHTAB",
},
},
{
validInput: "!",
invalidInput: " ",
options: {
peg$startRuleFunction: "peg$parseVCHAR",
},
},
];
await testPeggy(new URL("../lib/abnfp.js", import.meta.url), starts);
});
134 changes: 134 additions & 0 deletions test/snapshots/parse.ava.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,137 @@ Generated by [AVA](https://avajs.dev).
simple: true,
type: 'rules',
}

## testPeggy

> Snapshot 1
Rules {
defs: {
FOO: Rule {
def: CaseSensitiveString {
base: 10,
simple: true,
str: ' ',
type: 'caseSensitveString',
},
name: 'foo',
simple: true,
type: 'rule',
},
},
first: 'FOO',
refs: [],
simple: true,
type: 'rules',
}

> Snapshot 2
Rules {
defs: {
FOO: Rule {
def: CaseSensitiveString {
base: 10,
simple: true,
str: ' ',
type: 'caseSensitveString',
},
name: 'foo',
simple: true,
type: 'rule',
},
},
first: 'FOO',
refs: [],
simple: true,
type: 'rules',
}

> Snapshot 3
Rules {
defs: {
FOO: Rule {
def: CaseSensitiveString {
base: 10,
simple: true,
str: ' ',
type: 'caseSensitveString',
},
name: 'foo',
simple: true,
type: 'rule',
},
},
first: 'FOO',
refs: [],
simple: true,
type: 'rules',
}

> Snapshot 4
Rules {
defs: {
FOO: Rule {
def: CaseSensitiveString {
base: 10,
simple: true,
str: ' ',
type: 'caseSensitveString',
},
name: 'foo',
simple: true,
type: 'rule',
},
},
first: 'FOO',
refs: [],
simple: true,
type: 'rules',
}

> Snapshot 5
Rules {
defs: {
FOO: Rule {
def: CaseSensitiveString {
base: 10,
simple: true,
str: ' ',
type: 'caseSensitveString',
},
name: 'foo',
simple: true,
type: 'rule',
},
},
first: 'FOO',
refs: [],
simple: true,
type: 'rules',
}

> Snapshot 6
Rules {
defs: {
FOO: Rule {
def: CaseSensitiveString {
base: 10,
simple: true,
str: ' ',
type: 'caseSensitveString',
},
name: 'foo',
simple: true,
type: 'rule',
},
},
first: 'FOO',
refs: [],
simple: true,
type: 'rules',
}
Binary file modified test/snapshots/parse.ava.js.snap
Binary file not shown.

0 comments on commit c1dd466

Please sign in to comment.