v2 / vlib / toml / tests / datetime_test.v
72 lines · 61 sloc · 1.84 KB · ee6b23c2a780522c1a4ecf31eda44bf883c98954
Raw
1import toml
2
3fn test_dates() {
4 toml_txt := '
5 # Offset Date-Time
6 odt1 = 1979-05-27T07:32:00Z
7 odt2 = 1979-05-27T00:32:00-07:00
8 odt3 = 1979-05-27T00:32:00.999999-07:00
9 odt4 = 1979-05-27 07:32:00Z
10 # Local Date-Time
11 ldt1 = 1979-05-27T07:32:00
12 ldt2 = 1979-05-27T00:32:00.999999
13 # Local Date
14 ld1 = 1979-05-27
15 # Local Time
16 lt1 = 07:32:00
17 lt2 = 00:32:00.999999
18'
19 toml_doc := toml.parse_text(toml_txt) or { panic(err) }
20
21 // Re-use vars
22 mut odt_time := toml.DateTime{'1979-05-27T07:32:00Z'}
23 mut odt_str := toml_doc.value('odt1').string()
24
25 // odt1 test section
26 assert odt_str == '1979-05-27T07:32:00Z'
27 odt1 := toml_doc.value('odt1')
28 assert odt1.datetime() == odt_time
29
30 // odt2 test section
31 odt_time = toml.DateTime{'1979-05-27T00:32:00-07:00'}
32 odt2 := toml_doc.value('odt2')
33 assert odt2.datetime() == odt_time
34
35 // odt3 test section
36 odt_time = toml.DateTime{'1979-05-27T00:32:00.999999-07:00'}
37 odt3 := toml_doc.value('odt3')
38 assert odt3.datetime() == odt_time
39
40 // odt4 test section
41 odt_time = toml.DateTime{'1979-05-27 07:32:00Z'}
42 odt4 := toml_doc.value('odt4')
43 assert odt4.datetime() == odt_time
44
45 // ldt1 test section
46 odt_time = toml.DateTime{'1979-05-27T07:32:00'}
47 ldt1 := toml_doc.value('ldt1')
48 assert ldt1.datetime() == odt_time
49
50 // ldt2 test section
51 odt_time = toml.DateTime{'1979-05-27T00:32:00.999999'}
52 ldt2 := toml_doc.value('ldt2')
53 assert ldt2.datetime() == odt_time
54
55 // ld1 test section
56 od_time := toml.Date{'1979-05-27'}
57 ld1 := toml_doc.value('ld1')
58 assert ld1.date() == od_time
59 assert ld1.string() == '1979-05-27'
60
61 // lt1 test section
62 mut ot_time := toml.Time{'07:32:00'}
63 lt1 := toml_doc.value('lt1')
64 assert lt1.time() == ot_time
65 assert lt1.string() == '07:32:00'
66
67 // lt2 test section
68 ot_time = toml.Time{'00:32:00.999999'}
69 lt2 := toml_doc.value('lt2')
70 assert lt2.time() == ot_time
71 assert lt2.string() == '00:32:00.999999'
72}
73