File: json_test.go

package info (click to toggle)
golang-github-spdx-tools-golang 0.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,252 kB
  • sloc: xml: 428; makefile: 22; ansic: 5; python: 2
file content (60 lines) | stat: -rw-r--r-- 871 bytes parent folder | download
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package marshal

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func Test_MarshalJSON(t *testing.T) {
	tests := []struct {
		name     string
		in       interface{}
		expected string
	}{
		{
			name:     "basic usage",
			in:       "<html>",
			expected: `"<html>"`,
		},
		{
			name: "within MarshalJSON callbacks",
			in: s1{
				s2{
					s3{
						Value: "<html>",
					},
				},
			},
			expected: `{"S2":{"S3":{"Value":"<html>"}}}`,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			got, err := JSON(test.in)
			require.NoError(t, err)
			require.Equal(t, test.expected, string(got))
		})
	}
}

type s1 struct {
	S2 s2
}

type s2 struct {
	S3 s3
}

func (s *s2) MarshalJSON() ([]byte, error) {
	return JSON(s.S3)
}

type s3 struct {
	Value string
}

func (s *s3) MarshalJSON() ([]byte, error) {
	return JSON(s.Value)
}