Something went wrong. Try again.
A geocoder you run yourself, for addresses and postal codes in the United States and Canada
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119package main
import "testing"
func containsInterpretation(interpretations []Interpretation, want Interpretation) bool { for _, in := range interpretations { if in == want { return true } } return false}
func TestParseFullAddress(t *testing.T) { interpretations, postal := parseQuery("210 state st augusta me") if postal != nil { t.Fatalf("no postal code expected, got %+v", postal) } want := Interpretation{ Number: "210", Street: "state street", City: "augusta", Region: "ME", Country: "US", } if !containsInterpretation(interpretations, want) { t.Errorf("missing %+v in %+v", want, interpretations) }}
func TestParseStreetOnlySplit(t *testing.T) { interpretations, _ := parseQuery("210 state st augusta me") want := Interpretation{ Number: "210", Street: "state street augusta", Region: "ME", Country: "US", } if !containsInterpretation(interpretations, want) { t.Errorf("missing whole-middle-as-street reading in %+v", interpretations) }}
func TestParseBarePlace(t *testing.T) { interpretations, postal := parseQuery("welshpool") if postal != nil { t.Fatalf("no postal code expected, got %+v", postal) } want := Interpretation{Place: "welshpool"} if !containsInterpretation(interpretations, want) { t.Errorf("missing bare place reading in %+v", interpretations) }}
func TestParsePlaceWithFullRegionName(t *testing.T) { interpretations, _ := parseQuery("welshpool new brunswick") want := Interpretation{Place: "welshpool", Region: "NB", Country: "CA"} if !containsInterpretation(interpretations, want) { t.Errorf("missing place-with-region reading in %+v", interpretations) }}
func TestParseBareZIP(t *testing.T) { interpretations, postal := parseQuery("04330") if postal == nil || postal.Country != "US" || postal.Code != "04330" || !postal.Bare { t.Fatalf("want bare US ZIP 04330, got %+v", postal) } if len(interpretations) != 0 { t.Errorf("a bare ZIP needs no other interpretations, got %+v", interpretations) }}
func TestParseCanadianPostal(t *testing.T) { cases := []struct { name string in string code string }{ {"six characters together", "e5e1a1", "E5E1A1"}, {"split in two tokens", "e5e 1a1", "E5E1A1"}, {"bare fsa", "e5e", "E5E"}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { _, postal := parseQuery(c.in) if postal == nil || postal.Country != "CA" || postal.Code != c.code { t.Errorf("parseQuery(%q) postal = %+v, want CA %s", c.in, postal, c.code) } }) }}
func TestParseZIPInsideQuery(t *testing.T) { interpretations, postal := parseQuery("augusta 04330") if postal == nil || postal.Bare { t.Fatalf("want an embedded ZIP, got %+v", postal) } if len(interpretations) == 0 { t.Error("the words around an embedded ZIP still deserve interpretations") }}
func TestParseThreeLetterWordIsNotAnFSA(t *testing.T) { _, postal := parseQuery("f1r street portland") if postal != nil { t.Errorf("a three-letter token inside a longer query is a word, got %+v", postal) }}
func TestParseCountrySuffix(t *testing.T) { interpretations, _ := parseQuery("portland maine usa") want := Interpretation{Place: "portland", Region: "ME", Country: "US"} if !containsInterpretation(interpretations, want) { t.Errorf("missing country-stripped reading in %+v", interpretations) }}
func TestParseEmpty(t *testing.T) { interpretations, postal := parseQuery(" ") if interpretations != nil || postal != nil { t.Errorf("empty query parses to nothing, got %+v, %+v", interpretations, postal) }}