Skip to content
Permalink
Browse files

add more test coverage to core types

  • Loading branch information...
paulmach committed Mar 9, 2018
1 parent b326c57 commit 287993cf518af026cfc821b7311e6635d755bf8b
Showing with 500 additions and 32 deletions.
  1. +18 −4 element.go
  2. +175 −0 element_test.go
  3. +3 −17 feature.go
  4. +110 −0 feature_test.go
  5. +1 −1 object.go
  6. +49 −1 object_test.go
  7. +2 −2 osm.go
  8. +131 −0 osm_test.go
  9. +1 −1 osmxml/scanner_test.go
  10. +1 −1 way.go
  11. +9 −5 way_test.go
@@ -55,7 +55,7 @@ func (id ElementID) FeatureID() FeatureID {
// NodeID returns the id of this feature as a node id.
// The function will panic if this feature is not of NodeType.
func (id ElementID) NodeID() NodeID {
if id&nodeMask == 0 {
if id&nodeMask != nodeMask {
panic(fmt.Sprintf("not a node: %v", id))
}

@@ -65,7 +65,7 @@ func (id ElementID) NodeID() NodeID {
// WayID returns the id of this feature as a way id.
// The function will panic if this feature is not of WayType.
func (id ElementID) WayID() WayID {
if id&wayMask == 0 {
if id&wayMask != wayMask {
panic(fmt.Sprintf("not a way: %v", id))
}

@@ -75,7 +75,7 @@ func (id ElementID) WayID() WayID {
// RelationID returns the id of this feature as a relation id.
// The function will panic if this feature is not of RelationType.
func (id ElementID) RelationID() RelationID {
if id&relationMask == 0 {
if int64(id)&relationMask != relationMask {
panic(fmt.Sprintf("not a relation: %v", id))
}

@@ -100,7 +100,7 @@ func ParseElementID(s string) (ElementID, error) {
}

parts2 := strings.Split(parts[1], ":")
if l := len(parts); l != 1 && l != 2 {
if l := len(parts2); l != 1 && l != 2 {
return 0, fmt.Errorf("invalid element id: %v", s)
}

@@ -155,6 +155,20 @@ func (es Elements) ElementIDs() ElementIDs {
return ids
}

// FeatureIDs returns a slice of the feature ids of the elements.
func (es Elements) FeatureIDs() FeatureIDs {
if len(es) == 0 {
return nil
}

ids := make(FeatureIDs, 0, len(es))
for _, e := range es {
ids = append(ids, e.FeatureID())
}

return ids
}

// Sort will order the elements by type, node, way, relation, changeset,
// then id and lastly the version.
func (es Elements) Sort() {
@@ -6,6 +6,73 @@ import (
"testing"
)

func TestElementID_ids(t *testing.T) {
id := NodeID(1).ElementID(1)

oid := id.ObjectID()
if v := oid.Type(); v != TypeNode {
t.Errorf("incorrect type: %v", v)
}

if v := oid.Ref(); v != 1 {
t.Errorf("incorrect id: %v", v)
}

fid := id.FeatureID()
if v := fid.Type(); v != TypeNode {
t.Errorf("incorrect type: %v", v)
}

if v := fid.Ref(); v != 1 {
t.Errorf("incorrect id: %v", v)
}

if v := NodeID(1).ElementID(1).NodeID(); v != 1 {
t.Errorf("incorrect id: %v", v)
}

if v := WayID(1).ElementID(1).WayID(); v != 1 {
t.Errorf("incorrect id: %v", v)
}

if v := RelationID(1).ElementID(1).RelationID(); v != 1 {
t.Errorf("incorrect id: %v", v)
}

t.Run("not a node", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("should panic?")
}
}()

id := WayID(1).ElementID(1)
id.NodeID()
})

t.Run("not a way", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("should panic?")
}
}()

id := NodeID(1).ElementID(1)
id.WayID()
})

t.Run("not a relation", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("should panic?")
}
}()

id := WayID(1).ElementID(1)
id.RelationID()
})
}

func TestParseElementID(t *testing.T) {
cases := []struct {
name string
@@ -61,6 +128,27 @@ func TestParseElementID(t *testing.T) {
}
})
}

// errors
if _, err := ParseElementID("123"); err == nil {
t.Errorf("should return error if only one part")
}

if _, err := ParseElementID("node/1:1:1"); err == nil {
t.Errorf("should return error if multiple :")
}

if _, err := ParseElementID("node/abc:1"); err == nil {
t.Errorf("should return error if id not a number")
}

if _, err := ParseElementID("node/1:abc"); err == nil {
t.Errorf("should return error if version not a number")
}

if _, err := ParseElementID("lake/1:1"); err == nil {
t.Errorf("should return error if not a valid type")
}
}

func TestElement_implementations(t *testing.T) {
@@ -100,6 +188,93 @@ func TestElement_implementations(t *testing.T) {
}
}

func TestElements_ElementIDs(t *testing.T) {
es := Elements{
&Node{ID: 1, Version: 5},
&Way{ID: 2, Version: 6},
&Relation{ID: 3, Version: 7},
&Node{ID: 4, Version: 8},
}

expected := ElementIDs{
NodeID(1).ElementID(5),
WayID(2).ElementID(6),
RelationID(3).ElementID(7),
NodeID(4).ElementID(8),
}

if ids := es.ElementIDs(); !reflect.DeepEqual(ids, expected) {
t.Errorf("incorrect ids: %v", ids)
}
}

func TestElements_FeatureIDs(t *testing.T) {
es := Elements{
&Node{ID: 1, Version: 5},
&Way{ID: 2, Version: 6},
&Relation{ID: 3, Version: 7},
&Node{ID: 4, Version: 8},
}

expected := FeatureIDs{
NodeID(1).FeatureID(),
WayID(2).FeatureID(),
RelationID(3).FeatureID(),
NodeID(4).FeatureID(),
}

if ids := es.FeatureIDs(); !reflect.DeepEqual(ids, expected) {
t.Errorf("incorrect ids: %v", ids)
}
}

func TestElements_Sort(t *testing.T) {
es := Elements{
&Node{ID: 1, Version: 4},
&Node{ID: 1, Version: 5},
&Way{ID: 2, Version: 6},
&Relation{ID: 3, Version: 7},
&Way{ID: 2, Version: 5},
&Node{ID: 4, Version: 8},
}
es.Sort()

expected := ElementIDs{
NodeID(1).ElementID(4),
NodeID(1).ElementID(5),
NodeID(4).ElementID(8),
WayID(2).ElementID(5),
WayID(2).ElementID(6),
RelationID(3).ElementID(7),
}

if ids := es.ElementIDs(); !reflect.DeepEqual(ids, expected) {
t.Errorf("incorrect sort: %v", ids)
}
}

func TestElementIDs_Counts(t *testing.T) {
ids := ElementIDs{
RelationID(1).ElementID(1),
NodeID(1).ElementID(2),
WayID(2).ElementID(3),
WayID(1).ElementID(2),
RelationID(1).ElementID(1),
WayID(1).ElementID(1),
}

n, w, r := ids.Counts()
if n != 1 {
t.Errorf("incorrect nodes: %v", n)
}
if w != 3 {
t.Errorf("incorrect nodes: %v", w)
}
if r != 2 {
t.Errorf("incorrect nodes: %v", r)
}
}

func TestElementIDs_Sort(t *testing.T) {
ids := ElementIDs{
RelationID(1).ElementID(1),
@@ -107,7 +107,7 @@ func (id FeatureID) ElementID(v int) ElementID {
// NodeID returns the id of this feature as a node id.
// The function will panic if this feature is not of NodeType.
func (id FeatureID) NodeID() NodeID {
if id&nodeMask == 0 {
if id&nodeMask != nodeMask {
panic(fmt.Sprintf("not a node: %v", id))
}

@@ -117,7 +117,7 @@ func (id FeatureID) NodeID() NodeID {
// WayID returns the id of this feature as a way id.
// The function will panic if this feature is not of WayType.
func (id FeatureID) WayID() WayID {
if id&wayMask == 0 {
if id&wayMask != wayMask {
panic(fmt.Sprintf("not a way: %v", id))
}

@@ -127,7 +127,7 @@ func (id FeatureID) WayID() WayID {
// RelationID returns the id of this feature as a relation id.
// The function will panic if this feature is not of RelationType.
func (id FeatureID) RelationID() RelationID {
if id&relationMask == 0 {
if id&relationMask != relationMask {
panic(fmt.Sprintf("not a relation: %v", id))
}

@@ -172,20 +172,6 @@ func ParseFeatureID(s string) (FeatureID, error) {
// FeatureIDs is a slice of FeatureIDs with some helpers on top.
type FeatureIDs []FeatureID

// FeatureIDs returns a slice of the feature ids of the elements.
func (es Elements) FeatureIDs() FeatureIDs {
if len(es) == 0 {
return nil
}

ids := make(FeatureIDs, 0, len(es))
for _, e := range es {
ids = append(ids, e.FeatureID())
}

return ids
}

// Counts returns the number of each type of feature in the set of ids.
func (ids FeatureIDs) Counts() (nodes, ways, relations int) {
for _, id := range ids {

0 comments on commit 287993c

Please sign in to comment.
You can’t perform that action at this time.