File: jsonvalue_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.16.18%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports, experimental
  • size: 93,084 kB
  • sloc: ruby: 193; makefile: 174; xml: 11
file content (93 lines) | stat: -rw-r--r-- 1,877 bytes parent folder | download | duplicates (4)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package protocol

import (
	"fmt"
	"reflect"
	"strings"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
)

var testJSONValueCases = []struct {
	Value  aws.JSONValue
	Mode   EscapeMode
	String string
}{
	{
		Value: aws.JSONValue{
			"abc": 123.,
		},
		Mode:   NoEscape,
		String: `{"abc":123}`,
	},
	{
		Value: aws.JSONValue{
			"abc": 123.,
		},
		Mode:   Base64Escape,
		String: `eyJhYmMiOjEyM30=`,
	},
	{
		Value: aws.JSONValue{
			"abc": 123.,
		},
		Mode:   QuotedEscape,
		String: `"{\"abc\":123}"`,
	},
}

func TestEncodeJSONValue(t *testing.T) {
	for i, c := range testJSONValueCases {
		str, err := EncodeJSONValue(c.Value, c.Mode)
		if err != nil {
			t.Fatalf("%d, expect no error, got %v", i, err)
		}
		if e, a := c.String, str; e != a {
			t.Errorf("%d, expect %v encoded value, got %v", i, e, a)
		}
	}
}

func TestDecodeJSONValue(t *testing.T) {
	for i, c := range testJSONValueCases {
		val, err := DecodeJSONValue(c.String, c.Mode)
		if err != nil {
			t.Fatalf("%d, expect no error, got %v", i, err)
		}
		if e, a := c.Value, val; !reflect.DeepEqual(e, a) {
			t.Errorf("%d, expect %v encoded value, got %v", i, e, a)
		}
	}
}

func TestEncodeJSONValue_PanicUnkownMode(t *testing.T) {
	defer func() {
		if r := recover(); r == nil {
			t.Errorf("expect panic, got none")
		} else {
			reason := fmt.Sprintf("%v", r)
			if e, a := "unknown EscapeMode", reason; !strings.Contains(a, e) {
				t.Errorf("expect %q to be in %v", e, a)
			}
		}
	}()

	val := aws.JSONValue{}

	EncodeJSONValue(val, 123456)
}
func TestDecodeJSONValue_PanicUnkownMode(t *testing.T) {
	defer func() {
		if r := recover(); r == nil {
			t.Errorf("expect panic, got none")
		} else {
			reason := fmt.Sprintf("%v", r)
			if e, a := "unknown EscapeMode", reason; !strings.Contains(a, e) {
				t.Errorf("expect %q to be in %v", e, a)
			}
		}
	}()

	DecodeJSONValue(`{"abc":123}`, 123456)
}