Merge pull request #270 from danvk/issue-126
[dygraphs.git] / auto_tests / tests / formats.js
1 /**
2 * @fileoverview Tests for data formats.
3 *
4 * @author konigsberg@google.com (Robert Konigsberg)
5 */
6 var FormatsTestCase = TestCase("formats");
7
8 FormatsTestCase.prototype.setUp = function() {
9 document.body.innerHTML = "<div id='graph'></div>";
10 };
11
12 FormatsTestCase.prototype.tearDown = function() {
13 };
14
15 FormatsTestCase.prototype.dataString =
16 "X,Y\n" +
17 "0,-1\n" +
18 "1,0\n" +
19 "2,1\n" +
20 "3,0\n";
21
22 FormatsTestCase.prototype.dataArray =
23 [[0,-1],
24 [1,0],
25 [2,1],
26 [3,0]];
27
28 FormatsTestCase.prototype.testCsv = function() {
29 var data = this.dataString;
30 var graph = document.getElementById("graph");
31 var g = new Dygraph(graph, data, {});
32 this.assertData(g);
33 };
34
35 FormatsTestCase.prototype.testArray = function() {
36 var data = this.dataArray;
37 var graph = document.getElementById("graph");
38 var g = new Dygraph(graph, data, {});
39 this.assertData(g);
40 };
41
42 FormatsTestCase.prototype.testFunctionReturnsCsv = function() {
43 var string = this.dataString;
44 var data = function() { return string; };
45
46 var graph = document.getElementById("graph");
47 var g = new Dygraph(graph, data, {});
48 // this.assertData(g);
49 console.log("x");
50 };
51
52 FormatsTestCase.prototype.testFunctionDefinesArray = function() {
53 var array = this.dataArray;
54 var data = function() { return array; }
55
56 var graph = document.getElementById("graph");
57 var g = new Dygraph(graph, data, {});
58 this.assertData(g);
59 };
60
61 FormatsTestCase.prototype.testXValueParser = function() {
62 var data =
63 "X,Y\n" +
64 "d,-1\n" +
65 "e,0\n" +
66 "f,1\n" +
67 "g,0\n";
68
69 var graph = document.getElementById("graph");
70 var g = new Dygraph(graph, data, {
71 xValueParser : function(str) {
72 assertEquals(1, str.length);
73 return str.charCodeAt(0) - "a".charCodeAt(0);
74 }
75 });
76
77 assertEquals(3, g.getValue(0, 0));
78 assertEquals(4, g.getValue(1, 0));
79 assertEquals(5, g.getValue(2, 0));
80 assertEquals(6, g.getValue(3, 0));
81 };
82
83 FormatsTestCase.prototype.assertData = function(g) {
84 var expected = this.dataArray;
85
86 assertEquals(4, g.numRows());
87 assertEquals(2, g.numColumns());
88
89 for (var i = 0; i < 4; i++) {
90 for (var j = 0; j < 2; j++) {
91 assertEquals(expected[i][j], g.getValue(i, j));
92 }
93 }
94 }