File: decode_test.go

package info (click to toggle)
golang-github-ajg-form 1.5+git20160822.523a5da-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 132 kB
  • sloc: sh: 11; makefile: 2
file content (114 lines) | stat: -rw-r--r-- 2,952 bytes parent folder | download | duplicates (2)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2014 Alvaro J. Genial. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package form

import (
	"net/url"
	"reflect"
	"strings"
	"testing"
)

func TestDecodeString(t *testing.T) {
	for _, c := range testCases(decOnly) {
		if err := DecodeString(c.a, c.s); err != nil {
			t.Errorf("DecodeString(%#v): %s", c.s, err)
		} else if !reflect.DeepEqual(c.a, c.b) {
			t.Errorf("DecodeString(%#v)\n want (%#v)\n have (%#v)", c.s, c.b, c.a)
		}
	}
}

func TestDecodeValues(t *testing.T) {
	for _, c := range testCases(decOnly) {
		vs := mustParseQuery(c.s)

		if err := DecodeValues(c.a, vs); err != nil {
			t.Errorf("DecodeValues(%#v): %s", vs, err)
		} else if !reflect.DeepEqual(c.a, c.b) {
			t.Errorf("DecodeValues(%#v)\n want (%#v)\n have (%#v)", vs, c.b, c.a)
		}
	}
}

func TestDecode(t *testing.T) {
	for _, c := range testCases(decOnly) {
		r := strings.NewReader(c.s)
		d := NewDecoder(r)

		if err := d.Decode(c.a); err != nil {
			t.Errorf("Decode(%#v): %s", r, err)
		} else if !reflect.DeepEqual(c.a, c.b) {
			t.Errorf("Decode(%#v)\n want (%#v)\n have (%#v)", r, c.b, c.a)
		}
	}
}

func TestDecodeIgnoreUnknown(t *testing.T) {
	type simpleStruct struct{ A string }
	var dst simpleStruct
	values := url.Values{
		"b": []string{"2"},
		"A": []string{"1"},
	}
	expected := simpleStruct{A: "1"}
	d := NewDecoder(nil)
	err := d.DecodeValues(&dst, values)
	if err == nil || err.Error() != "b doesn't exist in form.simpleStruct" {
		t.Errorf("Decode(%#v): expected error got nil", values)
	}
	d.IgnoreUnknownKeys(true)
	err = d.DecodeValues(&dst, values)
	if err != nil {
		t.Errorf("Decode(%#v): %s", values, err)
	}
	if !reflect.DeepEqual(dst, expected) {
		t.Errorf("Decode(%#v)\n want (%#v)\n have (%#v)", values, expected, dst)
	}
}

func TestDecodeIgnoreCase(t *testing.T) {
	type simpleStruct struct{ AaAA string }
	var dst simpleStruct
	values := url.Values{
		"aAaA": []string{"1"},
	}
	expected := simpleStruct{AaAA: "1"}
	d := NewDecoder(nil)
	err := d.DecodeValues(&dst, values)
	if err == nil || err.Error() != "aAaA doesn't exist in form.simpleStruct" {
		t.Errorf("Decode(%#v): expected error got nil", values)
	}
	d.IgnoreCase(true)
	err = d.DecodeValues(&dst, values)
	if err != nil {
		t.Errorf("Decode(%#v): %s", values, err)
	}
	if !reflect.DeepEqual(dst, expected) {
		t.Errorf("Decode(%#v)\n want (%#v)\n have (%#v)", values, expected, dst)
	}
}

func TestDecodeIgnoreCasePriority(t *testing.T) {
	type simpleStruct struct {
		Aaa string
		AaA string
		AAA string
	}
	var dst simpleStruct
	values := url.Values{
		"AaA": []string{"1"},
	}
	expected := simpleStruct{AaA: "1"}
	d := NewDecoder(nil)
	d.IgnoreCase(true)
	err := d.DecodeValues(&dst, values)
	if err != nil {
		t.Errorf("Decode(%#v): %s", values, err)
	}
	if !reflect.DeepEqual(dst, expected) {
		t.Errorf("Decode(%#v)\n want (%#v)\n have (%#v)", values, expected, dst)
	}
}