spiegel-keyman/common/predictive-text/unit_tests/headless/worker-custom-punctuation.js

104 lines
No EOL
3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Unit tests for the Dummy prediction model.
*/
var assert = require('chai').assert;
var DummyModel = require('../../build/intermediate').models.DummyModel;
var ModelCompositor = require('../../build/intermediate').ModelCompositor;
describe('Custom Punctuation', function () {
it('appears in the keep suggestion', function () {
let dummySuggestions = [
{
transform: {
insert: 'Hrllo',
deleteLeft: 0,
},
tag: 'keep',
displayAs: 'Hrllo',
},
{
transform: {
insert: 'Hello',
deleteLeft: 0,
},
displayAs: 'Hello',
},
{
transform: {
insert: 'Jello',
deleteLeft: 0,
},
displayAs: 'Jello',
}
];
var model = new DummyModel({
futureSuggestions: [dummySuggestions],
punctuation: {
quotesForKeepSuggestion: {
open: "«", close: "»"
},
}
});
// The model compositor is responsible for adding this to the display as
// string.
var composite = new ModelCompositor(model);
var suggestions = composite.predict([{ sample: { insert: 'o', deleteLeft: 0 }, p: 1.00 }], {
left: 'Hrll', startOfBuffer: false, endOfBuffer: true
});
assert.lengthOf(suggestions, 3);
// We only care about the "keep" suggestion.
var keepSuggestions = suggestions.filter(suggestion => suggestion.tag === 'keep');
assert.lengthOf(keepSuggestions, 1,
`Expected exactly one "keep" suggestion, but found ${keepSuggestions.length}`
);
// The moment of truth: has our punctuation been applied?
var suggestion = keepSuggestions[0];
assert.equal(suggestion.displayAs, `«Hrllo»`);
});
describe("insertAfterWord", function () {
it('appears after "word" suggestion', function () {
let dummySuggestions = [
{
transform: { insert: 'ᚈᚑᚋ', deleteLeft: 0, },
displayAs: 'ᚈᚑᚋ',
},
{
transform: { insert: 'ᚄ', deleteLeft: 0, },
displayAs: 'ᚄ',
},
{
transform: { insert: 'ᚉᚑᚈᚈ', deleteLeft: 0, },
displayAs: 'ᚉᚑᚈᚈ',
}
];
var model = new DummyModel({
futureSuggestions: [dummySuggestions],
punctuation: {
// U+1680 OGHAM SPACE MARK:
// it's technically whitespace, but it don't look it!
insertAfterWord: "",
}
});
// The model compositor is responsible for adding this to the display as
// string.
var composite = new ModelCompositor(model);
var suggestions = composite.predict([{ sample: { insert: 'ᚋ', deleteLeft: 0 }, p: 1.00 }], {
left: '᚛ᚈᚑ', startOfBuffer: false, endOfBuffer: true
});
assert.lengthOf(suggestions, dummySuggestions.length);
// Check that it has been changed:
for (var i = 0; i < dummySuggestions.length; i++) {
assert.isTrue(suggestions[i].transform.insert.endsWith(''));
}
});
})
});