chore(core): ldml: add unit tests 🙀

- for layer invalid cases

For: #7986
This commit is contained in:
Steven R. Loomis 2023-01-19 22:09:03 -06:00
parent 666d71f342
commit 42fdba4bd1
7 changed files with 126 additions and 5 deletions

View file

@ -22,19 +22,14 @@ export class LayrCompiler extends SectionCompiler {
this.callbacks.reportMessage(CompilerMessages.Error_MustBeAtLeastOneLayerElement());
}
let hardwareLayers = 0;
let touchLayers = 0;
this.keyboard.layers.forEach(({ hardware, form }) => {
// TODO-LDML: in the future >1 hardware layer may be allowed, check for duplicates
if (form === 'touch') {
touchLayers++;
if (hardware) {
valid = false;
this.callbacks.reportMessage(CompilerMessages.Error_InvalidFile({
errorText: `Not allowed: hardware="${hardware}" with layers form="touch"`
}));
} else if (touchLayers > 1) { // TODO-LDML: revisit if spec changes
valid = false;
this.callbacks.reportMessage(CompilerMessages.Error_MustHaveAtMostOneLayersElementPerForm({ form }));
}
} else if (form === 'hardware') {
hardwareLayers++;
@ -53,6 +48,7 @@ export class LayrCompiler extends SectionCompiler {
this.callbacks.reportMessage(CompilerMessages.Error_MustHaveAtMostOneLayersElementPerForm({ form }));
}
} else {
// Should not be reached due to XML validation.
valid = false;
this.callbacks.reportMessage(CompilerMessages.Error_InvalidFile({
errorText: `Invalid form="${form}" on layers element`

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE keyboard SYSTEM "../../../../../../../resources/standards-data/ldml-keyboards/techpreview/dtd/ldmlKeyboard.dtd">
<keyboard locale="und" conformsTo="techpreview">
<names>
<name value="layr-invalid-form" />
</names>
<keys>
<key id="grave" to="`" />
<key id="one" to="1" />
</keys>
<layers form="holographic">
<layer id="base">
<!-- beware: this is mapping ` and 1! -->
<row keys="grave one" />
</layer>
</layers>
</keyboard>

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE keyboard SYSTEM "../../../../../../../resources/standards-data/ldml-keyboards/techpreview/dtd/ldmlKeyboard.dtd">
<keyboard locale="und" conformsTo="techpreview">
<names>
<name value="layr-invalid-form" />
</names>
<keys>
<key id="grave" to="`" />
<key id="one" to="1" />
</keys>
<layers form="hardware" hardware="stenography">
<layer id="base">
<!-- beware: this is mapping ` and 1! -->
<row keys="grave one " />
</layer>
</layers>
</keyboard>

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE keyboard SYSTEM "../../../../../../../resources/standards-data/ldml-keyboards/techpreview/dtd/ldmlKeyboard.dtd">
<keyboard locale="und" conformsTo="techpreview">
<names>
<name value="layr-invalid-form" />
</names>
<keys>
<key id="grave" to="`" />
<key id="one" to="1" />
</keys>
<layers form="hardware">
<layer id="base">
<!-- beware: this is mapping ` and 1! -->
<row keys="grave one " />
</layer>
</layers>
</keyboard>

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE keyboard SYSTEM "../../../../../../../resources/standards-data/ldml-keyboards/techpreview/dtd/ldmlKeyboard.dtd">
<keyboard locale="und" conformsTo="techpreview">
<names>
<name value="layr-invalid-form" />
</names>
<keys>
<key id="grave" to="`" />
<key id="one" to="1" />
</keys>
<layers form="hardware" hardware="us">
<layer id="base">
<!-- beware: this is mapping ` and 1! -->
<row keys="grave one" />
</layer>
</layers>
<layers form="hardware" hardware="iso"> <!-- TODO-LDML: not allowed currently -->
<layer id="base">
<!-- beware: this is mapping ` and 1! -->
<row keys="one grave" />
</layer>
</layers>
</keyboard>

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE keyboard SYSTEM "../../../../../../../resources/standards-data/ldml-keyboards/techpreview/dtd/ldmlKeyboard.dtd">
<keyboard locale="und" conformsTo="techpreview">
<names>
<name value="layr-invalid-form" />
</names>
<keys>
<key id="grave" to="`" />
<key id="one" to="1" />
</keys>
<layers form="hardware" hardware="us">
<layer id="base">
<!-- beware: this is mapping ` and 1! -->
<row keys="grave one" />
</layer>
</layers>
<layers form="touch" hardware="iso"> <!-- not allowed: hardware on touch -->
<layer id="base">
<!-- beware: this is mapping ` and 1! -->
<row keys="one grave" />
</layer>
</layers>
</keyboard>

View file

@ -86,4 +86,17 @@ describe('layr', function () {
assert.equal(touch0row0.keys.length, 4);
allKeysOk(touch0row0,'Q q W w', 'touch0row0');
});
for (let badcase of ['invalid-hardware', 'missing-hardware', 'multi-hardware', 'touch-hardware']) {
it(`should reject invalid: ${badcase}`, function () {
let layr = loadSectionFixture(LayrCompiler, `sections/layr/invalid-${badcase}.xml`, compilerTestCallbacks) as Layr;
assert.isNull(layr);
assert.isAtLeast(compilerTestCallbacks.messages.length, 1);
// TODO-LDML: assert specific err cases
});
}
for (let badcase of ['invalid-form']) {
it(`should throw on invalid invalid: ${badcase}`, function () {
assert.throws(() => loadSectionFixture(LayrCompiler, `sections/layr/invalid-${badcase}.xml`, compilerTestCallbacks));
});
}
});