Skip to content
Browse files

migrated tests to mocha

  • Loading branch information...
1 parent 9d655fe commit 3e0133d3775a5213aa1f032f15ea82c8cea2bb61 @puzrin puzrin committed Dec 31, 2012
Showing with 687 additions and 644 deletions.
  1. +2 −2 Makefile
  2. +2 −1 package.json
  3. +288 −264 test/babelfish-test.js
  4. +0 −37 test/issues-test.js
  5. +10 −11 test/issues/issue-3.js
  6. +13 −10 test/issues/issue-7.js
  7. +2 −0 test/mocha.opts
  8. +86 −82 test/parser-test.js
  9. +284 −237 test/pluralizer-test.js
View
4 Makefile
@@ -23,12 +23,12 @@ lint:
jshint . --show-non-errors
test:
- @if test ! `which vows` ; then \
+ @if test ! `which mocha` ; then \
echo "You need 'vows' installed in order to run tests." >&2 ; \
echo " $ make dev-deps" >&2 ; \
exit 128 ; \
fi
- NODE_ENV=test vows --spec
+ NODE_ENV=test mocha
doc:
@if test ! `which ndoc` ; then \
View
3 package.json
@@ -19,7 +19,8 @@
"dependencies" : { "underscore": "~ 1.4.2" },
"devDependencies" : {
- "vows" : "~ 0.6.1",
+ "mocha" : "~ 1.7.4",
+ "chai" : "~ 1.4.2",
"collector" : "~ 0.0.1",
"pegjs" : "~> 0.7.0"
},
View
552 test/babelfish-test.js
@@ -1,271 +1,295 @@
+/*global it, describe*/
+
'use strict';
-var Assert = require('assert');
+var expect = require('chai').expect;
var BabelFish = require('../lib/babelfish');
+describe('BabelFish', function () {
+
+
+ describe('API consistence tests', function () {
+
+
+ describe('Exported module', function () {
+
+ it('is a constructor', function () {
+ expect(BabelFish).to.be.a('function');
+ expect(new BabelFish()).to.be.instanceOf(BabelFish);
+ });
+
+ it('has `create` (constructor proxy)', function () {
+ expect(BabelFish.create).to.be.a('function');
+ expect(BabelFish.create.length).is.equal(BabelFish.length);
+ expect(BabelFish.create()).is.instanceof(BabelFish);
+ });
+ });
+
+
+ describe('Instance', function () {
+ var b = new BabelFish();
+
+ it('has `addPhrase()` method', function () {
+ expect(b.addPhrase).to.be.a('function');
+ });
+
+ it('has `getCompiledData()` method', function () {
+ expect(b.getCompiledData).to.be.a('function');
+ });
+
+ it('has `setFallback()` method', function () {
+ expect(b.setFallback).to.be.a('function');
+ });
+
+ it('has `translate()` method', function () {
+ expect(b.translate).to.be.a('function');
+ });
+
+ it('has `t()` alias', function () {
+ expect(b.t).is.equal(b.translate);
+ });
+
+ it('has `defaultLocale` property', function () {
+ expect(b).to.have.property('defaultLocale');
+ expect(b.defaultLocale).is.not.a('function');
+ });
+
+ it('`defaultLocale` property is read-only', function () {
+ expect(function () { b.defaultLocale = 'ru'; }).to.throw(TypeError);
+ expect(function () { delete b.defaultLocale; }).to.throw(TypeError);
+ });
+ });
+
+
+ describe('New instance with defaults', function () {
+ var b = BabelFish.create();
+
+ it('has defaultLocale = `en`', function () {
+ expect(b.defaultLocale).is.equal('en');
+ });
+ });
+
+
+ describe('New instance with defaultLocale given', function () {
+ var b = BabelFish.create('ru');
+
+ it('has defaultLocale equal to the specified one', function () {
+ expect(b.defaultLocale).is.equal('ru');
+ });
+ });
+ });
+
+
+ describe('Behavior and unit tests come here', function () {
+
+
+ describe('When fallback is given', function () {
+ var b = BabelFish.create('en');
+
+ b.setFallback('es', ['es-ES', 'es-MX']);
+ b.setFallback('es-ES', ['es', 'es-US']);
+
+ b.addPhrase('en', 'aaa', 'aaa (en)');
+ b.addPhrase('en', 'bbb', 'bbb (en)');
+ b.addPhrase('en', 'ccc', 'ccc (en)');
+ b.addPhrase('en', 'ddd', 'ddd (en)');
+ b.addPhrase('es', 'aaa', 'aaa (es)');
+ b.addPhrase('es-ES', 'bbb', 'bbb (es-ES)');
+ b.addPhrase('es-MX', 'ccc', 'ccc (es-MX)');
+ b.addPhrase('es-US', 'ddd', 'ddd (es-US)');
+
+ b.setFallback('es-US', ['es']);
+
+ it('use defaultLocale in worst case', function () {
+ expect(b.t('es', 'ddd')).is.equal('ddd (en)');
+ expect(b.t('ru', 'ddd')).is.equal('ddd (en)');
+ });
+
+ it('allows specify more than one fallback locale', function () {
+ expect(b.t('es', 'aaa')).is.equal('aaa (es)');
+ expect(b.t('es', 'bbb')).is.equal('bbb (es-ES)');
+ expect(b.t('es', 'ccc')).is.equal('ccc (es-MX)');
+ expect(b.t('es', 'ddd')).is.equal('ddd (en)');
+ });
+
+ it('do not recursively resolve locale fallbacks', function () {
+ expect(b.t('es-ES', 'aaa')).is.equal('aaa (es)');
+ expect(b.t('es-ES', 'bbb')).is.equal('bbb (es-ES)');
+ expect(b.t('es-ES', 'ccc')).is.equal('ccc (en)');
+ expect(b.t('es-ES', 'ddd')).is.equal('ddd (es-US)');
+ });
+
+ it('allow specify fallbacks after phrases were added', function () {
+ expect(b.t('es-US', 'aaa')).is.equal('aaa (es)');
+ expect(b.t('es-US', 'bbb')).is.equal('bbb (en)');
+ expect(b.t('es-US', 'ccc')).is.equal('ccc (en)');
+ expect(b.t('es-US', 'ddd')).is.equal('ddd (es-US)');
+ });
+
+ it('allows re-assign fallbacks', function () {
+ b.setFallback('es-US', ['es-ES', 'es-MX']);
+
+ expect(b.t('es', 'aaa')).is.equal('aaa (es)');
+ expect(b.t('es', 'bbb')).is.equal('bbb (es-ES)');
+ expect(b.t('es', 'ccc')).is.equal('ccc (es-MX)');
+ expect(b.t('es', 'ddd')).is.equal('ddd (en)');
+ });
+ });
+
+
+ describe('Setting fallback for defaultLocale', function () {
+ var b = BabelFish.create('en');
+
+ it('cause exception', function () {
+ expect(function () { b.setFallback('en', ['en-GB']); })
+ .to.throw(Error);
+ });
+ });
+
+
+ describe('Adding phrases', function () {
+ var b = BabelFish.create('en');
+
+ b.addPhrase('en', 'phrase1', 'foobar');
+ b.addPhrase('en', 'scope.phrase2', 'foobar');
+ b.addPhrase('en', 'scope', {phrase3: 'foobar'});
+
+ it('allows specify phrase within `global` scope', function () {
+ expect(b.t('en', 'phrase1'))
+ .is.equal('foobar');
+ });
+
+ it('allows specify phrase prefixed with scope', function () {
+ expect(b.t('en', 'scope.phrase2'))
+ .is.equal('foobar');
+ });
+
+ it('allows specify translations as inner scope', function () {
+ expect(b.t('en', 'scope.phrase3'))
+ .is.equal('foobar');
+ });
+ });
+
+ describe('Getting compiled data', function () {
+ var b = BabelFish.create('en');
+
+ b.addPhrase('en', 'test.simple_string', 'test');
+ b.addPhrase('en', 'test.complex.variable', '-#{count}-');
+ b.addPhrase('en', 'test.complex.plurals', '-((foo|bar)):count-');
+ b.addPhrase('ru', 'test.complex.plurals', '-((ruu|bar)):count-');
+
+ it('data is a String when scope has no macros or variables', function () {
+ var compiled = b.getCompiledData('en', 'test.simple_string');
+
+ expect(compiled.type).is.equal('string');
+ expect(compiled.translation).is.equal('test');
+ });
+
+ // locale is needed on stage of locale recompiling (to override fallback
+ // translations if needed)
+ it('data has field with actual locale of translation', function () {
+ expect(b.getCompiledData('ru', 'test.simple_string').locale)
+ .is.equal('en');
+ expect(b.getCompiledData('ru', 'test.complex.variable').locale)
+ .is.equal('en');
+ expect(b.getCompiledData('ru', 'test.complex.plurals').locale)
+ .is.equal('ru');
+ });
+
+ it('data is a Function when scope has macros or variable', function () {
+ var data;
+ ['test.complex.plurals', 'test.complex.variable'].forEach(function (scope) {
+ data = b.getCompiledData('en', scope);
+ expect(data.type).is.equal('function');
+ expect(data.translation).to.be.a('function');
+ });
+ });
+
+ it('returns inner scope Object when locale only requested', function () {
+ var data = b.getCompiledData('ru');
+
+ expect(data).is.an('object');
+
+ expect(data).to.have.property('test.simple_string');
+ expect(data).to.have.property('test.complex.variable');
+
+ expect(data['test.simple_string'].type)
+ .is.equal('string');
+ expect(data['test.complex.variable'].type)
+ .is.equal('function');
+ });
+ });
+
+
+ describe('Translating a phrase', function () {
+ var b = BabelFish.create('en');
+
+ b.addPhrase('en', 'a', 'a (en)');
+ b.addPhrase('en', 'b', 'b (en)');
+ b.addPhrase('en', 'c', 'c (en) ((one|other)):count');
+ b.addPhrase('fr', 'd', 'd (fr) ((une|autre)):count');
+ b.addPhrase('ru', 'b', 'b (ru) #{foo}');
+ b.addPhrase('es', 'b', 'b (es) #{f.o}');
+
+
+ it('always returns a string', function () {
+ expect(b.t('en', 'a')).is.equal('a (en)');
+ expect(b.t('en', 'b')).is.equal('b (en)');
+ expect(b.t('ru', 'b', {foo: 'bar'})).is.equal('b (ru) bar');
+ });
+
+ it('ignores provided params when they are not needed', function () {
+ expect(b.t('en', 'b', {foo: 'bar', bar: 'baz'}))
+ .is.equal('b (en)');
+ });
+
+ it('replaces missing params with [missed variable: <name>]', function () {
+ expect(b.t('ru', 'b'))
+ .is.equal('b (ru) [missed variable: foo]');
+ expect(b.t('es', 'b'))
+ .is.equal('b (es) [missed variable: f.o]');
+ });
+
+ it('honors objects in params', function () {
+ expect(b.t('es', 'b', {f: {o: 'bar'}}))
+ .is.equal('b (es) bar');
+ });
+
+ it('reports missing translation', function () {
+ expect(b.t('en', 'd', {count: 0}))
+ .is.equal('en: No translation for [d]');
+ });
+
+ it('honors pluralization', function () {
+ expect(b.t('en', 'c', {count: 0}))
+ .is.equal('c (en) other');
+ expect(b.t('en', 'c', {count: 1}))
+ .is.equal('c (en) one');
+ expect(b.t('en', 'c', {count: 2}))
+ .is.equal('c (en) other');
+ expect(b.t('fr', 'c', {count: 0}))
+ .is.equal('c (en) other');
+
+ // check that we use correct pluralizer
+ expect(b.t('en', 'c', {count: 1}))
+ .is.equal('c (en) one');
+ expect(b.t('en', 'c', {count: 1.5}))
+ .is.equal('c (en) other');
+ expect(b.t('fr', 'd', {count: 0}))
+ .is.equal('d (fr) une');
+ expect(b.t('fr', 'd', {count: 1.5}))
+ .is.equal('d (fr) une');
+ });
-function hasFunction(func) {
- return function (i18n) {
- Assert.isFunction(i18n[func], 'has ' + func + ' function');
- };
-}
-
-
-function hasAlias(alias, original) {
- return function (i18n) {
- Assert.ok(i18n[original] === i18n[alias],
- alias + ' is alias of ' + original);
- };
-}
-
-
-function hasProperty(prop) {
- return function (i18n) {
- Assert.include(i18n, prop, 'has ' + prop + ' property');
- Assert.isFalse('function' === typeof i18n[prop],
- prop + ' is a scalar or getter');
- };
-}
-
-
-require('vows').describe('BabelFish').addBatch({
- // API consistence tests
- 'Exported module': {
- 'is a constructor': function () {
- Assert.isFunction(BabelFish);
- Assert.instanceOf(new BabelFish(), BabelFish);
- },
-
- 'has `create` (constructor proxy)': function () {
- Assert.isFunction(BabelFish.create);
- Assert.equal(BabelFish.create.length, BabelFish.length);
- Assert.instanceOf(BabelFish.create(), BabelFish);
- }
- },
-
- 'Instance': {
- topic: new BabelFish(),
- 'has `addPhrase()` method' : hasFunction('addPhrase'),
- 'has `getCompiledData()` method' : hasFunction('getCompiledData'),
- 'has `setFallback()` method' : hasFunction('setFallback'),
- 'has `translate()` method' : hasFunction('translate'),
- 'has `t()` aliase' : hasAlias('t', 'translate'),
- 'has `defaultLocale` property' : hasProperty('defaultLocale'),
-
- '`defaultLocale` property is read-only': function (i18n) {
- Assert.throws(function () { i18n.defaultLocale = 'ru'; }, TypeError);
- Assert.throws(function () { delete i18n.defaultLocale; }, TypeError);
- }
- },
-
- 'New instance with defaults': {
- topic: function () { return BabelFish.create(); },
- 'has defaultLocale = `en`': function (i18n) {
- Assert.equal(i18n.defaultLocale, 'en', 'defaultLocale is en');
- }
- },
-
- 'New instance with defaultLocale given': {
- topic: function () { return BabelFish.create('ru'); },
- 'has defaultLocale equal to the specified one': function (i18n) {
- Assert.equal(i18n.defaultLocale, 'ru', 'defaultLocale is ru');
- }
- }
-}).addBatch({
- // Behavior and unit tests come here
- 'When fallback is given': {
- topic: function () {
- var i18n = BabelFish.create('en');
-
- i18n.setFallback('es', ['es-ES', 'es-MX']);
- i18n.setFallback('es-ES', ['es', 'es-US']);
-
- i18n.addPhrase('en', 'aaa', 'aaa (en)');
- i18n.addPhrase('en', 'bbb', 'bbb (en)');
- i18n.addPhrase('en', 'ccc', 'ccc (en)');
- i18n.addPhrase('en', 'ddd', 'ddd (en)');
- i18n.addPhrase('es', 'aaa', 'aaa (es)');
- i18n.addPhrase('es-ES', 'bbb', 'bbb (es-ES)');
- i18n.addPhrase('es-MX', 'ccc', 'ccc (es-MX)');
- i18n.addPhrase('es-US', 'ddd', 'ddd (es-US)');
-
- i18n.setFallback('es-US', ['es']);
-
- return i18n;
- },
-
- 'use defaultLocale in worst case': function (i18n) {
- Assert.equal(i18n.t('es', 'ddd'), 'ddd (en)');
- Assert.equal(i18n.t('ru', 'ddd'), 'ddd (en)');
- },
-
- 'allows specify more than one fallback locale': function (i18n) {
- Assert.equal(i18n.t('es', 'aaa'), 'aaa (es)');
- Assert.equal(i18n.t('es', 'bbb'), 'bbb (es-ES)');
- Assert.equal(i18n.t('es', 'ccc'), 'ccc (es-MX)');
- Assert.equal(i18n.t('es', 'ddd'), 'ddd (en)');
- },
-
- 'do not recursively resolve locale fallbacks': function (i18n) {
- Assert.equal(i18n.t('es-ES', 'aaa'), 'aaa (es)');
- Assert.equal(i18n.t('es-ES', 'bbb'), 'bbb (es-ES)');
- Assert.equal(i18n.t('es-ES', 'ccc'), 'ccc (en)');
- Assert.equal(i18n.t('es-ES', 'ddd'), 'ddd (es-US)');
- },
-
- 'allow specify fallbacks after phrases were added': function (i18n) {
- Assert.equal(i18n.t('es-US', 'aaa'), 'aaa (es)');
- Assert.equal(i18n.t('es-US', 'bbb'), 'bbb (en)');
- Assert.equal(i18n.t('es-US', 'ccc'), 'ccc (en)');
- Assert.equal(i18n.t('es-US', 'ddd'), 'ddd (es-US)');
- },
-
- 'allows re-assign fallbacks': function (i18n) {
- i18n.setFallback('es-US', ['es-ES', 'es-MX']);
-
- Assert.equal(i18n.t('es', 'aaa'), 'aaa (es)');
- Assert.equal(i18n.t('es', 'bbb'), 'bbb (es-ES)');
- Assert.equal(i18n.t('es', 'ccc'), 'ccc (es-MX)');
- Assert.equal(i18n.t('es', 'ddd'), 'ddd (en)');
- }
- },
-
- 'Setting fallback for defaultLocale': {
- topic: function () {
- return BabelFish.create('en');
- },
- 'cause exception': function (i18n) {
- Assert.throws(function () { i18n.setFallback('en', ['en-GB']); }, Error);
- }
- },
-
- 'Adding phrases': {
- topic: function () {
- var i18n = BabelFish.create('en');
-
- i18n.addPhrase('en', 'phrase1', 'foobar');
- i18n.addPhrase('en', 'scope.phrase2', 'foobar');
- i18n.addPhrase('en', 'scope', {phrase3: 'foobar'});
-
- return i18n;
- },
-
- 'allows specify phrase within `global` scope': function (i18n) {
- Assert.equal(i18n.t('en', 'phrase1'), 'foobar');
- },
-
- 'allows specify phrase prefixed with scope': function (i18n) {
- Assert.equal(i18n.t('en', 'scope.phrase2'), 'foobar');
- },
-
- 'allows specify translations as inner scope': function (i18n) {
- Assert.equal(i18n.t('en', 'scope.phrase3'), 'foobar');
- }
- },
-
- 'Getting compiled data': {
- topic: function () {
- var i18n = BabelFish.create('en');
-
- i18n.addPhrase('en', 'test.simple_string', 'test');
- i18n.addPhrase('en', 'test.complex.variable', '-#{count}-');
- i18n.addPhrase('en', 'test.complex.plurals', '-((foo|bar)):count-');
- i18n.addPhrase('ru', 'test.complex.plurals', '-((ruu|bar)):count-');
-
- return i18n;
- },
-
- 'data is a String when scope has no macros or variables': function (i18n) {
- var translation = i18n.getCompiledData('en', 'test.simple_string');
-
- Assert.equal(translation.type, 'string');
- Assert.equal(translation.translation, 'test');
- },
-
- // locale is needed on stage of locale recompiling (to override fallback
- // translations if needed)
- 'data has field with actual locale of translation': function (i18n) {
- Assert.equal(i18n.getCompiledData('ru', 'test.simple_string').locale, 'en');
- Assert.equal(i18n.getCompiledData('ru', 'test.complex.variable').locale, 'en');
- Assert.equal(i18n.getCompiledData('ru', 'test.complex.plurals').locale, 'ru');
- },
-
- 'data is a Function when scope has macros or variable': function (i18n) {
- ['test.complex.plurals', 'test.complex.variable'].forEach(function (scope) {
- var data = i18n.getCompiledData('en', scope);
- Assert.equal(data.type, 'function', 'type of ' + scope + ' data is function');
- Assert.instanceOf(data.translation, Function, 'value of ' + scope + ' data is Function');
+ it('replaces invalid plurals amount with [invalid plurals amount: <name>(<value>)]', function () {
+ expect(b.t('en', 'c'))
+ .is.equal('c (en) [invalid plurals amount: count(undefined)]');
+ expect(b.t('en', 'c', {count: null}))
+ .is.equal('c (en) [invalid plurals amount: count(null)]');
+ expect(b.t('en', 'c', {count: "foo"}))
+ .is.equal('c (en) [invalid plurals amount: count(foo)]');
});
- },
-
- 'returns inner scope Object when locale only requested': function (i18n) {
- var data = i18n.getCompiledData('ru');
-
- Assert.typeOf(data, 'object');
-
- Assert.include(data, 'test.simple_string');
- Assert.include(data, 'test.complex.variable');
-
- Assert.equal(data['test.simple_string'].type, 'string');
- Assert.equal(data['test.complex.variable'].type, 'function');
- }
- },
-
- 'Translating a phrase': {
- topic: function () {
- var i18n = BabelFish.create('en');
-
- i18n.addPhrase('en', 'a', 'a (en)');
- i18n.addPhrase('en', 'b', 'b (en)');
- i18n.addPhrase('en', 'c', 'c (en) ((one|other)):count');
- i18n.addPhrase('fr', 'd', 'd (fr) ((une|autre)):count');
- i18n.addPhrase('ru', 'b', 'b (ru) #{foo}');
- i18n.addPhrase('es', 'b', 'b (es) #{f.o}');
-
- return i18n;
- },
-
- 'always returns a string': function (i18n) {
- Assert.equal(i18n.t('en', 'a'), 'a (en)');
- Assert.equal(i18n.t('en', 'b'), 'b (en)');
- Assert.equal(i18n.t('ru', 'b', {foo: 'bar'}), 'b (ru) bar');
- },
-
- 'ignores provided params when they are not needed': function (i18n) {
- Assert.equal(i18n.t('en', 'b', {foo: 'bar', bar: 'baz'}), 'b (en)');
- },
-
- 'replaces missing params with [missed variable: <name>]': function (i18n) {
- Assert.equal(i18n.t('ru', 'b'), 'b (ru) [missed variable: foo]');
- Assert.equal(i18n.t('es', 'b'), 'b (es) [missed variable: f.o]');
- },
-
- 'honors objects in params': function (i18n) {
- Assert.equal(i18n.t('es', 'b', {f: {o: 'bar'}}), 'b (es) bar');
- },
-
- 'reports missing translation': function (i18n) {
- Assert.equal(i18n.t('en', 'd', {count: 0}), 'en: No translation for [d]');
- },
-
- 'honors pluralization': function (i18n) {
- Assert.equal(i18n.t('en', 'c', {count: 0}), 'c (en) other');
- Assert.equal(i18n.t('en', 'c', {count: 1}), 'c (en) one');
- Assert.equal(i18n.t('en', 'c', {count: 2}), 'c (en) other');
- Assert.equal(i18n.t('fr', 'c', {count: 0}), 'c (en) other');
-
- // check that we use correct pluralizer
- Assert.equal(i18n.t('en', 'c', {count: 1}), 'c (en) one');
- Assert.equal(i18n.t('en', 'c', {count: 1.5}), 'c (en) other');
- Assert.equal(i18n.t('fr', 'd', {count: 0}), 'd (fr) une');
- Assert.equal(i18n.t('fr', 'd', {count: 1.5}), 'd (fr) une');
- },
-
- 'replaces invalid plurals amount with [invalid plurals amount: <name>(<value>)]': function (i18n) {
- Assert.equal(i18n.t('en', 'c'), 'c (en) [invalid plurals amount: count(undefined)]');
- Assert.equal(i18n.t('en', 'c', {count: null}), 'c (en) [invalid plurals amount: count(null)]');
- Assert.equal(i18n.t('en', 'c', {count: "foo"}), 'c (en) [invalid plurals amount: count(foo)]');
- }
- }
-}).export(module);
+ });
+ });
+});
View
37 test/issues-test.js
@@ -1,37 +0,0 @@
-'use strict';
-
-
-var Fs = require('fs');
-var Path = require('path');
-
-
-var ISSUES_DIR = Path.join(__dirname, 'issues');
-
-
-var re = /[.]js$/;
-var issues = {};
-Fs.readdirSync(ISSUES_DIR).forEach(function (f) {
- var data;
-
- if (!re.test(f)) {
- // skip non-js files
- return;
- }
-
- data = require(Path.join(ISSUES_DIR, f));
-
- if (data.pending) {
- issues[data.title] = 'pending';
- return;
- }
-
- issues[data.title] = function () {
- data.test();
- if (!data.fixed) {
- throw "Test passed, but it shouldn't!";
- }
- };
-});
-
-
-require('vows').describe('Issues').addBatch(issues).export(module);
View
21 test/issues/issue-3.js
@@ -1,18 +1,17 @@
+/*global it, describe*/
+
'use strict';
-var Assert = require('assert');
+var expect = require('chai').expect;
var BabelFish = require('../..');
-module.exports = {
- title: "#3: Compilation fails.",
- fixed: true,
- test: function () {
- var i18n = new BabelFish();
+describe('issue 3', function () {
+ it('Compilation fails', function () {
+ var b = new BabelFish();
- Assert.doesNotThrow(function () {
- i18n.addPhrase('en', 'test', 'foo #(bar) baz\n');
- }, SyntaxError);
- }
-};
+ expect(function () { b.addPhrase('en', 'test', 'foo #(bar) baz\n'); })
+ .to.not.throw(SyntaxError);
+ });
+});
View
23 test/issues/issue-7.js
@@ -1,16 +1,19 @@
+/*global it, describe*/
+
'use strict';
-var Assert = require('assert');
+var expect = require('chai').expect;
var BabelFish = require('../..');
-module.exports = {
- title: '#7: Numeric 0 in #{variable} should became "0"',
- fixed: true,
- test: function () {
- var i18n = new BabelFish('ru');
- i18n.addPhrase('ru', 'n', '#{n}');
- Assert.equal(i18n.translate('ru', 'n', {n: 0}), '0');
- }
-};
+describe('issue 7', function () {
+ it('Numeric 0 in #{variable} should became "0"', function () {
+ var b = new BabelFish('ru');
+
+ b.addPhrase('ru', 'n', '#{n}');
+
+ expect(b.translate('ru', 'n', {n: 0}))
+ .is.equal('0');
+ });
+});
View
2 test/mocha.opts
@@ -0,0 +1,2 @@
+-R spec
+--recursive
View
168 test/parser-test.js
@@ -1,7 +1,9 @@
+/*global it, describe*/
+
'use strict';
-var Underscore = require('underscore');
+var _ = require('underscore');
var Assert = require('assert');
var Parser = require('../lib/babelfish/parser');
@@ -29,7 +31,7 @@ function PluralNode(anchor, forms) {
function redistribute_ast(ast) {
var nodes = [], last = {};
- Underscore.each(ast, function (node) {
+ _.each(ast, function (node) {
if ('literal' === last.type && 'literal' === node.type) {
last.text += node.text;
return;
@@ -79,83 +81,85 @@ function testParsedNodes(definitions) {
}
-require('vows').describe('BabelFish.Parser').addBatch({
- 'Parsing strings': testParsedNodes({
- 'Simple string }{ with \b brackets and \t special chars': [
- new LiteralNode('Simple string }{ with \b brackets and \t special chars')
- ],
-
- 'Quirky #{} #{1} #{ } (()):foo ((|)) (( )):bar mess': [
- new LiteralNode('Quirky #{} #{1} #{ } (()):foo ((|)) (( )):bar mess')
- ],
-
- 'String with simple #{variable}...': [
- new LiteralNode('String with simple '),
- new VariableNode('variable'),
- new LiteralNode('...')
- ],
-
- 'String with complex #{foo.bar.baz} variable': [
- new LiteralNode('String with complex '),
- new VariableNode('foo.bar.baz'),
- new LiteralNode(' variable')
- ],
-
- 'String with plurals ((a|b)):c': [
- new LiteralNode('String with plurals '),
- new PluralNode('c', ['a', 'b'])
- ],
-
- 'Plurals with ((a\\)b\\|c\\(d|e)):myvar, escaping': [
- new LiteralNode('Plurals with '),
- new PluralNode('myvar', ['a)b|c(d', 'e']),
- new LiteralNode(', escaping')
- ],
-
- 'Plurals with ((a|b)):_compl3x.$variable.': [
- new LiteralNode('Plurals with '),
- new PluralNode('_compl3x.$variable', ['a', 'b']),
- new LiteralNode('.')
- ],
-
- 'Plurals with empty (()):myvar forms': [
- new LiteralNode('Plurals with empty (()):myvar forms')
- ],
-
- 'Plurals with single ((abc)):$myvar forms': [
- new LiteralNode('Plurals with single '),
- new PluralNode('$myvar', ['abc']),
- new LiteralNode(' forms')
- ],
-
- 'Plurals with lots of forms ((b|c|d|e|f|g|h)):a': [
- new LiteralNode('Plurals with lots of forms '),
- new PluralNode('a', ['b', 'c', 'd', 'e', 'f', 'g', 'h'])
- ],
-
- 'Escape \\((a|b)):plurals and \\#{variables}': [
- new LiteralNode('Escape ((a|b)):plurals and #{variables}')
- ],
-
- 'Invalid variable #{n..e}': [
- new LiteralNode('Invalid variable #{n..e}')
- ],
-
- 'Escape backslash ((a\\\\|b)):c': [
- new LiteralNode('Escape backslash '),
- new PluralNode('c', ['a\\', 'b'])
- ],
-
- 'Automagically set ((anchor|to|count)) when plural have no anchor': [
- new LiteralNode('Automagically set '),
- new PluralNode('count', ['anchor', 'to', 'count']),
- new LiteralNode(' when plural have no anchor')
- ],
-
- 'Treat ((trailing|semicolumn)): literally and use automagic anchor': [
- new LiteralNode('Treat '),
- new PluralNode('count', ['trailing', 'semicolumn']),
- new LiteralNode(': literally and use automagic anchor')
- ]
- })
-}).export(module);
+describe('BabelFish.Parser', function () {
+ it('Parsing strings', function () {
+ testParsedNodes({
+ 'Simple string }{ with \b brackets and \t special chars': [
+ new LiteralNode('Simple string }{ with \b brackets and \t special chars')
+ ],
+
+ 'Quirky #{} #{1} #{ } (()):foo ((|)) (( )):bar mess': [
+ new LiteralNode('Quirky #{} #{1} #{ } (()):foo ((|)) (( )):bar mess')
+ ],
+
+ 'String with simple #{variable}...': [
+ new LiteralNode('String with simple '),
+ new VariableNode('variable'),
+ new LiteralNode('...')
+ ],
+
+ 'String with complex #{foo.bar.baz} variable': [
+ new LiteralNode('String with complex '),
+ new VariableNode('foo.bar.baz'),
+ new LiteralNode(' variable')
+ ],
+
+ 'String with plurals ((a|b)):c': [
+ new LiteralNode('String with plurals '),
+ new PluralNode('c', ['a', 'b'])
+ ],
+
+ 'Plurals with ((a\\)b\\|c\\(d|e)):myvar, escaping': [
+ new LiteralNode('Plurals with '),
+ new PluralNode('myvar', ['a)b|c(d', 'e']),
+ new LiteralNode(', escaping')
+ ],
+
+ 'Plurals with ((a|b)):_compl3x.$variable.': [
+ new LiteralNode('Plurals with '),
+ new PluralNode('_compl3x.$variable', ['a', 'b']),
+ new LiteralNode('.')
+ ],
+
+ 'Plurals with empty (()):myvar forms': [
+ new LiteralNode('Plurals with empty (()):myvar forms')
+ ],
+
+ 'Plurals with single ((abc)):$myvar forms': [
+ new LiteralNode('Plurals with single '),
+ new PluralNode('$myvar', ['abc']),
+ new LiteralNode(' forms')
+ ],
+
+ 'Plurals with lots of forms ((b|c|d|e|f|g|h)):a': [
+ new LiteralNode('Plurals with lots of forms '),
+ new PluralNode('a', ['b', 'c', 'd', 'e', 'f', 'g', 'h'])
+ ],
+
+ 'Escape \\((a|b)):plurals and \\#{variables}': [
+ new LiteralNode('Escape ((a|b)):plurals and #{variables}')
+ ],
+
+ 'Invalid variable #{n..e}': [
+ new LiteralNode('Invalid variable #{n..e}')
+ ],
+
+ 'Escape backslash ((a\\\\|b)):c': [
+ new LiteralNode('Escape backslash '),
+ new PluralNode('c', ['a\\', 'b'])
+ ],
+
+ 'Automagically set ((anchor|to|count)) when plural have no anchor': [
+ new LiteralNode('Automagically set '),
+ new PluralNode('count', ['anchor', 'to', 'count']),
+ new LiteralNode(' when plural have no anchor')
+ ],
+
+ 'Treat ((trailing|semicolumn)): literally and use automagic anchor': [
+ new LiteralNode('Treat '),
+ new PluralNode('count', ['trailing', 'semicolumn']),
+ new LiteralNode(': literally and use automagic anchor')
+ ]
+ });
+ });
+});
View
521 test/pluralizer-test.js
@@ -1,3 +1,5 @@
+/*global it, describe*/
+
'use strict';
@@ -28,240 +30,285 @@ function testPluralizarionRules(definition) {
return tests;
}
-require('vows').describe('BabelFish.Pluralizer').addBatch({
-
- 'Azerbaijani': testPluralizarionRules({
- langs: ['az'],
- forms: ['other'],
- count: {
- other: [0, 1.5, 2, 2.5, 10]
- }
- }),
-
- 'Manx': testPluralizarionRules({
- langs: ['gv'],
- forms: ['one', 'other'],
- count: {
- one: [0, 1, 2, 11, 12, 20, 21, 22],
- other: [0.1, 1.4, 3, 4, 5.6, 10, 13, 19, 23, 24, 30]
- }
- }),
-
- 'Central Morocco Tamazight': testPluralizarionRules({
- langs: ['tzm'],
- forms: ['one', 'other'],
- count: {
- one: [0, 1, 11, 12, 99],
- other: [0.1, 1.4, 2, 3.5, 10, 100, 120.4]
- }
- }),
-
- 'Macedonian': testPluralizarionRules({
- langs: ['mk'],
- forms: ['one', 'other'],
- count: {
- one: [1, 21, 31, 41, 51, 61],
- other: [0, 0.1, 2, 3.5, 10, 11, 12, 20, 22]
- }
- }),
-
- 'Filipino': testPluralizarionRules({
- langs: ['fil'],
- forms: ['one', 'other'],
- count: {
- one: [0, 1],
- other: [0.1, 1.3, 2, 3, 3.5, 5, 7]
- }
- }),
-
- 'English': testPluralizarionRules({
- langs: ['en'],
- forms: ['one', 'other'],
- count: {
- one: [1],
- other: [0, 1.5, 2, 10]
- }
- }),
-
- 'Latvian': testPluralizarionRules({
- langs: ['lv'],
- forms: ['zero', 'one', 'other'],
- count: {
- zero: [0],
- one: [1, 21, 31, 41, 101],
- other: [0.1, 1.1, 2, 2.5, 3, 11, 100, 111, 120]
- }
- }),
-
- 'Colognian': testPluralizarionRules({
- langs: ['ksh'],
- forms: ['zero', 'one', 'other'],
- count: {
- zero: [0],
- one: [1],
- other: [0.1, 1.1, 2, 2.5, 3]
- }
- }),
-
- 'Irish': testPluralizarionRules({
- langs: ['ga'],
- forms: ['one', 'two', 'other'],
- count: {
- one: [1],
- two: [2],
- other: [0, 0.1, 1.1, 2.5, 3]
- }
- }),
-
- 'Russian': testPluralizarionRules({
- langs: ['ru'],
- forms: ['one', 'few', 'many', 'other'],
- count: {
- one: [1, 21, 31],
- few: [2, 22, 32],
- many: [0, 5, 20],
- other: [1.05, 1.1, 1.2]
- }
- }),
-
- 'Polish': testPluralizarionRules({
- langs: ['pl'],
- forms: ['one', 'few', 'many', 'other'],
- count: {
- one: [1],
- few: [2, 3, 4, 22, 23, 24, 32, 33, 34],
- many: [0, 5, 6, 10, 11, 12, 14, 20, 21, 25, 30, 31, 112],
- other: [1.05, 1.1, 1.2]
- }
- }),
-
- 'Lithuanian': testPluralizarionRules({
- langs: ['lt'],
- forms: ['one', 'few', 'other'],
- count: {
- one: [1, 21, 31, 41, 51, 61],
- few: [2, 3, 4, 9, 22, 23, 29, 32, 39],
- other: [1.2, 2.07, 10.94, 0, 10, 11, 20, 30, 40, 50]
- }
- }),
-
- 'Tachelhit': testPluralizarionRules({
- langs: ['shi'],
- forms: ['one', 'few', 'other'],
- count: {
- one: [0, 0.5, 1],
- few : [2, 3, 10],
- other: [1.5, 2.5, 11, 11.5, 12]
- }
- }),
-
- 'Moldavian': testPluralizarionRules({
- langs: ['mo'],
- forms: ['one', 'few', 'other'],
- count: {
- one: [1],
- few: [0, 2, 3, 10, 19, 101, 111, 119, 201, 211, 219],
- other: [20, 21, 99, 100, 120, 121, 200, 1.2, 2.07, 20.94]
- }
- }),
-
- 'Czech': testPluralizarionRules({
- langs: ['cs'],
- forms: ['one', 'few', 'other'],
- count: {
- one: [1],
- few: [2, 3, 4],
- other: [0, 5, 999, 1.2, 2.07, 5.94]
- }
- }),
-
- 'Slovenian': testPluralizarionRules({
- langs: ['sl'],
- forms: ['one', 'two', 'few', 'other'],
- count: {
- one: [1, 101, 201, 301, 401, 501],
- two: [2, 102, 202, 302, 402, 502],
- few: [3, 4, 103, 104, 203, 204],
- other: [0, 5, 100, 105, 200, 205, 300, 1.2, 2.07, 3.94, 5.81]
- }
- }),
-
- 'Maltese': testPluralizarionRules({
- langs: ['mt'],
- forms: ['one', 'few', 'many', 'other'],
- count: {
- one: [1],
- few: [0, 2, 3, 4, 10, 102, 110, 202, 210],
- many: [11, 12, 13, 19, 111, 119, 211, 219],
- other: [20, 21, 30, 100, 101, 120, 201, 220, 301, 1.2, 2.07, 11.94, 20.81]
- }
- }),
-
- 'Arabic': testPluralizarionRules({
- langs: ['ar'],
- forms: ['zero', 'one', 'two', 'few', 'many', 'other'],
- count: {
- zero: [0],
- one: [1],
- two: [2],
- few: [3, 4, 10, 103, 104, 110, 203, 204, 210],
- many: [11, 12, 13, 20, 99, 111, 112, 120, 199, 211, 299],
- other: [100, 101, 102, 200, 201, 202, 300, 301, 302, 0.2, 1.07, 2.94, 3.81, 11.68, 100.55]
- }
- }),
-
- 'Breton': testPluralizarionRules({
- langs: ['br'],
- forms: ['zero', 'one', 'two', 'few', 'many', 'other'],
- count: {
- zero: [0],
- one: [1],
- two: [2],
- few: [3],
- many: [6],
- other: [4, 5, 7, 10, 990, 0.2, 1.07, 2.94, 3.81, 6.68, 4.55]
- }
- }),
-
- //
- // Fractional mutations
- //
-
- 'French': testPluralizarionRules({
- langs: ['fr'],
- forms: ['one', 'other'],
- count: {
- one: [0, 0.5, 1, 1.5],
- other: [2, 2.5, 3, 10]
- }
- }),
-
- 'Langi': testPluralizarionRules({
- langs: ['lag'],
- forms: ['zero', 'one', 'other'],
- count: {
- zero: [0],
- one: [0.5, 1, 1.5],
- other: [2, 2.5, 3, 10]
- }
- }),
-
-}).addBatch({
- 'With unknown language': {
- topic: function () {
- return pluralize('unknown', 1, ['one']);
- },
- 'returns error message': function (result) {
- Assert.equal(result, '[pluralizer for (unknown) not exists]');
- }
- },
-
- 'With insufficient plural form': {
- topic: function () {
- return pluralize('ru', 1.1, ['one', 'few', 'many']);
- },
- 'returns error message': function (result) {
- Assert.equal(result, '[plural form N3 not found in translation]');
- }
- }
-}).export(module);
+describe('BabelFish.Pluralizer', function () {
+
+
+ describe('Languages', function () {
+
+ it('Azerbaijani', function () {
+ testPluralizarionRules({
+ langs: ['az'],
+ forms: ['other'],
+ count: {
+ other: [0, 1.5, 2, 2.5, 10]
+ }
+ });
+ });
+
+ it('Manx', function () {
+ testPluralizarionRules({
+ langs: ['gv'],
+ forms: ['one', 'other'],
+ count: {
+ one: [0, 1, 2, 11, 12, 20, 21, 22],
+ other: [0.1, 1.4, 3, 4, 5.6, 10, 13, 19, 23, 24, 30]
+ }
+ });
+ });
+
+ it('Central Morocco Tamazight', function () {
+ testPluralizarionRules({
+ langs: ['tzm'],
+ forms: ['one', 'other'],
+ count: {
+ one: [0, 1, 11, 12, 99],
+ other: [0.1, 1.4, 2, 3.5, 10, 100, 120.4]
+ }
+ });
+ });
+
+ it('Macedonian', function () {
+ testPluralizarionRules({
+ langs: ['mk'],
+ forms: ['one', 'other'],
+ count: {
+ one: [1, 21, 31, 41, 51, 61],
+ other: [0, 0.1, 2, 3.5, 10, 11, 12, 20, 22]
+ }
+ });
+ });
+
+ it('Filipino', function () {
+ testPluralizarionRules({
+ langs: ['fil'],
+ forms: ['one', 'other'],
+ count: {
+ one: [0, 1],
+ other: [0.1, 1.3, 2, 3, 3.5, 5, 7]
+ }
+ });
+ });
+
+ it('English', function () {
+ testPluralizarionRules({
+ langs: ['en'],
+ forms: ['one', 'other'],
+ count: {
+ one: [1],
+ other: [0, 1.5, 2, 10]
+ }
+ });
+ });
+
+ it('Latvian', function () {
+ testPluralizarionRules({
+ langs: ['lv'],
+ forms: ['zero', 'one', 'other'],
+ count: {
+ zero: [0],
+ one: [1, 21, 31, 41, 101],
+ other: [0.1, 1.1, 2, 2.5, 3, 11, 100, 111, 120]
+ }
+ });
+ });
+
+ it('Colognian', function () {
+ testPluralizarionRules({
+ langs: ['ksh'],
+ forms: ['zero', 'one', 'other'],
+ count: {
+ zero: [0],
+ one: [1],
+ other: [0.1, 1.1, 2, 2.5, 3]
+ }
+ });
+ });
+
+ it('Irish', function () {
+ testPluralizarionRules({
+ langs: ['ga'],
+ forms: ['one', 'two', 'other'],
+ count: {
+ one: [1],
+ two: [2],
+ other: [0, 0.1, 1.1, 2.5, 3]
+ }
+ });
+ });
+
+ it('Russian', function () {
+ testPluralizarionRules({
+ langs: ['ru'],
+ forms: ['one', 'few', 'many', 'other'],
+ count: {
+ one: [1, 21, 31],
+ few: [2, 22, 32],
+ many: [0, 5, 20],
+ other: [1.05, 1.1, 1.2]
+ }
+ });
+ });
+
+ it('Polish', function () {
+ testPluralizarionRules({
+ langs: ['pl'],
+ forms: ['one', 'few', 'many', 'other'],
+ count: {
+ one: [1],
+ few: [2, 3, 4, 22, 23, 24, 32, 33, 34],
+ many: [0, 5, 6, 10, 11, 12, 14, 20, 21, 25, 30, 31, 112],
+ other: [1.05, 1.1, 1.2]
+ }
+ });
+ });
+
+ it('Lithuanian', function () {
+ testPluralizarionRules({
+ langs: ['lt'],
+ forms: ['one', 'few', 'other'],
+ count: {
+ one: [1, 21, 31, 41, 51, 61],
+ few: [2, 3, 4, 9, 22, 23, 29, 32, 39],
+ other: [1.2, 2.07, 10.94, 0, 10, 11, 20, 30, 40, 50]
+ }
+ });
+ });
+
+ it('Tachelhit', function () {
+ testPluralizarionRules({
+ langs: ['shi'],
+ forms: ['one', 'few', 'other'],
+ count: {
+ one: [0, 0.5, 1],
+ few : [2, 3, 10],
+ other: [1.5, 2.5, 11, 11.5, 12]
+ }
+ });
+ });
+
+ it('Moldavian', function () {
+ testPluralizarionRules({
+ langs: ['mo'],
+ forms: ['one', 'few', 'other'],
+ count: {
+ one: [1],
+ few: [0, 2, 3, 10, 19, 101, 111, 119, 201, 211, 219],
+ other: [20, 21, 99, 100, 120, 121, 200, 1.2, 2.07, 20.94]
+ }
+ });
+ });
+
+ it('Czech', function () {
+ testPluralizarionRules({
+ langs: ['cs'],
+ forms: ['one', 'few', 'other'],
+ count: {
+ one: [1],
+ few: [2, 3, 4],
+ other: [0, 5, 999, 1.2, 2.07, 5.94]
+ }
+ });
+ });
+
+ it('Slovenian', function () {
+ testPluralizarionRules({
+ langs: ['sl'],
+ forms: ['one', 'two', 'few', 'other'],
+ count: {
+ one: [1, 101, 201, 301, 401, 501],
+ two: [2, 102, 202, 302, 402, 502],
+ few: [3, 4, 103, 104, 203, 204],
+ other: [0, 5, 100, 105, 200, 205, 300, 1.2, 2.07, 3.94, 5.81]
+ }
+ });
+ });
+
+ it('Maltese', function () {
+ testPluralizarionRules({
+ langs: ['mt'],
+ forms: ['one', 'few', 'many', 'other'],
+ count: {
+ one: [1],
+ few: [0, 2, 3, 4, 10, 102, 110, 202, 210],
+ many: [11, 12, 13, 19, 111, 119, 211, 219],
+ other: [20, 21, 30, 100, 101, 120, 201, 220, 301, 1.2, 2.07, 11.94, 20.81]
+ }
+ });
+ });
+
+ it('Arabic', function () {
+ testPluralizarionRules({
+ langs: ['ar'],
+ forms: ['zero', 'one', 'two', 'few', 'many', 'other'],
+ count: {
+ zero: [0],
+ one: [1],
+ two: [2],
+ few: [3, 4, 10, 103, 104, 110, 203, 204, 210],
+ many: [11, 12, 13, 20, 99, 111, 112, 120, 199, 211, 299],
+ other: [100, 101, 102, 200, 201, 202, 300, 301, 302, 0.2, 1.07, 2.94, 3.81, 11.68, 100.55]
+ }
+ });
+ });
+
+ it('Breton', function () {
+ testPluralizarionRules({
+ langs: ['br'],
+ forms: ['zero', 'one', 'two', 'few', 'many', 'other'],
+ count: {
+ zero: [0],
+ one: [1],
+ two: [2],
+ few: [3],
+ many: [6],
+ other: [4, 5, 7, 10, 990, 0.2, 1.07, 2.94, 3.81, 6.68, 4.55]
+ }
+ });
+ });
+
+ //
+ // Fractional mutations
+ //
+
+ it('French', function () {
+ testPluralizarionRules({
+ langs: ['fr'],
+ forms: ['one', 'other'],
+ count: {
+ one: [0, 0.5, 1, 1.5],
+ other: [2, 2.5, 3, 10]
+ }
+ });
+ });
+
+ it('Langi', function () {
+ testPluralizarionRules({
+ langs: ['lag'],
+ forms: ['zero', 'one', 'other'],
+ count: {
+ zero: [0],
+ one: [0.5, 1, 1.5],
+ other: [2, 2.5, 3, 10]
+ }
+ });
+ });
+ });
+
+
+ describe('Wrong params', function () {
+
+ it('Returns error message with unknown language', function () {
+ Assert.equal(
+ pluralize('unknown', 1, ['one']),
+ '[pluralizer for (unknown) not exists]'
+ );
+ });
+
+ it('Returns error message With insufficient plural form', function () {
+ Assert.equal(
+ pluralize('ru', 1.1, ['one', 'few', 'many']),
+ '[plural form N3 not found in translation]'
+ );
+ });
+ });
+});

0 comments on commit 3e0133d

Please sign in to comment.
Something went wrong with that request. Please try again.