summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/lunixbochs/struc/parse_test.go
blob: 861fdd192342f2ad91678880474264ecfbc43085 (plain)
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
package struc

import (
	"bytes"
	"reflect"
	"testing"
)

func parseTest(data interface{}) error {
	_, err := parseFields(reflect.ValueOf(data))
	return err
}

type empty struct{}

func TestEmptyStruc(t *testing.T) {
	if err := parseTest(&empty{}); err == nil {
		t.Fatal("failed to error on empty struct")
	}
}

type chanStruct struct {
	Test chan int
}

func TestChanError(t *testing.T) {
	if err := parseTest(&chanStruct{}); err == nil {
		// TODO: should probably ignore channel fields
		t.Fatal("failed to error on struct containing channel")
	}
}

type badSizeof struct {
	Size int `struc:"sizeof=Bad"`
}

func TestBadSizeof(t *testing.T) {
	if err := parseTest(&badSizeof{}); err == nil {
		t.Fatal("failed to error on missing Sizeof target")
	}
}

type missingSize struct {
	Test []byte
}

func TestMissingSize(t *testing.T) {
	if err := parseTest(&missingSize{}); err == nil {
		t.Fatal("failed to error on missing field size")
	}
}

type badNested struct {
	Empty empty
}

func TestNestedParseError(t *testing.T) {
	var buf bytes.Buffer
	if err := Pack(&buf, &badNested{}); err == nil {
		t.Fatal("failed to error on bad nested struct")
	}
}