-
Notifications
You must be signed in to change notification settings - Fork 0
/
IfTest.kt
191 lines (178 loc) · 6.36 KB
/
IfTest.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
188
189
190
191
package nl.devhaan.kotlinpoetdsl.codeblock
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.asTypeName
import io.kotlintest.shouldBe
import io.kotlintest.shouldThrow
import io.kotlintest.specs.StringSpec
import nl.devhaan.kotlinpoetdsl.functions.createFun
import nl.devhaan.kotlinpoetdsl.functions.func
import nl.devhaan.kotlinpoetdsl.helpers.UnFinishException
import nl.devhaan.kotlinpoetdsl.helpers.createCodeBlock
import nl.devhaan.kotlinpoetdsl.of
import nl.devhaan.kotlinpoetdsl.println
class IfTest : StringSpec({
val nullableBool = ParameterSpec.builder("bool", String::class.asTypeName().copy(nullable = false)).build()
"if end"{
createFun{
func("func", "bool" of Boolean::class) {
If("bool") {
statement("println(true)")
}.end()
}
} shouldBe FunSpec.builder("func")
.addParameter("bool".of(Boolean::class).toParamSpec())
.beginControlFlow("if (bool)")
.addStatement("println(true)")
.endControlFlow()
.build()
}
"ifp end"{
createFun {
func("func", nullableBool) {
ifp("bool") {
statement("println(true)")
}.end()
}
} shouldBe FunSpec.builder("func")
.addParameter(nullableBool)
.beginControlFlow("if ((bool) == true)")
.addStatement("println(true)")
.endControlFlow()
.build()
}
"ifn end"{
createFun {
func("func", nullableBool) {
ifn("bool") {
statement("println(false)")
}.end()
}
} shouldBe FunSpec.builder("func")
.addParameter(nullableBool)
.beginControlFlow("if ((bool) == false)")
.addStatement("println(false)")
.endControlFlow()
.build()
}
"orElseIfp"{
createFun {
func("func", nullableBool) {
ifn("bool") {
statement("println(false)")
}.orElseIfp("bool") {
statement("println(true)")
}.end()
}
} shouldBe FunSpec.builder("func")
.addParameter(nullableBool)
.beginControlFlow("if ((bool) == false)")
.addStatement("println(false)")
.nextControlFlow("else if ((bool) == true)")
.addStatement("println(true)")
.endControlFlow()
.build()
}
"orElseIfn"{
createFun {
func("func", nullableBool) {
ifp("bool") {
statement("println(true)")
}.orElseIfn("bool"){
statement("println(false)")
}.end()
}
} shouldBe FunSpec.builder("func")
.addParameter(nullableBool)
.beginControlFlow("if ((bool) == true)")
.addStatement("println(true)")
.nextControlFlow("else if ((bool) == false)")
.addStatement("println(false)")
.endControlFlow()
.build()
}
"orElseIf orElse"{
val int = ("int" of Int::class).toParamSpec()
createFun {
func("func", int) {
If("int == 1"){
statement("println(\"first\")")
}.orElseIf("int == 2"){
statement("println(\"second\")")
} orElse {
statement("println(\"last\")")
}
}
} shouldBe FunSpec.builder("func")
.addParameter(int)
.beginControlFlow("if (int == 1)")
.addStatement("println(\"first\")")
.nextControlFlow("else if (int == 2)")
.addStatement("println(\"second\")")
.nextControlFlow("else")
.addStatement("println(\"last\")")
.endControlFlow()
.build()
}
"unfinished in IfStart"{
shouldThrow<UnFinishException> {
createFun {
func("func", nullableBool) {
ifp("bool") {}
}
}
}
}
"unfinished in IfEnd"{
shouldThrow<UnFinishException> {
createFun {
func("func", nullableBool) {
ifp("bool") {}.orElseIfn("bool"){}
}
}
}
}
"lazy if"{
createCodeBlock {
switch("1"){
"1" then If("1==1"){
statement("println(1)")
}.end()
"2" then ifp("null") {
statement("""println("won't print, it's not positive")""")
}.orElseIfn("null"){
statement("""println("won't print, it's not negative")""")
}.orElseIfp("null") {
statement("""println("well this is stupid code")""")
}.orElse {
statement("""println("finally, smth to print")""")
}
"3" then ifn("null"){
statement("""println("won't print, it's not positive")""")
}.orElseIf("true"){
statement("""println(2)""")
}.end()
}
}.toString() shouldBe
"""|when(1) {
| 1 -> if (1==1) {
| println(1)
| }
| 2 -> if ((null) == true) {
| println("won't print, it's not positive")
| } else if ((null) == false) {
| println("won't print, it's not negative")
| } else if ((null) == true) {
| println("well this is stupid code")
| } else {
| println("finally, smth to print")
| }
| 3 -> if ((null) == false) {
| println("won't print, it's not positive")
| } else if (true) {
| println(2)
| }
|}
|""".trimMargin()
}
})