Skip to content

Commit

Permalink
complete test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
krak3n committed Aug 2, 2017
1 parent 2831514 commit 8902190
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 4 deletions.
22 changes: 22 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,25 @@ func ExampleRev() {
fmt.Println(novis.Rev("foo.bar", "baz"))
// Output: /foo/bar/baz
}

func ExampleGetBranch() {
novis.Add("foo", "/foo")
foo := novis.GetBranch("foo")
fmt.Println(foo.Rel())
// Output: /foo
}

func ExampleBranch_Rel() {
foo := novis.Add("foo", "/foo")
bar := foo.Add("bar", "/bar")
fmt.Println(bar.Rel())
// Output: /bar
}

func ExampleBranch_Path() {
foo := novis.Add("foo", "/foo")
bar := foo.Add("bar", "/bar")
baz := bar.Add("baz", "/baz")
fmt.Println(baz.Path())
// Output: /foo/bar/baz
}
9 changes: 5 additions & 4 deletions novis.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ func (novis *Novis) Rev(name string, values ...string) string {
// traverse follows the lookup path to the end placing branches
// ontp a receive only channel callers can range over
func (novis *Novis) traverse(lookup string) <-chan *Branch {
if novis.Root == nil {
return nil
}
ch := make(chan *Branch)
ok := false
branch := novis.Root
route := strings.Split(lookup, ".")
go func() {
defer close(ch)
for _, name := range route {
if branch == nil {
break
}
branch, ok = branch.Get(name)
if !ok {
break
Expand Down Expand Up @@ -120,8 +120,9 @@ func (branch *Branch) Rel() string {
// Returns the full branch path from root to tip
func (branch *Branch) Path() string {
parts := []string{branch.path}
b := branch
for {
b := branch.parent
b = b.parent
if b == nil {
break
}
Expand Down
124 changes: 124 additions & 0 deletions novis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,71 @@ import (
"github.com/stretchr/testify/assert"
)

func TestNovis_GetBranch(t *testing.T) {
type tc struct {
tname string
novis *Novis
name string
branch *Branch
}
tt := []tc{
{
tname: "branch does not exist",
novis: New(),
name: "foo",
branch: nil,
},
{
tname: "shallpw",
novis: &Novis{
Root: &Branch{
branches: map[string]*Branch{
"foo": &Branch{
name: "foo",
path: "/foo",
},
},
},
},
name: "foo",
branch: &Branch{
name: "foo",
path: "/foo",
},
},
{
tname: "deep",
novis: &Novis{
Root: &Branch{
branches: map[string]*Branch{
"foo": &Branch{
name: "foo",
path: "/foo",
branches: map[string]*Branch{
"bar": &Branch{
name: "bar",
path: "/bar",
},
},
},
},
},
},
name: "foo.bar",
branch: &Branch{
name: "bar",
path: "/bar",
},
},
}
for _, tc := range tt {
t.Run(tc.tname, func(t *testing.T) {
branch := tc.novis.GetBranch(tc.name)
assert.Equal(t, tc.branch, branch)
})
}
}

func TestNovis_Rev(t *testing.T) {
type tc struct {
tname string
Expand Down Expand Up @@ -168,3 +233,62 @@ func TestNovis_Rev(t *testing.T) {
})
}
}

func TestBranch_Rel(t *testing.T) {
type tc struct {
tname string
branch *Branch
path string
}
tt := []tc{
{
tname: "returns relative path",
branch: &Branch{
path: "/foo",
},
path: "/foo",
},
}
for _, tc := range tt {
t.Run(tc.tname, func(t *testing.T) {
path := tc.branch.Rel()
assert.Equal(t, tc.path, path)
})
}
}

func TestBranch_Path(t *testing.T) {
type tc struct {
tname string
branch *Branch
path string
}
tt := []tc{
{
tname: "shallow",
branch: &Branch{
path: "/foo",
},
path: "/foo",
},
{
tname: "deep",
branch: &Branch{
path: "/foo",
parent: &Branch{
path: "/bar",
parent: &Branch{
path: "/baz",
},
},
},
path: "/baz/bar/foo",
},
}
for _, tc := range tt {
t.Run(tc.tname, func(t *testing.T) {
path := tc.branch.Path()
assert.Equal(t, tc.path, path)
})
}
}

0 comments on commit 8902190

Please sign in to comment.