Permalink
Browse files

Setup and utilities

  • Loading branch information...
0 parents commit e0207dcc67f38641a942255522cd3a1a630e80ec @stevehorn committed Feb 27, 2011
@@ -0,0 +1,30 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+ "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+ <title>Javascript Koans</title>
+ <!-- CSS -->
+ <link rel="stylesheet" type="text/css" href="lib/jasmine/jskoans-jasmine.css">
+
+ <!-- JQuery -->
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
+
+ <!-- Jasmine -->
+ <script type="text/javascript" src="lib/jasmine/jasmine.js"></script>
+ <script type="text/javascript" src="lib/jasmine/jasmine-html.js"></script>
+ <script type="text/javascript" src="lib/jasmine/jskoans-jasmine-html.js"></script>
+ <script type="text/javascript" src="lib/jasmine/jasmine-jquery.js"></script>
+ <script type="text/javascript" src="lib/__.js"></script>
+
+ <!-- Koans -->
+ <script type="text/javascript" src="koans/AboutUtilities.js"></script>
+</head>
+<body>
+
+<script type="text/javascript">
+ jasmine.getEnv().addReporter(new JsKoansReporter());
+ jasmine.getEnv().execute();
+</script>
+
+</body>
+</html>
@@ -0,0 +1,3 @@
+<div id="1"></div>
+<div id="2"></div>
+<div id="3"></div>
@@ -0,0 +1,146 @@
+describe("About Utilities", function() {
+ beforeEach(function() {
+ loadFixtures('fixtures/AboutUtilitiesFixture.html');
+ });
+
+ it("should iterate an array with $.each", function() {
+ var result = 0;
+ var array = ["one", 2, null];
+
+ $.each(array, function(index, value) {
+ if(index === 1) {
+ result = value;
+ }
+ });
+
+ expect(__).toEqual(result);
+ });
+
+ it("should iterate an object literal with $.each", function() {
+ var object = { name: "Swedish Chef", age: 2.5 };
+
+ $.each(object, function(key, value) { var expectedValue = __;
+ var actualValue = 0;
+ if(key === "name") {
+ actualValue = value;
+ }
+ });
+
+ expect(__).toEqual(actualValue);
+ });
+
+ it("should merge two objects with $.extend", function() {
+ var object1 = { animal: "cardinal", color: "red" };
+ var object2 = { firstName: "woody", lastName: "woodpecker" };
+
+ var result = $.extend(false, object1, object2);
+
+ expect(__).toEqual(result.lastName);
+ });
+
+ it("should filter arrays with $.grep", function() {
+ var array = [1, 2, 3, 4, 5, 6, 7, 8];
+
+ array = $.grep(array, function(value, index) {
+ return value > 5;
+ });
+
+ expect(__).toEqual(array);
+ });
+
+ it("should find elements in array with $.inArray", function() {
+ var actualValue = 0;
+
+ var array = ["Pie", 42, "Cake", "BoogieMonster"];
+
+ var pieInArrayResult = $.inArray(array, "Pie");
+ var boogieMonsterInArrayResult = $.inArray(array, "BoogieMonster");
+ var notFoundInArrayResult = $.inArray(array, 98765);
+
+ expect(__).toEqual(pieInArrayResult);
+ expect(__).toEqual(boogieMonsterInArrayResult);
+ expect(__).toEqual(notFoundInArrayResult);
+ });
+ it("should determine types using $.type", function() {
+ var num = new Number(3);
+ var arr = [];
+
+ var type1 = $.type(num);
+ var type2 = $.type(arr);
+
+ expect(__).toEqual(type1);
+ expect(__).toEqual(type2);
+ });
+ it("should test for array type using $.isArray", function() {
+ var array = [1, "two", 3];
+ var object = { property: "value" };
+
+ var isArrayForArray = $.isArray(array);
+ var isArrayForObject = $.isArray(object);
+
+ expect(__).toEqual(isArrayForArray);
+ expect(__).toEqual(isArrayForObject);
+ });
+
+ it("should check for empty objects using $.isEmptyObject", function() {
+ var isEmpty = $.isEmptyObject({});
+ var isNotEmpty = $.isEmptyObject({name: "name", value: "value"});
+
+ expect(__).toEqual(isEmpty);
+ expect(__).toEqual(isNotEmpty);
+ });
+
+ it("should detect function types using $.isFunction", function() {
+ var object = {
+ name: "bob",
+ weight: 200,
+ getWeight: function() {
+ return weight;
+ }
+ };
+ '{"name":"John"}'
+ var isFunction1 = $.isFunction(object.name);
+ var isFunction2 = $.isFunction(object.getWeight);
+
+ expect(__).toEqual(isFunction1);
+ expect(__).toEqual(isFunction2);
+ });
+
+ it("should determine types using $.type", function() {
+ var num = new Number(3);
+ var arr = [];
+
+ var type1 = $.type(num);
+ var type2 = $.type(arr);
+
+ expect(__).toEqual(type1);
+ expect(__).toEqual(type2);
+ });
+
+ it("should perform translations on each array element returning a new array using $.map", function() {
+ var array = ["a", "b", "c", "d"];
+
+ var mapped = $.map(array, function(element, index) {
+ return element.toUpperCase();
+ });
+
+ expect(__).toEqual(mapped);
+ expect(__).toEqual($.type(mapped));
+ });
+
+ it("should parse json using $.parseJSON", function() {
+ var jsonString = '{"name":"animal", "age":2}'
+
+ var parsedJsonObject = $.parseJSON(jsonString);
+
+ expect(__).toEqual(parsedJsonObject.name);
+ expect(__).toEqual(parsedJsonObject.age);
+ });
+
+ it("should trim strings using $.trim", function() {
+ var string = " IE does not have a native implentation of trim, so this jquery method is handy ";
+
+ var trimmed = $.trim(string);
+ expect(__).toEqual(trimmed);
+ });
+});
@@ -0,0 +1 @@
+var __ = "Fill this value in";
@@ -0,0 +1,182 @@
+jasmine.TrivialReporter = function(doc) {
+ this.document = doc || document;
+ this.suiteDivs = {};
+ this.logRunningSpecs = false;
+};
+
+jasmine.TrivialReporter.prototype.createDom = function(type, attrs, childrenVarArgs) {
+ var el = document.createElement(type);
+
+ for (var i = 2; i < arguments.length; i++) {
+ var child = arguments[i];
+
+ if (typeof child === 'string') {
+ el.appendChild(document.createTextNode(child));
+ } else {
+ if (child) { el.appendChild(child); }
+ }
+ }
+
+ for (var attr in attrs) {
+ if (attr == "className") {
+ el[attr] = attrs[attr];
+ } else {
+ el.setAttribute(attr, attrs[attr]);
+ }
+ }
+
+ return el;
+};
+
+jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) {
+ var showPassed, showSkipped;
+
+ this.outerDiv = this.createDom('div', { className: 'jasmine_reporter' },
+ this.createDom('div', { className: 'banner' },
+ this.createDom('div', { className: 'logo' },
+ "Jasmine",
+ this.createDom('span', { className: 'version' }, runner.env.versionString())),
+ this.createDom('div', { className: 'options' },
+ "Show ",
+ showPassed = this.createDom('input', { id: "__jasmine_TrivialReporter_showPassed__", type: 'checkbox' }),
+ this.createDom('label', { "for": "__jasmine_TrivialReporter_showPassed__" }, " passed "),
+ showSkipped = this.createDom('input', { id: "__jasmine_TrivialReporter_showSkipped__", type: 'checkbox' }),
+ this.createDom('label', { "for": "__jasmine_TrivialReporter_showSkipped__" }, " skipped")
+ )
+ ),
+
+ this.runnerDiv = this.createDom('div', { className: 'runner running' },
+ this.createDom('a', { className: 'run_spec', href: '?' }, "run all"),
+ this.runnerMessageSpan = this.createDom('span', {}, "Running..."),
+ this.finishedAtSpan = this.createDom('span', { className: 'finished-at' }, ""))
+ );
+
+ this.document.body.appendChild(this.outerDiv);
+
+ var suites = runner.suites();
+ for (var i = 0; i < suites.length; i++) {
+ var suite = suites[i];
+ var suiteDiv = this.createDom('div', { className: 'suite' },
+ this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, "run"),
+ this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, suite.description));
+ this.suiteDivs[suite.id] = suiteDiv;
+ var parentDiv = this.outerDiv;
+ if (suite.parentSuite) {
+ parentDiv = this.suiteDivs[suite.parentSuite.id];
+ }
+ parentDiv.appendChild(suiteDiv);
+ }
+
+ this.startedAt = new Date();
+
+ var self = this;
+ showPassed.onchange = function(evt) {
+ if (evt.target.checked) {
+ self.outerDiv.className += ' show-passed';
+ } else {
+ self.outerDiv.className = self.outerDiv.className.replace(/ show-passed/, '');
+ }
+ };
+
+ showSkipped.onchange = function(evt) {
+ if (evt.target.checked) {
+ self.outerDiv.className += ' show-skipped';
+ } else {
+ self.outerDiv.className = self.outerDiv.className.replace(/ show-skipped/, '');
+ }
+ };
+};
+
+jasmine.TrivialReporter.prototype.reportRunnerResults = function(runner) {
+ var results = runner.results();
+ var className = (results.failedCount > 0) ? "runner failed" : "runner passed";
+ this.runnerDiv.setAttribute("class", className);
+ //do it twice for IE
+ this.runnerDiv.setAttribute("className", className);
+ var specs = runner.specs();
+ var specCount = 0;
+ for (var i = 0; i < specs.length; i++) {
+ if (this.specFilter(specs[i])) {
+ specCount++;
+ }
+ }
+ var message = "" + specCount + " spec" + (specCount == 1 ? "" : "s" ) + ", " + results.failedCount + " failure" + ((results.failedCount == 1) ? "" : "s");
+ message += " in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s";
+ this.runnerMessageSpan.replaceChild(this.createDom('a', { className: 'description', href: '?'}, message), this.runnerMessageSpan.firstChild);
+
+ this.finishedAtSpan.appendChild(document.createTextNode("Finished at " + new Date().toString()));
+};
+
+jasmine.TrivialReporter.prototype.reportSuiteResults = function(suite) {
+ var results = suite.results();
+ var status = results.passed() ? 'passed' : 'failed';
+ if (results.totalCount == 0) { // todo: change this to check results.skipped
+ status = 'skipped';
+ }
+ this.suiteDivs[suite.id].className += " " + status;
+};
+
+jasmine.TrivialReporter.prototype.reportSpecStarting = function(spec) {
+ if (this.logRunningSpecs) {
+ this.log('>> Jasmine Running ' + spec.suite.description + ' ' + spec.description + '...');
+ }
+};
+
+jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) {
+ var results = spec.results();
+ var status = results.passed() ? 'passed' : 'failed';
+ if (results.skipped) {
+ status = 'skipped';
+ }
+ var specDiv = this.createDom('div', { className: 'spec ' + status },
+ this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "run"),
+ this.createDom('a', {
+ className: 'description',
+ href: '?spec=' + encodeURIComponent(spec.getFullName()),
+ title: spec.getFullName()
+ }, spec.description));
+
+
+ var resultItems = results.getItems();
+ var messagesDiv = this.createDom('div', { className: 'messages' });
+ for (var i = 0; i < resultItems.length; i++) {
+ var result = resultItems[i];
+
+ if (result.type == 'log') {
+ messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage log'}, result.toString()));
+ } else if (result.type == 'expect' && result.passed && !result.passed()) {
+ messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage fail'}, result.message));
+
+ if (result.trace.stack) {
+ messagesDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack));
+ }
+ }
+ }
+
+ if (messagesDiv.childNodes.length > 0) {
+ specDiv.appendChild(messagesDiv);
+ }
+
+ this.suiteDivs[spec.suite.id].appendChild(specDiv);
+};
+
+jasmine.TrivialReporter.prototype.log = function() {
+ var console = jasmine.getGlobal().console;
+ if (console && console.log) console.log.apply(console, arguments);
+};
+
+jasmine.TrivialReporter.prototype.getLocation = function() {
+ return this.document.location;
+};
+
+jasmine.TrivialReporter.prototype.specFilter = function(spec) {
+ var paramMap = {};
+ var params = this.getLocation().search.substring(1).split('&');
+ for (var i = 0; i < params.length; i++) {
+ var p = params[i].split('=');
+ paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
+ }
+
+ if (!paramMap["spec"]) return true;
+ return spec.getFullName().indexOf(paramMap["spec"]) == 0;
+};
Oops, something went wrong.

0 comments on commit e0207dc

Please sign in to comment.