Permalink
Browse files

- added pluck in to mvc.Collection

- fix sorting on models passed in at creation
- added in all_tests.html
- fixed mvc.Router
- tests for mvc.Router
  • Loading branch information...
1 parent d0c9856 commit 91eab3b9de9940a98ea6645c174192659394c17d @rhysbrettbowen committed Feb 22, 2012
Showing with 259 additions and 20 deletions.
  1. +8 −0 README.md
  2. +113 −0 all_tests.html
  3. +17 −0 alltests.js
  4. +44 −11 collection.js
  5. +9 −0 collection_test.js
  6. +1 −1 control_test.html
  7. +7 −6 router.js
  8. +15 −0 router_test.html
  9. +26 −0 router_test.js
  10. +1 −0 testApp/testApp.html
  11. +15 −1 testApp/testApp.js
  12. +3 −1 test_deps.js
View
@@ -64,6 +64,14 @@ mvc.Router uses goog.History and hash tokens to hold and manage the state of the
### changelog ###
+#### v0.4 ####
+
+- added pluck in to mvc.Collection
+- fix sorting on models passed in at creation
+- added in all_tests.html
+- fixed mvc.Router
+- tests for mvc.Router_
+
#### v0.3 ####
- add dispose() to mvc.Model
View
@@ -0,0 +1,113 @@
+<!DOCTYPE html>
+<html>
+<!--
+Copyright 2009 The Closure Library Authors. All Rights Reserved.
+
+Use of this source code is governed by the Apache License, Version 2.0.
+See the COPYING file for details.
+-->
+<head>
+<title>Closure - All JsUnit Tests</title>
+<script src="closure-library/closure/goog/base.js"></script>
+<script src="alltests.js"></script>
+<script>
+goog.require('goog.userAgent.product');
+goog.require('goog.testing.MultiTestRunner');
+</script>
+<link rel="stylesheet" href="closure-library/closure/goog/css/multitestrunner.css" type="text/css">
+<style>
+h1 {
+ font: normal x-large arial, helvetica, sans-serif;
+ margin: 0;
+}
+p, form {
+ font: normal small sans-serif;
+ margin: 0;
+}
+#header {
+ position: absolute;
+ right: 10px;
+ top: 13px;
+}
+#footer {
+ margin-top: 8px;
+}
+a {
+ text-decoration: none;
+}
+a:hover {
+ text-decoration: underline;
+}
+.warning {
+ font-size: 14px;
+ font-weight: bold;
+ width: 80%;
+}
+</style>
+</head>
+<body>
+
+<script>
+ if (goog.userAgent.product.CHROME &&
+ window.location.toString().indexOf('file:') == 0) {
+ document.write(
+ '<div class="warning">' +
+ 'WARNING: Due to Chrome\'s security restrictions ' +
+ 'this test will not be able to load files off your local disk ' +
+ 'unless you start Chrome with:<br>' +
+ '<code>--allow-file-access-from-files</code></div><br>');
+ }
+</script>
+
+<h1>Closure - All JsUnit Tests</h1>
+<p id="header">
+ <a href="http://wiki/Main/ClosureUnitTests">Closure JS Testing HOWTO</a>
+</p>
+<div id="runner"></div>
+<!-- Use a form so browser persists input values -->
+<form id="footer" onsubmit="return false">
+ Settings:<br>
+ <input type="checkbox" name="hidepasses" id="hidepasses" checked>
+ <label for="hidepasses">Hide passes</label><br>
+ <input type="checkbox" name="parallel" id="parallel" checked>
+ <label for="parallel">Run in parallel</label>
+ <small>(timing stats not available if enabled)</small><br>
+ <input type="text" name="filter" id="filter" value="">
+ <label for="filter">Run only tests for path</label>
+</form>
+<script>
+ var hidePassesInput = document.getElementById('hidepasses');
+ var parallelInput = document.getElementById('parallel');
+ var filterInput = document.getElementById('filter');
+
+ function setFilterFunction() {
+ var matchValue = filterInput.value || '';
+ testRunner.setFilterFunction(function(testPath) {
+ return testPath.indexOf(matchValue) > -1;
+ });
+ }
+
+ // Create a test runner and render it.
+ var testRunner = new goog.testing.MultiTestRunner()
+ .setName(document.title)
+ .setBasePath('./')
+ .setPoolSize(parallelInput.checked ? 8 : 1)
+ .setStatsBucketSizes(5, 500)
+ .setHidePasses(hidePassesInput.checked)
+ //.setVerbosePasses(true)
+ .addTests(_allTests);
+ testRunner.render(document.getElementById('runner'));
+
+ goog.events.listen(hidePassesInput, 'click', function(e) {
+ testRunner.setHidePasses(e.target.checked);
+ });
+
+ goog.events.listen(parallelInput, 'click', function(e) {
+ testRunner.setPoolSize(e.target.checked ? 8 : 1);
+ });
+
+ goog.events.listen(filterInput, 'keyup', setFilterFunction);
+ setFilterFunction();
+</script>
+</body>
+</html>
View
@@ -0,0 +1,17 @@
+// Copyright 2009 The Closure Library Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS-IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+var _allTests = [
+"collection_test.html", "control_test.html", "model_test.html", 'router_test.html'
+]
View
@@ -1,4 +1,4 @@
-// v0.2
+// v0.4
goog.provide('mvc.Collection');
goog.require('mvc.Model');
@@ -25,10 +25,11 @@ mvc.Collection = function(models, modelType) {
*/
this.comparator_ = null;
+
this.modelType_ = modelType;
goog.array.forEach(models || [], function(model) {
- this.add(model, true);
+ this.add(model, undefined, true);
}, this);
};
@@ -37,11 +38,35 @@ goog.inherits(mvc.Collection, mvc.Model);
/**
* @return {Array}
*/
-mvc.Collection.prototype.toJson = function() {
- return goog.array.map(this.models_, function(model) {return model.toJson();});
+mvc.Collection.prototype.toArray = function() {
+ return this.models_;
+};
+
+/**
+ * plucks an attribute from each model and returns as an array
+ *
+ * @param {string|Array} key
+ * @return {Array.<Object.<string, *>>|Array.<*>}
+ */
+mvc.Collection.prototype.pluck = function(key) {
+ return goog.array.map(this.models_, function(model) {
+ if(goog.isString(key))
+ return model.get(key);
+ return goog.array.reduce(key, function(map, attr) {
+ var val = model.get(attr);
+ if(goog.isDefAndNotNull(val));
+ goog.object.set(map, attr, val);
+ return map;
+ }, {});
+ });
};
/**
+ * @return {Array}
+ */
+
+
+/**
* function to sort models by
*
* @param {function(mvc.Model, mvc.Model):number} fn
@@ -93,14 +118,20 @@ mvc.Collection.prototype.add = function(model, ind, silent) {
goog.array.insertAt(this.models_, model, (ind || this.models_.length));
goog.events.listen(model, goog.events.EventType.CHANGE, this.sort,
false, this);
+ goog.events.listen(model, goog.events.EventType.UNLOAD,
+ function(e){
+ goog.array.remove(this.models_, e.target);
+ this.sort()
+ }, false, this);
this.sort(true);
if(!silent)
this.dispatchEvent(goog.events.EventType.CHANGE, model);
}
+ this.length = this.models_.length;
};
mvc.Collection.prototype.newModel = function(ind, silent) {
- var model = new this.modelType();
+ var model = new this.modelType_();
this.add(model, ind, silent);
return model;
};
@@ -112,14 +143,16 @@ mvc.Collection.prototype.newModel = function(ind, silent) {
mvc.Collection.prototype.remove = function(model, silent) {
if(goog.isArray(models)) {
goog.array.forEach(model, function(mod) {
- this.add(mod, silent);
+ this.remove(mod, silent);
}, this);
return;
}
- if(goog.array.remove(this.models_, model) && !silent)
- this.dispatchEvent(goog.events.EventType.CHANGE, model);
- if(this.comparator_)
- this.models_.sort(this.comparator_);
+ if(goog.array.remove(this.models_, model)) {
+ this.sort(true);
+ if(!silent)
+ this.dispatchEvent(goog.events.EventType.CHANGE);
+ }
+ this.length = this.models_.length;
};
/**
@@ -142,4 +175,4 @@ mvc.Collection.prototype.getById = function(id) {
*/
mvc.Collection.prototype.at = function(index) {
return this.models_[index<0?this.models_.length+index:index];
-}
+};
View
@@ -35,6 +35,15 @@ var testSortedCollection = function() {
assertEquals('third object should be mock 1', test.at(2), model1);
};
+var testNewSortedCollection = function() {
+ var sort = function(a, b) {return a.get('sort')-b.get('sort');};
+ var test = new mvc.Collection([model1,model2,model3]);
+ test.setComparator(sort);
+ assertEquals('first object should be mock 2', test.at(0), model2);
+ assertEquals('second object should be mock 3', test.at(1), model3);
+ assertEquals('third object should be mock 1', test.at(2), model1);
+};
+
var testAsModel = function() {
var test = new mvc.Collection();
test.set('a', 1);
View
@@ -1,7 +1,7 @@
<!doctype html>
<html>
<head>
- <title>Test for mvc.Model</title>
+ <title>Test for mvc.Control</title>
</head>
<body>
<div id="control"><div id="test1" class="class1"><div id="test2" class="class2"><span id="test3" class="class1"></span></div></div></div>
View
@@ -1,16 +1,17 @@
-// v0.1
+// v0.4
goog.provide('mvc.Router');
goog.require('goog.events');
-goog.require('goog.history');
+goog.require('goog.History');
/**
* @constructor
*/
mvc.Router = function() {
this.history_ = new goog.History();
- goog.events(this.history_, goog.history.EventType.NAVIGATE,
+ goog.events.listen(this.history_, goog.history.EventType.NAVIGATE,
this.onChange_, false, this);
+ this.history_.setEnabled(true);
this.routes_ = [];
};
@@ -32,13 +33,13 @@ mvc.Router.prototype.navigate = function(fragment) {
*/
mvc.Router.prototype.route = function(route, fn) {
if(goog.isString(route))
- var routeRE = new RegExp('^' + goog.string.regExpEscape(route).replace(/:\w+/g, '(\w+)').replace(/\*\w+/g, '(.*?)') + '$');
+ var route = new RegExp('^' + goog.string.regExpEscape(route).replace(/:\w+/g, '(\w+)').replace(/\*\w+/g, '(.*?)') + '$');
this.routes_.push({route: route, callback: fn});
}
-mvc.Router.prototype.onChange_ = function() {
+mvc.Router.prototype.onChange_ = function(e) {
var fragment = this.history_.getToken();
- goog.array.forEach(this.routes_, function(route) {
+ goog.array.forEach(this.routes_ || [], function(route) {
var args = route.route.exec(fragment);
if(!args)
return;
View
@@ -0,0 +1,15 @@
+<!doctype html>
+<html>
+<head>
+ <title>Test for mvc.Router</title>
+</head>
+<body>
+ <script src="closure-library/closure/goog/base.js"></script>
+ <script src="test_deps.js"></script>
+ <script>
+ goog.require('goog.testing.ContinuationTestCase');
+ goog.require('goog.testing.jsunit');
+ </script>
+ <script src="router_test.js"></script>
+</body>
+</html>
View
@@ -0,0 +1,26 @@
+goog.require('mvc.Router');
+
+
+
+var router;
+
+var setUp = function() {
+ router = new mvc.Router();
+}
+
+var testNavigation = function() {
+ router.navigate("testing");
+ loc = document.location.toString();
+ assertEquals(loc.replace(/.*#/,''), "testing");
+};
+
+var testRoute = function() {
+ var reached = false;
+ var a = function(){reached = true};
+ router.route("test", a);
+ router.navigate("test");
+ waitForEvent(router.history_, goog.history.EventType.NAVIGATE,
+ function() {
+ assert(reached);
+ });
+};
View
@@ -9,6 +9,7 @@
<script>
goog.require('goog.events');
goog.require('mvc.Model');
+ goog.require('mvc.Collection');
goog.require('mvc.sync.Local');
goog.require('mvc.model.Schema');
goog.require('mvc.Control');
Oops, something went wrong.

0 comments on commit 91eab3b

Please sign in to comment.