From b584eeaade7cece9ff8bc5a12792693c303391e3 Mon Sep 17 00:00:00 2001 From: Ronald Date: Sat, 20 Jul 2024 16:53:54 +0100 Subject: [PATCH 1/3] Add encoding/ini tests --- tests/core/encoding/ini/test_core_ini.odin | 120 +++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tests/core/encoding/ini/test_core_ini.odin diff --git a/tests/core/encoding/ini/test_core_ini.odin b/tests/core/encoding/ini/test_core_ini.odin new file mode 100644 index 000000000..1e6cc246b --- /dev/null +++ b/tests/core/encoding/ini/test_core_ini.odin @@ -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 +prase_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) + 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) +} From f560b14d105b25e30c08fed76f8e5324d378ee90 Mon Sep 17 00:00:00 2001 From: Ronald Date: Sat, 20 Jul 2024 17:10:19 +0100 Subject: [PATCH 2/3] Fix typo in name of test --- tests/core/encoding/ini/test_core_ini.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core/encoding/ini/test_core_ini.odin b/tests/core/encoding/ini/test_core_ini.odin index 1e6cc246b..9106f61bd 100644 --- a/tests/core/encoding/ini/test_core_ini.odin +++ b/tests/core/encoding/ini/test_core_ini.odin @@ -7,7 +7,7 @@ import "core:strings" import "core:testing" @test -prase_ini :: proc(t: ^testing.T) { +parse_ini :: proc(t: ^testing.T) { ini_data := ` [LOG] level = "devel" From e0a8bd04d5cbccb003bd80486089f0ea11a1857e Mon Sep 17 00:00:00 2001 From: Ronald Date: Sat, 20 Jul 2024 17:10:34 +0100 Subject: [PATCH 3/3] Ensure deletion of maybe is delayed until we're finished with it. --- tests/core/encoding/ini/test_core_ini.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core/encoding/ini/test_core_ini.odin b/tests/core/encoding/ini/test_core_ini.odin index 9106f61bd..6e6c8152e 100644 --- a/tests/core/encoding/ini/test_core_ini.odin +++ b/tests/core/encoding/ini/test_core_ini.odin @@ -19,7 +19,7 @@ parse_ini :: proc(t: ^testing.T) { ` m, err := ini.load_map_from_string(ini_data, context.allocator) - ini.delete_map(m) + defer ini.delete_map(m) testing.expectf( t,