File: encode_test.go

package info (click to toggle)
golang-github-tent-canonical-json-go 0.0~git20130607.0.96e4ba3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 76 kB
  • sloc: makefile: 2
file content (37 lines) | stat: -rw-r--r-- 986 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package cjson

import (
	"testing"
)

type marshaling struct{}

func (m marshaling) MarshalJSON() ([]byte, error) { return []byte("\t\n  {\"b\":234,\"a\":123}"), nil }

func TestCanonicalization(t *testing.T) {
	input := struct {
		C map[string]interface{} `json:"c"`
		A string                 `json:"a"`
		D []int                  `json:"d"`
		B int                    `json:"b"`
		E *marshaling            `json:"e"`
	}{map[string]interface{}{"b": "b", "a": "\n\r", "c": "\"\\<>"}, "a", []int{1, 2, 3}, 1, &marshaling{}}
	expected := `{"a":"a","b":1,"c":{"a":"` + "\n\r" + `","b":"b","c":"\"\\<>"},"d":[1,2,3],"e":{"a":123,"b":234}}`

	output, err := Marshal(input)
	if err != nil {
		t.Errorf("got err = %v, want nil", err)
	}
	if expected != string(output) {
		t.Errorf("got %s, want %s", string(output), expected)
	}
}

func TestFloatError(t *testing.T) {
	input := struct{ A float64 }{1.1}

	_, err := Marshal(input)
	if err == nil {
		t.Errorf("want float error, got nil")
	}
}