6634cd5262a10f493df3fda92d14ac99b88106b9
[dygraphs.git] / auto_tests / tests / parser.js
1 /**
2 * @fileoverview Tests the way that dygraphs parses data.
3 *
4 * @author danvk@google.com (Dan Vanderkam)
5 */
6
7 import Dygraph from '../../src/dygraph';
8 import * as utils from '../../src/dygraph-utils';
9
10 describe("parser", function() {
11
12 cleanupAfterEach();
13
14 it('testDetectLineDelimiter', function() {
15 var data = "X,Y\r" +
16 "0,-1\r" +
17 "1,0\r" +
18 "2,1\r" +
19 "3,0\r"
20 ;
21 assert.equal("\r", utils.detectLineDelimiter(data));
22
23 data = "X,Y\n" +
24 "0,-1\n" +
25 "1,0\n" +
26 "2,1\n" +
27 "3,0\n"
28 ;
29 assert.equal("\n", utils.detectLineDelimiter(data));
30
31 data = "X,Y\n\r" +
32 "0,-1\n\r" +
33 "1,0\n\r" +
34 "2,1\n\r" +
35 "3,0\n\r"
36 ;
37 assert.equal("\n\r", utils.detectLineDelimiter(data));
38 });
39
40 it('testParseDosNewlines', function() {
41 var opts = {
42 width: 480,
43 height: 320
44 };
45 var data = "X,Y\r" +
46 "0,-1\r" +
47 "1,0\r" +
48 "2,1\r" +
49 "3,0\r"
50 ;
51
52 var g = new Dygraph('graph', data, opts);
53 assert.equal(0, g.getValue(0, 0));
54 assert.equal(-1, g.getValue(0, 1));
55 assert.equal(1, g.getValue(1, 0));
56 assert.equal(0, g.getValue(1, 1));
57 assert.deepEqual(['X', 'Y'], g.getLabels());
58 });
59
60 it('should parse tab-delimited data', function() {
61 var data = "X\tY\n" +
62 "0\t-1\n" +
63 "1\t0\n" +
64 "2\t1\n" +
65 "3\t0\n";
66
67 var g = new Dygraph('graph', data);
68 assert.equal(0, g.getValue(0, 0));
69 assert.equal(-1, g.getValue(0, 1));
70 assert.equal(1, g.getValue(1, 0));
71 assert.equal(0, g.getValue(1, 1));
72 assert.deepEqual(['X', 'Y'], g.getLabels());
73 });
74
75 it('should parse fractions', function() {
76 var data = "X,Y\n" +
77 "0,1/4\n" +
78 "1,2/4\n" +
79 "2,3/4\n" +
80 "3,4/4\n";
81 var g = new Dygraph('graph', data, {fractions:true});
82
83 assert.equal(0, g.getValue(0, 0));
84 assert.deepEqual([1, 4], g.getValue(0, 1));
85 assert.equal(1, g.getValue(1, 0));
86 assert.deepEqual([2, 4], g.getValue(1, 1));
87 assert.deepEqual(['X', 'Y'], g.getLabels());
88 });
89
90 it('should parse error bars', function() {
91 var data = "X,Y\n" +
92 "0,1,4\n" +
93 "1,2,4\n" +
94 "2,3,4\n" +
95 "3,4,4\n";
96 var g = new Dygraph('graph', data, {errorBars:true});
97
98 assert.equal(0, g.getValue(0, 0));
99 assert.deepEqual([1, 4], g.getValue(0, 1));
100 assert.equal(1, g.getValue(1, 0));
101 assert.deepEqual([2, 4], g.getValue(1, 1));
102 assert.deepEqual(['X', 'Y'], g.getLabels());
103 });
104
105 it('should parse custom bars', function() {
106 var data = "X,Y1,Y2\n" +
107 "1,10;20;30,20;5;25\n" +
108 "2,10;25;35,20;10;25\n";
109 var g = new Dygraph('graph', data, {customBars:true});
110
111 assert.equal(1, g.getValue(0, 0));
112 assert.deepEqual([10, 20, 30], g.getValue(0, 1));
113 assert.deepEqual([20, 5, 25], g.getValue(0, 2));
114 assert.equal(2, g.getValue(1, 0));
115 assert.deepEqual([10, 25, 35], g.getValue(1, 1));
116 assert.deepEqual([20, 10, 25], g.getValue(1, 2));
117 assert.deepEqual(['X', 'Y1', 'Y2'], g.getLabels());
118 });
119
120 /*
121 it('should warn on unsorted input', function() {
122 });
123
124 it('should warn on different length columns', function() {
125 });
126
127 it('should detect double-labeled data', function() {
128 });
129 */
130
131 });