Skip to content

Commit

Permalink
Removed debugging (doTrace) code from t-chars.test.ts".
Browse files Browse the repository at this point in the history
  • Loading branch information
ldthomas committed May 18, 2024
1 parent b52ce69 commit ee48397
Showing 1 changed file with 64 additions and 72 deletions.
136 changes: 64 additions & 72 deletions packages/siwe-parser/lib/t-chars.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import * as fs from "node:fs";
// import { cwd } from "node:process";
// console.log(`Current Working Directory: ${cwd()}`);
import { grammar as Grammar } from "./siwe-grammar";
// The ABNF in the siwe message format and RFC3986
// have a number of rules for primative strings which are
// processed many times during the parsing of a message.
// apg-js processes the terminal nodes the strings are defined by
// much more effeciently than the rule names themselves.
// The terminal nodes are processed most effeciently in the order
// TRG(%d65-90) > TBS(%d65.66.67) > TLS(%s"some string") > rule.
// Therefore, in siwe-abnf.txt these strings have been expanded
// in there terminal nodes. These tests check that the
// expansions have been done correctly.

import { grammar } from "./siwe-grammar";
import apgLib from "apg-js/src/apg-lib/node-exports";
const grammarObj = new Grammar();

const grammarObj = new grammar();
const parser = new apgLib.parser();
const alpha = "abcdefghijklmnopqrstuvwxyz";
const ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const digit = "0123456789";
const unreservedS = "-._~";
const unreserved = alpha + ALPHA + digit + unreservedS;
const unreserved = alpha + ALPHA + digit + "-._~";
const genDelims = ":/?#[]@";
const subDelims = "!$&'()*+,;=";
const reserved = genDelims + subDelims;
Expand All @@ -20,136 +29,119 @@ const userinfo = unreserved + subDelims + ":" + pctEncoded;
const IPvFuture = "v123." + unreserved + subDelims + ":";
const regName = unreserved + subDelims + pctEncoded;
const fragment = pchar + "/?";
const dir = "./output";
const doParse = function doParse(rule, input, doTrace) {
const parser = new apgLib.parser();
if (doTrace) {
parser.trace = new apgLib.trace();
parser.trace.filter.operators["<ALL>"] = true;
}
const result = parser.parse(grammarObj, rule, input);
if (doTrace) {
const html = parser.trace.toHtmlPage("ascii", "siwe, default trace");
const name = `${dir}/siwe-${rule}.html`;
try {
fs.mkdirSync(dir);
} catch (e) {
if (e.code !== "EEXIST") {
throw new Error(`fs.mkdir failed: ${e.message}`);
}
}
fs.writeFileSync(name, html);
console.log(`view "${name}" in any browser to display parser's trace`);
console.dir(result);
}
let result;

const doParse = function doParse(rule, input) {
result = parser.parse(grammarObj, rule, input);
return result;
};

describe("test strings with explicit special character definitions", () => {
test("test pchar", () => {
const rule = "segment-nz";
let result = doParse(rule, pchar, false);
let result = doParse(rule, pchar);
expect(result.success).toBe(true);
result = doParse(rule, pchar + "/", false);
result = doParse(rule, pchar + "/");
expect(result.success).toBe(false);
result = doParse(rule, pchar + "?", false);
result = doParse(rule, pchar + "?");
expect(result.success).toBe(false);
result = doParse(rule, "/", false);
result = doParse(rule, "/");
expect(result.success).toBe(false);
result = doParse(rule, "?", false);
result = doParse(rule, "?");
expect(result.success).toBe(false);
result = doParse(rule, "#", false);
result = doParse(rule, "#");
expect(result.success).toBe(false);
result = doParse(rule, "[", false);
result = doParse(rule, "[");
expect(result.success).toBe(false);
});
test("test statment", () => {
const rule = "statement";
let result = doParse(rule, statement, false);
let result = doParse(rule, statement);
expect(result.success).toBe(true);
result = doParse(rule, "<", false);
result = doParse(rule, "<");
expect(result.success).toBe(false);
result = doParse(rule, ">", false);
result = doParse(rule, ">");
expect(result.success).toBe(false);
result = doParse(rule, "{", false);
result = doParse(rule, "{");
expect(result.success).toBe(false);
result = doParse(rule, "|", false);
result = doParse(rule, "|");
expect(result.success).toBe(false);
result = doParse(rule, "}", false);
result = doParse(rule, "}");
expect(result.success).toBe(false);
});
test("test scheme", () => {
const rule = "scheme";
let result = doParse(rule, scheme, false);
let result = doParse(rule, scheme);
expect(result.success).toBe(true);
result = doParse(rule, ":", false);
result = doParse(rule, ":");
expect(result.success).toBe(false);
result = doParse(rule, "#", false);
result = doParse(rule, "#");
expect(result.success).toBe(false);
result = doParse(rule, "?", false);
result = doParse(rule, "?");
expect(result.success).toBe(false);
result = doParse(rule, "@", false);
result = doParse(rule, "@");
expect(result.success).toBe(false);
result = doParse(rule, "!", false);
result = doParse(rule, "!");
expect(result.success).toBe(false);
});
test("test userinfo", () => {
const rule = "userinfo";
let result = doParse(rule, userinfo, false);
let result = doParse(rule, userinfo);
expect(result.success).toBe(true);
result = doParse(rule, "/", false);
result = doParse(rule, "/");
expect(result.success).toBe(false);
result = doParse(rule, "#", false);
result = doParse(rule, "#");
expect(result.success).toBe(false);
result = doParse(rule, "?", false);
result = doParse(rule, "?");
expect(result.success).toBe(false);
result = doParse(rule, "@", false);
result = doParse(rule, "@");
expect(result.success).toBe(false);
result = doParse(rule, "[", false);
result = doParse(rule, "[");
expect(result.success).toBe(false);
result = doParse(rule, "]", false);
result = doParse(rule, "]");
expect(result.success).toBe(false);
});
test("test IPvFuture", () => {
const rule = "IPvFuture";
let result = doParse(rule, IPvFuture, false);
let result = doParse(rule, IPvFuture);
expect(result.success).toBe(true);
result = doParse(rule, "/", false);
result = doParse(rule, "/");
expect(result.success).toBe(false);
result = doParse(rule, "#", false);
result = doParse(rule, "#");
expect(result.success).toBe(false);
result = doParse(rule, "?", false);
result = doParse(rule, "?");
expect(result.success).toBe(false);
result = doParse(rule, "@", false);
result = doParse(rule, "@");
expect(result.success).toBe(false);
result = doParse(rule, "[", false);
result = doParse(rule, "[");
expect(result.success).toBe(false);
result = doParse(rule, "]", false);
result = doParse(rule, "]");
expect(result.success).toBe(false);
});
test("test reg-name", () => {
const rule = "reg-name";
let result = doParse(rule, regName, false);
let result = doParse(rule, regName);
expect(result.success).toBe(true);
result = doParse(rule, "/", false);
result = doParse(rule, "/");
expect(result.success).toBe(false);
result = doParse(rule, "#", false);
result = doParse(rule, "#");
expect(result.success).toBe(false);
result = doParse(rule, "?", false);
result = doParse(rule, "?");
expect(result.success).toBe(false);
result = doParse(rule, "@", false);
result = doParse(rule, "@");
expect(result.success).toBe(false);
result = doParse(rule, "[", false);
result = doParse(rule, "[");
expect(result.success).toBe(false);
result = doParse(rule, "]", false);
result = doParse(rule, "]");
expect(result.success).toBe(false);
});
test("test fragment", () => {
const rule = "fragment";
let result = doParse(rule, fragment, false);
let result = doParse(rule, fragment);
expect(result.success).toBe(true);
result = doParse(rule, "#", false);
result = doParse(rule, "#");
expect(result.success).toBe(false);
result = doParse(rule, "[", false);
result = doParse(rule, "[");
expect(result.success).toBe(false);
});
});

0 comments on commit ee48397

Please sign in to comment.