Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Murdoch
committed
May 24, 2012
1 parent
eb4be7f
commit 5aa84c8
Showing
9 changed files
with
279 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
.DS_Store | ||
lib-cov | ||
testing | ||
node_modules | ||
node_modules/* | ||
.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>RazorJs</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
</buildSpec> | ||
<natures> | ||
<nature>com.aptana.projects.webnature</nature> | ||
</natures> | ||
</projectDescription> |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var testrunner = require("qunit"), | ||
walk = require("walk"); | ||
|
||
var walker = walk.walk("./test", {followLinks: false}), | ||
tests = []; | ||
walker.on("file", function(root, fileStats, next){ | ||
|
||
tests.push({ | ||
"code" : { | ||
"path" : "./lib/" + fileStats.name, | ||
"namespace": "testObject" | ||
}, | ||
"tests": "./test/" + fileStats.name | ||
}); | ||
|
||
next(); | ||
}); | ||
|
||
walker.on("end", function(){ | ||
|
||
testrunner.run(tests, function(err, report){ | ||
console.log(err); | ||
console.dir(report); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module = QUnit.module; | ||
|
||
var Html = testObject; | ||
var HtmlString = require("../lib/htmlstring.js"); | ||
|
||
module("Html"); | ||
|
||
test("test html.js functions", function() { | ||
expect(7); | ||
|
||
var string = "<string>"; | ||
var htmlstring = Html.Raw(string); | ||
ok(htmlstring instanceof HtmlString, "Html.Raw returns HtmlString"); | ||
|
||
equal(Html.Raw(), "", "Html.Raw returns empty string for null/undefined values"); | ||
|
||
equal(Html.Encode(string), "<string>", "Html.Encode(html) returns encoded HTML"); | ||
|
||
equal(Html.toString(string), "<string>", "Html.toString(html) returns escaped HTML"); | ||
|
||
equal(Html.toString(htmlstring), string, "Html.toString(HtmlString) returns raw HTML"); | ||
|
||
equal(Html.toString(undefined), "", "Html.toString(undefined) returns an empty string"); | ||
|
||
raises( function(){ Html.Encode( {} ) }, "Html.Encode throws if non-string is passed in" ); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module = QUnit.module; | ||
|
||
var HtmlString = testObject; | ||
|
||
module("HtmlString"); | ||
|
||
test("test htmlstring.js functions", function() { | ||
expect(2); | ||
var string = "<string>"; | ||
var htmlString = HtmlString(string); | ||
strictEqual(true, htmlString instanceof HtmlString, "HtmlString initialized without `new` returns a new HtmlString"); | ||
strictEqual(true, string === htmlString.toString(), "toString() returns original value"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module = QUnit.module; | ||
|
||
var Razor = testObject; | ||
|
||
module("Razor"); | ||
|
||
test("test razor.js functions", function() { | ||
expect(8); | ||
|
||
var compile, object = {}; | ||
|
||
try{ | ||
|
||
compile = Razor.compile("<div>@message</div>"); | ||
object = {"message":"hello world"}; | ||
equal(compile(object), "<div>hello world</div>", "Test simple compilation"); | ||
|
||
|
||
compile = Razor.compile("<ul>@for(var name in obj){<li>@obj[name]</li>}</ul>"); | ||
object = {"obj":{"foo":"foo", "bar":"bar"}}; | ||
equal(compile(object), "<ul><li>foo</li><li>bar</li></ul>", "Test for name in obj"); | ||
|
||
|
||
compile = Razor.compile("<div>@if(bool){<span>true</span>}</div>"); | ||
object = {"bool":true}; | ||
equal(compile(object), "<div><span>true</span></div>", "Test if(true)"); | ||
|
||
|
||
object.bool = false; | ||
equal(compile(object), "<div></div>", "Test if(false)"); | ||
|
||
|
||
compile = Razor.compile("<div>@Html.Raw(message)</div>"); | ||
object = {"message": "<goodbye cruel='world!'>\"Hi\""}; | ||
equal(compile(object), "<div><goodbye cruel='world!'>\"Hi\"</div>", "Test Html.Raw"); | ||
|
||
|
||
compile = Razor.compile("<ul>@while(--i){<li>@i}</ul>"); | ||
object = {"i": 6}; | ||
equal(compile(object), "<ul><li>5<li>4<li>3<li>2<li>1</ul>", "Test while()"); | ||
|
||
|
||
compile = Razor.compile("<div>\n" + | ||
"@{\n" + | ||
"<div>@message</div>\n" + | ||
"}\n" + | ||
"</div>"); | ||
object = {"message": "test"}; | ||
equal(compile(object), "<div>\n<div>test</div>\n</div>", "Test code block"); | ||
|
||
|
||
compile = Razor.compile("<div>\n" + | ||
"@{\n" + | ||
"<img src='@Html.Raw(src)' />\n" + | ||
"}\n" + | ||
"</div>"); | ||
object = {"src": "//localhost/img.png"}; | ||
equal(compile(object), "<div>\n<img src='//localhost/img.png' />\n</div>", "Test template with self-closing tag"); | ||
} | ||
catch(e){ | ||
console.log(compile.fn); | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module = QUnit.module; | ||
|
||
var rules = testObject; | ||
|
||
test("test rules.js functions", function() { | ||
expect(3); | ||
var value = rules.isReserved("continue"); | ||
strictEqual(true, value, "isReserved"); | ||
value = rules.isValidName("0"); | ||
strictEqual(false, value, "0 is not a valid name"); | ||
value = rules.isValidName("?_?"); | ||
strictEqual(true, value, "?_? is a valid name"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
module = QUnit.module; | ||
|
||
var Utils = testObject; | ||
|
||
test("Utils.extend(Object, Object)", function() { | ||
expect(26); | ||
|
||
function isArray(obj){ | ||
return toString.call(obj) === "[object Array]"; | ||
} | ||
|
||
var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, | ||
options = { xnumber2: 1, xstring2: "x", xxx: "newstring" }, | ||
optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" }, | ||
merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" }, | ||
deep1 = { foo: { bar: true } }, | ||
deep1copy = { foo: { bar: true } }, | ||
deep2 = { foo: { baz: true }, foo2: deep1 }, | ||
deep2copy = { foo: { baz: true }, foo2: deep1 }, | ||
deepmerged = { foo: { bar: true, baz: true }, foo2: deep1 }, | ||
arr = [1, 2, 3], | ||
nestedarray = { arr: arr }; | ||
|
||
Utils.extend(settings, options); | ||
deepEqual( settings, merged, "Check if extended: settings must be extended" ); | ||
deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" ); | ||
|
||
Utils.extend(settings, null, options); | ||
deepEqual( settings, merged, "Check if extended: settings must be extended" ); | ||
deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" ); | ||
|
||
Utils.extend(true, deep1, deep2); | ||
deepEqual( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" ); | ||
deepEqual( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" ); | ||
|
||
ok( Utils.extend(true, {}, nestedarray).arr !== arr, "Deep extend of object must clone child array" ); | ||
|
||
ok( isArray( Utils.extend(true, { arr: {} }, nestedarray).arr ), "Cloned array heve to be an Array" ); | ||
|
||
var empty = {}; | ||
var optionsWithLength = { foo: { length: -1 } }; | ||
Utils.extend(true, empty, optionsWithLength); | ||
deepEqual( empty.foo, optionsWithLength.foo, "The length property must copy correctly" ); | ||
|
||
empty = {}; | ||
var optionsWithDate = { foo: { date: new Date } }; | ||
Utils.extend(true, empty, optionsWithDate); | ||
deepEqual( empty.foo, optionsWithDate.foo, "Dates copy correctly" ); | ||
|
||
var myKlass = function() {}; | ||
var customObject = new myKlass(); | ||
var optionsWithCustomObject = { foo: { date: customObject } }; | ||
empty = {}; | ||
Utils.extend(true, empty, optionsWithCustomObject); | ||
ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" ); | ||
|
||
// Makes the class a little more realistic | ||
myKlass.prototype = { someMethod: function(){} }; | ||
empty = {}; | ||
Utils.extend(true, empty, optionsWithCustomObject); | ||
ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" ); | ||
|
||
var ret = Utils.extend(true, { foo: 4 }, { foo: new Number(5) } ); | ||
ok( ret.foo == 5, "Wrapped numbers copy correctly" ); | ||
|
||
var nullUndef; | ||
nullUndef = Utils.extend({}, options, { xnumber2: null }); | ||
ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied"); | ||
|
||
nullUndef = Utils.extend({}, options, { xnumber2: undefined }); | ||
ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied"); | ||
|
||
nullUndef = Utils.extend({}, options, { xnumber0: null }); | ||
ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted"); | ||
|
||
var target = {}; | ||
var recursive = { foo:target, bar:5 }; | ||
Utils.extend(true, target, recursive); | ||
deepEqual( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" ); | ||
|
||
var ret = Utils.extend(true, { foo: [] }, { foo: [0] } ); // 1907 | ||
equal( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" ); | ||
|
||
var ret = Utils.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } ); | ||
ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" ); | ||
|
||
var ret = Utils.extend(true, { foo:"bar" }, { foo:null } ); | ||
ok( typeof ret.foo !== "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" ); | ||
|
||
var obj = { foo:null }; | ||
Utils.extend(true, obj, { foo:"notnull" } ); | ||
equal( obj.foo, "notnull", "Make sure a null value can be overwritten" ); | ||
|
||
function func() {} | ||
Utils.extend(func, { key: "value" } ); | ||
equal( func.key, "value", "Verify a function can be extended" ); | ||
|
||
var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, | ||
defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, | ||
options1 = { xnumber2: 1, xstring2: "x" }, | ||
options1Copy = { xnumber2: 1, xstring2: "x" }, | ||
options2 = { xstring2: "xx", xxx: "newstringx" }, | ||
options2Copy = { xstring2: "xx", xxx: "newstringx" }, | ||
merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" }; | ||
|
||
var settings = Utils.extend({}, defaults, options1, options2); | ||
deepEqual( settings, merged2, "Check if extended: settings must be extended" ); | ||
deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" ); | ||
deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" ); | ||
deepEqual( options2, options2Copy, "Check if not modified: options2 must not be modified" ); | ||
}); | ||
|
||
|
||
test("Utils.escapeHTML", function() { | ||
expect(1); | ||
var string = "<string attribute=\"This is a 'test'\" />"; | ||
equals(Utils.escapeHTML(string), "<string attribute="This is a 'test'" />", "Utils.escapeHTML works"); | ||
}); | ||
|
||
test("Utils.RegExpEscape", function(){ | ||
expect(1); | ||
equals(Utils.RegExpEscape("/([te|st]).+?.{0,2}/"), "\\/\\(\\[te\\|st\\]\\)\\.\\+\\?\\.\\{0,2\\}\\/", "Regex string escaped successfully"); | ||
}); |