mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-19 01:01:31 -07:00
Allow to marshal and unmarshal maps with int keys
This commit is contained in:
@@ -3,6 +3,10 @@ package test_core_json
|
||||
import "core:encoding/json"
|
||||
import "core:testing"
|
||||
import "core:mem/virtual"
|
||||
import "core:fmt"
|
||||
import "base:runtime"
|
||||
import "core:log"
|
||||
import "core:strings"
|
||||
|
||||
@test
|
||||
parse_json :: proc(t: ^testing.T) {
|
||||
@@ -368,4 +372,40 @@ utf8_string_of_multibyte_characters :: proc(t: ^testing.T) {
|
||||
val, err := json.parse_string(`"🐛✅"`)
|
||||
defer json.destroy_value(val)
|
||||
testing.expectf(t, err == nil, "Expected `json.parse` to return nil, got %v", err)
|
||||
}
|
||||
|
||||
@test
|
||||
map_with_integer_keys :: proc(t: ^testing.T) {
|
||||
my_map := make(map[i32]string)
|
||||
defer delete_map(my_map)
|
||||
|
||||
my_map[-1] = "a"
|
||||
my_map[0] = "b"
|
||||
my_map[42] = "c"
|
||||
my_map[99999999] = "d"
|
||||
|
||||
marshaled_data, marshal_err := json.marshal(my_map)
|
||||
defer delete(marshaled_data)
|
||||
|
||||
testing.expectf(t, marshal_err == nil, "Expected `json.marshal` to return nil error, got %v", marshal_err)
|
||||
|
||||
my_map2 := make(map[i32]string)
|
||||
defer delete_map(my_map2)
|
||||
|
||||
unmarshal_err := json.unmarshal(marshaled_data, &my_map2)
|
||||
defer for key, item in my_map2 {
|
||||
runtime.delete_string(item)
|
||||
}
|
||||
testing.expectf(t, unmarshal_err == nil, "Expected `json.unmarshal` to return nil, got %v", unmarshal_err)
|
||||
|
||||
testing.expectf(t, len(my_map) == len(my_map2), "Expected %v map items to have been unmarshaled, got %v", len(my_map), len(my_map2))
|
||||
|
||||
for key, item in my_map {
|
||||
testing.expectf(t, key in my_map2, "Expected key %v to be present in unmarshaled map", key)
|
||||
|
||||
value_from_map2, ok := my_map2[key]
|
||||
if ok {
|
||||
testing.expectf(t, runtime.string_eq(item, my_map2[key]), "Expected value %s to be present in unmarshaled map", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user