v2 / vlib / v / tests / enums / enum_val_test.v
26 lines · 23 sloc · 485 bytes · 4dc66e2675668abc4929b28139e41458ed09206c
Raw
1enum Traffic {
2 rx_bytes
3 tx_bytes
4}
5
6fn (t Traffic) str() string {
7 return match t {
8 .rx_bytes { 'rx-bytes' }
9 .tx_bytes { 'tx-bytes' }
10 }
11}
12
13fn Traffic.from_string(s string) ?Traffic {
14 return match s {
15 'rx-bytes' { .rx_bytes }
16 'tx-bytes' { .tx_bytes }
17 else { none }
18 }
19}
20
21fn test_main() {
22 traffic := Traffic.from_string('rx-bytes') or { return }
23 assert Traffic.rx_bytes.str() == 'rx-bytes'
24 assert '${traffic}' == 'rx-bytes'
25 assert '${Traffic.rx_bytes}' == 'rx-bytes'
26}
27