diff --git a/hierarchy.go b/hierarchy.go index 2d4bec5..3c70b5e 100644 --- a/hierarchy.go +++ b/hierarchy.go @@ -84,3 +84,18 @@ const ( 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 ) + +// 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", +} diff --git a/mtree_test.go b/mtree_test.go index b4f12bd..836fa6f 100644 --- a/mtree_test.go +++ b/mtree_test.go @@ -1,14 +1,23 @@ package mtree import ( - "fmt" + "io/ioutil" "os" "testing" ) -var testFiles = []string{ - "testdata/source.mtree", -} +var ( + 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) { for _, file := range testFiles { @@ -24,13 +33,36 @@ func TestParser(t *testing.T) { if err != nil { 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 { 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 +}