-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLDR-16836 kbd: ebnf: move tests to js out of shell, check XML files #4270
Open
srl295
wants to merge
3
commits into
unicode-org:main
Choose a base branch
from
srl295:srl295/kbd/cldr-18197/bnf-jstest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+421
−58
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ | ||
/node_modules |
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,23 @@ | ||
# Keyboard-abnf-tests | ||
|
||
Tests for and against the ABNF files, written in Node.js | ||
|
||
## To use | ||
|
||
- `npm ci` | ||
- `npm t` | ||
|
||
## To update | ||
|
||
Note there are four files. There's a `.d` directory for each ABNF file in keyboards/abnf/. The "pass" files are expected to pass the ABNF and the "fail" to fail it. Lines beginning with # are comments and skipped. | ||
|
||
- transform-from-required.d/from-match.pass.txt | ||
- transform-from-required.d/from-match.fail.txt | ||
- transform-to-required.d/to-replacement.pass.txt | ||
- transform-to-required.d/to-replacement.fail.txt | ||
|
||
## Copyright | ||
|
||
Copyright © 1991-2025 Unicode, Inc. | ||
All rights reserved. | ||
[Terms of use](https://www.unicode.org/copyright.html) |
This file was deleted.
Oops, something went wrong.
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,123 @@ | ||
// Copyright (c) 2025 Unicode, Inc. | ||
// For terms of use, see http://www.unicode.org/copyright.html | ||
// SPDX-License-Identifier: Unicode-3.0 | ||
|
||
import { XMLParser } from "fast-xml-parser"; | ||
import { readFileSync } from "node:fs"; | ||
import { join } from "node:path"; | ||
import * as abnf from "abnf"; | ||
import peggy from "peggy"; | ||
|
||
/** relative path to ABNF */ | ||
export const ABNF_DIR = "../../../keyboards/abnf"; | ||
|
||
/** | ||
* @param {string} abnfPath path to .abnf file | ||
* @returns the raw parser | ||
*/ | ||
export async function getAbnfParser(abnfPath) { | ||
const parsed = await abnf.parseFile(abnfPath); | ||
const opts = { | ||
grammarSource: abnfPath, | ||
trace: false, | ||
}; | ||
const text = parsed.toFormat({ format: "peggy" }); | ||
const parser = peggy.generate(text, opts); | ||
|
||
return parser; | ||
} | ||
|
||
/** | ||
* @param {string} abnfPath path to .abnf file | ||
* @param {Object} parser parser from getAbnfParser | ||
* @returns function taking a string and returning results (or throwing) | ||
*/ | ||
export async function getParseFunction(abnfPath) { | ||
const parser = await getAbnfParser(abnfPath); | ||
const opts = { | ||
grammarSource: abnfPath, | ||
trace: false, | ||
}; | ||
const fn = (str) => parser.parse(str, opts); | ||
return fn; | ||
} | ||
|
||
/** | ||
* Check XML file for valid transform from= and to= | ||
* @param {string} path path to keyboard XML | ||
* @returns true if OK,otherwise throws | ||
*/ | ||
export async function checkXml(path) { | ||
// load the ABNF files. This creates two functions, parseFrom and parseTo | ||
// that can take any text and match against the grammar. | ||
|
||
const parseFrom = await getParseFunction( | ||
join(ABNF_DIR, "transform-from-required.abnf") | ||
); | ||
const parseTo = await getParseFunction( | ||
join(ABNF_DIR, "transform-to-required.abnf") | ||
); | ||
|
||
// read the XML and parse it | ||
const text = readFileSync(path); | ||
const parser = new XMLParser({ | ||
ignoreAttributes: false, | ||
trimValues: false, | ||
htmlEntities: true, | ||
}); | ||
const r = parser.parse(text, false); | ||
|
||
// pull out the transforms | ||
let transforms = r?.keyboard3?.transforms; | ||
|
||
if (!transforms) return true; // no transforms | ||
|
||
// If there's only one element, it will be element: {} instead of element: [{ … }] | ||
// this is how the XML parser works. We convert it to an array for processing. | ||
if (!Array.isArray(transforms)) { | ||
transforms = [transforms]; | ||
} | ||
|
||
for (const transformSet of transforms) { | ||
let transformGroups = transformSet?.transformGroup; | ||
|
||
if (!transformGroups) continue; // no transforms | ||
|
||
// If there's only one element, it will be element: {} instead of element: [{ … }] | ||
// this is how the XML parser works. We convert it to an array for processing. | ||
if (!Array.isArray(transformGroups)) { | ||
// there was only one transformGroup | ||
transformGroups = [transformGroups]; | ||
} | ||
|
||
for (const transformGroup of transformGroups) { | ||
let transforms = transformGroup?.transform; | ||
if (!transforms) continue; | ||
|
||
// If there's only one element, it will be element: {} instead of element: [{ … }] | ||
// this is how the XML parser works. We convert it to an array for processing. | ||
if (!Array.isArray(transforms)) { | ||
transforms = [transforms]; | ||
} | ||
for (const transform of transforms) { | ||
// Check the from= string against the from ABNF | ||
const fromStr = transform["@_from"]; | ||
try { | ||
parseFrom(fromStr); | ||
} catch (e) { | ||
throw Error(`Bad from="${fromStr}"`, { cause: e }); | ||
} | ||
// Check the to= string against the to ABNF | ||
const toStr = transform["@_to"]; | ||
if (toStr) { // it's legal to have a missing to= | ||
try { | ||
parseTo(toStr); | ||
} catch (e) { | ||
throw Error(`Bad to="${toStr}"`, { cause: e }); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} |
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,22 @@ | ||
// Copyright (c) 2025 Unicode, Inc. | ||
// For terms of use, see http://www.unicode.org/copyright.html | ||
// SPDX-License-Identifier: Unicode-3.0 | ||
|
||
import { readFileSync, readdirSync } from "node:fs"; | ||
import { join } from "node:path"; | ||
import { ABNF_DIR } from "./index.mjs"; | ||
|
||
/** | ||
* | ||
* @param {function} callback given abnfFile, abnfPath, abnfText | ||
*/ | ||
export async function forEachAbnf(callback) { | ||
return await Promise.all( | ||
readdirSync(ABNF_DIR).map((abnfFile) => { | ||
if (!/^.*\.abnf$/.test(abnfFile)) return; | ||
const abnfPath = join(ABNF_DIR, abnfFile); | ||
const abnfText = readFileSync(abnfPath); | ||
return callback({ abnfFile, abnfPath, abnfText }); | ||
}) | ||
); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,18 @@ | ||
{ | ||
"name": "@unicode-org/keyboard-abnf-tests", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node --test" | ||
}, | ||
"keywords": [], | ||
"author": "Steven R. Loomis <[email protected]>", | ||
"license": "Unicode-3.0", | ||
"description": "Tests for the keyboard ABNF", | ||
"private": true, | ||
"dependencies": { | ||
"abnf": "^4.3.1", | ||
"fast-xml-parser": "^4.5.1", | ||
"peggy": "^4.2.0" | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where are these files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are in that same directory (keyboard-abnf-tests) and were added in #4261
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that previous PR had a shell script (now deleted) to test the files, this changes it to JS