@@ -2,13 +2,13 @@ import { Expression } from '../../../src/render/expression'
2
2
import { expect } from 'chai'
3
3
import { Context } from '../../../src/context/context'
4
4
import { toThenable } from '../../../src/util/async'
5
- import { operatorImpls } from '../../../src/render/operator'
5
+ import { Operators } from '../../../src/render/operator'
6
6
7
7
describe ( 'Expression' , function ( ) {
8
8
const ctx = new Context ( { } )
9
9
10
10
it ( 'should throw when context not defined' , done => {
11
- toThenable ( new Expression ( 'foo' , operatorImpls ) . value ( undefined ! ) )
11
+ toThenable ( new Expression ( 'foo' , Operators ) . value ( undefined ! ) )
12
12
. then ( ( ) => done ( new Error ( 'should not resolved' ) ) )
13
13
. catch ( err => {
14
14
expect ( err . message ) . to . match ( / c o n t e x t n o t d e f i n e d / )
@@ -18,19 +18,19 @@ describe('Expression', function () {
18
18
19
19
describe ( 'single value' , function ( ) {
20
20
it ( 'should eval literal' , async function ( ) {
21
- expect ( await toThenable ( new Expression ( '2.4' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( 2.4 )
22
- expect ( await toThenable ( new Expression ( '"foo"' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( 'foo' )
23
- expect ( await toThenable ( new Expression ( 'false' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( false )
21
+ expect ( await toThenable ( new Expression ( '2.4' , Operators ) . value ( ctx ) ) ) . to . equal ( 2.4 )
22
+ expect ( await toThenable ( new Expression ( '"foo"' , Operators ) . value ( ctx ) ) ) . to . equal ( 'foo' )
23
+ expect ( await toThenable ( new Expression ( 'false' , Operators ) . value ( ctx ) ) ) . to . equal ( false )
24
24
} )
25
25
it ( 'should eval range expression' , async function ( ) {
26
26
const ctx = new Context ( { two : 2 } )
27
- expect ( await toThenable ( new Expression ( '(2..4)' , operatorImpls ) . value ( ctx ) ) ) . to . deep . equal ( [ 2 , 3 , 4 ] )
28
- expect ( await toThenable ( new Expression ( '(two..4)' , operatorImpls ) . value ( ctx ) ) ) . to . deep . equal ( [ 2 , 3 , 4 ] )
27
+ expect ( await toThenable ( new Expression ( '(2..4)' , Operators ) . value ( ctx ) ) ) . to . deep . equal ( [ 2 , 3 , 4 ] )
28
+ expect ( await toThenable ( new Expression ( '(two..4)' , Operators ) . value ( ctx ) ) ) . to . deep . equal ( [ 2 , 3 , 4 ] )
29
29
} )
30
30
it ( 'should eval literal' , async function ( ) {
31
- expect ( await toThenable ( new Expression ( '2.4' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( 2.4 )
32
- expect ( await toThenable ( new Expression ( '"foo"' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( 'foo' )
33
- expect ( await toThenable ( new Expression ( 'false' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( false )
31
+ expect ( await toThenable ( new Expression ( '2.4' , Operators ) . value ( ctx ) ) ) . to . equal ( 2.4 )
32
+ expect ( await toThenable ( new Expression ( '"foo"' , Operators ) . value ( ctx ) ) ) . to . equal ( 'foo' )
33
+ expect ( await toThenable ( new Expression ( 'false' , Operators ) . value ( ctx ) ) ) . to . equal ( false )
34
34
} )
35
35
36
36
it ( 'should eval property access' , async function ( ) {
@@ -39,112 +39,112 @@ describe('Expression', function () {
39
39
coo : 'bar' ,
40
40
doo : { foo : 'bar' , bar : { foo : 'bar' } }
41
41
} )
42
- expect ( await toThenable ( new Expression ( 'foo.bar' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( 'BAR' )
43
- expect ( await toThenable ( new Expression ( 'foo["bar"]' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( 'BAR' )
44
- expect ( await toThenable ( new Expression ( 'foo[coo]' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( 'BAR' )
45
- expect ( await toThenable ( new Expression ( 'foo[doo.foo]' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( 'BAR' )
46
- expect ( await toThenable ( new Expression ( 'foo[doo["foo"]]' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( 'BAR' )
47
- expect ( await toThenable ( new Expression ( 'doo[coo].foo' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( 'bar' )
42
+ expect ( await toThenable ( new Expression ( 'foo.bar' , Operators ) . value ( ctx ) ) ) . to . equal ( 'BAR' )
43
+ expect ( await toThenable ( new Expression ( 'foo["bar"]' , Operators ) . value ( ctx ) ) ) . to . equal ( 'BAR' )
44
+ expect ( await toThenable ( new Expression ( 'foo[coo]' , Operators ) . value ( ctx ) ) ) . to . equal ( 'BAR' )
45
+ expect ( await toThenable ( new Expression ( 'foo[doo.foo]' , Operators ) . value ( ctx ) ) ) . to . equal ( 'BAR' )
46
+ expect ( await toThenable ( new Expression ( 'foo[doo["foo"]]' , Operators ) . value ( ctx ) ) ) . to . equal ( 'BAR' )
47
+ expect ( await toThenable ( new Expression ( 'doo[coo].foo' , Operators ) . value ( ctx ) ) ) . to . equal ( 'bar' )
48
48
} )
49
49
} )
50
50
51
51
describe ( 'simple expression' , function ( ) {
52
52
it ( 'should return false for "1==2"' , async ( ) => {
53
- expect ( await toThenable ( new Expression ( '1==2' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( false )
53
+ expect ( await toThenable ( new Expression ( '1==2' , Operators ) . value ( ctx ) ) ) . to . equal ( false )
54
54
} )
55
55
it ( 'should return true for "1<2"' , async ( ) => {
56
- expect ( await toThenable ( new Expression ( '1<2' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
56
+ expect ( await toThenable ( new Expression ( '1<2' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
57
57
} )
58
58
it ( 'should return true for "1 < 2"' , async ( ) => {
59
- expect ( await toThenable ( new Expression ( '1 < 2' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
59
+ expect ( await toThenable ( new Expression ( '1 < 2' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
60
60
} )
61
61
it ( 'should return true for "1 < 2"' , async ( ) => {
62
- expect ( await toThenable ( new Expression ( '1 < 2' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
62
+ expect ( await toThenable ( new Expression ( '1 < 2' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
63
63
} )
64
64
it ( 'should return true for "2 <= 2"' , async ( ) => {
65
- expect ( await toThenable ( new Expression ( '2 <= 2' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
65
+ expect ( await toThenable ( new Expression ( '2 <= 2' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
66
66
} )
67
67
it ( 'should return true for "one <= two"' , async ( ) => {
68
68
const ctx = new Context ( { one : 1 , two : 2 } )
69
- expect ( await toThenable ( new Expression ( 'one <= two' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
69
+ expect ( await toThenable ( new Expression ( 'one <= two' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
70
70
} )
71
71
it ( 'should return false for "x contains "x""' , async ( ) => {
72
72
const ctx = new Context ( { x : 'XXX' } )
73
- expect ( await toThenable ( new Expression ( 'x contains "x"' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( false )
73
+ expect ( await toThenable ( new Expression ( 'x contains "x"' , Operators ) . value ( ctx ) ) ) . to . equal ( false )
74
74
} )
75
75
it ( 'should return true for "x contains "X""' , async ( ) => {
76
76
const ctx = new Context ( { x : 'XXX' } )
77
- expect ( await toThenable ( new Expression ( 'x contains "X"' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
77
+ expect ( await toThenable ( new Expression ( 'x contains "X"' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
78
78
} )
79
79
it ( 'should return false for "1 contains "x""' , async ( ) => {
80
80
const ctx = new Context ( { x : 'XXX' } )
81
- expect ( await toThenable ( new Expression ( '1 contains "x"' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( false )
81
+ expect ( await toThenable ( new Expression ( '1 contains "x"' , Operators ) . value ( ctx ) ) ) . to . equal ( false )
82
82
} )
83
83
it ( 'should return false for "y contains "x""' , async ( ) => {
84
84
const ctx = new Context ( { x : 'XXX' } )
85
- expect ( await toThenable ( new Expression ( 'y contains "x"' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( false )
85
+ expect ( await toThenable ( new Expression ( 'y contains "x"' , Operators ) . value ( ctx ) ) ) . to . equal ( false )
86
86
} )
87
87
it ( 'should return false for "z contains "x""' , async ( ) => {
88
88
const ctx = new Context ( { x : 'XXX' } )
89
- expect ( await toThenable ( new Expression ( 'z contains "x"' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( false )
89
+ expect ( await toThenable ( new Expression ( 'z contains "x"' , Operators ) . value ( ctx ) ) ) . to . equal ( false )
90
90
} )
91
91
it ( 'should return true for "(1..5) contains 3"' , async ( ) => {
92
92
const ctx = new Context ( { x : 'XXX' } )
93
- expect ( await toThenable ( new Expression ( '(1..5) contains 3' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
93
+ expect ( await toThenable ( new Expression ( '(1..5) contains 3' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
94
94
} )
95
95
it ( 'should return false for "(1..5) contains 6"' , async ( ) => {
96
96
const ctx = new Context ( { x : 'XXX' } )
97
- expect ( await toThenable ( new Expression ( '(1..5) contains 6' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( false )
97
+ expect ( await toThenable ( new Expression ( '(1..5) contains 6' , Operators ) . value ( ctx ) ) ) . to . equal ( false )
98
98
} )
99
99
it ( 'should return true for ""<=" == "<=""' , async ( ) => {
100
- expect ( await toThenable ( new Expression ( '"<=" == "<="' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
100
+ expect ( await toThenable ( new Expression ( '"<=" == "<="' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
101
101
} )
102
102
} )
103
103
104
104
it ( 'should allow space in quoted value' , async function ( ) {
105
105
const ctx = new Context ( { space : ' ' } )
106
- expect ( await toThenable ( new Expression ( '" " == space' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
106
+ expect ( await toThenable ( new Expression ( '" " == space' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
107
107
} )
108
108
109
109
describe ( 'escape' , ( ) => {
110
110
it ( 'should escape quote' , async function ( ) {
111
111
const ctx = new Context ( { quote : '"' } )
112
- expect ( await toThenable ( new Expression ( '"\\"" == quote' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
112
+ expect ( await toThenable ( new Expression ( '"\\"" == quote' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
113
113
} )
114
114
it ( 'should escape square bracket' , async function ( ) {
115
115
const ctx = new Context ( { obj : { ']' : 'bracket' } } )
116
- expect ( await toThenable ( new Expression ( 'obj["]"] == "bracket"' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
116
+ expect ( await toThenable ( new Expression ( 'obj["]"] == "bracket"' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
117
117
} )
118
118
} )
119
119
120
120
describe ( 'complex expression' , function ( ) {
121
121
it ( 'should support value or value' , async function ( ) {
122
- expect ( await toThenable ( new Expression ( 'false or true' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
122
+ expect ( await toThenable ( new Expression ( 'false or true' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
123
123
} )
124
124
it ( 'should support < and contains' , async function ( ) {
125
- expect ( await toThenable ( new Expression ( '1 < 2 and x contains "x"' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( false )
125
+ expect ( await toThenable ( new Expression ( '1 < 2 and x contains "x"' , Operators ) . value ( ctx ) ) ) . to . equal ( false )
126
126
} )
127
127
it ( 'should support < or contains' , async function ( ) {
128
- expect ( await toThenable ( new Expression ( '1 < 2 or x contains "x"' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
128
+ expect ( await toThenable ( new Expression ( '1 < 2 or x contains "x"' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
129
129
} )
130
130
it ( 'should support value and !=' , async function ( ) {
131
131
const ctx = new Context ( { empty : '' } )
132
- expect ( await toThenable ( new Expression ( 'empty and empty != ""' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( false )
132
+ expect ( await toThenable ( new Expression ( 'empty and empty != ""' , Operators ) . value ( ctx ) ) ) . to . equal ( false )
133
133
} )
134
134
it ( 'should recognize quoted value' , async function ( ) {
135
- expect ( await toThenable ( new Expression ( '">"' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( '>' )
135
+ expect ( await toThenable ( new Expression ( '">"' , Operators ) . value ( ctx ) ) ) . to . equal ( '>' )
136
136
} )
137
137
it ( 'should evaluate from right to left' , async function ( ) {
138
- expect ( await toThenable ( new Expression ( 'true or false and false' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
139
- expect ( await toThenable ( new Expression ( 'true and false and false or true' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( false )
138
+ expect ( await toThenable ( new Expression ( 'true or false and false' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
139
+ expect ( await toThenable ( new Expression ( 'true and false and false or true' , Operators ) . value ( ctx ) ) ) . to . equal ( false )
140
140
} )
141
141
it ( 'should recognize property access' , async function ( ) {
142
142
const ctx = new Context ( { obj : { foo : true } } )
143
- expect ( await toThenable ( new Expression ( 'obj["foo"] and true' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( true )
143
+ expect ( await toThenable ( new Expression ( 'obj["foo"] and true' , Operators ) . value ( ctx ) ) ) . to . equal ( true )
144
144
} )
145
145
it ( 'should allow nested property access' , async function ( ) {
146
146
const ctx = new Context ( { obj : { foo : 'FOO' } , keys : { "what's this" : 'foo' } } )
147
- expect ( await toThenable ( new Expression ( 'obj[keys["what\'s this"]]' , operatorImpls ) . value ( ctx ) ) ) . to . equal ( 'FOO' )
147
+ expect ( await toThenable ( new Expression ( 'obj[keys["what\'s this"]]' , Operators ) . value ( ctx ) ) ) . to . equal ( 'FOO' )
148
148
} )
149
149
} )
150
150
} )
0 commit comments