Skip to content

Commit

Permalink
Strenghened tests and code
Browse files Browse the repository at this point in the history
  • Loading branch information
seborama committed Nov 28, 2018
1 parent e50589f commit 2bbe124
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .gometalinter.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"Enable": ["deadcode", "gochecknoglobals", "gochecknoinits", "lll", "megacheck", "nakedret", "test", "testify", "unparam", "unconvert"],
"Enable": ["deadcode", "gochecknoglobals", "gochecknoinits", "golint", "lll", "megacheck", "nakedret", "test", "testify", "unparam", "unconvert"],
"cyclo-over": 10,
"deadline": "120s"
}
}
2 changes: 1 addition & 1 deletion gometalinter.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

gometalinter | grep -v "_test\.go.*[(]lll[)]$"
gometalinter ./ | grep -v "_test\.go.*[(]lll[)]$"
9 changes: 6 additions & 3 deletions mapentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ func (me MapEntry) Hash() uint32 {
return me.K.Hash()
}

// Equal compares the hashes of the key of this MapEntry with
// that of the given Entry.
// Equal returns true when me and e are equal.
// Note that MapEntry defines equality as equality in Hash and
// the Hash of a MapEntry is its MapEntry.K hash (MapEntry.V is
// not considered)
func (me MapEntry) Equal(e hamt.Entry) bool {
return me.Hash() == e.Hash()
}

// Equal compares the key of this MapEntry with the given MapEntry.
// EqualMapEntry compares the key of this MapEntry with the
// given MapEntry.
// Note that MapEntry defines equality as equality in Hash and
// the Hash of a MapEntry is its MapEntry.K hash (MapEntry.V is
// not considered)
func (me MapEntry) EqualMapEntry(ome MapEntry) bool {
return me.Hash() == ome.Hash()
}
Expand Down
6 changes: 4 additions & 2 deletions maybe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Maybe struct {
isEmpty bool
}

// A MaybeNone is a Maybe that does not have a value.
// MaybeNone is a Maybe that does not have a value.
func MaybeNone() Maybe {
return Maybe{
value: nil,
Expand All @@ -33,6 +33,8 @@ func MaybeOf(i Entry) Maybe {
return MaybeSome(i)
}

// IsEmpty returns true when this Maybe does not have
// a value.
func (m Maybe) IsEmpty() bool { return m.isEmpty }

// Filter \
Expand All @@ -56,7 +58,7 @@ func (m Maybe) GetOrElse(e Entry) Entry {
return m.value
}

// GetOrElse returns this Maybe or the given Maybe if this Maybe is empty.
// OrElse returns this Maybe or the given Maybe if this Maybe is empty.
func (m Maybe) OrElse(other Maybe) Maybe {
if m.IsEmpty() {
return other
Expand Down
25 changes: 21 additions & 4 deletions stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,18 @@ func TestReferenceStream_LeftReduce(t *testing.T) {
args args
want interface{}
}{
{
name: "Should return nil for a nil Stream",
fields: fields{
iterator: NewSetIterator(nil),
},
args: args{f2: concatenateStringsBiFunc},
want: nil,
},
{
name: "Should return nil for an empty Stream",
fields: fields{
iterator: NewSetIterator(NewHamtSet()),
iterator: SetIterator{set: NewHamtSet()},
},
args: args{f2: concatenateStringsBiFunc},
want: nil,
Expand Down Expand Up @@ -231,9 +239,9 @@ func TestReferenceStream_RightReduce(t *testing.T) {
want interface{}
}{
{
name: "Should return nil for an empty Stream",
name: "Should return nil for a nil Stream",
fields: fields{
iterator: NewSetIterator(NewHamtSet()),
iterator: NewSetIterator(nil),
},
args: args{f2: concatenateStringsBiFunc},
want: nil,
Expand Down Expand Up @@ -286,14 +294,23 @@ func TestReferenceStream_Intersperse(t *testing.T) {
want Stream
}{
{
name: "Should return a Stream of nil",
name: "Should return a Stream of nil for nil iterator",
fields: fields{iterator: nil},
args: args{
e: EntryString(" - "),
},
want: NewStream(
NewSliceIterator([]Entry{})),
},
{
name: "Should return a Stream of nil for empty Set",
fields: fields{iterator: SetIterator{set: NewHamtSet()}},
args: args{
e: EntryString(" - "),
},
want: NewStream(
NewSliceIterator([]Entry{})),
},
{
name: "Should return the original Set when it has a single value",
fields: fields{
Expand Down
2 changes: 2 additions & 0 deletions tuple.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fuego

// A Tuple is a container of value(s).
// A special case is Tuple0 which does not hold any value.
type Tuple interface {
Hash() uint32
Equal(o Tuple) bool
Expand Down
5 changes: 5 additions & 0 deletions tuple0.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package fuego

// Tuple0 is a tuple with 0 element.
type Tuple0 struct{}

// Hash returns the hash of this tuple.
func (t Tuple0) Hash() uint32 {
return 1
}

// Equal returns true if 'o' and 't' are equal.
func (t Tuple0) Equal(o Tuple) bool {
// Tuple0 is considered to meet equality when o and t are the
// same object (in memory)
Expand All @@ -14,10 +17,12 @@ func (t Tuple0) Equal(o Tuple) bool {
return false
}

// Arity is the number of elements in this tuple.
func (t Tuple0) Arity() int {
return 0
}

// ToSet returns an empty Set.
func (t Tuple0) ToSet() Set {
return NewOrderedSet()
}
5 changes: 5 additions & 0 deletions tuple1.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package fuego

// Tuple1 is a tuple with 1 element.
type Tuple1 struct {
E1 Entry
}

// Hash returns the hash of this tuple.
func (t Tuple1) Hash() uint32 {
if t.E1 == nil {
return 0
}
return t.E1.Hash()
}

// Equal returns true if 'o' and 't' are equal.
func (t Tuple1) Equal(o Tuple) bool {
oT, ok := o.(Tuple1)
return t == o ||
(ok &&
(t.E1 != nil && t.E1.Equal(oT.E1)))
}

// Arity is the number of elements in this tuple.
func (t Tuple1) Arity() int {
return 1
}

// ToSet returns the elements of this tuple as a Set.
func (t Tuple1) ToSet() Set {
return NewOrderedSet().
Insert(t.E1)
Expand Down
8 changes: 7 additions & 1 deletion tuple1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@ func TestTuple1_Equal(t *testing.T) {
want: false,
},
{
name: "Should equal: nil == nil",
name: "Should equal: Tuple(nil) != nil",
fields: fields{E1: nil},
args: args{o: nil},
want: false,
},
{
name: "Should equal: Tuple(nil) == Tuple(nil)",
fields: fields{E1: nil},
args: args{o: Tuple1{E1: nil}},
want: true,
},
{
name: "Should equal: hi == hi",
fields: fields{
Expand Down
5 changes: 5 additions & 0 deletions tuple2.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package fuego

// Tuple2 is a tuple with 2 elements.
type Tuple2 struct {
E1 Entry
E2 Entry
}

// Hash returns the hash of this tuple.
func (t Tuple2) Hash() uint32 {
var tHash1, tHash2 uint32
if t.E1 != nil {
Expand All @@ -20,6 +22,7 @@ func (t Tuple2) Hash() uint32 {
return result
}

// Equal returns true if 'o' and 't' are equal.
func (t Tuple2) Equal(o Tuple) bool {
oT, ok := o.(Tuple2)
return t == o ||
Expand All @@ -28,10 +31,12 @@ func (t Tuple2) Equal(o Tuple) bool {
(t.E2 != nil && t.E2.Equal(oT.E2)))
}

// Arity is the number of elements in this tuple.
func (t Tuple2) Arity() int {
return 2
}

// ToSet returns the elements of this tuple as a Set.
func (t Tuple2) ToSet() Set {
return NewOrderedSet().
Insert(t.E1).
Expand Down
57 changes: 57 additions & 0 deletions tuple2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,63 @@ func TestTuple2_Equal(t *testing.T) {
args args
want bool
}{
{
name: "Should equal with E1 nil and args nil",
fields: fields{
E1: nil,
E2: EntryString("bye"),
},
args: args{o: nil},
want: false,
},
{
name: "Should NOT equal with E1 nil",
fields: fields{
E1: nil,
E2: EntryString("hi"),
},
args: args{
o: Tuple2{
E1: nil,
E2: EntryString("bye")}},
want: false,
},
{
name: "Should equal with E1 nil",
fields: fields{
E1: nil,
E2: EntryString("bye"),
},
args: args{
o: Tuple2{
E1: nil,
E2: EntryString("bye")}},
want: true,
},
{
name: "Should equal with E2 nil",
fields: fields{
E1: EntryString("hi"),
E2: nil,
},
args: args{
o: Tuple2{
E1: EntryString("hi"),
E2: EntryString("bye")}},
want: false,
},
{
name: "Should NOT equal with E2 nil",
fields: fields{
E1: EntryString("hi"),
E2: nil,
},
args: args{
o: Tuple2{
E1: EntryString("bye"),
E2: nil}},
want: false,
},
{
name: "Should equal",
fields: fields{
Expand Down

0 comments on commit 2bbe124

Please sign in to comment.