search: add LocationConstraint tests, make Any work

Change-Id: I1a13f0cc7333bb209edcd7f28184a10efeee544d
This commit is contained in:
Brad Fitzpatrick 2014-06-15 13:57:13 -07:00
parent a0caaa3bec
commit abc247c34d
2 changed files with 29 additions and 1 deletions

View File

@ -589,3 +589,31 @@ func TestMatchPrefix(t *testing.T) {
t.Error("Expected simple mismatch")
}
}
func TestLocationConstraint(t *testing.T) {
var c LocationConstraint
if c.matchesLatLong(1, 2) {
t.Error("zero value shouldn't match")
}
c.Any = true
if !c.matchesLatLong(1, 2) {
t.Error("Any should match")
}
c = LocationConstraint{North: 2, South: 1, West: 0, East: 2}
tests := []struct {
lat, long float64
want bool
}{
{1, 1, true},
{3, 1, false}, // too north
{1, 3, false}, // too east
{1, -1, false}, // too west
{0, 1, false}, // too south
}
for _, tt := range tests {
if got := c.matchesLatLong(tt.lat, tt.long); got != tt.want {
t.Errorf("matches(%v, %v) = %v; want %v", tt.lat, tt.long, got, tt.want)
}
}
}

View File

@ -463,7 +463,7 @@ type LocationConstraint struct {
}
func (c *LocationConstraint) matchesLatLong(lat, long float64) bool {
return c.West <= long && long <= c.East && c.South <= lat && lat <= c.North
return c.Any || (c.West <= long && long <= c.East && c.South <= lat && lat <= c.North)
}
// A StringConstraint specifies constraints on a string.