Skip to content

Commit

Permalink
fix: throw a descriptive error when an undefined attribute is used fo…
Browse files Browse the repository at this point in the history
…r a component

Fixes ariatemplates#253
  • Loading branch information
PK1A committed Jul 24, 2014
1 parent 4e0fa3b commit 710dfd4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
4 changes: 3 additions & 1 deletion hsp/rt/cptcomponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ exports.$CptComponent = {
for (var i = 0, sz = this.atts.length; sz > i; i++) {
att = atts[i];
nm = att.name;
if (this.ctlAttributes[nm].type!=="template") {
if (!this.ctlAttributes || !this.ctlAttributes[nm]) {
throw new Error('The attribute "' + nm + '" was used but the component doesn\'t define this attribute.');
} else if (this.ctlAttributes[nm].type!=="template") {
attributes[nm]=att.getValue(eh, pvs, null);
}
}
Expand Down
51 changes: 51 additions & 0 deletions test/rt/cpterrors.spec.hsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var klass=require("hsp/klass"),
ht=require("hsp/utils/hashtester");

var FooNoAttrsCtrl=klass({
});

var FooEmptyAttrsCtrl=klass({
attributes: {
}
});

{template fooNoAttrs using ctrl:FooNoAttrsCtrl}
<div>foo</div>
{/template}

{template fooEmptyAttrs using ctrl:FooEmptyAttrsCtrl}
<div>foo</div>
{/template}

{template test1}
<#fooNoAttrs foo="bar"></#fooNoAttrs>
{/template}

{template test2}
<#fooEmptyAttrs foo="bar"></#fooEmptyAttrs>
{/template}

describe('error reporting for custom components', function() {

var h;
beforeEach(function() {
h=ht.newTestContext();
});

it('attribute used for a component without attributes', function() {
expect(function() {
test1().render(h.container);
}).to.throwException(/The attribute "foo" was used but the component doesn't define this attribute./);
});

it('attribute used for a component without attributes', function() {
expect(function() {
test2().render(h.container);
} ).to.throwException(/The attribute "foo" was used but the component doesn't define this attribute./);
});

afterEach(function(){
h.$dispose();
});

});

0 comments on commit 710dfd4

Please sign in to comment.