From 42fdba4bd17c6fc2cce4ea6ee4c07e98d5b7e960 Mon Sep 17 00:00:00 2001 From: "Steven R. Loomis" Date: Thu, 19 Jan 2023 22:09:03 -0600 Subject: [PATCH] =?UTF-8?q?chore(core):=20ldml:=20add=20unit=20tests=20?= =?UTF-8?q?=F0=9F=99=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - for layer invalid cases For: #7986 --- .../src/kmc-keyboard/src/compiler/layr.ts | 6 +---- .../sections/layr/invalid-invalid-form.xml | 20 ++++++++++++++ .../layr/invalid-invalid-hardware.xml | 20 ++++++++++++++ .../layr/invalid-missing-hardware.xml | 20 ++++++++++++++ .../sections/layr/invalid-multi-hardware.xml | 26 +++++++++++++++++++ .../sections/layr/invalid-touch-hardware.xml | 26 +++++++++++++++++++ developer/src/kmc-keyboard/test/test-layr.ts | 13 ++++++++++ 7 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-invalid-form.xml create mode 100644 developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-invalid-hardware.xml create mode 100644 developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-missing-hardware.xml create mode 100644 developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-multi-hardware.xml create mode 100644 developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-touch-hardware.xml diff --git a/developer/src/kmc-keyboard/src/compiler/layr.ts b/developer/src/kmc-keyboard/src/compiler/layr.ts index beb0dba6f1..ecacf26e8a 100644 --- a/developer/src/kmc-keyboard/src/compiler/layr.ts +++ b/developer/src/kmc-keyboard/src/compiler/layr.ts @@ -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` diff --git a/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-invalid-form.xml b/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-invalid-form.xml new file mode 100644 index 0000000000..a6a34242ac --- /dev/null +++ b/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-invalid-form.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-invalid-hardware.xml b/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-invalid-hardware.xml new file mode 100644 index 0000000000..da079b7483 --- /dev/null +++ b/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-invalid-hardware.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-missing-hardware.xml b/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-missing-hardware.xml new file mode 100644 index 0000000000..a3847a95ca --- /dev/null +++ b/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-missing-hardware.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-multi-hardware.xml b/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-multi-hardware.xml new file mode 100644 index 0000000000..943c65c196 --- /dev/null +++ b/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-multi-hardware.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-touch-hardware.xml b/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-touch-hardware.xml new file mode 100644 index 0000000000..b39c0c4796 --- /dev/null +++ b/developer/src/kmc-keyboard/test/fixtures/sections/layr/invalid-touch-hardware.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/developer/src/kmc-keyboard/test/test-layr.ts b/developer/src/kmc-keyboard/test/test-layr.ts index 4709f5855b..f33d2fca92 100644 --- a/developer/src/kmc-keyboard/test/test-layr.ts +++ b/developer/src/kmc-keyboard/test/test-layr.ts @@ -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)); + }); + } });