Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Make sure FieldValidator methods act on the object we pass in.
Browse files Browse the repository at this point in the history
Added a unit test for FieldValidator
  • Loading branch information
bboreham authored and rade committed Apr 10, 2015
1 parent d19d485 commit d228a44
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
12 changes: 6 additions & 6 deletions router/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type FieldValidator struct {
err error
}

func NewFieldValidator(fields map[string]string) FieldValidator {
return FieldValidator{fields, nil}
func NewFieldValidator(fields map[string]string) *FieldValidator {
return &FieldValidator{fields, nil}
}

func (fv FieldValidator) Value(fieldName string) (string, error) {
func (fv *FieldValidator) Value(fieldName string) (string, error) {
if fv.err != nil {
return "", fv.err
}
Expand All @@ -28,7 +28,7 @@ func (fv FieldValidator) Value(fieldName string) (string, error) {
return val, nil
}

func (fv FieldValidator) CheckEqual(fieldName, expectedValue string) error {
func (fv *FieldValidator) CheckEqual(fieldName, expectedValue string) error {
val, err := fv.Value(fieldName)
if err != nil {
return err
Expand All @@ -40,7 +40,7 @@ func (fv FieldValidator) CheckEqual(fieldName, expectedValue string) error {
return nil
}

func (fv FieldValidator) Err() error {
func (fv *FieldValidator) Err() error {
return fv.err
}

Expand Down Expand Up @@ -146,7 +146,7 @@ func (conn *LocalConnection) handshakeSendRecv(localConnID uint64, usingPassword
fv.CheckEqual("Protocol", Protocol)
fv.CheckEqual("ProtocolVersion", versionStr)
fv.CheckEqual("PeerNameFlavour", PeerNameFlavour)
return &fv, private, nil
return fv, private, nil
}

func (conn *LocalConnection) setRemote(toPeer *Peer) error {
Expand Down
29 changes: 29 additions & 0 deletions router/handshake_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package router

import (
wt "github.com/weaveworks/weave/testing"
"testing"
)

func TestFieldValidator(t *testing.T) {
testMap := map[string]string{"a": "a"}

fv := NewFieldValidator(testMap)
val, err := fv.Value("a")
wt.AssertNoErr(t, err)
wt.AssertNoErr(t, fv.Err())
wt.AssertEqualString(t, val, "a", "a")
_, err = fv.Value("x")
wt.AssertFalse(t, err == nil || fv.Err() == nil, "Expected error")
_, err = fv.Value("a")
wt.AssertFalse(t, err == nil || fv.Err() == nil, "Previous error should be retained")

fv = NewFieldValidator(testMap)
err = fv.CheckEqual("a", "a")
wt.AssertNoErr(t, err)
wt.AssertNoErr(t, fv.Err())
err = fv.CheckEqual("a", "b")
wt.AssertFalse(t, err == nil || fv.Err() == nil, "Expected error")
err = fv.CheckEqual("a", "a")
wt.AssertFalse(t, err == nil || fv.Err() == nil, "Previous error should be retained")
}

0 comments on commit d228a44

Please sign in to comment.