2023-05-19 13:05:18 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2024-03-15 22:48:34 +00:00
|
|
|
"encoding/json"
|
2023-05-19 13:05:18 +00:00
|
|
|
"testing"
|
2023-11-10 20:36:15 +00:00
|
|
|
|
|
|
|
"git.sr.ht/~ashkeel/containers/sync"
|
2023-05-19 13:05:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadJSONToWrapped(t *testing.T) {
|
|
|
|
// Create test struct and object
|
|
|
|
type test struct {
|
|
|
|
TestValue string
|
|
|
|
}
|
|
|
|
testObj := test{
|
|
|
|
TestValue: "test",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode test object to JSON
|
2024-03-15 22:48:34 +00:00
|
|
|
testByt, err := json.Marshal(testObj)
|
2023-05-19 13:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a wrapped instance of the test object
|
|
|
|
wrapped := sync.NewSync[test](test{})
|
|
|
|
|
|
|
|
// Load JSON to wrapped
|
2024-03-15 22:48:34 +00:00
|
|
|
err = LoadJSONToWrapped[test](string(testByt), wrapped)
|
2023-05-19 13:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the wrapped value and compare to original
|
|
|
|
if wrapped.Get().TestValue != testObj.TestValue {
|
|
|
|
t.Fatal("JSON was not loaded correctly")
|
|
|
|
}
|
|
|
|
}
|