Skip to content

Commit

Permalink
Merge pull request #9 from spekary/code_coverage
Browse files Browse the repository at this point in the history
Improved test coverage.
  • Loading branch information
spekary authored Jun 8, 2019
2 parents a7a7eb3 + afb1cf4 commit 243bfec
Show file tree
Hide file tree
Showing 26 changed files with 1,024 additions and 139 deletions.
22 changes: 22 additions & 0 deletions pkg/maps/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"sort"
"strings"

)

Expand Down Expand Up @@ -313,6 +316,25 @@ func (o *Map) IsNil() bool {
return o == nil
}

func (o *Map) String() string {
var s string

// sort on keys to stabilize order
keys := o.Keys()
sort.Slice(keys, func(a,b int) bool {
return keys[a] < keys[b]
})

s = "{"
for _,k := range keys {
v := o.Get(k)
s += fmt.Sprintf(`%#v:%#v,`, k, v)
}
s = strings.TrimRight(s, ",")
s += "}"
return s
}


func init() {
gob.Register(new (Map))
Expand Down
75 changes: 73 additions & 2 deletions pkg/maps/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,33 @@ func TestMapNotEqual(t *testing.T) {
}
}

func TestMapLoaders(t *testing.T) {
n := map[string]interface{}{"a":1,"b":"2","c":3.0, "d":true}
m := NewMapFromMap(n)

if i,ok := m.LoadInt("a"); i != 1 || !ok {
t.Error("LoadInt failed")
}
if j,ok := m.LoadString("b"); j != "2" || !ok {
t.Error("LoadString failed")
}
if k,ok := m.LoadFloat64("c"); k != 3.0 || !ok {
t.Error("LoadFloat failed")
}
if l,ok := m.LoadBool("d"); l != true || !ok {
t.Error("LoadBool failed")
}

if _,ok := m.LoadFloat64("d"); ok {
t.Error("Type check failed")
}
}

func ExampleMap_Set() {
m := NewMap()
m.Set("a", "Here")
fmt.Println(m.Get("a"))
// Output: Here
fmt.Println(m.String())
// Output: {"a":"Here"}
}

func ExampleMap_Values() {
Expand Down Expand Up @@ -307,3 +329,52 @@ func ExampleMap_UnmarshalJSON() {

// Output: 3
}

func TestMapEmpty(t *testing.T) {
var m *Map
var n = new(Map)

if !m.IsNil() {
t.Error("Empty Nil test failed")
}

if n.IsNil() {
t.Error("Empty Nil test failed")
}

for _, o := range ([]*Map{m, n}) {
i := o.Get("A")
if i != nil {
t.Error("Empty Get failed")
}
if o.Has("A") {
t.Error("Empty Has failed")
}
o.Delete("E")
o.Clear()

if len(o.Values()) != 0 {
t.Error("Empty Values() failed")
}

if len(o.Keys()) != 0 {
t.Error("Empty Keys() failed")
}

o.Merge(nil)

}

if !m.Equals(n) {
t.Error("Empty Equals() failed")
}
n.Set("a","b")
if m.Equals(n) {
t.Error("Empty Equals() failed")
}
if n.Equals(m) {
t.Error("Empty Equals() failed")
}


}
1 change: 1 addition & 0 deletions pkg/maps/mapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ type MapI interface {
// Range will iterate over the keys and values in the map. Pattern is taken from sync.Map
Range(f func(key string, value interface{}) bool)
Merge(i MapI)
String() string
}
22 changes: 22 additions & 0 deletions pkg/maps/safemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"sort"
"strings"
"sync"


Expand Down Expand Up @@ -349,6 +352,25 @@ func (o *SafeMap) IsNil() bool {
return o == nil
}

func (o *SafeMap) String() string {
var s string

// sort on keys to stabilize order
keys := o.Keys()
sort.Slice(keys, func(a,b int) bool {
return keys[a] < keys[b]
})

s = "{"
for _,k := range keys {
v := o.Get(k)
s += fmt.Sprintf(`%#v:%#v,`, k, v)
}
s = strings.TrimRight(s, ",")
s += "}"
return s
}


func init() {
gob.Register(new (SafeMap))
Expand Down
75 changes: 73 additions & 2 deletions pkg/maps/safemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,33 @@ func TestSafeMapNotEqual(t *testing.T) {
}
}

func TestSafeMapLoaders(t *testing.T) {
n := map[string]interface{}{"a":1,"b":"2","c":3.0, "d":true}
m := NewSafeMapFromMap(n)

if i,ok := m.LoadInt("a"); i != 1 || !ok {
t.Error("LoadInt failed")
}
if j,ok := m.LoadString("b"); j != "2" || !ok {
t.Error("LoadString failed")
}
if k,ok := m.LoadFloat64("c"); k != 3.0 || !ok {
t.Error("LoadFloat failed")
}
if l,ok := m.LoadBool("d"); l != true || !ok {
t.Error("LoadBool failed")
}

if _,ok := m.LoadFloat64("d"); ok {
t.Error("Type check failed")
}
}

func ExampleSafeMap_Set() {
m := NewSafeMap()
m.Set("a", "Here")
fmt.Println(m.Get("a"))
// Output: Here
fmt.Println(m.String())
// Output: {"a":"Here"}
}

func ExampleSafeMap_Values() {
Expand Down Expand Up @@ -307,3 +329,52 @@ func ExampleSafeMap_UnmarshalJSON() {

// Output: 3
}

func TestSafeMapEmpty(t *testing.T) {
var m *SafeMap
var n = new(SafeMap)

if !m.IsNil() {
t.Error("Empty Nil test failed")
}

if n.IsNil() {
t.Error("Empty Nil test failed")
}

for _, o := range ([]*SafeMap{m, n}) {
i := o.Get("A")
if i != nil {
t.Error("Empty Get failed")
}
if o.Has("A") {
t.Error("Empty Has failed")
}
o.Delete("E")
o.Clear()

if len(o.Values()) != 0 {
t.Error("Empty Values() failed")
}

if len(o.Keys()) != 0 {
t.Error("Empty Keys() failed")
}

o.Merge(nil)

}

if !m.Equals(n) {
t.Error("Empty Equals() failed")
}
n.Set("a","b")
if m.Equals(n) {
t.Error("Empty Equals() failed")
}
if n.Equals(m) {
t.Error("Empty Equals() failed")
}


}
17 changes: 9 additions & 8 deletions pkg/maps/safeslicemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"encoding/json"
"sort"
"strings"
"sync"

"fmt"

"sync"
)

// A SafeSliceMap combines a map with a slice so that you can range over a
Expand Down Expand Up @@ -79,6 +77,8 @@ func keySortSafeSliceMap(key1, key2 string, val1, val2 interface{}) bool {
return key1 < key2
}



// SetChanged sets the value.
// It returns true if something in the map changed. If the key
// was already in the map, and you have not provided a sort function,
Expand All @@ -103,14 +103,15 @@ func (o *SafeSliceMap) SetChanged(key string, val interface{}) (changed bool) {
if ok {
// delete old key location
loc := sort.Search (len(o.items), func(n int) bool {
return o.lessF(key, o.order[n], oldVal, o.items[o.order[n]])
return !o.lessF(o.order[n], key, o.items[o.order[n]], oldVal)
})
o.order = append(o.order[:loc], o.order[loc+1:]...)
}

loc := sort.Search (len(o.items), func(n int) bool {
loc := sort.Search (len(o.order), func(n int) bool {
return o.lessF(key, o.order[n], val, o.items[o.order[n]])
})
// insert
o.order = append(o.order, key)
copy(o.order[loc+1:], o.order[loc:])
o.order[loc] = key
Expand Down Expand Up @@ -181,7 +182,7 @@ func (o *SafeSliceMap) Delete(key string) {
if o.lessF != nil {
oldVal := o.items[key]
loc := sort.Search (len(o.items), func(n int) bool {
return o.lessF(key, o.order[n], oldVal, o.items[o.order[n]])
return !o.lessF(o.order[n], key, o.items[o.order[n]], oldVal)
})
o.order = append(o.order[:loc], o.order[loc+1:]...)
} else {
Expand Down Expand Up @@ -351,6 +352,7 @@ func (o *SafeSliceMap) Copy() *SafeSliceMap {
cp.Set(key, value)
return true
})
cp.lessF = o.lessF
return cp
}

Expand Down Expand Up @@ -490,8 +492,7 @@ func (o *SafeSliceMap) String() string {

s = "{"
o.Range(func(k string, v interface{}) bool {
s += `"` + k + `":"` +
fmt.Sprintf("%v", v)+ `",`
s += fmt.Sprintf(`%#v:%#v,`, k, v)
return true
})
s = strings.TrimRight(s, ",")
Expand Down
28 changes: 27 additions & 1 deletion pkg/maps/safeslicemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ func ExampleSafeSliceMap_Range() {

// Iterate after sorting keys
m.SortByKeys()
m.Set("D", "Other2")

m.Range(func(key string, val interface{}) bool {
fmt.Printf("%s:%s,", key, val)
return true // keep iterating to the end
})
fmt.Println()

// Output: B:This,A:That,C:Other,
// A:That,B:This,C:Other,
// A:That,B:This,C:Other,D:Other2,
}

func ExampleSafeSliceMap_MarshalBinary() {
Expand Down Expand Up @@ -266,6 +268,30 @@ func TestSafeSliceMap_SetAt(t *testing.T) {
}
}

func TestSafeSliceMapLoaders(t *testing.T) {
n := map[string]interface{}{"a":1,"b":"2","c":3.0, "d":true}
m := NewSafeSliceMapFromMap(n)

if i,ok := m.LoadInt("a"); i != 1 || !ok {
t.Error("LoadInt failed")
}
if j,ok := m.LoadString("b"); j != "2" || !ok {
t.Error("LoadString failed")
}
if k,ok := m.LoadFloat64("c"); k != 3.0 || !ok {
t.Error("LoadFloat failed")
}
if l,ok := m.LoadBool("d"); l != true || !ok {
t.Error("LoadBool failed")
}

if _,ok := m.LoadFloat64("d"); ok {
t.Error("Type check failed")
}

}



func TestSafeSliceMapEmpty(t *testing.T) {
var m *SafeSliceMap
Expand Down
Loading

0 comments on commit 243bfec

Please sign in to comment.