Skip to content

Commit

Permalink
fix: plural compiler bug (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Apr 29, 2020
1 parent fe7cd02 commit 44b8d43
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/message/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const DEFAULT_MODIFIER = (str: unknown): unknown => str
const DEFAULT_MESSAGE = (ctx: MessageContext): unknown => '' // eslint-disable-line
export const DEFAULT_MESSAGE_DATA_TYPE = 'text'
const DEFAULT_NORMALIZE = (values: unknown[]): unknown =>
values.length === 0 ? values[0] : values.join('')
values.length === 0 ? '' : values.join('')
const DEFAULT_INTERPOLATE = toDisplayString

function pluralDefault(choice: number, choicesLength: number): number {
Expand Down
38 changes: 37 additions & 1 deletion test/message/__snapshots__/compiler.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`compile: code 1`] = `
exports[`@.caml:{'no apples'} | {0} apple | {n} apples: code 1`] = `
"function __msg__ (ctx) {
return [
ctx.normalize([
Expand All @@ -13,3 +13,39 @@ exports[`compile: code 1`] = `
][ctx.pluralRule(ctx.pluralIndex, 3, ctx.orgPluralRule)]
}"
`;

exports[`edge cases | | | : code 1`] = `
"function __msg__ (ctx) {
return [
ctx.normalize([
]), ctx.normalize([
]), ctx.normalize([
]), ctx.normalize([
])
][ctx.pluralRule(ctx.pluralIndex, 4, ctx.orgPluralRule)]
}"
`;

exports[`edge cases | | | : error 1`] = `
Object {
"code": 10,
"domain": "parser",
"location": Object {
"end": Object {
"column": 8,
"line": 1,
"offset": 7,
},
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"message": "Plural must have messages",
}
`;
20 changes: 20 additions & 0 deletions test/message/__snapshots__/context.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,23 @@ Array [
},
]
`;

exports[`edge cases plural: ' | foo | ': error 1`] = `
Object {
"code": 10,
"domain": "parser",
"location": Object {
"end": Object {
"column": 10,
"line": 1,
"offset": 9,
},
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"message": "Plural must have messages",
}
`;
19 changes: 15 additions & 4 deletions test/message/compiler.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { compile } from '../../src/message/compiler'

test('compile', () => {
const code = compile(
`@.caml:{'no apples'} | {0} apple | {n} apples` // eslint-disable-line no-irregular-whitespace
)
/* eslint-disable no-irregular-whitespace */
test(`@.caml:{'no apples'} | {0} apple | {n} apples`, () => {
const code = compile(`@.caml:{'no apples'} | {0} apple | {n} apples`)
expect(code.toString()).toMatchSnapshot('code')
})
/* eslint-enable no-irregular-whitespace */

describe('edge cases', () => {
test(` | | | `, () => {
const code = compile(` | | | `, {
onError(error) {
expect({ ...error, message: error.message }).toMatchSnapshot('error')
}
})
expect(code.toString()).toMatchSnapshot('code')
})
})
24 changes: 18 additions & 6 deletions test/message/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ describe('text', () => {
const ctx = createMessageContext()
expect(JSON.stringify(msg(ctx))).toMatch(`hello\\n world`)
})

test(' ', () => {
const msg = compile(' ')
const ctx = createMessageContext()
expect(msg(ctx)).toMatch(` `)
})
})

describe('list', () => {
Expand Down Expand Up @@ -348,3 +342,21 @@ describe('custom process', () => {
expect(msg(ctx)).toMatchSnapshot()
})
})

describe('edge cases', () => {
test(`text: ' '`, () => {
const msg = compile(' ')
const ctx = createMessageContext()
expect(msg(ctx)).toMatch(` `)
})

test(`plural: ' | foo | '`, () => {
const msg = compile(` | foo | `, {
onError(error) {
expect({ ...error, message: error.message }).toMatchSnapshot('error')
}
})
const ctx = createMessageContext()
expect(msg(ctx)).toMatch('')
})
})

0 comments on commit 44b8d43

Please sign in to comment.