Commit | Line | Data |
---|---|---|
36d4fabf RK |
1 | /** |
2 | * @fileoverview Tests for data formats. | |
3 | * | |
4 | * @author konigsberg@google.com (Robert Konigsberg) | |
5 | */ | |
36d4fabf | 6 | |
e8c70e4e | 7 | import Dygraph from '../../src/dygraph'; |
36d4fabf | 8 | |
e8c70e4e DV |
9 | describe("formats", function() { |
10 | ||
11 | cleanupAfterEach(); | |
36d4fabf | 12 | |
319d0361 | 13 | var dataString = |
36d4fabf RK |
14 | "X,Y\n" + |
15 | "0,-1\n" + | |
16 | "1,0\n" + | |
17 | "2,1\n" + | |
18 | "3,0\n"; | |
19 | ||
319d0361 | 20 | var dataArray = |
36d4fabf RK |
21 | [[0,-1], |
22 | [1,0], | |
23 | [2,1], | |
24 | [3,0]]; | |
65129ba8 | 25 | var BASE_OPTS = {labels: ['X', 'Y']}; |
36d4fabf | 26 | |
89fdcedb | 27 | it('testCsv', function() { |
319d0361 | 28 | var data = dataString; |
36d4fabf RK |
29 | var graph = document.getElementById("graph"); |
30 | var g = new Dygraph(graph, data, {}); | |
319d0361 | 31 | assertData(g); |
89fdcedb | 32 | }); |
36d4fabf | 33 | |
89fdcedb | 34 | it('testArray', function() { |
319d0361 | 35 | var data = dataArray; |
36d4fabf | 36 | var graph = document.getElementById("graph"); |
65129ba8 | 37 | var g = new Dygraph(graph, data, BASE_OPTS); |
319d0361 | 38 | assertData(g); |
89fdcedb | 39 | }); |
36d4fabf | 40 | |
89fdcedb | 41 | it('testFunctionReturnsCsv', function() { |
1b7afc93 | 42 | var data = function() { return dataString; }; |
36d4fabf RK |
43 | |
44 | var graph = document.getElementById("graph"); | |
45 | var g = new Dygraph(graph, data, {}); | |
1b7afc93 | 46 | assertData(g); |
89fdcedb | 47 | }); |
36d4fabf | 48 | |
89fdcedb | 49 | it('testFunctionDefinesArray', function() { |
319d0361 | 50 | var array = dataArray; |
36d4fabf RK |
51 | var data = function() { return array; } |
52 | ||
53 | var graph = document.getElementById("graph"); | |
65129ba8 | 54 | var g = new Dygraph(graph, data, BASE_OPTS); |
319d0361 | 55 | assertData(g); |
89fdcedb | 56 | }); |
36d4fabf | 57 | |
89fdcedb | 58 | it('testXValueParser', function() { |
57154831 RK |
59 | var data = |
60 | "X,Y\n" + | |
61 | "d,-1\n" + | |
62 | "e,0\n" + | |
63 | "f,1\n" + | |
64 | "g,0\n"; | |
65 | ||
66 | var graph = document.getElementById("graph"); | |
67 | var g = new Dygraph(graph, data, { | |
68 | xValueParser : function(str) { | |
89fdcedb | 69 | assert.equal(1, str.length); |
57154831 RK |
70 | return str.charCodeAt(0) - "a".charCodeAt(0); |
71 | } | |
72 | }); | |
73 | ||
89fdcedb DV |
74 | assert.equal(3, g.getValue(0, 0)); |
75 | assert.equal(4, g.getValue(1, 0)); | |
76 | assert.equal(5, g.getValue(2, 0)); | |
77 | assert.equal(6, g.getValue(3, 0)); | |
78 | }); | |
57154831 | 79 | |
319d0361 DV |
80 | var assertData = function(g) { |
81 | var expected = dataArray; | |
36d4fabf | 82 | |
89fdcedb DV |
83 | assert.equal(4, g.numRows()); |
84 | assert.equal(2, g.numColumns()); | |
36d4fabf RK |
85 | |
86 | for (var i = 0; i < 4; i++) { | |
87 | for (var j = 0; j < 2; j++) { | |
89fdcedb | 88 | assert.equal(expected[i][j], g.getValue(i, j)); |
36d4fabf RK |
89 | } |
90 | } | |
319d0361 | 91 | }; |
89fdcedb DV |
92 | |
93 | }); |