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