Skip to content

Commit

Permalink
WIP added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-g committed Oct 20, 2014
1 parent 2321a32 commit 3ccc97b
Show file tree
Hide file tree
Showing 19 changed files with 336 additions and 15 deletions.
60 changes: 46 additions & 14 deletions hsp/compiler/parser/hspblocks.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ TemplateStart "template statement"
var attMap = {}; // { attrib1 : ..., attrib2: ...}
var acceptedAttribs = ['id', 'args', 'ctrl', 'export', 'export-module'];
var code = "<template"; // to be used in error messages
// To be used in error messages.
// Note that we're rebuilding code from partials, so it may differ when compared with real code
// when it comes to spacing etc.
var code = "<template";
var templateId = null;
var modifier = null;
Expand All @@ -71,7 +74,7 @@ TemplateStart "template statement"
// att.errors foreach, line, column
var err = att.errors[k];
if (err.errorType == "name") {
errors.push("invalid attribute name: unexpected character '" + err.value + "'");
errors.push("invalid attribute name: unexpected character(s) found in '" + err.value + "'");
} else if (err.errorType = "tail") {
errors.push("unexpected character '" + err.tail + "' found at the end of attribute value");
} else {
Expand All @@ -86,12 +89,18 @@ TemplateStart "template statement"
var codeChunk = " " + att.name;
if (att.type == "attribute") {
if (att.value && att.value[0]) {
codeChunk += ('="' + att.value[0].value + '"');
// foo => ""
// foo="" => []
// foo="x" => [{"value":"x"}]
if (att.value) {
var valueOfAtt = att.value[0] ? att.value[0].value : "";
codeChunk += ('="' + valueOfAtt + '"');
}
code += codeChunk;
} else if (att.type == "tpl-arguments") {
code += " " + att.name + '="' + att.args.join(",")+ '"';
code += " " + att.name + '="' + att.args.join(",") + '"';
} else if (att.type == "tpl-controller") {
code += " " + att.name + '="' + att.controller.code + " as " + att.controllerRef + '"';
}
}
}
Expand Down Expand Up @@ -130,26 +139,43 @@ TemplateStart "template statement"
// instead of the specialized rules for maching 'args' / 'ctrl' (TemplateCtrlAttribute / TemplateArgsAttribute)
// => that means the value provided was invalid
if (attMap["args"]) {
var att = attMap["args"];
if (att.type === "tpl-arguments" && Array.isArray(att.args) && att.args.length === 0) {
// let's forbid empty args for consistency with ctrl;
// set appropriate type and value to have error handling consistent with that of ctrl
attMap["args"] = {
type : "attribute",
value : []
}
}
if (attMap["args"].type !== "tpl-arguments") {
var value = attMap["args"].value;
if (typeof value == "object") {
value = value[0].value;
} else {
if (value === "") {
value = "[empty value]";
} else if (Array.isArray(value) && value.length === 0) {
value = "[empty string]";
} else {
value = value[0].value;
}
errors.push("invalid value of 'args' attribute: " + value);
} else {
args = attMap["args"].args;
}
}
// ctrl => ""
// ctrl="" => []
// ctrl="x" => [{"value":"x"}]
if (attMap["ctrl"]) {
if (attMap["ctrl"].type !== "tpl-controller") {
var value = attMap["ctrl"].value;
if (typeof value == "object") {
value = value[0].value; // TODO value can be "" or [] or [0].value
} else {
if (value === "") {
value = "[empty value]";
} else if (Array.isArray(value) && value.length === 0) {
value = "[empty string]";
} else {
value = value[0].value;
}
errors.push("invalid value of 'ctrl' attribute: " + value);
} else {
Expand Down Expand Up @@ -374,9 +400,16 @@ HTMLAttribute
}
else {
var errors = [];
var code;
if (typeof attName.tail === "undefined" || attName.tail && attName.tail.length) {
errors.push({errorType: "name", value: attName.name + (attName.tail && attName.tail.length > 0?attName.tail.join(''):""), line: line(), column: column()});
var tailStr = (attName.tail && attName.tail.length > 0 ? attName.tail.join('') : "");
var nameStr = attName.name + tailStr;
errors.push({errorType: "name", value: nameStr, line: line(), column: column()});
code = nameStr;
} else {
code = attName.name;
}

if (!isValueValid) {
errors.push({errorType: "value", value: v.value});
}
Expand All @@ -386,8 +419,7 @@ HTMLAttribute
if (v2 != null && v2.value && v2.value.length > 0) {
errors.push({errorType: "invalidquotes", value: v2.value, line: v2.line, column: v2.column});
}
var code = attName.name;
if (v) {
if (v && v.value && v.value[0]) {
code += '="' + v.value[0].value + '"';
if (v.tail) {
code += v.tail.join('');
Expand Down
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-args-and-ctrl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="id" ctrl="MyCtrl as c" args="foo,bar">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="id" ctrl="MyCtrl as c" args="foo,bar">',
"suberrors" : [
"a template can not have both 'args' and 'ctrl' attributes"
]
}
]
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-args-syntax-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="id" args="foo,bar,#">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="id" args="foo,bar,#">',
"suberrors" : [
"invalid value of 'args' attribute: foo,bar,#"
]
}
]
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-args-syntax-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="id" args="foo,bar,33">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="id" args="foo,bar,33">',
"suberrors" : [
"invalid value of 'args' attribute: foo,bar,33"
]
}
]
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-args-syntax-3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="id" args="foo,bar,">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="id" args="foo,bar,">',
"suberrors" : [
"invalid value of 'args' attribute: foo,bar,"
]
}
]
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-args-syntax-4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="id" args="foo;bar">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="id" args="foo;bar">',
"suberrors" : [
"invalid value of 'args' attribute: foo;bar"
]
}
]
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-args-syntax-5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="id" args>
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="id" args>',
"suberrors" : [
"invalid value of 'args' attribute: [empty value]"
]
}
]
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-args-syntax-6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="id" args="">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="id" args="">',
"suberrors" : [
"invalid value of 'args' attribute: [empty string]"
]
}
]
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-bad-name.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="id" very#weird$Att#="xx">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="id" very#weird$Att#="xx">',
"suberrors" : [
"invalid attribute name: unexpected character(s) found in 'very#weird$Att#'"
]
}
]
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-ctrl-syntax-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="id" ctrl="this is not a good value">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id=\"id\" ctrl=\"this is not a good value\">',
"suberrors" : [
"invalid value of 'ctrl' attribute: this is not a good value"
]
}
]
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-ctrl-syntax-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="id" ctrl="">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="id" ctrl="">',
"suberrors" : [
"invalid value of 'ctrl' attribute: [empty string]"
]
}
]
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-ctrl-syntax-3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="id" ctrl>
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="id" ctrl>',
"suberrors" : [
"invalid value of 'ctrl' attribute: [empty value]"
]
}
]
17 changes: 17 additions & 0 deletions test/compiler/errsamples/template-attribs-duplicated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="test1" id="test2">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="test1" id="test2">',
"suberrors" : [
"duplicated template attribute: id"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="test" export export-module>
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="test" export export-module>',
"suberrors" : [
"a template can not have both 'export' and 'export-module' attributes"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="test" export-module="">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="test" export-module="">',
"suberrors" : [
"the 'export-module' attribute must not have a value"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##### Template:
<template id="test" export-module="bar">
test
</template>

##### Errors:
[
{
"description": "Invalid template declaration",
"line": 1,
"column": 1,
"code": '<template id="test" export-module="bar">',
"suberrors" : [
"the 'export-module' attribute must not have a value"
]
}
]

0 comments on commit 3ccc97b

Please sign in to comment.