v2 / vlib / v / tests / comptime_generic_comptime_variant_test.v
38 lines · 33 sloc · 817 bytes · 6ab25623e3006e10a4196d5157833c4cd9dab509
Raw
1pub type DesiredCapabilities = FireFox | Edge
2
3struct FireFox {
4 browser_name string = 'firefox'
5 accept_insecure_certs bool = true
6 moz_debugger_address bool = true
7}
8
9struct Edge {
10 browser_name string = 'MicrosoftEdge'
11}
12
13fn struct_values[T](s T) map[string]string {
14 mut res := map[string]string{}
15 $if T is $struct {
16 $for field in T.fields {
17 res[field.name] = s.$(field.name).str()
18 }
19 }
20 return res
21}
22
23fn useit(dc DesiredCapabilities) string {
24 $for v in dc.variants {
25 if dc is v {
26 $if v is $struct {
27 result := struct_values(dc)
28 return result.str()
29 }
30 }
31 }
32 return ''
33}
34
35fn test_main() {
36 assert useit(Edge{}) == "{'browser_name': 'MicrosoftEdge'}"
37 assert useit(FireFox{}) == "{'browser_name': 'firefox', 'accept_insecure_certs': 'true', 'moz_debugger_address': 'true'}"
38}
39