From b3cb0794c0513e0a4c9b31d11fbe8b65778fc02e Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Wed, 19 Nov 2014 22:24:08 -0500 Subject: [PATCH] Tests for option warnings --- auto_tests/tests/dygraph-options-tests.js | 69 ++++++++++++++++++++++++++++--- dygraph-options.js | 12 ++++++ 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/auto_tests/tests/dygraph-options-tests.js b/auto_tests/tests/dygraph-options-tests.js index 792dcdb..094ebd6 100644 --- a/auto_tests/tests/dygraph-options-tests.js +++ b/auto_tests/tests/dygraph-options-tests.js @@ -35,7 +35,7 @@ DygraphOptionsTestCase.prototype.testGetSeriesNames = function() { * Ensures that even if logscale is set globally, it doesn't impact the * x axis. */ -DygraphOptionsTestCase.prototype.getLogscaleForX = function() { +DygraphOptionsTestCase.prototype.testGetLogscaleForX = function() { var opts = { width: 480, height: 320 @@ -47,10 +47,69 @@ DygraphOptionsTestCase.prototype.getLogscaleForX = function() { var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); - assertFalse(g.getOptionForAxis('logscale', 'x')); - assertFalse(g.getOptionForAxis('logscale', 'y')); + assertFalse(!!g.getOptionForAxis('logscale', 'x')); + assertFalse(!!g.getOptionForAxis('logscale', 'y')); g.updateOptions({ logscale : true }); - assertFalse(g.getOptionForAxis('logscale', 'x')); - assertTrue(g.getOptionForAxis('logscale', 'y')); + assertFalse(!!g.getOptionForAxis('logscale', 'x')); + assertTrue(!!g.getOptionForAxis('logscale', 'y')); +}; + +// Helper to gather all warnings emitted by Dygraph constructor. +// Removes everything after the first open parenthesis in each warning. +// Returns them in a (possibly empty) list. +var getWarnings = function(div, data, opts) { + var warnings = []; + var oldWarn = console.warn; + console.warn = function(message) { + warnings.push(message.replace(/ \(.*/, '')); + }; + new Dygraph(graph, data, opts); + console.warn = oldWarn; + return warnings; +}; + +DygraphOptionsTestCase.prototype.testLogWarningForNonexistentOption = function() { + if (typeof(Dygraph.OPTIONS_REFERENCE) === 'undefined') { + return; // this test won't pass in non-debug mode. + } + + var graph = document.getElementById("graph"); + var data = "X,Y,Y2,Y3\n" + + "1,-1,2,3"; + + var expectWarning = function(opts, badOptionName) { + DygraphOptions.resetWarnings_(); + var warnings = getWarnings(graph, data, opts); + assertEquals(['Unknown option ' + badOptionName], warnings); + }; + var expectNoWarning = function(opts) { + DygraphOptions.resetWarnings_(); + var warnings = getWarnings(graph, data, opts); + assertEquals([], warnings); + }; + + expectNoWarning({}); + expectWarning({nonExistentOption: true}, 'nonExistentOption'); + expectWarning({series: {Y: {nonExistentOption: true}}}, 'nonExistentOption'); + // expectWarning({Y: {nonExistentOption: true}}); + expectWarning({axes: {y: {anotherNonExistentOption: true}}}, 'anotherNonExistentOption'); + expectWarning({highlightSeriesOpts: {anotherNonExistentOption: true}}, 'anotherNonExistentOption'); + expectNoWarning({highlightSeriesOpts: {strokeWidth: 20}}); + expectNoWarning({strokeWidth: 20}); +}; + +DygraphOptionsTestCase.prototype.testOnlyLogsEachWarningOnce = function() { + if (typeof(Dygraph.OPTIONS_REFERENCE) === 'undefined') { + return; // this test won't pass in non-debug mode. + } + + var graph = document.getElementById("graph"); + var data = "X,Y,Y2,Y3\n" + + "1,-1,2,3"; + + var warnings1 = getWarnings(graph, data, {nonExistent: true}); + var warnings2 = getWarnings(graph, data, {nonExistent: true}); + assertEquals(['Unknown option nonExistent'], warnings1); + assertEquals([], warnings2); }; diff --git a/dygraph-options.js b/dygraph-options.js index 2fbf656..1dff17a 100644 --- a/dygraph-options.js +++ b/dygraph-options.js @@ -405,6 +405,13 @@ DygraphOptions.prototype.validateOptions_ = function() { this.global_, this.user_, this.highlightSeries_]; + var names = this.seriesNames(); + for (var i = 0; i < names.length; i++) { + var name = names[i]; + if (this.series_.hasOwnProperty(name)) { + optionsDicts.push(this.series_[name].options); + } + } for (var i = 0; i < optionsDicts.length; i++) { var dict = optionsDicts[i]; if (!dict) continue; @@ -436,6 +443,11 @@ DygraphOptions.prototype.warnInvalidOption_ = function(optionName) { } }; +// Reset list of previously-shown warnings. Used for testing. +DygraphOptions.resetWarnings_ = function() { + WARNINGS = {}; +}; + } return DygraphOptions; -- 2.7.4