consolidating scripts
[dygraphs.git] / auto_tests / tests / formats.js
1 /**
2 * @fileoverview Tests for data formats.
3 *
4 * @author konigsberg@google.com (Robert Konigsberg)
5 */
6
7 import Dygraph from '../../src/dygraph';
8
9 describe("formats", function() {
10
11 cleanupAfterEach();
12
13 var dataString =
14 "X,Y\n" +
15 "0,-1\n" +
16 "1,0\n" +
17 "2,1\n" +
18 "3,0\n";
19
20 var dataArray =
21 [[0,-1],
22 [1,0],
23 [2,1],
24 [3,0]];
25 var BASE_OPTS = {labels: ['X', 'Y']};
26
27 it('testCsv', function() {
28 var data = dataString;
29 var graph = document.getElementById("graph");
30 var g = new Dygraph(graph, data, {});
31 assertData(g);
32 });
33
34 it('testArray', function() {
35 var data = dataArray;
36 var graph = document.getElementById("graph");
37 var g = new Dygraph(graph, data, BASE_OPTS);
38 assertData(g);
39 });
40
41 it('testFunctionReturnsCsv', function() {
42 var data = function() { return dataString; };
43
44 var graph = document.getElementById("graph");
45 var g = new Dygraph(graph, data, {});
46 assertData(g);
47 });
48
49 it('testFunctionDefinesArray', function() {
50 var array = dataArray;
51 var data = function() { return array; }
52
53 var graph = document.getElementById("graph");
54 var g = new Dygraph(graph, data, BASE_OPTS);
55 assertData(g);
56 });
57
58 it('testXValueParser', function() {
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) {
69 assert.equal(1, str.length);
70 return str.charCodeAt(0) - "a".charCodeAt(0);
71 }
72 });
73
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 });
79
80 var assertData = function(g) {
81 var expected = dataArray;
82
83 assert.equal(4, g.numRows());
84 assert.equal(2, g.numColumns());
85
86 for (var i = 0; i < 4; i++) {
87 for (var j = 0; j < 2; j++) {
88 assert.equal(expected[i][j], g.getValue(i, j));
89 }
90 }
91 };
92
93 });