208 lines
4.4 KiB
Go
208 lines
4.4 KiB
Go
package dict
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func sampleDict() Dict {
|
|
return Dict{
|
|
"name": "Test Project",
|
|
"version": 1.0,
|
|
"active": true,
|
|
"description": "This is a sample YAML file for testing the Dict package.",
|
|
"metadata": Dict{
|
|
"author": "John Doe",
|
|
"license": "MIT",
|
|
"tags": []interface{}{"sample", "test", "yaml"},
|
|
},
|
|
"configuration": Dict{
|
|
"threads": 4,
|
|
"verbose": true,
|
|
"features": []interface{}{"logging", "monitoring"},
|
|
},
|
|
"resources": []interface{}{
|
|
Dict{
|
|
"type": "database",
|
|
"name": "primary-db",
|
|
"properties": Dict{
|
|
"host": "localhost",
|
|
"port": 5432,
|
|
"credentials": Dict{
|
|
"username": "admin",
|
|
"password": "secret",
|
|
},
|
|
},
|
|
},
|
|
Dict{
|
|
"type": "cache",
|
|
"name": "redis-cache",
|
|
"properties": Dict{
|
|
"host": "localhost",
|
|
"port": 6379,
|
|
"ttl": 3600,
|
|
},
|
|
},
|
|
},
|
|
"nested": Dict{
|
|
"level1": Dict{
|
|
"level2": Dict{
|
|
"level3": Dict{
|
|
"key": "value",
|
|
"array": []interface{}{1, 2, 3, 4.5},
|
|
"nestedArray": []interface{}{
|
|
Dict{"key1": "val1"},
|
|
Dict{
|
|
"key2": "val2",
|
|
"subNested": Dict{
|
|
"subKey1": "subVal1",
|
|
"subKey2": 2,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
"additionalNotes": "This is a multiline string.\nIt can contain new lines and spaces.\nIt's useful for longer descriptions or content.",
|
|
}
|
|
}
|
|
|
|
func TestSampleDict(t *testing.T) {
|
|
originalDict, err := loadYaml("test_data/dict2.yaml")
|
|
if err != nil {
|
|
t.Fatalf("loadYaml() returned an error: %v", err)
|
|
}
|
|
|
|
dict := sampleDict()
|
|
|
|
if !reflect.DeepEqual(dict, originalDict) {
|
|
t.Errorf("Expected dict to be %v, got %v", originalDict, dict)
|
|
}
|
|
|
|
}
|
|
|
|
func TestNew(t *testing.T) {
|
|
d := New()
|
|
if d == nil {
|
|
t.Error("New() should not return nil")
|
|
}
|
|
if len(d) != 0 {
|
|
t.Errorf("New() should return an empty dict, got %v", d)
|
|
}
|
|
}
|
|
|
|
func TestIsDict(t *testing.T) {
|
|
d := Dict{
|
|
"key1": "value1",
|
|
}
|
|
|
|
r := IsDict(d)
|
|
if !r {
|
|
t.Errorf("Expected true, got %v", r)
|
|
}
|
|
|
|
r = IsDict(12)
|
|
if r {
|
|
t.Errorf("Expected false, got %v", r)
|
|
}
|
|
|
|
r = IsDict("Cool!")
|
|
if r {
|
|
t.Errorf("Expected false, got %v", r)
|
|
}
|
|
|
|
var v1 interface{}
|
|
r = IsDict(v1)
|
|
if r {
|
|
t.Errorf("Expected false, got %v", r)
|
|
}
|
|
|
|
var v2 []interface{}
|
|
r = IsDict(v2)
|
|
if r {
|
|
t.Errorf("Expected false, got %v", r)
|
|
}
|
|
|
|
var v3 map[string]interface{}
|
|
r = IsDict(v3)
|
|
if r {
|
|
t.Errorf("Expected false, got %v", r)
|
|
}
|
|
}
|
|
|
|
func TestCopy(t *testing.T) {
|
|
originalDict, err := loadYaml("test_data/dict2.yaml")
|
|
if err != nil {
|
|
t.Fatalf("loadYaml() returned an error: %v", err)
|
|
}
|
|
|
|
newDict1 := Dict{}
|
|
Copy(&newDict1, originalDict)
|
|
if !reflect.DeepEqual(newDict1, originalDict) {
|
|
t.Errorf("Expected dict to be %v, got %v", originalDict, newDict1)
|
|
}
|
|
|
|
newDict2 := make(Dict)
|
|
Copy(&newDict2, originalDict)
|
|
if !reflect.DeepEqual(newDict2, originalDict) {
|
|
t.Errorf("Expected dict to be %v, got %v", originalDict, newDict2)
|
|
}
|
|
|
|
var newDict3 Dict
|
|
Copy(&newDict3, originalDict)
|
|
if !reflect.DeepEqual(newDict3, originalDict) {
|
|
t.Errorf("Expected dict to be %v, got %v", originalDict, newDict3)
|
|
}
|
|
}
|
|
|
|
func TestCopyFrom(t *testing.T) {
|
|
originalDict, err := loadYaml("test_data/dict2.yaml")
|
|
if err != nil {
|
|
t.Fatalf("loadYaml() returned an error: %v", err)
|
|
}
|
|
|
|
newDict1 := Dict{}
|
|
newDict1.CopyFrom(originalDict)
|
|
if !reflect.DeepEqual(newDict1, originalDict) {
|
|
t.Errorf("Expected dict to be %v, got %v", originalDict, newDict1)
|
|
}
|
|
|
|
newDict2 := make(Dict)
|
|
newDict2.CopyFrom(originalDict)
|
|
if !reflect.DeepEqual(newDict2, originalDict) {
|
|
t.Errorf("Expected dict to be %v, got %v", originalDict, newDict2)
|
|
}
|
|
|
|
var newDict3 Dict
|
|
newDict3.CopyFrom(originalDict)
|
|
if !reflect.DeepEqual(newDict3, originalDict) {
|
|
t.Errorf("Expected dict to be %v, got %v", originalDict, newDict3)
|
|
}
|
|
}
|
|
|
|
func TestCopyTo(t *testing.T) {
|
|
originalDict, err := loadYaml("test_data/dict2.yaml")
|
|
if err != nil {
|
|
t.Fatalf("loadYaml() returned an error: %v", err)
|
|
}
|
|
|
|
newDict1 := Dict{}
|
|
originalDict.CopyTo(&newDict1)
|
|
if !reflect.DeepEqual(newDict1, originalDict) {
|
|
t.Errorf("Expected dict to be %v, got %v", originalDict, newDict1)
|
|
}
|
|
|
|
newDict2 := make(Dict)
|
|
originalDict.CopyTo(&newDict2)
|
|
if !reflect.DeepEqual(newDict2, originalDict) {
|
|
t.Errorf("Expected dict to be %v, got %v", originalDict, newDict2)
|
|
}
|
|
|
|
var newDict3 Dict
|
|
originalDict.CopyTo(&newDict3)
|
|
if !reflect.DeepEqual(newDict3, originalDict) {
|
|
t.Errorf("Expected dict to be %v, got %v", originalDict, newDict3)
|
|
}
|
|
}
|