-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
test(common/web/types): unit tests for string-list #12702
Merged
markcsinclair
merged 15 commits into
master
from
test/common/web/types/9052-unit-tests-string-list
Nov 28, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2cb2e8c
chore(developer): initial commit of test-string-lists.ts
markcsinclair fa4f1e5
chore(developer): add one ListItem test case
markcsinclair a5c8024
chore(developer): add six test cases for ListIndex
markcsinclair ef46a8e
chore(developer): add a ListItem.fromStrings() test case
markcsinclair 15c71d7
chore(developer): add a further ListItem.fromStrings() test case
markcsinclair 88b0f71
chore(developer): add three ListItem.getItemOrder() test cases
markcsinclair 769259e
chore(developer): add four test cases for ListItem.isEqual, plus refa…
markcsinclair d77c5ae
chore(developer): add seven additional test cases for ListItem.isEqual
markcsinclair 04a8a0a
chore(developer): add seven test cases for ListItem.compareTo
markcsinclair b3398fb
chore(developer): add test cases for ListItem.toString() and toString…
markcsinclair 19da0c7
chore(developer): add four additional test cases
markcsinclair 402b406
chore(common/web): refactor tests for readability
markcsinclair 6096737
chore(common/web): Merge branch 'master' into test/common/web/types/9…
markcsinclair 4dbb33e
chore(common/web): add copyright banner
markcsinclair 1837529
chore(common/web): add braces to for loop in initListItem()
markcsinclair 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
src/schemas/ | ||
obj/ | ||
obj/ | ||
coverage/ |
216 changes: 216 additions & 0 deletions
216
common/web/types/test/ldml-keyboard/test-string-list.ts
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,216 @@ | ||
/* | ||
* 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'; | ||
import { ListIndex, ListItem } from '../../src/ldml-keyboard/string-list.js'; | ||
|
||
describe('Test of String-List', () => { | ||
describe('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 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 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 listItem = new ListIndex(new StrsItem("abc")); | ||
const aString = "abc"; | ||
assert.isTrue(listItem.isEqual(aString)); | ||
}); | ||
it('can check a ListIndex and string for inequality', () => { | ||
const listItem = new ListIndex(new StrsItem("abc")); | ||
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('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(), []); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
function stubSectionsStrsAllocString(s?: string, opts?: StrsOptions, sections?: DependencySections): StrsItem { | ||
return new StrsItem(s); | ||
} | ||
|
||
function initListItem(source: Array<string>): ListItem { | ||
const listItem = new ListItem(); | ||
for (const s of source) { | ||
listItem.push(new ListIndex(new StrsItem(s))); | ||
} | ||
return listItem; | ||
} |
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.
Let's add the standard header https://github.com/keymanapp/keyman/wiki/Keyman-Code-Style-Guide#file-header
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.
Done