Browse files

Couple of tests added.

  • Loading branch information...
1 parent 245c270 commit f477127ceb962183c56a1b381762139b141a49e9 @dfsq committed Aug 9, 2012
Showing with 185 additions and 3 deletions.
  1. +5 −3 ashe.js
  2. +1 −0 ashe.modifiers.js
  3. +53 −0 test/SpecRunner.html
  4. +91 −0 test/spec/asheSpec.js
  5. +35 −0 test/spec/specHelper.js
View
8 ashe.js
@@ -3,6 +3,8 @@
*/
(function() {
+ "use strict";
+
var uid, tokens,
/**
@@ -11,10 +13,10 @@
* @param {Object} data Data for template.
*/
process = function(str, data) {
- return str.replace(/{_(\d+?)}/g, function(a, b) {
- var token = tokens[b];
+ return str.replace(/\{_(\d+?)\}/g, function(a, b) {
+ var token = tokens[b], repl;
if (!token.expr) {
- var repl = evl(data, tokens[b].buffer);
+ repl = evl(data, tokens[b].buffer);
if (token.modif.length) {
for (var i in token.modif) {
var modif = token.modif[i],
View
1 ashe.modifiers.js
@@ -1,6 +1,7 @@
/**
* List of Ashe modifiers.
*/
+var Ashe = Ashe || {};
Ashe.addModifiers({
/**
* Convert value to uppercase.
View
53 test/SpecRunner.html
@@ -0,0 +1,53 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+ "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+ <title>Jasmine Spec Runner</title>
+
+ <link rel="shortcut icon" type="image/png" href="lib/jasmine-1.2.0/jasmine_favicon.png">
+ <link rel="stylesheet" type="text/css" href="lib/jasmine-1.2.0/jasmine.css">
+ <script type="text/javascript" src="lib/jasmine-1.2.0/jasmine.js"></script>
+ <script type="text/javascript" src="lib/jasmine-1.2.0/jasmine-html.js"></script>
+
+ <!-- include source files here... -->
+ <script src="spec/specHelper.js"></script>
+ <script type="text/javascript" src="spec/asheSpec.js"></script>
+
+ <!-- include spec files here... -->
+ <script type="text/javascript" src="../ashe.js"></script>
+ <script type="text/javascript" src="../ashe.modifiers.js"></script>
+
+ <script type="text/javascript">
+ (function() {
+ var jasmineEnv = jasmine.getEnv();
+ jasmineEnv.updateInterval = 1000;
+
+ var htmlReporter = new jasmine.HtmlReporter();
+
+ jasmineEnv.addReporter(htmlReporter);
+
+ jasmineEnv.specFilter = function(spec) {
+ return htmlReporter.specFilter(spec);
+ };
+
+ var currentWindowOnload = window.onload;
+
+ window.onload = function() {
+ if (currentWindowOnload) {
+ currentWindowOnload();
+ }
+ execJasmine();
+ };
+
+ function execJasmine() {
+ jasmineEnv.execute();
+ }
+
+ })();
+ </script>
+
+</head>
+
+<body>
+</body>
+</html>
View
91 test/spec/asheSpec.js
@@ -0,0 +1,91 @@
+/**
+ * Generals Ashe tests.
+ */
+describe('General Tests', function() {
+
+ // Test Ashe is globally available
+ it('Ashe should be defined as window.Ashe', function() {
+ expect(window.Ashe).toBeDefined();
+ });
+
+ // Test Ashe has public methods
+ it('should have public method "parse"', function() {
+ expect(typeof Ashe.parse == 'function').toBe(true);
+ });
+
+ it('should have public method "addModifiers"', function() {
+ expect(typeof Ashe.addModifiers == 'function').toBe(true);
+ });
+
+ it('should have property "modifiers"', function() {
+ expect(typeof Ashe.modifiers == 'object').toBe(true);
+ });
+});
+
+/**
+ * Test rendering functionality.
+ */
+describe("Render variables", function() {
+ it("Simple variable {{simpleVar}}", function() {
+ var out = Ashe.parse("This is a test {{simpleVar}}", {simpleVar: "string"});
+ expect(out).toEqual("This is a test string");
+ });
+
+ it("Object property {{object.property}}", function() {
+ var out = Ashe.parse("This is an object {{object.property}}", {
+ object: {property: 'property'}
+ });
+ expect(out).toEqual("This is an object property");
+ });
+
+ it("Function as a variable {{someFunction}}", function() {
+ var out = Ashe.parse("This is a {{someFunction}} as variable", {
+ someFunction: function() {
+ return "function reference";
+ }
+ });
+ expect(out).toEqual("This is a function reference as variable");
+ });
+});
+
+describe("Render loops", function() {
+
+ var tpl;
+
+ beforeEach(function() {
+ tpl =
+ "<ul> \
+ {% for user in users %} \
+ <li>{{user.name}}, {{user.age}}</li> \
+ {% else %} \
+ <li>No users yet</li> \
+ {% endfor %} \
+ </ul>";
+ });
+
+ it("Simple non-empty loop", function() {
+ var out = Ashe.parse(tpl, {
+ users: [
+ {name: 'Thomas Mann', age: 26},
+ {name: 'Jassy Brook', age: 23}
+ ]
+ });
+ expect(out).htmlToBeEqual("<ul><li>Thomas Mann, 26</li><li>Jassy Brook, 23</li></ul>");
+ });
+
+ it("Empty loop with else block", function() {
+ var out = Ashe.parse(tpl, {users: []});
+ expect(out).htmlToBeEqual("<ul><li>No users yet</li></ul>");
+ });
+
+ it("Empty loop without else-block", function() {
+ var tpl =
+ "<ul> \
+ {% for user in users %} \
+ <li>{{user.name}}, {{user.age}}</li> \
+ {% endfor %} \
+ </ul>",
+ out = Ashe.parse(tpl, {});
+ expect(out).htmlToBeEqual('<ul></ul>');
+ })
+});
View
35 test/spec/specHelper.js
@@ -0,0 +1,35 @@
+var Util = (function() {
+
+ var __cleanWhitespace = function(node) {
+ for (var i = 0; i < node.childNodes.length; i++) {
+ var child = node.childNodes[i];
+ if (child.nodeType == 3 && !/\S/.test(child.nodeValue)) {
+ node.removeChild(child);
+ i--;
+ }
+ if (child.nodeType == 1) {
+ __cleanWhitespace(child);
+ }
+ }
+ return node;
+ };
+
+ return {
+ cleanWhitespace: __cleanWhitespace
+ }
+})();
+
+
+beforeEach(function() {
+ this.addMatchers({
+ htmlToBeEqual: function(expected) {
+ var actualObj = document.createElement('div');
+ actualObj.innerHTML = this.actual;
+
+ var expectedObj = document.createElement('div');
+ expectedObj.innerHTML = expected;
+
+ return Util.cleanWhitespace(actualObj).innerHTML == Util.cleanWhitespace(expectedObj).innerHTML;
+ }
+ });
+});

0 comments on commit f477127

Please sign in to comment.