1
0

client_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package localapi
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestPrettyPreservesKeyOrderAndPrecision(t *testing.T) {
  8. // Keys arrive in the order the daemon wrote them and a uint64 past 2^53 must not be
  9. // rounded, so the output has to come from re-indenting the bytes rather than from
  10. // decoding into `any` and encoding again.
  11. raw := json.RawMessage(`{"z":18446744073709551615,"a":{"y":[1,2],"b":true},"m":"text"}`)
  12. out, ok := Pretty(raw)
  13. assert.True(t, ok)
  14. //nolint:testifylint // the layout is what is under test, which JSONEq would not see
  15. assert.Equal(t, `{
  16. "z": 18446744073709551615,
  17. "a": {
  18. "y": [
  19. 1,
  20. 2
  21. ],
  22. "b": true
  23. },
  24. "m": "text"
  25. }`, out)
  26. }
  27. func TestPrettyKeepsAStringValueQuoted(t *testing.T) {
  28. // A value that is a JSON string is still JSON to whoever asked for it: it has nothing
  29. // to indent and comes back as it is, never unquoted.
  30. out, ok := Pretty(json.RawMessage(`"some string"`))
  31. assert.True(t, ok)
  32. assert.Equal(t, `"some string"`, out)
  33. out, ok = Pretty(json.RawMessage(` "padded"`))
  34. assert.True(t, ok)
  35. assert.Equal(t, `"padded"`, out)
  36. }
  37. func TestPrettyRejectsEmptyAndInvalid(t *testing.T) {
  38. _, ok := Pretty(nil)
  39. assert.False(t, ok)
  40. _, ok = Pretty(json.RawMessage(" "))
  41. assert.False(t, ok)
  42. _, ok = Pretty(json.RawMessage(`{"a":`))
  43. assert.False(t, ok)
  44. }