-
Notifications
You must be signed in to change notification settings - Fork 0
/
deno-test.sl
141 lines (114 loc) · 3.73 KB
/
deno-test.sl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def genString():
tsMethodCall("fc", "string", [])
end
def genInt():
tsMethodCall("fc", "integer", [])
end
def genFloat():
tsMethodCall("fc", "float", [])
end
def genVariantCaseAttr(attr: TypedAttribute):
tsObjectProp(attr.name, genType(attr.type))
end
def genVariantCase(vc: VariantCase):
tsMethodCall("fc", "record", [tsObject(vc.attrs.map(genVariantCaseAttr))])
end
def genVariant(name: String, cases: VariantCaseList):
tsMethodCall("fc", "oneOf", cases.map(genVariantCase))
end
def genGeneric(name: String, types: Array(Type)):
case name:
| "Set": tsMethodCall("fc", "uniqueArray", [genType(types.index(0))])
end
end
def genType(type: Type):
case type:
| Schema(s): genSchemaValue(s)
| Variant(name, cases): genVariant(name, cases)
| Generic(name, types): genGeneric(name, types)
| String(): genString()
| Int(): genInt()
| Decimal(): genFloat()
end
end
def genTypeValueObjProp(attr: TypedAttribute):
tsObjectProp(attr.name, genType(attr.type))
end
def genSchemaValue(s: Schema):
tsMethodCall(
"fc",
"record",
[tsObject(s.attributes.map(genTypeValueObjProp))]
)
end
def toTestValue(attr: TypedAttribute):
case attr.type:
| Schema(s): tsLet(attr.name, genSchemaValue(s))
| String(): tsLet(attr.name, genString())
| Generic(name, types): tsLet(attr.name, genGeneric(name, types))
| Int(): tsLet(attr.name, genInt())
| Decimal(): tsLet(attr.name, genFloat())
end
end
def toCallValue(arg: TypedAttribute):
tsIden("state.".appendStr(arg.name))
end
def actionStateTypeName(actionName: String):
actionName.appendStr("State")
end
def toTsTypedAttr(attr: TypedAttr):
tsTypedAttr(attr.name, attr.type)
end
def actionState(action: Action):
action.args.concat(action.stateVars)
end
def toActionStateType(action: Action):
tsInterface(actionStateTypeName(action.name),
actionState(action).map(toTsTypedAttr))
end
def toStateProp(attr: TypedAttribute):
tsObjectProp(attr.name, tsIden(attr.name))
end
def toActionTest(action: Action):
let clientName = "client"
let dataSetup = actionState(action).map(toTestValue)
let stateSetup = tsLet("state", tsObject(actionState(action).map(toStateProp)))
let property = [tsAwait(
tsMethodCall("fc", "assert", [
tsMethodCall("fc", "asyncProperty", [tsIden("state"),
tsClosure([tsTypedAttr("state", tsType(actionStateTypeName(action.name)))], [
tsLet("client", tsNew("Client", [])),
tsLet("model", tsNew("Budget", [])),
tsLet("cresp", tsAwait(tsMethodCall(clientName, "setup", [tsIden("state.db")]))),
tsAwait(tsMethodCall("cresp", "arrayBuffer", [])),
tsAwait(tsMethodCall(clientName, action.name, action.args.map(toCallValue))),
tsMethodCall("model", action.name, action.args.map(toCallValue)),
tsAwait(tsMethodCall("client", "teardown", []))
], true)
])
])
)]
let testBody = [dataSetup, [stateSetup], property].flatten()
let testWrapper = tsClosure([tsTypedAttr("t", tsType("Deno.Test"))], testBody, false)
tsMethodCall("Deno", "test", [action.name, testWrapper])
end
def actionTests():
tsClosure([], Model.actions.map(toActionTest), false)
end
def toSchemaImplImport(schema: Schema):
tsSymbolImport(schema.name, schema.name.appendStr("Impl"))
end
typescript:
{{ tsClosure([
tsObjectPat([
tsObjectPatProp("a", "b")
], tsType("Somethin"))
], [], false) }}
{{ tsAliasImport(
[tsSymbolImport("one", "two")], "file") }}
{{ tsDefaultImport("some", "file") }}
{{ tsLet("x", typescript: {{ [1,2,3].length() }} end) }}
{{ tsAliasImport(Model.schemas.map(toSchemaImplImport), "./react_ui/src/state.ts") }}
{{* Model.actions.map(toActionStateType) }}
{{ tsExport(tsLet("runTests", actionTests())) }}
end