-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.kt
187 lines (164 loc) · 7.05 KB
/
Test.kt
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import cf.wayzer.placehold.*
import cf.wayzer.placehold.PlaceHoldApi.registerGlobalDynamicVar
import cf.wayzer.placehold.PlaceHoldApi.registerGlobalVar
import cf.wayzer.placehold.PlaceHoldApi.with
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.assertThrows
import java.io.File
import java.lang.IllegalArgumentException
import java.util.*
class Test {
@BeforeEach
fun reset() {
PlaceHoldApi.init()
}
@Test
fun base() {
Assertions.assertEquals("Hello World", "Hello World".with().toString())
Assertions.assertEquals("Hello World", "Hello {v}".with("v" to "World").toString())
}
@Test
fun nested() {
val nest1 = "Here will refer nest2: {nest2}".with()
val nest2 = "Here {nest2} is string".with("nest2" to "nest2")
Assertions.assertEquals(
"nest Test 'Here will refer nest2: Here nest2 is string' 'Here nest2 is string'",
"nest Test '{nest1}' '{nest2}'".with("nest1" to nest1, "nest2" to nest2).toString()
)
}
@Test
fun nestedVar() {
registerGlobalVar("nest3", "nested {v}".with())
PlaceHoldApi.registerGlobalDynamicVar("nestVar") { VarToken("nest3").get() }
Assertions.assertEquals("nested Var", "{nestVar}".with("v" to "Var").toString())
}
@Test
fun testList() {
val list = listOf(0, 1, 2, 3, 4, 5)
Assertions.assertEquals("0,1,2,3,4,5", "{list}".with("list" to list).toString())
Assertions.assertEquals("0", "{list.first}".with("list" to list).toString())
Assertions.assertEquals("3", "{list.3}".with("list" to list).toString())
Assertions.assertEquals("5", "{list.last}".with("list" to list).toString())
}
data class Data(val a: Int, val b: String) : VarContainer {
override fun resolve(ctx: VarString, child: String): Any? {
if (child == "testVarContainer") return "YES"
return null
}
}
@Test
fun badPath() {
Assertions.assertEquals("{ERR: not found o.d} YES", "{o.d} {o.testVarContainer}".with("o" to Data(22, "ab")).toString())
}
@Test
fun testTypeBind() {
PlaceHoldApi.typeBinder<Data>().apply {
registerChild("a") { it.a }
registerChild("b") { it.b }
}
Assertions.assertEquals("22 ab", "{o.a} {o.b}".with("o" to Data(22, "ab")).toString())
Assertions.assertEquals("{ERR: not found o.} Data(a=22, b=ab)", "{o.} {o}".with("o" to Data(22, "ab")).toString())
//移除绑定
PlaceHoldApi.typeBinder<Data>().apply {
registerChildAny("a", null)
}
Assertions.assertEquals("{ERR: not found o.a} ab", "{o.a} {o.b}".with("o" to Data(22, "ab")).toString())
}
@Test
fun testCache() {
PlaceHoldApi.typeBinder<Data>().apply {
registerChild("a") { it.a }
registerChild("b") { it.b }
}
var count = 0
val a = DynamicVar {
count++;Data(22, "ab")
}
"{o.a} {o.b} {o} {o.a}".with("o" to a).toString()
Assertions.assertEquals(1, count)
}
@Test
fun testFunctionVar() {
registerGlobalVar("upperCase", DynamicVar {
it.get<String>(0).uppercase()
})
Assertions.assertEquals("{ERR: Fail resolve upperCase: Parma 0 required type String}", "{upperCase}".with().toString())
Assertions.assertEquals("UPPER", "{upperCase a}".with("a" to "upper").toString())
}
@Test
fun testLocalOverlay() {
registerGlobalVar("v", "global")
registerGlobalVar("v2", "global")
Assertions.assertEquals("local{ERR: not found v2}", "{v}{v2}".with("v" to "local", "v2" to null).toString())
}
@Test
fun testCacheBug() {
val sub1 = "{a}".with()
Assertions.assertEquals("a", "{sub}".with("sub" to sub1, "a" to "a").toString())
Assertions.assertEquals("b", "{sub}".with("sub" to sub1, "a" to "b").toString())
val sub2 = "{a}".with("a" to "a")
Assertions.assertEquals("ba", "{a}{sub}".with("sub" to sub2, "a" to "b").toString())
}
interface CustomBuilderMessage
@Test
fun testCustomBuilder() {
registerGlobalVar("image", DynamicVar { "{ERR: receiver not support image}" })
val text = "这是文字{image imageFile}这是文字".with("imageFile" to File("image.png"))
Assertions.assertEquals("这是文字{ERR: receiver not support image}这是文字", text.toString())
data class Text(val value: String) : CustomBuilderMessage
data class Image(val value: String) : CustomBuilderMessage
val builderCtx = VarString(
"", mapOf(
"image" to DynamicVar {
Image("{OUTPUT IMAGE ${it.get<File>(0)}}")
},
), cache = null
)
val resolved = builderCtx.createChild(text.text, text.vars).parsed().map { token ->
if (token is String) Text(token) else (token as VarString.VarToken).let {
val obj = it.get()
if (obj is CustomBuilderMessage) return@map obj
Text(it.getForString(obj))
}
}
Assertions.assertEquals(3, resolved.size)
Assertions.assertTrue(resolved[0] is Text)
Assertions.assertTrue(resolved[1] is Image)
Assertions.assertTrue(resolved[2] is Text)
}
@Test
fun testStdVariable() {
//join
Assertions.assertEquals("0,1,2", "{list|join}".with("list" to listOf(0, 1, 2)).toString())
Assertions.assertEquals("<0>|<1>|<2>", """{join list "|" "<{it}>"}""".with("list" to listOf(0, 1, 2)).toString())
//listPrefix
registerGlobalVar("list.2", 2)
registerGlobalVar("list.3", DynamicVar { "DD" })
registerGlobalVar("list.null", DynamicVar { null })
Assertions.assertEquals("0,1,2,DD", "{listPrefix list}".with("list.0" to 0, "list.1" to 1).toString())
//Not support now
// PlaceHoldApi.typeBinder<Data>().apply {
// registerChild("list.1", DynamicVar.obj { it.a })
// registerChild("list.2", DynamicVar.obj { it.b })
// }
// Assertions.assertEquals("8,10", "{listPrefix o.list}".with("o" to Data(8, "10")).toString())
//or
Assertions.assertEquals("{ERR: not found if}", """{if v "YES"}""".with().toString())
Assertions.assertEquals("NO", """{if v "YES"|else "NO"}""".with().toString())
Assertions.assertEquals("YES", """{v|else "NO"}""".with("v" to "YES").toString())
//date
Assertions.assertEquals("01-01", "{t | date}".with("t" to Date(0)).toString())
Assertions.assertEquals("1970-01-01", """{t | date "yyyy-MM-dd"}""".with("t" to Date(0)).toString())
}
@Test
fun testLazyUnwrap() {
registerGlobalDynamicVar("C", DynamicVar {
Assertions.assertNotEquals(VarString.globalContext, this)
VarToken("local").get()
})
Assertions.assertEquals("LOCAL", "{C}".with("local" to "LOCAL").toString())
}
}