Skip to content

Commit

Permalink
Merge pull request #8 from SierraSoftworks/fix/breadcrumbrace
Browse files Browse the repository at this point in the history
Fix some bugs in the breadcrumbs code
  • Loading branch information
spartan563 committed Sep 25, 2019
2 parents 34b47bb + 7a2d535 commit fd33901
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
21 changes: 12 additions & 9 deletions breadcrumbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type breadcrumbsList struct {
Head *breadcrumbListNode
Tail *breadcrumbListNode
Length int
mutex sync.Mutex
mutex sync.RWMutex
}

func (l *breadcrumbsList) Class() string {
Expand All @@ -79,10 +79,13 @@ func (l *breadcrumbsList) WithSize(length int) BreadcrumbsList {

l.MaxLength = length

for l.Length > l.MaxLength {
if l.Head == nil {
break
}
if length == 0 {
l.Head = nil
l.Tail = nil
l.Length = 0
}

for l.Length > l.MaxLength && l.Head != nil {
l.Head = l.Head.Next
l.Length--
}
Expand Down Expand Up @@ -147,16 +150,16 @@ func (l *breadcrumbsList) append(b Breadcrumb) {
l.Tail = n
l.Length++

for l.Length > l.MaxLength {
if l.Head == nil {
break
}
for l.Length > l.MaxLength && l.Head != nil {
l.Head = l.Head.Next
l.Length--
}
}

func (l *breadcrumbsList) list() []Breadcrumb {
l.mutex.RLock()
defer l.mutex.RUnlock()

current := l.Head
out := []Breadcrumb{}
for current != nil {
Expand Down
15 changes: 15 additions & 0 deletions breadcrumbs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ func TestBreadcrumbs(t *testing.T) {
"index": 2,
})
})

Convey("Should empty the list if the size is set to 0", func() {
var b Breadcrumb
for i := 0; i < 3; i++ {
b = l.NewDefault(map[string]interface{}{
"index": i,
})
So(b, ShouldNotBeNil)
}

l.WithSize(0).WithSize(3)
So(ll.Length, ShouldEqual, 0)
So(ll.Head, ShouldBeNil)
So(ll.Tail, ShouldBeNil)
})
})

Convey("append()", func() {
Expand Down

0 comments on commit fd33901

Please sign in to comment.