From 2cb2e8c4fb8a272e38cd7744aad2b9d5a39d8dc0 Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Tue, 26 Nov 2024 08:08:15 +0000 Subject: [PATCH 01/14] chore(developer): initial commit of test-string-lists.ts --- common/web/types/.gitignore | 3 ++- .../web/types/test/ldml-keyboard/test-string-list.ts | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 common/web/types/test/ldml-keyboard/test-string-list.ts diff --git a/common/web/types/.gitignore b/common/web/types/.gitignore index 2f943a2f4e9..dcb567b832b 100644 --- a/common/web/types/.gitignore +++ b/common/web/types/.gitignore @@ -1,2 +1,3 @@ src/schemas/ -obj/ \ No newline at end of file +obj/ +coverage/ diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts new file mode 100644 index 00000000000..ea29249d7ef --- /dev/null +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -0,0 +1,11 @@ +import 'mocha'; +import { assert } from 'chai'; +//import { ListIndex, ListItem } from '../../src/ldml-keyboard/string-list.js'; + +describe('Test of String-List', () => { + describe('should test ultimate truth', () => { + it('the truth', () => { + assert.isTrue(true); + }); + }); +}); \ No newline at end of file From fa4f1e5d0fc7acb8c41c4787c68a4df13f8b1bd2 Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Tue, 26 Nov 2024 08:50:36 +0000 Subject: [PATCH 02/14] chore(developer): add one ListItem test case --- .../web/types/test/ldml-keyboard/test-string-list.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index ea29249d7ef..0f73bbf93fd 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -1,11 +1,14 @@ import 'mocha'; import { assert } from 'chai'; //import { ListIndex, ListItem } from '../../src/ldml-keyboard/string-list.js'; +import { ListItem } from '../../src/ldml-keyboard/string-list.js'; describe('Test of String-List', () => { - describe('should test ultimate truth', () => { - it('the truth', () => { - assert.isTrue(true); + describe('should test ListItem', () => { + it('fromStrings returns an empty ListItem if source is null', () => { + const actual = ListItem.fromStrings(null, null, null); + const expected = new ListItem(); + assert.deepEqual(actual, expected); }); }); -}); \ No newline at end of file +}); From a5c80243e742746dbc29995464959ef3982b926d Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Wed, 27 Nov 2024 02:24:11 +0000 Subject: [PATCH 03/14] chore(developer): add six test cases for ListIndex --- .../test/ldml-keyboard/test-string-list.ts | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index 0f73bbf93fd..9188f3b440a 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -1,9 +1,48 @@ import 'mocha'; import { assert } from 'chai'; -//import { ListIndex, ListItem } from '../../src/ldml-keyboard/string-list.js'; -import { ListItem } from '../../src/ldml-keyboard/string-list.js'; +import { StrsItem } from '../../src/kmx/kmx-plus/kmx-plus.js'; +import { ListIndex, ListItem } from '../../src/ldml-keyboard/string-list.js'; describe('Test of String-List', () => { + describe('should test ListIndex', () => { + it('can construct a ListIndex', () => { + const strsItem = new StrsItem("abc"); + const actual = new ListIndex(strsItem); + assert.deepEqual(actual.value, strsItem); + }); + it('can check two ListIndex for equality', () => { + const strsItemOne = new StrsItem("abc"); + const strsItemTwo = new StrsItem("abc"); + const listItemOne = new ListIndex(strsItemOne); + const listItemTwo = new ListIndex(strsItemTwo); + assert.isTrue(listItemOne.isEqual(listItemTwo)); + }); + it('can check two different ListIndex are not equal', () => { + const strsItemOne = new StrsItem("abc"); + const strsItemTwo = new StrsItem("def"); + const listItemOne = new ListIndex(strsItemOne); + const listItemTwo = new ListIndex(strsItemTwo); + assert.isFalse(listItemOne.isEqual(listItemTwo)); + }); + it('can check a ListIndex and string for equality', () => { + const strsItem = new StrsItem("abc"); + const listItem = new ListIndex(strsItem); + const aString = "abc"; + assert.isTrue(listItem.isEqual(aString)); + }); + it('can check a ListIndex and string for inequality', () => { + const strsItem = new StrsItem("abc"); + const listItem = new ListIndex(strsItem); + const aString = "def"; + assert.isFalse(listItem.isEqual(aString)); + }); + it('can provide a correct string representation', () => { + const strsItem = new StrsItem("abc"); + const listItem = new ListIndex(strsItem); + const expected = "abc"; + assert.deepEqual(listItem.toString(), expected); + }); + }); describe('should test ListItem', () => { it('fromStrings returns an empty ListItem if source is null', () => { const actual = ListItem.fromStrings(null, null, null); From ef46a8eab19f19591b68e49b26374ce943faea11 Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Wed, 27 Nov 2024 03:28:36 +0000 Subject: [PATCH 04/14] chore(developer): add a ListItem.fromStrings() test case --- .../test/ldml-keyboard/test-string-list.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index 9188f3b440a..66e724e148f 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -1,6 +1,6 @@ import 'mocha'; import { assert } from 'chai'; -import { StrsItem } from '../../src/kmx/kmx-plus/kmx-plus.js'; +import { StrsItem, StrsOptions, DependencySections, Strs } from '../../src/kmx/kmx-plus/kmx-plus.js'; import { ListIndex, ListItem } from '../../src/ldml-keyboard/string-list.js'; describe('Test of String-List', () => { @@ -44,10 +44,23 @@ describe('Test of String-List', () => { }); }); describe('should test ListItem', () => { - it('fromStrings returns an empty ListItem if source is null', () => { + it('fromStrings should return an empty ListItem if source is null', () => { const actual = ListItem.fromStrings(null, null, null); const expected = new ListItem(); assert.deepEqual(actual, expected); }); + it('fromStrings should return a valid ListItem from a single source string', () => { + const source = ["abc"]; + const sections = { strs: new Strs }; + sections.strs.allocString = stubSectionsStrsAllocString; + const actual = ListItem.fromStrings(source, null, sections); + const expected = new ListItem(); + expected.push(new ListIndex(new StrsItem("abc"))); + assert.deepEqual(actual, expected); + }); }); }); + +function stubSectionsStrsAllocString(s?: string, opts?: StrsOptions, sections?: DependencySections): StrsItem { + return new StrsItem(s); +} From 15c71d7b951fe62ee21a4e31e32e79d6bd6807c9 Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Wed, 27 Nov 2024 03:38:24 +0000 Subject: [PATCH 05/14] chore(developer): add a further ListItem.fromStrings() test case --- .../web/types/test/ldml-keyboard/test-string-list.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index 66e724e148f..cfb09322e3b 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -58,6 +58,16 @@ describe('Test of String-List', () => { expected.push(new ListIndex(new StrsItem("abc"))); assert.deepEqual(actual, expected); }); + it('fromStrings should return a valid ListItem from a longer source', () => { + const source = ["abc", "def", "ghi"]; + const sections = { strs: new Strs }; + sections.strs.allocString = stubSectionsStrsAllocString; + const actual = ListItem.fromStrings(source, null, sections); + const expected = new ListItem(); + for (const s of source) + expected.push(new ListIndex(new StrsItem(s))); + assert.deepEqual(actual, expected); + }); }); }); From 88b0f712d09adb86e60d85229aa3480f3b3c7d38 Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Wed, 27 Nov 2024 03:47:28 +0000 Subject: [PATCH 06/14] chore(developer): add three ListItem.getItemOrder() test cases --- .../test/ldml-keyboard/test-string-list.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index cfb09322e3b..f593bae9fe0 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -68,6 +68,27 @@ describe('Test of String-List', () => { expected.push(new ListIndex(new StrsItem(s))); assert.deepEqual(actual, expected); }); + it('getItemOrder should return a valid index for the first item', () => { + const listItem = new ListItem(); + for (const s of ["abc", "def", "ghi"]) + listItem.push(new ListIndex(new StrsItem(s))); + const index = listItem.getItemOrder("abc"); + assert.equal(index, 0); + }); + it('getItemOrder should return a valid index for a later item', () => { + const listItem = new ListItem(); + for (const s of ["abc", "def", "ghi"]) + listItem.push(new ListIndex(new StrsItem(s))); + const index = listItem.getItemOrder("ghi"); + assert.equal(index, 2); + }); + it('getItemOrder should return -1 for a missing item', () => { + const listItem = new ListItem(); + for (const s of ["abc", "def", "ghi"]) + listItem.push(new ListIndex(new StrsItem(s))); + const index = listItem.getItemOrder("jkl"); + assert.equal(index, -1); + }); }); }); From 769259e6f6a78bc7763ce7d233b42ab2181988c2 Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Wed, 27 Nov 2024 05:04:20 +0000 Subject: [PATCH 07/14] chore(developer): add four test cases for ListItem.isEqual, plus refactor for length --- .../test/ldml-keyboard/test-string-list.ts | 66 +++++++++++-------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index f593bae9fe0..c638f0043eb 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -11,28 +11,22 @@ describe('Test of String-List', () => { assert.deepEqual(actual.value, strsItem); }); it('can check two ListIndex for equality', () => { - const strsItemOne = new StrsItem("abc"); - const strsItemTwo = new StrsItem("abc"); - const listItemOne = new ListIndex(strsItemOne); - const listItemTwo = new ListIndex(strsItemTwo); + const listItemOne = new ListIndex(new StrsItem("abc")); + const listItemTwo = new ListIndex(new StrsItem("abc")); assert.isTrue(listItemOne.isEqual(listItemTwo)); }); it('can check two different ListIndex are not equal', () => { - const strsItemOne = new StrsItem("abc"); - const strsItemTwo = new StrsItem("def"); - const listItemOne = new ListIndex(strsItemOne); - const listItemTwo = new ListIndex(strsItemTwo); + const listItemOne = new ListIndex(new StrsItem("abc")); + const listItemTwo = new ListIndex(new StrsItem("def")); assert.isFalse(listItemOne.isEqual(listItemTwo)); }); it('can check a ListIndex and string for equality', () => { - const strsItem = new StrsItem("abc"); - const listItem = new ListIndex(strsItem); + const listItem = new ListIndex(new StrsItem("abc")); const aString = "abc"; assert.isTrue(listItem.isEqual(aString)); }); it('can check a ListIndex and string for inequality', () => { - const strsItem = new StrsItem("abc"); - const listItem = new ListIndex(strsItem); + const listItem = new ListIndex(new StrsItem("abc")); const aString = "def"; assert.isFalse(listItem.isEqual(aString)); }); @@ -54,8 +48,7 @@ describe('Test of String-List', () => { const sections = { strs: new Strs }; sections.strs.allocString = stubSectionsStrsAllocString; const actual = ListItem.fromStrings(source, null, sections); - const expected = new ListItem(); - expected.push(new ListIndex(new StrsItem("abc"))); + const expected = initListItem(source); assert.deepEqual(actual, expected); }); it('fromStrings should return a valid ListItem from a longer source', () => { @@ -63,35 +56,54 @@ describe('Test of String-List', () => { const sections = { strs: new Strs }; sections.strs.allocString = stubSectionsStrsAllocString; const actual = ListItem.fromStrings(source, null, sections); - const expected = new ListItem(); - for (const s of source) - expected.push(new ListIndex(new StrsItem(s))); + const expected = initListItem(source); assert.deepEqual(actual, expected); }); it('getItemOrder should return a valid index for the first item', () => { - const listItem = new ListItem(); - for (const s of ["abc", "def", "ghi"]) - listItem.push(new ListIndex(new StrsItem(s))); + const listItem = initListItem(["abc", "def", "ghi"]); const index = listItem.getItemOrder("abc"); assert.equal(index, 0); }); it('getItemOrder should return a valid index for a later item', () => { - const listItem = new ListItem(); - for (const s of ["abc", "def", "ghi"]) - listItem.push(new ListIndex(new StrsItem(s))); + const listItem = initListItem(["abc", "def", "ghi"]); const index = listItem.getItemOrder("ghi"); assert.equal(index, 2); }); it('getItemOrder should return -1 for a missing item', () => { - const listItem = new ListItem(); - for (const s of ["abc", "def", "ghi"]) - listItem.push(new ListIndex(new StrsItem(s))); - const index = listItem.getItemOrder("jkl"); + const listItem = initListItem(["abc", "def", "ghi"]); + const index = listItem.getItemOrder("jkl"); assert.equal(index, -1); }); + it('isEqual should return true for two empty ListItems', () => { + const listItemOne = new ListItem(); + const listItemTwo = new ListItem(); + assert.isTrue(listItemOne.isEqual(listItemTwo)); + }); + it('isEqual should return false for empty and non-empty ListItems', () => { + const listItemOne = new ListItem(); + const listItemTwo = initListItem(["abc"]); + assert.isFalse(listItemOne.isEqual(listItemTwo)); + }); + it('isEqual should return true for identical ListItems', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.isTrue(listItemOne.isEqual(listItemTwo)); + }); + it('isEqual should return false for different length ListItems', () => { + const listItemOne = initListItem(["abc", "def"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.isFalse(listItemOne.isEqual(listItemTwo)); + }); }); }); function stubSectionsStrsAllocString(s?: string, opts?: StrsOptions, sections?: DependencySections): StrsItem { return new StrsItem(s); } + +function initListItem(source: Array): ListItem { + const listItem = new ListItem(); + for (const s of source) + listItem.push(new ListIndex(new StrsItem(s))); + return listItem; +} From d77c5aeb6bc6c94f22d94f64536ef8846b879553 Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Wed, 27 Nov 2024 05:16:24 +0000 Subject: [PATCH 08/14] chore(developer): add seven additional test cases for ListItem.isEqual --- .../test/ldml-keyboard/test-string-list.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index c638f0043eb..ce207129216 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -84,6 +84,11 @@ describe('Test of String-List', () => { const listItemTwo = initListItem(["abc"]); assert.isFalse(listItemOne.isEqual(listItemTwo)); }); + it('isEqual should return false for non-empty and empty ListItems', () => { + const listItemOne = initListItem(["abc"]); + const listItemTwo = new ListItem(); + assert.isFalse(listItemOne.isEqual(listItemTwo)); + }); it('isEqual should return true for identical ListItems', () => { const listItemOne = initListItem(["abc", "def", "ghi"]); const listItemTwo = initListItem(["abc", "def", "ghi"]); @@ -95,6 +100,26 @@ describe('Test of String-List', () => { assert.isFalse(listItemOne.isEqual(listItemTwo)); }); }); + it('isEqual should return true for empty ListItem and string[]', () => { + const listItem = new ListItem(); + assert.isTrue(listItem.isEqual([])); + }); + it('isEqual should return false for empty ListItem and non-empty string[]', () => { + const listItem = new ListItem(); + assert.isFalse(listItem.isEqual(["abc"])); + }); + it('isEqual should return false for non-empty ListItem and empty string[]', () => { + const listItem = initListItem(["abc"]);; + assert.isFalse(listItem.isEqual([])); + }); + it('isEqual should return true for identical ListItem and string[]', () => { + const listItem = initListItem(["abc", "def", "ghi"]); + assert.isTrue(listItem.isEqual(["abc", "def", "ghi"])); + }); + it('isEqual should return false for different length ListItem and string[]', () => { + const listItem = initListItem(["abc", "def"]); + assert.isFalse(listItem.isEqual(["abc", "def", "ghi"])); + }); }); function stubSectionsStrsAllocString(s?: string, opts?: StrsOptions, sections?: DependencySections): StrsItem { From 04a8a0a5d5486ba3803fcb7b266f5fcc7aa9428b Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Wed, 27 Nov 2024 05:33:07 +0000 Subject: [PATCH 09/14] chore(developer): add seven test cases for ListItem.compareTo --- .../test/ldml-keyboard/test-string-list.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index ce207129216..3a74678a32a 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -120,6 +120,41 @@ describe('Test of String-List', () => { const listItem = initListItem(["abc", "def"]); assert.isFalse(listItem.isEqual(["abc", "def", "ghi"])); }); + it('compareTo should return 0 for identical ListItems', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.equal(listItemOne.compareTo(listItemTwo), 0); + }); + it('compareTo should return -1 for ListItems with different first items (smallest first)', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abd", "def", "ghi"]); + assert.equal(listItemOne.compareTo(listItemTwo), -1); + }); + it('compareTo should return 1 for ListItems with different first items (smallest second)', () => { + const listItemOne = initListItem(["abd", "def", "ghi"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.equal(listItemOne.compareTo(listItemTwo), 1); + }); + it('compareTo should return -1 for ListItems with different later items (smallest first)', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abc", "def", "ghj"]); + assert.equal(listItemOne.compareTo(listItemTwo), -1); + }); + it('compareTo should return 1 for ListItems with different later items (smallest second)', () => { + const listItemOne = initListItem(["abc", "def", "ghj"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.equal(listItemOne.compareTo(listItemTwo), 1); + }); + it('compareTo should return -1 for identical ListItems, except shorter first', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abc", "def", "ghi", "jkl"]); + assert.equal(listItemOne.compareTo(listItemTwo), -1); + }); + it('compareTo should return 1 for identical ListItems, except longer first', () => { + const listItemOne = initListItem(["abc", "def", "ghi", "jkl"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.equal(listItemOne.compareTo(listItemTwo), 1); + }); }); function stubSectionsStrsAllocString(s?: string, opts?: StrsOptions, sections?: DependencySections): StrsItem { From b3398fb9418692c3b4d092679825d0fb807e1d59 Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Wed, 27 Nov 2024 05:39:56 +0000 Subject: [PATCH 10/14] chore(developer): add test cases for ListItem.toString() and toStringArray() --- common/web/types/test/ldml-keyboard/test-string-list.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index 3a74678a32a..ba200413298 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -155,6 +155,15 @@ describe('Test of String-List', () => { const listItemTwo = initListItem(["abc", "def", "ghi"]); assert.equal(listItemOne.compareTo(listItemTwo), 1); }); + it('toString should return correct string', () => { + const listItem = initListItem(["abc", "def", "ghi"]); + assert.deepEqual(listItem.toString(), "abc def ghi"); + }); + it('toStringArray should return correct string[]', () => { + const source = ["abc", "def", "ghi"]; + const listItem = initListItem(source); + assert.deepEqual(listItem.toStringArray(), source); + }); }); function stubSectionsStrsAllocString(s?: string, opts?: StrsOptions, sections?: DependencySections): StrsItem { From 19da0c73aee1180fff04f7331b18fa8a5ec3173b Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Wed, 27 Nov 2024 05:48:51 +0000 Subject: [PATCH 11/14] chore(developer): add four additional test cases --- .../test/ldml-keyboard/test-string-list.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index ba200413298..db5853d99d9 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -94,6 +94,11 @@ describe('Test of String-List', () => { const listItemTwo = initListItem(["abc", "def", "ghi"]); assert.isTrue(listItemOne.isEqual(listItemTwo)); }); + it('isEqual should return false for different ListItems', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abd", "def", "ghi"]); + assert.isFalse(listItemOne.isEqual(listItemTwo)); + }); it('isEqual should return false for different length ListItems', () => { const listItemOne = initListItem(["abc", "def"]); const listItemTwo = initListItem(["abc", "def", "ghi"]); @@ -116,6 +121,10 @@ describe('Test of String-List', () => { const listItem = initListItem(["abc", "def", "ghi"]); assert.isTrue(listItem.isEqual(["abc", "def", "ghi"])); }); + it('isEqual should return false for different ListItem and string[]', () => { + const listItem = initListItem(["abc", "def", "ghi"]); + assert.isFalse(listItem.isEqual(["abd", "def", "ghi"])); + }); it('isEqual should return false for different length ListItem and string[]', () => { const listItem = initListItem(["abc", "def"]); assert.isFalse(listItem.isEqual(["abc", "def", "ghi"])); @@ -159,11 +168,19 @@ describe('Test of String-List', () => { const listItem = initListItem(["abc", "def", "ghi"]); assert.deepEqual(listItem.toString(), "abc def ghi"); }); + it('toString should return correct string for empty ListItem', () => { + const listItem = new ListItem; + assert.deepEqual(listItem.toString(), ""); + }); it('toStringArray should return correct string[]', () => { const source = ["abc", "def", "ghi"]; const listItem = initListItem(source); assert.deepEqual(listItem.toStringArray(), source); }); + it('toStringArray should return correct string[] for empty ListItem', () => { + const listItem = new ListItem; + assert.deepEqual(listItem.toStringArray(), []); + }); }); function stubSectionsStrsAllocString(s?: string, opts?: StrsOptions, sections?: DependencySections): StrsItem { From 402b4065e0e39f69f0529bbdbda0b690a09fedf5 Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Wed, 27 Nov 2024 06:10:45 +0000 Subject: [PATCH 12/14] chore(common/web): refactor tests for readability --- .../test/ldml-keyboard/test-string-list.ts | 300 +++++++++--------- 1 file changed, 156 insertions(+), 144 deletions(-) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index db5853d99d9..3275c9a5162 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -4,7 +4,7 @@ import { StrsItem, StrsOptions, DependencySections, Strs } from '../../src/kmx/k import { ListIndex, ListItem } from '../../src/ldml-keyboard/string-list.js'; describe('Test of String-List', () => { - describe('should test ListIndex', () => { + describe('Test ListIndex', () => { it('can construct a ListIndex', () => { const strsItem = new StrsItem("abc"); const actual = new ListIndex(strsItem); @@ -37,149 +37,161 @@ describe('Test of String-List', () => { assert.deepEqual(listItem.toString(), expected); }); }); - describe('should test ListItem', () => { - it('fromStrings should return an empty ListItem if source is null', () => { - const actual = ListItem.fromStrings(null, null, null); - const expected = new ListItem(); - assert.deepEqual(actual, expected); - }); - it('fromStrings should return a valid ListItem from a single source string', () => { - const source = ["abc"]; - const sections = { strs: new Strs }; - sections.strs.allocString = stubSectionsStrsAllocString; - const actual = ListItem.fromStrings(source, null, sections); - const expected = initListItem(source); - assert.deepEqual(actual, expected); - }); - it('fromStrings should return a valid ListItem from a longer source', () => { - const source = ["abc", "def", "ghi"]; - const sections = { strs: new Strs }; - sections.strs.allocString = stubSectionsStrsAllocString; - const actual = ListItem.fromStrings(source, null, sections); - const expected = initListItem(source); - assert.deepEqual(actual, expected); - }); - it('getItemOrder should return a valid index for the first item', () => { - const listItem = initListItem(["abc", "def", "ghi"]); - const index = listItem.getItemOrder("abc"); - assert.equal(index, 0); - }); - it('getItemOrder should return a valid index for a later item', () => { - const listItem = initListItem(["abc", "def", "ghi"]); - const index = listItem.getItemOrder("ghi"); - assert.equal(index, 2); - }); - it('getItemOrder should return -1 for a missing item', () => { - const listItem = initListItem(["abc", "def", "ghi"]); - const index = listItem.getItemOrder("jkl"); - assert.equal(index, -1); - }); - it('isEqual should return true for two empty ListItems', () => { - const listItemOne = new ListItem(); - const listItemTwo = new ListItem(); - assert.isTrue(listItemOne.isEqual(listItemTwo)); - }); - it('isEqual should return false for empty and non-empty ListItems', () => { - const listItemOne = new ListItem(); - const listItemTwo = initListItem(["abc"]); - assert.isFalse(listItemOne.isEqual(listItemTwo)); - }); - it('isEqual should return false for non-empty and empty ListItems', () => { - const listItemOne = initListItem(["abc"]); - const listItemTwo = new ListItem(); - assert.isFalse(listItemOne.isEqual(listItemTwo)); - }); - it('isEqual should return true for identical ListItems', () => { - const listItemOne = initListItem(["abc", "def", "ghi"]); - const listItemTwo = initListItem(["abc", "def", "ghi"]); - assert.isTrue(listItemOne.isEqual(listItemTwo)); - }); - it('isEqual should return false for different ListItems', () => { - const listItemOne = initListItem(["abc", "def", "ghi"]); - const listItemTwo = initListItem(["abd", "def", "ghi"]); - assert.isFalse(listItemOne.isEqual(listItemTwo)); - }); - it('isEqual should return false for different length ListItems', () => { - const listItemOne = initListItem(["abc", "def"]); - const listItemTwo = initListItem(["abc", "def", "ghi"]); - assert.isFalse(listItemOne.isEqual(listItemTwo)); - }); - }); - it('isEqual should return true for empty ListItem and string[]', () => { - const listItem = new ListItem(); - assert.isTrue(listItem.isEqual([])); - }); - it('isEqual should return false for empty ListItem and non-empty string[]', () => { - const listItem = new ListItem(); - assert.isFalse(listItem.isEqual(["abc"])); - }); - it('isEqual should return false for non-empty ListItem and empty string[]', () => { - const listItem = initListItem(["abc"]);; - assert.isFalse(listItem.isEqual([])); - }); - it('isEqual should return true for identical ListItem and string[]', () => { - const listItem = initListItem(["abc", "def", "ghi"]); - assert.isTrue(listItem.isEqual(["abc", "def", "ghi"])); - }); - it('isEqual should return false for different ListItem and string[]', () => { - const listItem = initListItem(["abc", "def", "ghi"]); - assert.isFalse(listItem.isEqual(["abd", "def", "ghi"])); - }); - it('isEqual should return false for different length ListItem and string[]', () => { - const listItem = initListItem(["abc", "def"]); - assert.isFalse(listItem.isEqual(["abc", "def", "ghi"])); - }); - it('compareTo should return 0 for identical ListItems', () => { - const listItemOne = initListItem(["abc", "def", "ghi"]); - const listItemTwo = initListItem(["abc", "def", "ghi"]); - assert.equal(listItemOne.compareTo(listItemTwo), 0); - }); - it('compareTo should return -1 for ListItems with different first items (smallest first)', () => { - const listItemOne = initListItem(["abc", "def", "ghi"]); - const listItemTwo = initListItem(["abd", "def", "ghi"]); - assert.equal(listItemOne.compareTo(listItemTwo), -1); - }); - it('compareTo should return 1 for ListItems with different first items (smallest second)', () => { - const listItemOne = initListItem(["abd", "def", "ghi"]); - const listItemTwo = initListItem(["abc", "def", "ghi"]); - assert.equal(listItemOne.compareTo(listItemTwo), 1); - }); - it('compareTo should return -1 for ListItems with different later items (smallest first)', () => { - const listItemOne = initListItem(["abc", "def", "ghi"]); - const listItemTwo = initListItem(["abc", "def", "ghj"]); - assert.equal(listItemOne.compareTo(listItemTwo), -1); - }); - it('compareTo should return 1 for ListItems with different later items (smallest second)', () => { - const listItemOne = initListItem(["abc", "def", "ghj"]); - const listItemTwo = initListItem(["abc", "def", "ghi"]); - assert.equal(listItemOne.compareTo(listItemTwo), 1); - }); - it('compareTo should return -1 for identical ListItems, except shorter first', () => { - const listItemOne = initListItem(["abc", "def", "ghi"]); - const listItemTwo = initListItem(["abc", "def", "ghi", "jkl"]); - assert.equal(listItemOne.compareTo(listItemTwo), -1); - }); - it('compareTo should return 1 for identical ListItems, except longer first', () => { - const listItemOne = initListItem(["abc", "def", "ghi", "jkl"]); - const listItemTwo = initListItem(["abc", "def", "ghi"]); - assert.equal(listItemOne.compareTo(listItemTwo), 1); - }); - it('toString should return correct string', () => { - const listItem = initListItem(["abc", "def", "ghi"]); - assert.deepEqual(listItem.toString(), "abc def ghi"); - }); - it('toString should return correct string for empty ListItem', () => { - const listItem = new ListItem; - assert.deepEqual(listItem.toString(), ""); - }); - it('toStringArray should return correct string[]', () => { - const source = ["abc", "def", "ghi"]; - const listItem = initListItem(source); - assert.deepEqual(listItem.toStringArray(), source); - }); - it('toStringArray should return correct string[] for empty ListItem', () => { - const listItem = new ListItem; - assert.deepEqual(listItem.toStringArray(), []); + describe('Test ListItem', () => { + describe('Test fromStrings()', () => { + it('should return an empty ListItem if source is null', () => { + const actual = ListItem.fromStrings(null, null, null); + const expected = new ListItem(); + assert.deepEqual(actual, expected); + }); + it('should return a valid ListItem from a single source string', () => { + const source = ["abc"]; + const sections = { strs: new Strs }; + sections.strs.allocString = stubSectionsStrsAllocString; + const actual = ListItem.fromStrings(source, null, sections); + const expected = initListItem(source); + assert.deepEqual(actual, expected); + }); + it('should return a valid ListItem from a longer source', () => { + const source = ["abc", "def", "ghi"]; + const sections = { strs: new Strs }; + sections.strs.allocString = stubSectionsStrsAllocString; + const actual = ListItem.fromStrings(source, null, sections); + const expected = initListItem(source); + assert.deepEqual(actual, expected); + }); + }); + describe('Test getItemOrder()', () => { + it('should return a valid index for the first item', () => { + const listItem = initListItem(["abc", "def", "ghi"]); + const index = listItem.getItemOrder("abc"); + assert.equal(index, 0); + }); + it('should return a valid index for a later item', () => { + const listItem = initListItem(["abc", "def", "ghi"]); + const index = listItem.getItemOrder("ghi"); + assert.equal(index, 2); + }); + it('should return -1 for a missing item', () => { + const listItem = initListItem(["abc", "def", "ghi"]); + const index = listItem.getItemOrder("jkl"); + assert.equal(index, -1); + }); + }); + describe('Test isEqual()', () => { + it('should return true for two empty ListItems', () => { + const listItemOne = new ListItem(); + const listItemTwo = new ListItem(); + assert.isTrue(listItemOne.isEqual(listItemTwo)); + }); + it('should return false for empty and non-empty ListItems', () => { + const listItemOne = new ListItem(); + const listItemTwo = initListItem(["abc"]); + assert.isFalse(listItemOne.isEqual(listItemTwo)); + }); + it('should return false for non-empty and empty ListItems', () => { + const listItemOne = initListItem(["abc"]); + const listItemTwo = new ListItem(); + assert.isFalse(listItemOne.isEqual(listItemTwo)); + }); + it('should return true for identical ListItems', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.isTrue(listItemOne.isEqual(listItemTwo)); + }); + it('should return false for different ListItems', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abd", "def", "ghi"]); + assert.isFalse(listItemOne.isEqual(listItemTwo)); + }); + it('should return false for different length ListItems', () => { + const listItemOne = initListItem(["abc", "def"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.isFalse(listItemOne.isEqual(listItemTwo)); + }); + it('should return true for empty ListItem and string[]', () => { + const listItem = new ListItem(); + assert.isTrue(listItem.isEqual([])); + }); + it('should return false for empty ListItem and non-empty string[]', () => { + const listItem = new ListItem(); + assert.isFalse(listItem.isEqual(["abc"])); + }); + it('should return false for non-empty ListItem and empty string[]', () => { + const listItem = initListItem(["abc"]);; + assert.isFalse(listItem.isEqual([])); + }); + it('should return true for identical ListItem and string[]', () => { + const listItem = initListItem(["abc", "def", "ghi"]); + assert.isTrue(listItem.isEqual(["abc", "def", "ghi"])); + }); + it('should return false for different ListItem and string[]', () => { + const listItem = initListItem(["abc", "def", "ghi"]); + assert.isFalse(listItem.isEqual(["abd", "def", "ghi"])); + }); + it('should return false for different length ListItem and string[]', () => { + const listItem = initListItem(["abc", "def"]); + assert.isFalse(listItem.isEqual(["abc", "def", "ghi"])); + }); + }); + describe('Test compareTo()', () => { + it('should return 0 for identical ListItems', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.equal(listItemOne.compareTo(listItemTwo), 0); + }); + it('should return -1 for ListItems with different first items (smallest first)', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abd", "def", "ghi"]); + assert.equal(listItemOne.compareTo(listItemTwo), -1); + }); + it('should return 1 for ListItems with different first items (smallest second)', () => { + const listItemOne = initListItem(["abd", "def", "ghi"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.equal(listItemOne.compareTo(listItemTwo), 1); + }); + it('should return -1 for ListItems with different later items (smallest first)', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abc", "def", "ghj"]); + assert.equal(listItemOne.compareTo(listItemTwo), -1); + }); + it('should return 1 for ListItems with different later items (smallest second)', () => { + const listItemOne = initListItem(["abc", "def", "ghj"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.equal(listItemOne.compareTo(listItemTwo), 1); + }); + it('should return -1 for identical ListItems, except shorter first', () => { + const listItemOne = initListItem(["abc", "def", "ghi"]); + const listItemTwo = initListItem(["abc", "def", "ghi", "jkl"]); + assert.equal(listItemOne.compareTo(listItemTwo), -1); + }); + it('should return 1 for identical ListItems, except longer first', () => { + const listItemOne = initListItem(["abc", "def", "ghi", "jkl"]); + const listItemTwo = initListItem(["abc", "def", "ghi"]); + assert.equal(listItemOne.compareTo(listItemTwo), 1); + }); + }); + describe('Test toString()', () => { + it('should return correct string', () => { + const listItem = initListItem(["abc", "def", "ghi"]); + assert.deepEqual(listItem.toString(), "abc def ghi"); + }); + it('should return correct string for empty ListItem', () => { + const listItem = new ListItem; + assert.deepEqual(listItem.toString(), ""); + }); + }); + describe('Test toStringArray()', () => { + it('should return correct string[]', () => { + const source = ["abc", "def", "ghi"]; + const listItem = initListItem(source); + assert.deepEqual(listItem.toStringArray(), source); + }); + it('should return correct string[] for empty ListItem', () => { + const listItem = new ListItem; + assert.deepEqual(listItem.toStringArray(), []); + }); + }); }); }); From 4dbb33eac43d387b728d6bbc596940a817749a45 Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Thu, 28 Nov 2024 01:43:37 +0000 Subject: [PATCH 13/14] chore(common/web): add copyright banner --- common/web/types/test/ldml-keyboard/test-string-list.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index 3275c9a5162..f125f80abe3 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -1,3 +1,11 @@ +/* + * Keyman is copyright (C) SIL Global. MIT License. + * + * Created by Dr Mark C. Sinclair on 2024-11-28 + * + * Test code for string-lists.ts + */ + import 'mocha'; import { assert } from 'chai'; import { StrsItem, StrsOptions, DependencySections, Strs } from '../../src/kmx/kmx-plus/kmx-plus.js'; From 1837529765bb1f76048a2b4fa0fbe8db6e7b4510 Mon Sep 17 00:00:00 2001 From: "Dr Mark C. Sinclair" Date: Thu, 28 Nov 2024 01:48:16 +0000 Subject: [PATCH 14/14] chore(common/web): add braces to for loop in initListItem() --- common/web/types/test/ldml-keyboard/test-string-list.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/web/types/test/ldml-keyboard/test-string-list.ts b/common/web/types/test/ldml-keyboard/test-string-list.ts index f125f80abe3..58365bda66b 100644 --- a/common/web/types/test/ldml-keyboard/test-string-list.ts +++ b/common/web/types/test/ldml-keyboard/test-string-list.ts @@ -209,7 +209,8 @@ function stubSectionsStrsAllocString(s?: string, opts?: StrsOptions, sections?: function initListItem(source: Array): ListItem { const listItem = new ListItem(); - for (const s of source) + for (const s of source) { listItem.push(new ListIndex(new StrsItem(s))); + } return listItem; }