1
1
const expect = require ( 'chai' ) . expect ;
2
2
const FormulaError = require ( '../../formulas/error' ) ;
3
3
const { FormulaParser} = require ( '../../grammar/hooks' ) ;
4
+ const { DepParser} = require ( '../../grammar/dependency/hooks' ) ;
4
5
const { MAX_ROW , MAX_COLUMN } = require ( '../../index' ) ;
5
6
6
7
const parser = new FormulaParser ( {
@@ -25,56 +26,69 @@ const parser = new FormulaParser({
25
26
}
26
27
) ;
27
28
29
+ const depParser = new DepParser ( {
30
+ onVariable : variable => {
31
+ return 'aaaa' === variable ? { from : { row : 1 , col : 1 } , to : { row : 2 , col : 2 } } : { row : 1 , col : 1 } ;
32
+ }
33
+ } ) ;
34
+
35
+ const parsers = [ parser , depParser ] ;
36
+ const names = [ '' , ' (DepParser)' ]
37
+
28
38
const position = { row : 1 , col : 1 , sheet : 'Sheet1' } ;
29
39
30
40
describe ( '#ERROR! Error handling' , ( ) => {
31
- it ( 'should handle NotAllInputParsedException' , function ( ) {
32
- try {
33
- parser . parse ( 'SUM(1))' , position ) ;
34
- } catch ( e ) {
35
- expect ( e ) . to . be . instanceof ( FormulaError ) ;
36
- expect ( e . details . errorLocation . line ) . to . eq ( 1 ) ;
37
- expect ( e . details . errorLocation . column ) . to . eq ( 7 ) ;
38
- expect ( e . name ) . to . eq ( '#ERROR!' ) ;
39
- expect ( e . details . name ) . to . eq ( 'NotAllInputParsedException' ) ;
40
- return ;
41
- }
42
- throw Error ( 'Should not reach here.' ) ;
43
- } ) ;
41
+ parsers . forEach ( ( parser , idx ) => {
42
+ it ( 'should handle NotAllInputParsedException' + names [ idx ] , function ( ) {
43
+ try {
44
+ parser . parse ( 'SUM(1))' , position ) ;
45
+ } catch ( e ) {
46
+ expect ( e ) . to . be . instanceof ( FormulaError ) ;
47
+ expect ( e . details . errorLocation . line ) . to . eq ( 1 ) ;
48
+ expect ( e . details . errorLocation . column ) . to . eq ( 7 ) ;
49
+ expect ( e . name ) . to . eq ( '#ERROR!' ) ;
50
+ expect ( e . details . name ) . to . eq ( 'NotAllInputParsedException' ) ;
51
+ return ;
52
+ }
53
+ throw Error ( 'Should not reach here.' ) ;
54
+ } ) ;
44
55
45
- it ( 'should handle lexing error' , function ( ) {
46
- try {
47
- parser . parse ( 'SUM(1)$' , position ) ;
48
- } catch ( e ) {
49
- expect ( e ) . to . be . instanceof ( FormulaError ) ;
50
- expect ( e . details . errorLocation . line ) . to . eq ( 1 ) ;
51
- expect ( e . details . errorLocation . column ) . to . eq ( 7 ) ;
52
- expect ( e . name ) . to . eq ( '#ERROR!' ) ;
53
- return ;
54
- }
55
- throw Error ( 'Should not reach here.' ) ;
56
- } ) ;
56
+ it ( 'should handle lexing error' + names [ idx ] , function ( ) {
57
+ try {
58
+ parser . parse ( 'SUM(1)$' , position ) ;
59
+ } catch ( e ) {
60
+ expect ( e ) . to . be . instanceof ( FormulaError ) ;
61
+ expect ( e . details . errorLocation . line ) . to . eq ( 1 ) ;
62
+ expect ( e . details . errorLocation . column ) . to . eq ( 7 ) ;
63
+ expect ( e . name ) . to . eq ( '#ERROR!' ) ;
64
+ return ;
65
+ }
66
+ throw Error ( 'Should not reach here.' ) ;
57
67
58
- it ( 'should handle Parser error []' , function ( ) {
59
- try {
60
- parser . parse ( 'SUM([Sales.xlsx]Jan!B2:B5)' , position ) ;
61
- } catch ( e ) {
62
- expect ( e ) . to . be . instanceof ( FormulaError ) ;
63
- expect ( e . name ) . to . eq ( '#ERROR!' ) ;
64
- return ;
65
- }
66
- throw Error ( 'Should not reach here.' ) ;
67
- } ) ;
68
+ } ) ;
68
69
69
- it ( 'should handle Parser error' , function ( ) {
70
- try {
71
- parser . parse ( 'SUM(B2:B5, "123"+)' , position ) ;
72
- } catch ( e ) {
73
- expect ( e ) . to . be . instanceof ( FormulaError ) ;
74
- expect ( e . name ) . to . eq ( '#ERROR!' ) ;
75
- return ;
76
- }
77
- throw Error ( 'Should not reach here.' ) ;
70
+ it ( 'should handle Parser error []' + names [ idx ] , function ( ) {
71
+ try {
72
+ parser . parse ( 'SUM([Sales.xlsx]Jan!B2:B5)' , position ) ;
73
+ } catch ( e ) {
74
+ expect ( e ) . to . be . instanceof ( FormulaError ) ;
75
+ expect ( e . name ) . to . eq ( '#ERROR!' ) ;
76
+ return ;
77
+ }
78
+ throw Error ( 'Should not reach here.' ) ;
79
+ } ) ;
80
+
81
+ it ( 'should handle Parser error' + names [ idx ] , function ( ) {
82
+ try {
83
+ parser . parse ( 'SUM(B2:B5, "123"+)' , position ) ;
84
+ } catch ( e ) {
85
+ expect ( e ) . to . be . instanceof ( FormulaError ) ;
86
+ expect ( e . name ) . to . eq ( '#ERROR!' ) ;
87
+ return ;
88
+ }
89
+ throw Error ( 'Should not reach here.' ) ;
90
+
91
+ } ) ;
78
92
} ) ;
79
93
80
94
it ( 'should handle error from functions' , function ( ) {
@@ -87,6 +101,7 @@ describe('#ERROR! Error handling', () => {
87
101
return ;
88
102
}
89
103
throw Error ( 'Should not reach here.' ) ;
104
+
90
105
} ) ;
91
106
92
107
it ( 'should handle errors in async' , async function ( ) {
@@ -99,5 +114,12 @@ describe('#ERROR! Error handling', () => {
99
114
}
100
115
throw Error ( 'Should not reach here.' ) ;
101
116
} ) ;
102
- } ) ;
103
117
118
+ it ( 'should not throw error when ignoreError = true (DepParser)' , function ( ) {
119
+ try {
120
+ depParser . parse ( 'SUM(*()' , position , true ) ;
121
+ } catch ( e ) {
122
+ throw Error ( 'Should not reach here.' ) ;
123
+ }
124
+ } ) ;
125
+ } ) ;
0 commit comments