Permalink
Browse files

update KhanTests to run tests on a js object with test parameters

  • Loading branch information...
1 parent b98f944 commit 39a805932949e61d67f59326950a506b21b622a1 @nsfmc nsfmc committed Sep 17, 2012
Showing with 56 additions and 16 deletions.
  1. +3 −4 test/index.html
  2. +32 −1 test/utils/unit-tests.js
  3. +21 −11 utils/test/angles.js
View
@@ -13,10 +13,9 @@
<script src="qunit/qunit/qunit.js"></script>
<script src="utils/unit-tests.js"></script>
<script type="text/javascript" charset="utf-8">
- var tests = {}
- files = ["math", "angles"];
- $.getScript("../utils/test/angles.js", function(){
- KhanTests.init(files)
+ KhanTests.run({
+ tests: "angles",
+ deps: ["math", "angles"]
})
</script>
View
@@ -1,6 +1,10 @@
var KhanTests = (function(){
+ // the tests object is populated by loadTest and contains the names
+ // of all the functions to run in the module you care about
+ var tests = {};
+
// set up bare bones Khan module
var Khan = {
"Util": {
@@ -73,9 +77,36 @@ var KhanTests = (function(){
})
}
+ // create a simple setter for loading in tests without having to deal with window
+ var loadTest = function(obj){
+ $.extend(tests, obj);
+ }
+
+ // a wrapper for loading tests
+ // wants {tests:"basepath", deps: ["basepath", "basepath"]}
+ // where for `tests`, basepath is located in /utils/test/basepath.js
+ // and for deps, the basepath is located in /utils/basepath.js
+ var setupTests = function(spec){
+ if(!spec){ return; }
+
+ // reset the tests object when starting a new test
+ tests = {};
+
+ var testPath = "../utils/test/" + spec.tests + ".js";
+ var deps = spec.deps;
+ $.ajax({
+ dataType: "script",
+ url: testPath,
+ success: function(){ init(deps); },
+ error: function(){ console.error(arguments); }
+ })
+ }
+
var exported = {
init: init,
- util: Khan.Util
+ util: Khan.Util,
+ run: setupTests,
+ loadTest: loadTest
};
return exported;
View
@@ -1,11 +1,21 @@
-$.extend(tests, {"angles":{
- toRadians: function(){
- equal(KhanUtil.toRadians(45), (45 * Math.PI / 180 ), "toRadians works for 45 Degrees")
- },
- toDegrees: function(){
- equal(KhanUtil.toDegrees(.5), (.5 * 180 / Math.PI), "toDegrees works for .5 Radians")
- },
- wrongCommonAngle: function(){},
- wrongDegrees: function(){},
- wrongRadians: function(){}
-}})
+// test for angles.js
+
+/* For each function in /utils/angles.js, there should be a corresponding function in
+ * this file. This function can actually contain any number of tests, it doesn't need
+ * to contain anything even if the function is not worth testing (huh!) but not including
+ * it will cause the whole suite to fail.
+ */
+
+KhanTests.loadTest({
+ "angles":{
+ toRadians: function(){
+ equal(KhanUtil.toRadians(45), (45 * Math.PI / 180 ), "toRadians works for 45 Degrees")
+ },
+ toDegrees: function(){
+ equal(KhanUtil.toDegrees(.5), (.5 * 180 / Math.PI), "toDegrees works for .5 Radians")
+ },
+ wrongCommonAngle: function(){},
+ wrongDegrees: function(){},
+ wrongRadians: function(){}
+ }
+})

0 comments on commit 39a8059

Please sign in to comment.