-
-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3950 from Ronaldr1985/master
Add tests for encoding/ini
- Loading branch information
Showing
1 changed file
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package test_core_ini | ||
|
||
import "base:runtime" | ||
import "core:encoding/ini" | ||
import "core:mem/virtual" | ||
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]LOG = 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) | ||
} |