File: node_test.go

package info (click to toggle)
golang-github-ajg-form 1.5%2Bgit20160822.523a5da-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 132 kB
  • sloc: sh: 11; makefile: 2
file content (87 lines) | stat: -rw-r--r-- 2,196 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
// 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 (
	"reflect"
	"testing"
)

type foo int
type bar interface {
	void()
}
type qux struct{}
type zee []bar

func TestCanIndexOrdinally(t *testing.T) {
	for _, c := range []struct {
		x interface{}
		b bool
	}{
		{int(0), false},
		{foo(0), false},
		{qux{}, false},
		{(*int)(nil), false},
		{(*foo)(nil), false},
		{(*bar)(nil), false},
		{(*qux)(nil), false},
		{[]qux{}, true},
		{[5]qux{}, true},
		{&[]foo{}, true},
		{&[5]foo{}, true},
		{zee{}, true},
		{&zee{}, true},
		{map[int]foo{}, false},
		{map[string]interface{}{}, false},
		{map[interface{}]bar{}, false},
		{(chan<- int)(nil), false},
		{(chan bar)(nil), false},
		{(<-chan foo)(nil), false},
	} {
		v := reflect.ValueOf(c.x)
		if b := canIndexOrdinally(v); b != c.b {
			t.Errorf("canIndexOrdinally(%#v)\n want (%#v)\n have (%#v)", v, c.b, b)
		}
	}
}

var escapingTestCases = []struct {
	a, b string
	d, e rune
}{
	{"Foo", "Foo", defaultDelimiter, defaultEscape},
	{"Foo", "Foo", '/', '^'},
	{"Foo.Bar.Qux", "Foo\\.Bar\\.Qux", defaultDelimiter, defaultEscape},
	{"Foo.Bar.Qux", "Foo.Bar.Qux", '/', '^'},
	{"Foo/Bar/Qux", "Foo/Bar/Qux", defaultDelimiter, defaultEscape},
	{"Foo/Bar/Qux", "Foo^/Bar^/Qux", '/', '^'},
	{"0", "0", defaultDelimiter, defaultEscape},
	{"0", "0", '/', '^'},
	{"0.1.2", "0\\.1\\.2", defaultDelimiter, defaultEscape},
	{"0.1.2", "0.1.2", '/', '^'},
	{"0/1/2", "0/1/2", defaultDelimiter, defaultEscape},
	{"0/1/2", "0^/1^/2", '/', '^'},
	{"A\\B", "A\\\\B", defaultDelimiter, defaultEscape},
	{"A\\B", "A\\B", '/', '^'},
	{"A^B", "A^B", defaultDelimiter, defaultEscape},
	{"A^B", "A^^B", '/', '^'},
}

func TestEscape(t *testing.T) {
	for _, c := range escapingTestCases {
		if b := escape(c.d, c.e, c.a); b != c.b {
			t.Errorf("escape(%q, %q, %q)\n want (%#v)\n have (%#v)", c.d, c.e, c.a, c.b, b)
		}
	}
}

func TestUnescape(t *testing.T) {
	for _, c := range escapingTestCases {
		if a := unescape(c.d, c.e, c.b); a != c.a {
			t.Errorf("unescape(%q, %q, %q)\n want (%#v)\n have (%#v)", c.d, c.e, c.b, c.a, a)
		}
	}
}