Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pila: allow publicly to use custom Stack implementation #47

Merged
merged 6 commits into from Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions pila/stack.go
Expand Up @@ -43,13 +43,20 @@ type Stack struct {
}

// NewStack creates a new Stack given a name and a creation date,
// without an association to any Database.
// without an association to any Database. It uses the default
// ./pkg/stack implementation.
func NewStack(name string, t time.Time) *Stack {
return NewStackWithCustomImplementation(name, t, stack.NewStack())
}

// NewStackWithCustomImplementation creates a new Stack given a name, a creation date,
// and a stack.Stacker implementation without an association to any Database.
func NewStackWithCustomImplementation(name string, t time.Time, stack stack.Stacker) *Stack {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe don't shadow stack package with stack argument, and rename it like impl, s or base. You get the idea...

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I exactly had this thought when I named the parameter as stack. So if a second mind think likewise, change the name it is.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s := &Stack{}
s.Name = name
s.SetID()
s.CreatedAt = t
s.base = stack.NewStack()
s.base = stack
return s
}

Expand Down
36 changes: 36 additions & 0 deletions pila/stack_test.go
Expand Up @@ -7,6 +7,14 @@ import (
"time"
)

type TestStack struct{}

func (s *TestStack) Push(element interface{}) { return }
func (s *TestStack) Pop() (interface{}, bool) { return nil, false }
func (s *TestStack) Size() int { return 0 }
func (s *TestStack) Peek() interface{} { return nil }
func (s *TestStack) Flush() { return }

func TestNewStack(t *testing.T) {
now := time.Now()
stack := NewStack("test-stack", now)
Expand Down Expand Up @@ -35,6 +43,34 @@ func TestNewStack(t *testing.T) {
}
}

func TestNewStackWithCustomImplementation(t *testing.T) {
now := time.Now()
stack := NewStackWithCustomImplementation("test-stack", now, &TestStack{})

if stack == nil {
t.Fatal("stack is nil")
}

if stack.ID.String() != "2f44edeaa249ba81db20e9ddf000ba65" {
t.Errorf("stack.ID is %s, expected %s", stack.ID.String(), "2f44edeaa249ba81db20e9ddf000ba65")
}
if stack.Name != "test-stack" {
t.Errorf("stack.Name is %s, expected %s", stack.Name, "test-stack")
}
if stack.Database != nil {
t.Error("stack.Database is not nil")
}
if stack.CreatedAt != now {
t.Errorf("stack.CreatedAt is %v, expected %v", stack.CreatedAt, now)
}
if stack.base == nil {
t.Fatalf("stack.base is nil")
}
if stack.base.Size() != 0 {
t.Fatalf("stack.base.Size() is %d, expected %d", stack.base.Size(), 0)
}
}

func TestSetDatabase(t *testing.T) {
db := NewDatabase("test-db")
stack := NewStack("test-stack", time.Now())
Expand Down