fx/pkg/dict/dict_test.go

66 lines
1.2 KiB
Go
Raw Normal View History

2022-04-17 20:57:12 +00:00
package dict
2022-03-11 15:15:24 +00:00
import "testing"
func Test_dict(t *testing.T) {
2022-04-17 20:57:12 +00:00
d := NewDict()
d.Set("number", 3)
v, _ := d.Get("number")
2022-03-11 15:15:24 +00:00
if v.(int) != 3 {
t.Error("Set number")
}
// string
2022-04-17 20:57:12 +00:00
d.Set("string", "x")
v, _ = d.Get("string")
2022-03-11 15:15:24 +00:00
if v.(string) != "x" {
t.Error("Set string")
}
// string slice
2022-04-17 20:57:12 +00:00
d.Set("strings", []string{
2022-03-11 15:15:24 +00:00
"t",
"u",
})
2022-04-17 20:57:12 +00:00
v, _ = d.Get("strings")
2022-03-11 15:15:24 +00:00
if v.([]string)[0] != "t" {
t.Error("Set strings first index")
}
if v.([]string)[1] != "u" {
t.Error("Set strings second index")
}
// mixed slice
2022-04-17 20:57:12 +00:00
d.Set("mixed", []interface{}{
2022-03-11 15:15:24 +00:00
1,
"1",
})
2022-04-17 20:57:12 +00:00
v, _ = d.Get("mixed")
2022-03-11 15:15:24 +00:00
if v.([]interface{})[0].(int) != 1 {
t.Error("Set mixed int")
}
if v.([]interface{})[1].(string) != "1" {
t.Error("Set mixed string")
}
// overriding existing key
2022-04-17 20:57:12 +00:00
d.Set("number", 4)
v, _ = d.Get("number")
2022-03-11 15:15:24 +00:00
if v.(int) != 4 {
t.Error("Override existing key")
}
2022-04-17 20:57:12 +00:00
// Keys
2022-03-11 15:15:24 +00:00
expectedKeys := []string{
"number",
"string",
"strings",
"mixed",
}
2022-04-17 20:57:12 +00:00
for i, key := range d.Keys {
2022-03-11 15:15:24 +00:00
if key != expectedKeys[i] {
t.Error("Keys method", key, "!=", expectedKeys[i])
}
}
for i, key := range expectedKeys {
if key != expectedKeys[i] {
t.Error("Keys method", key, "!=", expectedKeys[i])
}
}
}