File: structtag_test.go

package info (click to toggle)
golang-github-facebookgo-structtag 0.0~git20150214.217e25f-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 76 kB
  • sloc: makefile: 2
file content (78 lines) | stat: -rw-r--r-- 1,366 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
package structtag_test

import (
	"testing"

	"github.com/facebookgo/structtag"
)

func TestExtract(t *testing.T) {
	cases := []struct {
		Name  string // Input name
		Tag   string // Input tag
		Found bool   // Expected found status
		Value string // Expected value
		Error bool   // Indicates if an error is expected
	}{
		{
			Name:  "inject",
			Tag:   `inject:`,
			Error: true,
		},
		{
			Name:  "inject",
			Tag:   `inject:"`,
			Error: true,
		},
		{
			Name:  "inject",
			Tag:   `inject:""`,
			Found: true,
		},
		{
			Name:  "inject",
			Tag:   `inject:"a"`,
			Found: true,
			Value: "a",
		},
		{
			Name:  "inject",
			Tag:   ` inject:"a"`,
			Found: true,
			Value: "a",
		},
		{
			Name: "inject",
			Tag:  `  `,
		},
		{
			Name:  "inject",
			Tag:   `inject:"\"a"`,
			Found: true,
			Value: `"a`,
		},
	}

	for _, e := range cases {
		found, value, err := structtag.Extract(e.Name, e.Tag)

		if !e.Error && err != nil {
			t.Fatalf("unexpected error %s for case %+v", err, e)
		}
		if e.Error && err == nil {
			t.Fatalf("did not get expected error for case %+v", e)
		}

		if found != e.Found {
			if e.Found {
				t.Fatalf("did not find value when expecting to %+v", e)
			} else {
				t.Fatalf("found value when not expecting to %+v", e)
			}
		}

		if value != e.Value {
			t.Fatalf(`found unexpected value "%s" for %+v`, value, e)
		}
	}
}