UNPKG

ngx-i18nsupport-lib

Version:

A Typescript library to work with Angular generated i18n files (xliff, xmb)

142 lines 6.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _1 = require("."); var fs = require("fs"); /** * Created by martin on 10.04.2017. * Testcases for public API. * Just reading different file formats. * Detail Tests are in the files for the specific formats. */ describe('ngx-i18nsupport-lib API test spec', function () { var SRCDIR = 'test/testdata/i18n/'; var ENCODING = 'UTF-8'; /** * Helper function to read Xliff from File * @type {string} */ function readXliff(path) { var content = fs.readFileSync(path, ENCODING); return _1.TranslationMessagesFileFactory.fromFileContent('xlf', content, path, ENCODING); } /** * Helper function to read XLIFF 2.0 from File * @type {string} */ function readXliff2(path) { var content = fs.readFileSync(path, ENCODING); return _1.TranslationMessagesFileFactory.fromFileContent('xlf2', content, path, ENCODING); } /** * Helper function to read Xmb from File * @type {string} */ function readXmb(path) { var content = fs.readFileSync(path, ENCODING); return _1.TranslationMessagesFileFactory.fromFileContent('xmb', content, path, ENCODING); } /** * Helper function to read Xmb from 2 Files, the xmb and the master * @type {string} */ function readXtbWithMaster(path, masterPath) { var content = fs.readFileSync(path, ENCODING); if (masterPath) { var masterContent = fs.readFileSync(masterPath, ENCODING); var optionalMaster = { xmlContent: masterContent, path: masterPath, encoding: ENCODING }; return _1.TranslationMessagesFileFactory.fromFileContent('xtb', content, path, ENCODING, optionalMaster); } else { return _1.TranslationMessagesFileFactory.fromFileContent('xtb', content, path, ENCODING); } } /** * Helper function to read translation file of any format * @type {string} */ function readFile(path, masterPath) { var content = fs.readFileSync(path, ENCODING); if (masterPath) { var masterContent = fs.readFileSync(masterPath, ENCODING); var optionalMaster = { xmlContent: masterContent, path: masterPath, encoding: ENCODING }; return _1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(content, path, ENCODING, optionalMaster); } else { return _1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(content, path, ENCODING); } } describe('api tests', function () { var MASTER1SRC_XLIFF = SRCDIR + 'ngExtractedMaster1.xlf'; var MASTER1SRC_XLIFF2 = SRCDIR + 'ngExtractedMaster1.xlf2'; var TRANSLATED_FILE_SRC_XLIFF = SRCDIR + 'translatedFile.xlf'; var MASTER1SRC_XMB = SRCDIR + 'ngExtractedMaster1.xmb'; var MASTER_DE_XMB = SRCDIR + 'ngExtractedMaster1.de.xmb'; var MASTER_EN_XTB = SRCDIR + 'ngExtractedMaster1.en.xtb'; it('should read xlf file', function () { var file = readXliff(MASTER1SRC_XLIFF); expect(file).toBeTruthy(); expect(file.fileType()).toBe('XLIFF 1.2'); }); it('should read XLIFF 2.0 file', function () { var file = readXliff2(MASTER1SRC_XLIFF2); expect(file).toBeTruthy(); expect(file.fileType()).toBe('XLIFF 2.0'); }); it('should read xmb file', function () { var file = readXmb(MASTER1SRC_XMB); expect(file).toBeTruthy(); expect(file.fileType()).toBe('XMB'); }); it('should read xtb file with master', function () { var file = readXtbWithMaster(MASTER_EN_XTB, MASTER_DE_XMB); expect(file).toBeTruthy(); expect(file.fileType()).toBe('XTB'); expect(file.sourceLanguage()).toBe('de'); expect(file.targetLanguage()).toBe('en'); }); it('should autodetect file format', function () { var file1 = readFile(MASTER1SRC_XLIFF); expect(file1).toBeTruthy(); expect(file1.fileType()).toBe('XLIFF 1.2'); var file2 = readFile(MASTER1SRC_XMB); expect(file2).toBeTruthy(); expect(file2.fileType()).toBe('XMB'); var file3 = readFile(MASTER_EN_XTB, MASTER1SRC_XMB); expect(file3).toBeTruthy(); expect(file3.fileType()).toBe('XTB'); var file4 = readFile(MASTER1SRC_XLIFF2); expect(file4).toBeTruthy(); expect(file4.fileType()).toBe('XLIFF 2.0'); }); it('should detect files with wrong format', function () { try { _1.TranslationMessagesFileFactory.fromUnknownFormatFileContent('schrott', 'dummyfile', 'UTF-X'); } catch (error) { expect(error.toString()).toBe('Error: could not identify file format, it is neiter XLIFF (1.2 or 2.0) nor XMB/XTB'); return; } fail('expected error not received'); }); it('should report wrong format as error', function () { try { _1.TranslationMessagesFileFactory.fromFileContent('schrott', 'schrott', 'dummyfile', 'UTF-X'); } catch (error) { expect(error.toString()).toBe('Error: oops, unsupported format "schrott"'); return; } fail('expected error not received'); }); it('should detect error when reading xtb file with no xmb master', function () { try { var file = readXtbWithMaster(MASTER_EN_XTB, MASTER1SRC_XLIFF); fail('expected error not received'); } catch (error) { expect(error.toString()).toContain('An xtb file needs xmb as master file.'); } }); }); }); //# sourceMappingURL=S:/experimente/ngx-i18nsupport-lib/src/api/api.spec.js.map