File: issues_test.go

package info (click to toggle)
golang-gopkg-gcfg.v1 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 268 kB
  • sloc: makefile: 7
file content (110 lines) | stat: -rw-r--r-- 2,493 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
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
package gcfg

import (
	"fmt"
	"math/big"
	"strings"
	"testing"
)

type Config1 struct {
	Section struct {
		Int    int
		BigInt big.Int
	}
}

var testsGoogleCodeIssue1 = []struct {
	cfg      string
	typename string
}{
	{"[section]\nint=X", "int"},
	{"[section]\nint=", "int"},
	{"[section]\nint=1A", "int"},
	{"[section]\nbigint=X", "big.Int"},
	{"[section]\nbigint=", "big.Int"},
	{"[section]\nbigint=1A", "big.Int"},
}

// Value parse error should:
//  - include plain type name
//  - not include reflect internals
func TestGoogleCodeIssue1(t *testing.T) {
	for i, tt := range testsGoogleCodeIssue1 {
		var c Config1
		err := ReadStringInto(&c, tt.cfg)
		switch {
		case err == nil:
			t.Errorf("%d fail: got ok; wanted error", i)
		case !strings.Contains(err.Error(), tt.typename):
			t.Errorf("%d fail: error message doesn't contain type name %q: %v",
				i, tt.typename, err)
		case strings.Contains(err.Error(), "reflect"):
			t.Errorf("%d fail: error message includes reflect internals: %v",
				i, err)
		default:
			t.Logf("%d pass: %v", i, err)
		}
	}
}

type confGoogleCodeIssue2 struct{ Main struct{ Foo string } }

var testsGoogleCodeIssue2 = []readtest{
	{"[main]\n;\nfoo = bar\n", &confGoogleCodeIssue2{struct{ Foo string }{"bar"}}, true},
	{"[main]\r\n;\r\nfoo = bar\r\n", &confGoogleCodeIssue2{struct{ Foo string }{"bar"}}, true},
}

func TestGoogleCodeIssue2(t *testing.T) {
	for i, tt := range testsGoogleCodeIssue2 {
		id := fmt.Sprintf("issue2:%d", i)
		testRead(t, id, tt)
	}
}

type ConfigIssue11 struct {
	Sect struct {
		Var bool
	}
}

var testsIssue11 = []struct {
	cfg string
	loc string
}{
	{"[Sect]\nVar=X", "Sect"},
	{"[Sect]\nVar=X", "Var"},
}

// Value parse error should include location
func TestIssue11(t *testing.T) {
	for i, tt := range testsIssue11 {
		var c ConfigIssue11
		err := ReadStringInto(&c, tt.cfg)
		switch {
		case err == nil:
			t.Errorf("%d fail: got ok; wanted error", i)
		case !strings.Contains(err.Error(), tt.loc):
			t.Errorf("%d fail: error message doesn't contain location %q: %v",
				i, tt.loc, err)
		default:
			t.Logf("%d pass: %v", i, err)
		}
	}
}

// Escaped double quote should be supported in "raw" string literals
func TestIssue12(t *testing.T) {
	var c struct {
		Section struct {
			Name string
		}
	}
	err := ReadFileInto(&c, "testdata/issue12.gcfg")
	if err != nil {
		t.Fatalf("fail: want ok, got error %v", err)
	}
	if c.Section.Name != `"value"` {
		t.Errorf("fail: want `\"value\"`, got %q", c.Section.Name)
	}
}