spiegel-keyman/developer/src/kmc-kmn/test/test-compiler.ts
Marc Durdin d763cf315a chore(developer): refactor kmcmplib interfaces
Relates to #8493.

* Removes kmcmplib calls from kmcmpdll (now that we have kmc)
* Removes old kmcmp_CompileKeyboardFile and
  kmcmp_CompileKeyboardFileToBuffer functions in preference for a much
  cleaner kmcmp_CompileKeyboard function
* Removes json validation helper from kmcmplib (we'll use js-native
  json schema validation instead)

This change means that we no longer need to keep compfile.h consistent
between kmcmplib and kmcmpdll. This will simplify upcoming refactoring
of kmcmplib.

kmcmplib no longer writes files, but it does still read them. The next
refactor will move file load responsibility into the caller.
2023-05-30 08:14:23 +07:00

108 lines
4.1 KiB
TypeScript

import 'mocha';
import { assert } from 'chai';
import { KmnCompiler } from '../src/main.js';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
import { TestCompilerCallbacks } from '@keymanapp/developer-test-helpers';
const __dirname = dirname(fileURLToPath(import.meta.url)).replace(/\\/g, '/');
const keyboardsDir = __dirname + '/../../../../../common/test/keyboards/';
const baselineDir = keyboardsDir + 'baseline/';
describe('Compiler class', function() {
it('should throw on failure', async function() {
const compiler = new KmnCompiler();
const callbacks : any = null; // ERROR
try {
await compiler.init(callbacks)
assert.fail('Expected exception');
} catch(e) {
assert.ok(e);
}
assert.throws(() => compiler.verifyInitialized());
});
it('should start', async function() {
const compiler = new KmnCompiler();
const callbacks = new TestCompilerCallbacks();
assert(await compiler.init(callbacks));
assert(compiler.verifyInitialized());
});
it('should compile a basic keyboard', async function() {
const compiler = new KmnCompiler();
const callbacks = new TestCompilerCallbacks();
assert(await compiler.init(callbacks));
assert(compiler.verifyInitialized());
const fixtureName = baselineDir + 'k_000___null_keyboard.kmx';
const infile = baselineDir + 'k_000___null_keyboard.kmn';
const outfile = __dirname + '/k_000___null_keyboard.kmx';
assert(compiler.run(infile, outfile, {saveDebug: true, shouldAddCompilerVersion: false}));
assert(fs.existsSync(outfile));
const outfileData = fs.readFileSync(outfile);
const fixtureData = fs.readFileSync(fixtureName);
assert.equal(outfileData.byteLength, fixtureData.byteLength);
assert.deepEqual(outfileData, fixtureData);
});
// Note, above test case is essentially a subset of this one, but will leave both because
// the basic keyboard test is slightly simpler to read
it('should build all baseline fixtures', async function() {
const compiler = new KmnCompiler();
const callbacks = new TestCompilerCallbacks();
assert(await compiler.init(callbacks));
assert(compiler.verifyInitialized());
const files = fs.readdirSync(baselineDir);
for(let file of files) {
if(file.match(/\.kmx$/)) {
const fixtureName = baselineDir + file;
const infile = baselineDir + file.replace(/x$/, 'n');
const outfile = __dirname + '/' + file;
assert(compiler.run(infile, outfile, {saveDebug: true, shouldAddCompilerVersion: false}));
assert(fs.existsSync(outfile));
const outfileData = fs.readFileSync(outfile);
const fixtureData = fs.readFileSync(fixtureName);
assert.equal(outfileData.byteLength, fixtureData.byteLength);
assert.deepEqual(outfileData, fixtureData);
}
}
});
it('should compile a keyboard with visual keyboard', async function() {
const compiler = new KmnCompiler();
const callbacks = new TestCompilerCallbacks();
assert(await compiler.init(callbacks));
assert(compiler.verifyInitialized());
const fixtureDir = keyboardsDir + 'caps_lock_layer_3620/'
const infile = fixtureDir + 'source/caps_lock_layer_3620.kmn';
const kmxFixture = fixtureDir + '/binary/caps_lock_layer_3620.kmx';
const kvkFixture = fixtureDir + '/binary/caps_lock_layer_3620.kvk';
const resultingKmxfile = __dirname + '/caps_lock_layer_3620.kmx';
const resultingKvkfile = __dirname + '/caps_lock_layer_3620.kvk';
assert(compiler.run(infile, resultingKmxfile, {saveDebug: true, shouldAddCompilerVersion: false}));
assert(fs.existsSync(resultingKmxfile));
assert(fs.existsSync(resultingKvkfile));
const kmxData = fs.readFileSync(resultingKmxfile);
const kmxFixtureData = fs.readFileSync(kmxFixture);
assert.equal(kmxData.byteLength, kmxFixtureData.byteLength);
assert.deepEqual(kmxData, kmxFixtureData);
const kvkData = fs.readFileSync(resultingKvkfile);
const kvkFixtureData = fs.readFileSync(kvkFixture);
assert.equal(kvkData.byteLength, kvkFixtureData.byteLength);
assert.deepEqual(kvkData, kvkFixtureData);
});
});