extend mapping/filter tests

master
Oliver Tonnhofer 2013-05-23 14:05:09 +02:00
parent f07b970951
commit 4a19dae36e
1 changed files with 142 additions and 1 deletions

View File

@ -15,6 +15,148 @@ func init() {
}
}
func stringMapEquals(t *testing.T, expected, actual map[string]string) {
if len(expected) != len(actual) {
t.Fatalf("different length in %v and %v\n", expected, actual)
}
for k, v := range expected {
if actualV, ok := actual[k]; ok {
if actualV != v {
t.Fatalf("%s != %s in %v and %v\n", v, actualV, expected, actual)
}
} else {
t.Fatalf("%s not in %v\n", k, actual)
}
}
}
func matchesEqual(t *testing.T, expected []Match, actual []Match) {
expectedMatches := make(map[string]Match)
actualMatches := make(map[string]Match)
if len(expected) != len(actual) {
t.Fatalf("different length in %v and %v\n", expected, actual)
}
for _, match := range expected {
expectedMatches[match.Table] = match
}
for _, match := range actual {
actualMatches[match.Table] = match
}
for name, expectedMatch := range expectedMatches {
if actualMatch, ok := actualMatches[name]; ok {
if expectedMatch.Table != actualMatch.Table ||
expectedMatch.Key != actualMatch.Key ||
expectedMatch.Value != actualMatch.Value {
t.Fatalf("match differ %v != %v", expectedMatch, actualMatch)
}
} else {
t.Fatalf("%s not in %v", name, actualMatches)
}
}
}
func TestTagFilterNodes(t *testing.T) {
var tags element.Tags
nodes := mapping.NodeTagFilter()
tags = element.Tags{"name": "foo"}
if nodes.Filter(&tags) != false {
t.Fatal("unexpected filter response for", tags)
}
stringMapEquals(t, element.Tags{}, tags)
tags = element.Tags{"name": "foo", "unknown": "baz"}
if nodes.Filter(&tags) != false {
t.Fatal("unexpected filter response for", tags)
}
stringMapEquals(t, element.Tags{}, tags)
tags = element.Tags{"name": "foo", "place": "village"}
if nodes.Filter(&tags) != true {
t.Fatal("unexpected filter response for", tags)
}
stringMapEquals(t, element.Tags{"name": "foo", "place": "village"}, tags)
// TODO
}
func TestTagFilterWays(t *testing.T) {
var tags element.Tags
ways := mapping.WayTagFilter()
tags = element.Tags{"name": "foo"}
if ways.Filter(&tags) != false {
t.Fatal("unexpected filter response for", tags)
}
stringMapEquals(t, element.Tags{}, tags)
tags = element.Tags{"name": "foo", "unknown": "baz"}
if ways.Filter(&tags) != false {
t.Fatal("unexpected filter response for", tags)
}
stringMapEquals(t, element.Tags{}, tags)
tags = element.Tags{"name": "foo", "highway": "unknown"}
if ways.Filter(&tags) != false {
t.Fatal("unexpected filter response for", tags)
}
stringMapEquals(t, element.Tags{}, tags)
// TODO
}
func TestTagFilterRelations(t *testing.T) {
var tags element.Tags
relations := mapping.RelationTagFilter()
tags = element.Tags{"name": "foo"}
if relations.Filter(&tags) != false {
t.Fatal("unexpected filter response for", tags)
}
stringMapEquals(t, element.Tags{}, tags)
// TODO
}
func TestPointMatcher(t *testing.T) {
var tags element.Tags
points := mapping.PointMatcher()
tags = element.Tags{"unknown": "baz"}
matchesEqual(t, []Match{}, points.Match(&tags))
tags = element.Tags{"place": "unknown"}
matchesEqual(t, []Match{}, points.Match(&tags))
tags = element.Tags{"place": "city"}
matchesEqual(t, []Match{{"place", "city", "places", nil}}, points.Match(&tags))
// TODO
}
func TestLineStringMatcher(t *testing.T) {
var tags element.Tags
ls := mapping.LineStringMatcher()
tags = element.Tags{"unknown": "baz"}
matchesEqual(t, []Match{}, ls.Match(&tags))
// TODO
}
func TestPolygonMatcher(t *testing.T) {
var tags element.Tags
polys := mapping.PolygonMatcher()
tags = element.Tags{"unknown": "baz"}
matchesEqual(t, []Match{}, polys.Match(&tags))
// TODO
}
func TestFilterNodes(t *testing.T) {
var tags element.Tags
@ -68,7 +210,6 @@ func TestFilterNodes(t *testing.T) {
if len(tags) != 3 && tags["population"] == "0" && tags["name"] == "foo" && tags["place"] == "village" {
t.Fatal("Filter result not expected", tags)
}
}
func BenchmarkFilterNodes(b *testing.B) {