go/json_test.go
Snider 8626710f9d feat: add JSON primitives + fix api.go placeholder
core.JSONMarshal(), JSONMarshalString(), JSONUnmarshal(), JSONUnmarshalString()
wrap encoding/json so consumers don't import it directly.
Same guardrail pattern as string.go wraps strings.

api.go Call() now uses JSONMarshalString instead of placeholder optionsToJSON.
7 AX-7 tests. 490 tests total, 84.8% coverage.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-25 17:40:55 +00:00

63 lines
1.4 KiB
Go

package core_test
import (
"testing"
. "dappco.re/go/core"
"github.com/stretchr/testify/assert"
)
type testJSON struct {
Name string `json:"name"`
Port int `json:"port"`
}
// --- JSONMarshal ---
func TestJson_JSONMarshal_Good(t *testing.T) {
r := JSONMarshal(testJSON{Name: "brain", Port: 8080})
assert.True(t, r.OK)
assert.Contains(t, string(r.Value.([]byte)), `"name":"brain"`)
}
func TestJson_JSONMarshal_Bad_Unmarshalable(t *testing.T) {
r := JSONMarshal(make(chan int))
assert.False(t, r.OK)
}
// --- JSONMarshalString ---
func TestJson_JSONMarshalString_Good(t *testing.T) {
s := JSONMarshalString(testJSON{Name: "x", Port: 1})
assert.Contains(t, s, `"name":"x"`)
}
func TestJson_JSONMarshalString_Ugly_Fallback(t *testing.T) {
s := JSONMarshalString(make(chan int))
assert.Equal(t, "{}", s)
}
// --- JSONUnmarshal ---
func TestJson_JSONUnmarshal_Good(t *testing.T) {
var target testJSON
r := JSONUnmarshal([]byte(`{"name":"brain","port":8080}`), &target)
assert.True(t, r.OK)
assert.Equal(t, "brain", target.Name)
assert.Equal(t, 8080, target.Port)
}
func TestJson_JSONUnmarshal_Bad_Invalid(t *testing.T) {
var target testJSON
r := JSONUnmarshal([]byte(`not json`), &target)
assert.False(t, r.OK)
}
// --- JSONUnmarshalString ---
func TestJson_JSONUnmarshalString_Good(t *testing.T) {
var target testJSON
r := JSONUnmarshalString(`{"name":"x","port":1}`, &target)
assert.True(t, r.OK)
assert.Equal(t, "x", target.Name)
}