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