11import { generateCompositeType } from '../../src/utils/compositeType' ;
2+ import { parseExpressionConvertThis2Context } from '../../src/utils/expressionParser' ;
23import { Scope } from '../../src/utils/Scope' ;
34
45test ( 'single line string' , ( ) => {
@@ -16,3 +17,125 @@ test('string with single quote', () => {
1617test ( 'string with double quote' , ( ) => {
1718 expect ( generateCompositeType ( 'a"bc' , Scope . createRootScope ( ) ) ) . toEqual ( '"a\\"bc"' ) ;
1819} ) ;
20+
21+ const marcoFactory = ( ) => {
22+ const cases : any [ ] = [ ] ;
23+
24+ const marco = ( value : any , cb : ( expression : string ) => void ) => {
25+ cases . push ( [ value , cb ] ) ;
26+ } ;
27+
28+ const start = ( ) => {
29+ test . each ( cases ) ( 'parse expression %s' , ( item , cb ) => {
30+ const testObj = {
31+ globalConfig : { } ,
32+ online : [
33+ {
34+ description : '表格(CnTable)的数据源' ,
35+ initialData : {
36+ type : 'variable' ,
37+ variable : item ,
38+ value : '' ,
39+ } ,
40+ somethingelse : 'somethingelse' ,
41+ } ,
42+ ] ,
43+ } ;
44+ const ret = generateCompositeType ( testObj , Scope . createRootScope ( ) , {
45+ handlers : {
46+ function : ( jsFunc ) => parseExpressionConvertThis2Context ( jsFunc . value , '_this' ) ,
47+ expression : ( jsExpr ) => parseExpressionConvertThis2Context ( jsExpr . value , '_this' ) ,
48+ } ,
49+ } ) ;
50+ cb ( ret ) ;
51+ } ) ;
52+ } ;
53+
54+ return { marco, start } ;
55+ } ;
56+
57+ const { marco : testMarco , start : startMarco } = marcoFactory ( ) ;
58+
59+ /**
60+ * dataSource 为低码编辑器里面数据源的输入
61+ * variable 为 schema 存储的结果
62+ * expect 为出码后期望生产的串
63+
64+ * |dataSource | variable | expect
65+ * |-------------------|----------------------------|--------------
66+ * |"" | "\"\"" | ""
67+ * |"helo world" | "\"hello world\"" | "hello world"
68+ * |true | "true" | true
69+ * |false | "false" | false
70+ * |{"name": gaokai} | "{\"name\": \"cone\"}" | {"name": gaokai}
71+ * | | "" | undefined
72+ * |undefined | "undefined" | undefined
73+ * |null | "null" | null
74+ */
75+
76+ testMarco ( '""' , ( expression ) => {
77+ expect ( expression ) . toMatchInlineSnapshot ( `
78+ "{\\"globalConfig\\": {},
79+ \\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
80+ \\"initialData\\": \\"\\",
81+ \\"somethingelse\\": \\"somethingelse\\"}]}"
82+ ` ) ;
83+ } ) ;
84+
85+ testMarco ( '"hello world"' , ( expression ) => {
86+ expect ( expression ) . toMatchInlineSnapshot ( `
87+ "{\\"globalConfig\\": {},
88+ \\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
89+ \\"initialData\\": \\"hello world\\",
90+ \\"somethingelse\\": \\"somethingelse\\"}]}"
91+ ` ) ;
92+ } ) ;
93+
94+ testMarco ( 'true' , ( expression ) => {
95+ expect ( expression ) . toMatchInlineSnapshot ( `
96+ "{\\"globalConfig\\": {},
97+ \\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
98+ \\"initialData\\": true,
99+ \\"somethingelse\\": \\"somethingelse\\"}]}"
100+ ` ) ;
101+ } ) ;
102+
103+ testMarco ( '{"name": "cone"}' , ( expression ) => {
104+ expect ( expression ) . toMatchInlineSnapshot ( `
105+ "{\\"globalConfig\\": {},
106+ \\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
107+ \\"initialData\\": {
108+ \\"name\\": \\"cone\\"
109+ },
110+ \\"somethingelse\\": \\"somethingelse\\"}]}"
111+ ` ) ;
112+ } ) ;
113+
114+ testMarco ( '' , ( expression ) => {
115+ expect ( expression ) . toMatchInlineSnapshot ( `
116+ "{\\"globalConfig\\": {},
117+ \\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
118+ \\"initialData\\": undefined,
119+ \\"somethingelse\\": \\"somethingelse\\"}]}"
120+ ` ) ;
121+ } ) ;
122+
123+ testMarco ( 'undefined' , ( expression ) => {
124+ expect ( expression ) . toMatchInlineSnapshot ( `
125+ "{\\"globalConfig\\": {},
126+ \\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
127+ \\"initialData\\": undefined,
128+ \\"somethingelse\\": \\"somethingelse\\"}]}"
129+ ` ) ;
130+ } ) ;
131+
132+ testMarco ( 'null' , ( expression ) => {
133+ expect ( expression ) . toMatchInlineSnapshot ( `
134+ "{\\"globalConfig\\": {},
135+ \\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
136+ \\"initialData\\": null,
137+ \\"somethingelse\\": \\"somethingelse\\"}]}"
138+ ` ) ;
139+ } ) ;
140+
141+ startMarco ( ) ;
0 commit comments