1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-11-22 08:25:38 +00:00

hierarchy: testing works

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2016-04-05 17:16:44 -04:00
parent 7777d9a010
commit 34b9f4dc46
2 changed files with 53 additions and 6 deletions

View file

@ -84,3 +84,18 @@ const (
DotDotType // .. - A relative path step. keywords/options are ignored DotDotType // .. - A relative path step. keywords/options are ignored
FullType // if the first word on the line has a `/` after the first character, it interpretted as a file pathname with options FullType // if the first word on the line has a `/` after the first character, it interpretted as a file pathname with options
) )
// String returns the name of the EntryType
func (et EntryType) String() string {
return typeNames[et]
}
var typeNames = map[EntryType]string{
SignatureType: "SignatureType",
BlankType: "BlankType",
CommentType: "CommentType",
SpecialType: "SpecialType",
RelativeType: "RelativeType",
DotDotType: "DotDotType",
FullType: "FullType",
}

View file

@ -1,14 +1,23 @@
package mtree package mtree
import ( import (
"fmt" "io/ioutil"
"os" "os"
"testing" "testing"
) )
var testFiles = []string{ var (
"testdata/source.mtree", testFiles = []string{"testdata/source.mtree"}
} numEntries = map[EntryType]int{
FullType: 0,
RelativeType: 45,
CommentType: 37,
SpecialType: 7,
DotDotType: 17,
BlankType: 34,
}
expectedLength = int64(7887)
)
func TestParser(t *testing.T) { func TestParser(t *testing.T) {
for _, file := range testFiles { for _, file := range testFiles {
@ -24,13 +33,36 @@ func TestParser(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
fmt.Printf("%q", dh) gotNums := countTypes(dh)
for typ, num := range numEntries {
if gNum, ok := gotNums[typ]; ok {
if num != gNum {
t.Errorf("for type %s: expected %d, got %d", typ, num, gNum)
}
}
}
_, err = dh.WriteTo(os.Stdout) i, err := dh.WriteTo(ioutil.Discard)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if i != expectedLength {
t.Errorf("expected to write %d, but wrote %d", expectedLength, 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
}