File: mtree_test.go

package info (click to toggle)
golang-github-vbatts-go-mtree 0.5.4%2Bds-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 796 kB
  • sloc: sh: 198; makefile: 80
file content (100 lines) | stat: -rw-r--r-- 1,679 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
package mtree

import (
	"io"
	"os"
	"testing"

	"github.com/davecgh/go-spew/spew"
)

var (
	testFiles = []struct {
		Name   string
		Counts map[EntryType]int
		Len    int64
	}{
		{
			Name: "testdata/source.mtree",
			Counts: map[EntryType]int{
				FullType:     0,
				RelativeType: 45,
				CommentType:  37,
				SpecialType:  7,
				DotDotType:   17,
				BlankType:    34,
			},
			Len: int64(7887),
		},
		{
			Name: "testdata/source.casync-mtree",
			Counts: map[EntryType]int{
				FullType:     744,
				RelativeType: 56,
				CommentType:  37,
				SpecialType:  7,
				DotDotType:   17,
				BlankType:    34,
			},
			Len: int64(168439),
		},
	}
)

func TestParser(t *testing.T) {
	for i, tf := range testFiles {
		_ = i
		func() {
			fh, err := os.Open(tf.Name)
			if err != nil {
				t.Error(err)
				return
			}
			defer fh.Close()

			dh, err := ParseSpec(fh)
			if err != nil {
				t.Error(err)
			}

			if i == 1 {
				spew.Dump(dh)
				//buf, err := xml.MarshalIndent(dh, "", "  ")
				//if err == nil {
				//t.Error(string(buf))
				//}
			}

			gotNums := countTypes(dh)
			for typ, num := range tf.Counts {
				if gNum, ok := gotNums[typ]; ok {
					if num != gNum {
						t.Errorf("for type %s: expected %d, got %d", typ, num, gNum)
					}
				}
			}

			i, err := dh.WriteTo(io.Discard)
			if err != nil {
				t.Error(err)
			}
			if i != tf.Len {
				t.Errorf("expected to write %d, but wrote %d", tf.Len, i)
			}

		}()
	}
}

func countTypes(dh *DirectoryHierarchy) map[EntryType]int {
	nT := map[EntryType]int{}
	for i := range dh.Entries {
		typ := dh.Entries[i].Type
		if _, ok := nT[typ]; !ok {
			nT[typ] = 1
		} else {
			nT[typ]++
		}
	}
	return nT
}