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