From 86a3e64f8f73d854b3b4ac35f52b54bfda3056a4 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Sat, 31 Mar 2012 17:45:02 -0400 Subject: [PATCH] Add "test" script, which uses phantomjs for a pure command-line test. --- Makefile | 3 ++ auto_tests/README | 12 ++++++++ dygraph.js | 20 ++++++------- phantom-driver.js | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.sh | 12 ++++++++ 5 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 phantom-driver.js create mode 100755 test.sh diff --git a/Makefile b/Makefile index 2614ea3..6a645a5 100644 --- a/Makefile +++ b/Makefile @@ -15,3 +15,6 @@ gwt: generate-gwt generate-gwt: @echo Generating GWT JAR file @./generate-jar.sh + +test: + @./test.sh diff --git a/auto_tests/README b/auto_tests/README index e5ec593..d07f2e5 100644 --- a/auto_tests/README +++ b/auto_tests/README @@ -4,6 +4,18 @@ These tests are run with js-test-driver Running tests ------------- +With phantomjs: + +- Install phantomjs (http://www.phantomjs.org). + +- Start a terminal window at the dygraphs root directory (one + directory up from here.) + +- Run "./test.sh". This will tell you whether the tests passed. + + +With a real browser: + - Start a terminal window at the dygraphs root directory (one directory up from here.) diff --git a/dygraph.js b/dygraph.js index c07fe35..da2a11a 100644 --- a/dygraph.js +++ b/dygraph.js @@ -61,17 +61,15 @@ * whether the input data contains error ranges. For a complete list of * options, see http://dygraphs.com/options.html. */ -var Dygraph = function(div, data, opts) { - if (arguments.length > 0) { - if (arguments.length == 4) { - // Old versions of dygraphs took in the series labels as a constructor - // parameter. This doesn't make sense anymore, but it's easy to continue - // to support this usage. - this.warn("Using deprecated four-argument dygraph constructor"); - this.__old_init__(div, data, arguments[2], arguments[3]); - } else { - this.__init__(div, data, opts); - } +var Dygraph = function(div, data, opts, opt_fourth_param) { + if (opt_fourth_param !== undefined) { + // Old versions of dygraphs took in the series labels as a constructor + // parameter. This doesn't make sense anymore, but it's easy to continue + // to support this usage. + this.warn("Using deprecated four-argument dygraph constructor"); + this.__old_init__(div, data, opts, opt_fourth_param); + } else { + this.__init__(div, data, opts); } }; diff --git a/phantom-driver.js b/phantom-driver.js new file mode 100644 index 0000000..43e811c --- /dev/null +++ b/phantom-driver.js @@ -0,0 +1,89 @@ +var page = require('webpage').create(); + +// NOTE: Cannot include '#' or '?' in this URL. +var url = 'auto_tests/misc/local.html'; + +// NOTE: changing the line below to this: +// page.open(url, function(status)) { +// makes phantomjs hang. +page.open(url, function(status) { + if (status !== 'success') { + console.warn('Page status: ' + status); + console.log(page); + phantom.exit(); + } + + var testCase, test; + if (phantom.args.length == 1) { + var parts = phantom.args[0].split('.'); + if (2 != parts.length) { + console.warn('Usage: phantomjs phantom-driver.js [testCase.test]'); + phantom.exit(); + } + testCase = parts[0]; + test = parts[1]; + } + + var loggingOn = false; + page.onConsoleMessage = function (msg) { + if (msg == 'Running ' + test) { + loggingOn = true; + } else if (msg.substr(0, 'Running'.length) == 'Running') { + loggingOn = false; + } + if (loggingOn) console.log(msg); + }; + + page.onError = function (msg, trace) { + console.log(msg); + trace.forEach(function(item) { + console.log(' ', item.file, ':', item.line); + }) + }; + + var results; + + // Run all tests. + results = page.evaluate(function() { + // Phantom doesn't like stacktrace.js using the "arguments" object + // in stacktrace.js, which it interprets in strict mode. + printStackTrace = undefined; + + var testCases = getAllTestCases(); + var results = {}; + for (var idx in testCases) { + var entry = testCases[idx]; + + var prototype = entry.testCase; + var tc = new entry.testCase(); + var result = tc.runAllTests(); + results[entry.name] = result; + } + return results; + }); + + var num_passing = 0, num_failing = 0; + for (var testCase in results) { + var caseResults = results[testCase]; + for (var test in caseResults) { + if (caseResults[test] !== true) { + // TODO(danvk): print an auto_test/misc/local URL that runs this test. + num_failing++; + console.log(testCase + '.' + test + ' failed'); + } else { + // console.log(testCase + '.' + test + ' passed'); + num_passing++; + } + } + } + console.log(num_passing + ' test(s) passed'); + console.log(num_failing + ' test(s) failed'); + + if (num_failing == 0) { + console.log('PASS'); + } else { + console.log('FAIL'); + } + + phantom.exit(); +}); diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..3d3bfbb --- /dev/null +++ b/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +which phantomjs > /dev/null +if [ $? != 0 ]; then + echo You must install phantomjs to use command-line testing. + echo Visit http://www.phantomjs.org/ to get it. + echo + echo OR open auto_tests/misc/local.html in a browser. + echo OR follow the instructions in auto_tests/README + exit 1 +fi + +phantomjs phantom-driver.js -- 2.7.4