-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from mikeedwards/1.0.0-beta-3
updated tests for beta-3 and rebuilt packages for node 10 and up
- Loading branch information
Showing
9 changed files
with
4,044 additions
and
1,435 deletions.
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,6 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- 9 | ||
- 8 | ||
- 7 | ||
- 6 | ||
- 14 | ||
- 13 | ||
- 12 | ||
- 10 |
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
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,198 @@ | ||
const Jed = require("jed"); | ||
const MessageFormat = require("messageformat"); | ||
const po2json = require("../index"); | ||
const fs = require("fs"); | ||
const { promisify } = require("util"); | ||
const readFile = promisify(fs.readFile); | ||
|
||
describe("parse", () => { | ||
|
||
it("parse", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/pl.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/pl.json", "utf-8")); | ||
const parsed = po2json.parse(po); | ||
expect(parsed).toEqual(json); | ||
}); | ||
|
||
it("parse with old Jed format", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/pl.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/pl-jedold.json", "utf-8")); | ||
const parsed = po2json.parse(po, {format: 'jedold'}); | ||
expect(parsed).toEqual(json); | ||
expect(() => { | ||
new Jed(parsed) | ||
}).not.toThrowError(Error); | ||
}); | ||
|
||
it("parse with current Jed format", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/pl.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/pl-jed.json", "utf-8")); | ||
const parsed = po2json.parse(po, {format: 'jed'}); | ||
expect(parsed).toEqual(json); | ||
expect(() => { | ||
new Jed(parsed) | ||
}).not.toThrowError(Error); | ||
}); | ||
|
||
it("parse with MessageFormatter format", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/pl.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/pl-mf.json", "utf-8")); | ||
const parsed = po2json.parse(po, {format: 'mf'}); | ||
expect(parsed).toEqual(json); | ||
}); | ||
|
||
it("parse with MessageFormatter and compile successfully", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/pl.po"); | ||
const translations = await po2json.parse(po, {format: 'mf'}); | ||
|
||
const f = (n) => { | ||
return (n === 1 ? 'p0' : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 'p1' : 'p2'); | ||
}; | ||
f.cardinal = ['p0', 'p1', 'p2']; | ||
const mf = new MessageFormat( | ||
{ | ||
'pl': f | ||
} | ||
); | ||
const messages = mf.compile(translations); | ||
expect(messages['A sentence with "quotation" marks.']({})).toEqual("Zdanie w \"cudzysłowie\"."); | ||
expect(messages['one product']([1])).toEqual('jeden produkt'); | ||
expect(messages['one product']([2])).toEqual('2 produkty'); | ||
expect(messages['one product']([12])).toEqual('12 produktów'); | ||
expect(messages['one product']([22])).toEqual('22 produkty'); | ||
expect(messages['string context']['the contextual phrase']({})).toEqual('zwrot kontekstowe'); | ||
}); | ||
|
||
it("parse with full MessageFormatter format", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/pl.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/pl-mf-full.json", "utf-8")); | ||
const parsed = po2json.parse(po, {format: 'mf', fullMF: true}); | ||
expect(parsed.headers).toEqual(json.headers); | ||
expect(parsed.translations).toEqual(json.translations); | ||
}); | ||
|
||
it("parse with full MessageFormatter format and get plural function", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/pl.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/pl-mf-full.json", "utf-8")); | ||
const parsed = po2json.parse(po, {format: 'mf', fullMF: true}); | ||
expect(parsed.pluralFunction).toBeTruthy(); | ||
expect(typeof parsed.pluralFunction).toBe('function'); | ||
}); | ||
|
||
it("parse with full MessageFormatter and compile successfully", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/pl.po"); | ||
const parsed = await po2json.parse(po, {format: 'mf', fullMF: true}); | ||
|
||
const locale = {}; | ||
locale[parsed.headers.language] = parsed.pluralFunction; | ||
const mf = new MessageFormat(locale); | ||
const messages = mf.compile(parsed.translations); | ||
|
||
expect(messages['']['A sentence with "quotation" marks.']({})).toEqual("Zdanie w \"cudzysłowie\"."); | ||
expect(messages['']['one product']([1])).toEqual('jeden produkt'); | ||
expect(messages['']['one product']([2])).toEqual('2 produkty'); | ||
expect(messages['']['one product']([12])).toEqual('12 produktów'); | ||
expect(messages['']['one product']([22])).toEqual('22 produkty'); | ||
expect(messages['string context']['the contextual phrase']({})).toEqual('zwrot kontekstowe'); | ||
}); | ||
|
||
it("parse with MessageFormatter format + fallback-to-msgid", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/en-empty.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/en-mf-fallback-to-msgid.json", "utf-8")); | ||
|
||
const parsed = po2json.parse(po, {format: 'mf', 'fallback-to-msgid': true}); | ||
expect(parsed).toEqual(json); | ||
}); | ||
|
||
it("parse with fallback-to-msgid", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/en-empty.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/en-empty.json", "utf-8")); | ||
const parsed = po2json.parse(po, {'fallback-to-msgid': true}); | ||
expect(parsed).toEqual(json); | ||
}); | ||
|
||
it("parse with Plural-Forms == nplurals=1; plural=0;", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/ja.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/ja.json", "utf-8")); | ||
const parsed = po2json.parse(po); | ||
expect(parsed).toEqual(json); | ||
}); | ||
|
||
it("parse with Plural-Forms == nplurals=1; plural=0; and with the current Jed format", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/ja.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/ja-jed.json", "utf-8")); | ||
const parsed = po2json.parse(po, {format: 'jed'}); | ||
expect(parsed).toEqual(json); | ||
}); | ||
|
||
it("parse with no headers", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/en-no-header.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/en-no-header.json", "utf-8")); | ||
const parsed = po2json.parse(po); | ||
expect(parsed).toEqual(json); | ||
}); | ||
|
||
it("parse with raw JSON context correctly", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/es-context.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/es-context.json", "utf-8")); | ||
const parsed = po2json.parse(po, {format: 'raw'}); | ||
expect(parsed).toEqual(json); | ||
}); | ||
|
||
it("parse with jed < 1.1.0 context correctly", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/es-context.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/es-context-jedold.json", "utf-8")); | ||
const parsed = po2json.parse(po, {format: 'jedold'}); | ||
expect(parsed).toEqual(json); | ||
}); | ||
|
||
it("parse with jed >= 1.1.0 context correctly", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/es-context.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/es-context-jed.json", "utf-8")); | ||
const parsed = po2json.parse(po, {format: 'jed'}); | ||
expect(parsed).toEqual(json); | ||
}); | ||
|
||
it("parse with MessageFormat context correctly", async () => { | ||
const po = await readFile(__dirname + "/../test/fixtures/es-context.po"); | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/es-context-mf.json", "utf-8")); | ||
const parsed = po2json.parse(po, {format: 'mf'}); | ||
expect(parsed).toEqual(json); | ||
}); | ||
|
||
it("handle braces in mf with messageformat options", async () => { | ||
this.po = ` | ||
msgid "test" | ||
msgstr "Hi %{firstname}" | ||
`; | ||
//this.json = JSON.parse(`{ "test": "Hi %\\\\{firstname\\\\}" }`); | ||
this.json = await JSON.parse(`{ "test": "Hi %{firstname}" }`); | ||
|
||
const mfOptions = { | ||
replacements: [ | ||
{ | ||
pattern: /%(\d+)(?:\$\w)?/g, | ||
replacement: (_, n) => `{${n - 1}}` | ||
}, | ||
{ | ||
pattern: /%\((\w+)\)\w/g, | ||
replacement: '{$1}' | ||
}, | ||
{ | ||
pattern: /%\w/g, | ||
replacement: function () { | ||
return `{${this.n++}}` | ||
}, | ||
state: {n: 0} | ||
}, | ||
{ | ||
pattern: /%%/g, | ||
replacement: '%' | ||
} | ||
] | ||
}; | ||
|
||
const parsed = po2json.parse(this.po, {format: 'mf', mfOptions: mfOptions}); | ||
expect(parsed).toEqual(this.json); | ||
}); | ||
}); |
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,16 @@ | ||
const Jed = require("jed"); | ||
const MessageFormat = require("messageformat"); | ||
const po2json = require("../index"); | ||
const fs = require("fs"); | ||
const { promisify } = require("util"); | ||
const readFile = promisify(fs.readFile); | ||
|
||
describe("parseFile", () => { | ||
it("parseFile", async (done) => { | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/pl.json", "utf-8")); | ||
await po2json.parseFile(__dirname + "/../test/fixtures/pl.po", null, function (_, parsed) { | ||
expect(parsed).toEqual(json); | ||
done(); | ||
}); | ||
}); | ||
}); |
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,14 @@ | ||
const Jed = require("jed"); | ||
const MessageFormat = require("messageformat"); | ||
const po2json = require("../index"); | ||
const fs = require("fs"); | ||
const { promisify } = require("util"); | ||
const readFile = promisify(fs.readFile); | ||
|
||
describe("parseFileSync", () => { | ||
it("parseFileSync", async () => { | ||
const json = await JSON.parse(await readFile(__dirname + "/../test/fixtures/pl.json", "utf-8")); | ||
const parsed = po2json.parseFileSync(__dirname + "/../test/fixtures/pl.po", null); | ||
expect(parsed).toEqual(json); | ||
}); | ||
}); |
Oops, something went wrong.