aboutsummaryrefslogtreecommitdiff
path: root/tests/core/encoding/ini/test_core_ini.odin
blob: 8c554c6b5e485dd78b48678036c60cd4372be714 (plain)
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
#+feature dynamic-literals
package test_core_ini

import "core:encoding/ini"
import "core:strings"
import "core:testing"

@test
parse_ini :: proc(t: ^testing.T) {
	ini_data := `
		[LOG]
		level = "devel"
		file = "/var/log/testing.log"

		[USER]
		first_name = "John"
		surname = "Smith"
	`

	m, err := ini.load_map_from_string(ini_data, context.allocator)
	defer ini.delete_map(m)

	testing.expectf(
		t,
		strings.contains(m["LOG"]["level"], "devel"),
		"Expected m[\"LOG\"][\"level\"] to be equal to 'devel' instead got %v",
		m["LOG"]["level"],
	)
	testing.expectf(
		t,
		strings.contains(m["LOG"]["file"], "/var/log/testing.log"),
		"Expected m[\"LOG\"][\"file\"] to be equal to '/var/log/testing.log' instead got %v",
		m["LOG"]["file"],
	)
	testing.expectf(
		t,
		strings.contains(m["USER"]["first_name"], "John"),
		"Expected m[\"USER\"][\"first_name\"] to be equal to 'John' instead got %v",
		m["USER"]["first_name"],
	)
	testing.expectf(
		t,
		strings.contains(m["USER"]["surname"], "Smith"),
		"Expected m[\"USER\"][\"surname\"] to be equal to 'Smith' instead got %v",
		m["USER"]["surname"],
	)

	testing.expectf(t, err == nil, "Expected `ini.load_map_from_string` to return a nil error, got %v", err)
}

@test
ini_to_string :: proc(t: ^testing.T) {
	m := ini.Map{
		"LEVEL" = {
			"LOG" = "debug",
		},
	}

	str := ini.save_map_to_string(m, context.allocator)
	defer delete(str)
	delete(m["LEVEL"])
	delete(m)

	testing.expectf(
		t,
		strings.contains(str, "[LEVEL]\nLOG = debug"),
		"Expected `ini.save_map_to_string` to return a string equal to \"[LEVEL]LOG = debug\", got %v",
		str,
	)
}

@test
ini_iterator :: proc(t: ^testing.T) {
	ini_data := `
		[LOG]
		level = "devel"
		file = "/var/log/testing.log"

		[USER]
		first_name = "John"
		surname = "Smith"
	`

	i := 0
	iterator := ini.iterator_from_string(ini_data)
	for key, value in ini.iterate(&iterator) {
		if strings.contains(key, "level") {
			testing.expectf(
				t,
				strings.contains(value, "devel"),
				"Expected 'level' to be equal to 'devel' instead got '%v'",
				value,
			)
		} else if strings.contains(key, "file") {
			testing.expectf(
				t,
				strings.contains(value, "/var/log/testing.log"),
				"Expected 'file' to be equal to '/var/log/testing.log' instead got '%v'",
				value,
			)
		} else if strings.contains(key, "first_name") {
			testing.expectf(
				t,
				strings.contains(value, "John"),
				"Expected 'first_name' to be equal to 'John' instead got '%v'",
				value,
			)
		} else if strings.contains(key, "surname") {
			testing.expectf(
				t,
				strings.contains(value, "Smith"),
				"Expected 'surname' to be equal to 'Smith' instead got '%v'",
				value,
			)
		}
		i += 1
		}
	testing.expectf(t, i == 4, "Expected to loop 4 times, only looped %v times", i)
}