193 lines
4.7 KiB
Go
193 lines
4.7 KiB
Go
package langext
|
|
|
|
import (
|
|
"encoding/json"
|
|
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestTryPrettyPrintJsonValid(t *testing.T) {
|
|
in := `{"a":1,"b":2}`
|
|
out := TryPrettyPrintJson(in)
|
|
if !strings.Contains(out, "\n") {
|
|
t.Errorf("expected pretty-printed result with newlines, got %q", out)
|
|
}
|
|
if !strings.Contains(out, `"a"`) {
|
|
t.Errorf("expected key in result, got %q", out)
|
|
}
|
|
}
|
|
|
|
func TestTryPrettyPrintJsonInvalidPassThrough(t *testing.T) {
|
|
in := `not valid json`
|
|
tst.AssertEqual(t, TryPrettyPrintJson(in), in)
|
|
}
|
|
|
|
func TestPrettyPrintJsonValid(t *testing.T) {
|
|
out, ok := PrettyPrintJson(`{"a":1}`)
|
|
tst.AssertEqual(t, ok, true)
|
|
if !strings.Contains(out, "\n") {
|
|
t.Errorf("expected formatted output, got %q", out)
|
|
}
|
|
}
|
|
|
|
func TestPrettyPrintJsonInvalid(t *testing.T) {
|
|
in := `not json`
|
|
out, ok := PrettyPrintJson(in)
|
|
tst.AssertEqual(t, ok, false)
|
|
tst.AssertEqual(t, out, in)
|
|
}
|
|
|
|
func TestPatchJsonString(t *testing.T) {
|
|
in := `{"a":1,"b":2}`
|
|
out, err := PatchJson(in, "c", 3)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
var m map[string]any
|
|
if err := json.Unmarshal([]byte(out), &m); err != nil {
|
|
t.Fatalf("invalid json result: %v", err)
|
|
}
|
|
if v, ok := m["c"].(float64); !ok || v != 3 {
|
|
t.Errorf("expected c=3, got %v", m["c"])
|
|
}
|
|
if v, ok := m["a"].(float64); !ok || v != 1 {
|
|
t.Errorf("expected a=1, got %v", m["a"])
|
|
}
|
|
}
|
|
|
|
func TestPatchJsonBytes(t *testing.T) {
|
|
in := []byte(`{"a":1}`)
|
|
out, err := PatchJson(in, "b", "hello")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal(out, &m); err != nil {
|
|
t.Fatalf("invalid json result: %v", err)
|
|
}
|
|
if v, ok := m["b"].(string); !ok || v != "hello" {
|
|
t.Errorf("expected b=hello, got %v", m["b"])
|
|
}
|
|
}
|
|
|
|
func TestPatchJsonInvalid(t *testing.T) {
|
|
_, err := PatchJson("not json", "k", "v")
|
|
if err == nil {
|
|
t.Errorf("expected error on invalid json")
|
|
}
|
|
}
|
|
|
|
func TestPatchRemJson(t *testing.T) {
|
|
in := `{"a":1,"b":2}`
|
|
out, err := PatchRemJson(in, "a")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal([]byte(out), &m); err != nil {
|
|
t.Fatalf("invalid json result: %v", err)
|
|
}
|
|
if _, exists := m["a"]; exists {
|
|
t.Errorf("expected key 'a' to be removed")
|
|
}
|
|
if v, ok := m["b"].(float64); !ok || v != 2 {
|
|
t.Errorf("expected b=2, got %v", m["b"])
|
|
}
|
|
}
|
|
|
|
func TestPatchRemJsonMissingKey(t *testing.T) {
|
|
in := `{"a":1}`
|
|
out, err := PatchRemJson(in, "missing")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal([]byte(out), &m); err != nil {
|
|
t.Fatalf("invalid json: %v", err)
|
|
}
|
|
if v, ok := m["a"].(float64); !ok || v != 1 {
|
|
t.Errorf("expected a=1, got %v", m["a"])
|
|
}
|
|
}
|
|
|
|
func TestMarshalJsonOrPanic(t *testing.T) {
|
|
tst.AssertEqual(t, MarshalJsonOrPanic(42), "42")
|
|
tst.AssertEqual(t, MarshalJsonOrPanic("hi"), `"hi"`)
|
|
}
|
|
|
|
func TestMarshalJsonOrPanicPanics(t *testing.T) {
|
|
defer func() {
|
|
if r := recover(); r == nil {
|
|
t.Errorf("expected panic on un-marshalable input")
|
|
}
|
|
}()
|
|
// channels can't be marshaled
|
|
MarshalJsonOrPanic(make(chan int))
|
|
}
|
|
|
|
func TestMarshalJsonOrDefault(t *testing.T) {
|
|
tst.AssertEqual(t, MarshalJsonOrDefault(42, "def"), "42")
|
|
tst.AssertEqual(t, MarshalJsonOrDefault(make(chan int), "def"), "def")
|
|
}
|
|
|
|
func TestMarshalJsonOrNilSuccess(t *testing.T) {
|
|
p := MarshalJsonOrNil(42)
|
|
if p == nil {
|
|
t.Fatalf("expected non-nil pointer")
|
|
}
|
|
tst.AssertEqual(t, *p, "42")
|
|
}
|
|
|
|
func TestMarshalJsonOrNilError(t *testing.T) {
|
|
p := MarshalJsonOrNil(make(chan int))
|
|
if p != nil {
|
|
t.Errorf("expected nil pointer on error, got %v", *p)
|
|
}
|
|
}
|
|
|
|
func TestMarshalJsonIndentOrPanic(t *testing.T) {
|
|
out := MarshalJsonIndentOrPanic(map[string]int{"a": 1}, "", " ")
|
|
if !strings.Contains(out, "\n") {
|
|
t.Errorf("expected indented output, got %q", out)
|
|
}
|
|
}
|
|
|
|
func TestMarshalJsonIndentOrDefault(t *testing.T) {
|
|
out := MarshalJsonIndentOrDefault(make(chan int), "", " ", "DEF")
|
|
tst.AssertEqual(t, out, "DEF")
|
|
}
|
|
|
|
func TestMarshalJsonIndentOrNilSuccess(t *testing.T) {
|
|
p := MarshalJsonIndentOrNil(map[string]int{"a": 1}, "", " ")
|
|
if p == nil || !strings.Contains(*p, "\n") {
|
|
t.Errorf("expected indented JSON pointer")
|
|
}
|
|
}
|
|
|
|
func TestMarshalJsonIndentOrNilFailure(t *testing.T) {
|
|
p := MarshalJsonIndentOrNil(make(chan int), "", " ")
|
|
if p != nil {
|
|
t.Errorf("expected nil pointer on error")
|
|
}
|
|
}
|
|
|
|
func TestHTypeIsMap(t *testing.T) {
|
|
h := H{"a": 1}
|
|
out, err := json.Marshal(h)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
tst.AssertEqual(t, string(out), `{"a":1}`)
|
|
}
|
|
|
|
func TestATypeIsArray(t *testing.T) {
|
|
a := A{1, "x", true}
|
|
out, err := json.Marshal(a)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
tst.AssertEqual(t, string(out), `[1,"x",true]`)
|
|
}
|