mirror of
https://github.com/keymanapp/keyman.git
synced 2026-11-03 17:47:50 +00:00
The boilerplate code for custom lexical models has never really been tested. For use in a browser/worker context, we need to define `exports`. The added unit test verifies that the model will build. Test-bot: skip
139 lines
3.4 KiB
ObjectPascal
139 lines
3.4 KiB
ObjectPascal
unit Keyman.System.Test.LexicalModelParserTest;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.Classes,
|
|
System.SysUtils,
|
|
DUnitX.TestFramework;
|
|
|
|
type
|
|
[TestFixture]
|
|
TLexicalModelParserTest = class(TObject)
|
|
private
|
|
m: TStringList;
|
|
AssetRootPath: string;
|
|
mcustom: TStringList;
|
|
mnocomment: TStrings;
|
|
public
|
|
[Setup]
|
|
procedure Setup;
|
|
[TearDown]
|
|
procedure TearDown;
|
|
|
|
[Test]
|
|
procedure TestRead;
|
|
|
|
[Test]
|
|
procedure TestWrite;
|
|
|
|
[Test]
|
|
procedure TestWriteDeleteComment;
|
|
|
|
[Test]
|
|
procedure TestWriteNewComment;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
Keyman.Developer.System.LexicalModelParser,
|
|
Keyman.Developer.System.LexicalModelParserTypes;
|
|
|
|
procedure TLexicalModelParserTest.Setup;
|
|
begin
|
|
Assert.IgnoreCaseDefault := False;
|
|
|
|
// Running from bin/platform/configuration/ folder has
|
|
// assets two levels up.
|
|
if FileExists('..\..\..\assets\nrc.en.mtnt.model.ts') then AssetRootPath := '..\..\..\assets\'
|
|
// Running from project folder
|
|
else if FileExists('assets\nrc.en.mtnt.model.ts') then AssetRootPath := 'assets\'
|
|
// You must know what you are doing
|
|
else AssetRootPath := '';
|
|
|
|
m := TStringList.Create;
|
|
m.LoadFromFile(AssetRootPath + 'nrc.en.mtnt.model.ts');
|
|
|
|
mcustom := TStringList.Create;
|
|
mcustom.LoadFromFile(AssetRootPath + 'custom.model.ts');
|
|
|
|
mnocomment := TStringList.Create;
|
|
mnocomment.LoadFromFile(AssetRootPath + 'nocomment.model.ts');
|
|
end;
|
|
|
|
procedure TLexicalModelParserTest.TearDown;
|
|
begin
|
|
m.Free;
|
|
mcustom.Free;
|
|
mnocomment.Free;
|
|
end;
|
|
|
|
procedure TLexicalModelParserTest.TestRead;
|
|
var
|
|
lm: TLexicalModelParser;
|
|
begin
|
|
lm := TLexicalModelParser.Create(m.Text);
|
|
try
|
|
Assert.IsTrue(lm.Format = lmfTrie10, 'Expected lmfTrie10');
|
|
Assert.IsTrue(lm.WordBreaker = lmwbDefault, 'Expected lmwbDefault');
|
|
Assert.AreEqual(1, lm.Wordlists.Count, 'Expected 1 wordlist');
|
|
Assert.AreEqual('mtnt.tsv', lm.Wordlists[0], 'Wordlist name');
|
|
Assert.AreEqual(#13#10' Sample model for testing parser'#13#10, lm.Comment);
|
|
finally
|
|
lm.Free;
|
|
end;
|
|
end;
|
|
|
|
procedure TLexicalModelParserTest.TestWrite;
|
|
var
|
|
lm: TLexicalModelParser;
|
|
begin
|
|
lm := TLexicalModelParser.Create(m.Text);
|
|
try
|
|
// TODO: LexicalModelParser does not support `rootClass` which is needed for
|
|
// custom lexical models, and the property `Wordlists` is inappropriate for
|
|
// the list of sources files. This is part of a bigger project for better
|
|
// custom model support in TIKE
|
|
//
|
|
// For now, this test excludes the `rootClass` property in order to pass
|
|
lm.Comment := 'Testing';
|
|
lm.Format := lmfCustom10;
|
|
lm.WordBreaker := lmwbAscii;
|
|
lm.Wordlists.Clear;
|
|
lm.Wordlists.Add('CustomModel.ts');
|
|
Assert.AreEqual(mcustom.Text.Trim, lm.Text.Trim); // Ignoring whitespace before/after
|
|
finally
|
|
lm.Free;
|
|
end;
|
|
end;
|
|
|
|
procedure TLexicalModelParserTest.TestWriteNewComment;
|
|
var
|
|
lm: TLexicalModelParser;
|
|
begin
|
|
lm := TLexicalModelParser.Create(mnocomment.Text);
|
|
try
|
|
lm.Comment := #13#10' Sample model for testing parser'#13#10;
|
|
Assert.AreEqual(m.Text.Trim, lm.Text.Trim); // Ignoring whitespace before/after
|
|
finally
|
|
lm.Free;
|
|
end;
|
|
end;
|
|
|
|
procedure TLexicalModelParserTest.TestWriteDeleteComment;
|
|
var
|
|
lm: TLexicalModelParser;
|
|
begin
|
|
lm := TLexicalModelParser.Create(m.Text);
|
|
try
|
|
lm.Comment := '';
|
|
Assert.AreEqual(mnocomment.Text.Trim, lm.Text.Trim); // Ignoring whitespace before/after
|
|
finally
|
|
lm.Free;
|
|
end;
|
|
end;
|
|
|
|
initialization
|
|
TDUnitX.RegisterTestFixture(TLexicalModelParserTest);
|
|
end.
|