Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Bug 1113023 - Remove the arity check for macros. r=gandalf
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Dec 30, 2014
1 parent faee3c8 commit 9dee8ca
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions bindings/l20n/buildtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ navigator.mozL10n.bootstrap = function(file, debug) {
ctx.addEventListener('fetcherror', error);
ctx.addEventListener('manifesterror', warn);
ctx.addEventListener('parseerror', warn);
ctx.addEventListener('resolveerror', warn);
ctx.addEventListener('notfounderror', error);
}

Expand Down
2 changes: 2 additions & 0 deletions bindings/l20n/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ if (DEBUG) {
console.error.bind(console));
navigator.mozL10n.ctx.addEventListener('parseerror',
console.error.bind(console));
navigator.mozL10n.ctx.addEventListener('resolveerror',
console.error.bind(console));
}

function getDirection(lang) {
Expand Down
10 changes: 2 additions & 8 deletions lib/l20n/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,8 @@ function resolveSelector(args, env, expr, index) {
return selector;
}

var argLength = index.length - 1;
if (selector.length !== argLength) {
throw new L10nError('Macro ' + selectorName + ' expects ' +
selector.length + ' argument(s), yet ' + argLength +
' given');
}

var argValue = resolveIdentifier(args, env, index[1]);
var argValue = index[1] ?
resolveIdentifier(args, env, index[1]) : undefined;

if (selector === env.__plural) {
// special cases for zero, one, two if they are defined on the hash
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/resolver/indexes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Index', function(){
it('throws when the index is an uncalled macro', function() {
assert.throws(function() {
Resolver.format({n: 1}, env.indexUncalledMacro);
}, 'Macro plural expects 1 argument(s), yet 0 given');
}, 'Unresolvable value');
});
it('works when the index is a called macro', function() {
var value = Resolver.format({n: 1}, env.indexCalledMacro);
Expand Down
56 changes: 56 additions & 0 deletions tests/lib/resolver/macros_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,56 @@ describe('Macros', function(){
describe('A simple plural macro', function(){
var source, env;

beforeEach(function() {
env = createEntries(source);
env.__plural = function() {
return 'other';
};
});

before(function() {
source = [
'foo={[ plural(n) ]}',
'foo[zero]=Zero',
'foo[one]=One',
'foo[two]=Two',
'foo[few]=Few',
'foo[many]=Many',
'foo[other]=Other'
].join('\n');
});

it('returns zero for 0', function() {
var value = Resolver.format({n: 0}, env.foo);
assert.strictEqual(value, 'Zero');
});

it('returns one for 1', function() {
var value = Resolver.format({n: 1}, env.foo);
assert.strictEqual(value, 'One');
});

it('returns two for 2', function() {
var value = Resolver.format({n: 2}, env.foo);
assert.strictEqual(value, 'Two');
});

it('returns other for 3', function() {
var value = Resolver.format({n: 3}, env.foo);
assert.strictEqual(value, 'Other');
});

it('throws for no arg', function() {
assert.throws(function() {
Resolver.format(null, env.foo);
}, 'Unknown reference: n');
});

});

describe('A more complex plural macro', function(){
var source, env;

beforeEach(function() {
env = createEntries(source);
env.__plural = function(n) {
Expand Down Expand Up @@ -166,6 +216,12 @@ describe('A simple plural macro', function(){
assert.strictEqual(value, 'Other');
});

it('throws for no arg', function() {
assert.throws(function() {
Resolver.format(null, env.foo);
}, 'Unknown reference: n');
});

});

describe('an entity without the zero, one and two forms', function(){
Expand Down

0 comments on commit 9dee8ca

Please sign in to comment.