| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package currency |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"golang.org/x/text/language" |
| 12 |
) |
| 13 |
|
| 14 |
func TestQuery(t *testing.T) { |
| 15 |
r := func(region string) language.Region { |
| 16 |
return language.MustParseRegion(region) |
| 17 |
} |
| 18 |
t1800, _ := time.Parse("2006-01-02", "1800-01-01") |
| 19 |
type result struct { |
| 20 |
region language.Region |
| 21 |
unit Unit |
| 22 |
isTender bool |
| 23 |
from, to string |
| 24 |
} |
| 25 |
testCases := []struct { |
| 26 |
name string |
| 27 |
opts []QueryOption |
| 28 |
results []result |
| 29 |
}{{ |
| 30 |
name: "XA", |
| 31 |
opts: []QueryOption{Region(r("XA"))}, |
| 32 |
results: []result{}, |
| 33 |
}, { |
| 34 |
name: "AC", |
| 35 |
opts: []QueryOption{Region(r("AC"))}, |
| 36 |
results: []result{ |
| 37 |
{r("AC"), MustParseISO("SHP"), true, "1976-01-01", ""}, |
| 38 |
}, |
| 39 |
}, { |
| 40 |
name: "US", |
| 41 |
opts: []QueryOption{Region(r("US"))}, |
| 42 |
results: []result{ |
| 43 |
{r("US"), MustParseISO("USD"), true, "1792-01-01", ""}, |
| 44 |
}, |
| 45 |
}, { |
| 46 |
name: "US-hist", |
| 47 |
opts: []QueryOption{Region(r("US")), Historical}, |
| 48 |
results: []result{ |
| 49 |
{r("US"), MustParseISO("USD"), true, "1792-01-01", ""}, |
| 50 |
}, |
| 51 |
}, { |
| 52 |
name: "US-non-tender", |
| 53 |
opts: []QueryOption{Region(r("US")), NonTender}, |
| 54 |
results: []result{ |
| 55 |
{r("US"), MustParseISO("USD"), true, "1792-01-01", ""}, |
| 56 |
{r("US"), MustParseISO("USN"), false, "", ""}, |
| 57 |
}, |
| 58 |
}, { |
| 59 |
name: "US-historical+non-tender", |
| 60 |
opts: []QueryOption{Region(r("US")), Historical, NonTender}, |
| 61 |
results: []result{ |
| 62 |
{r("US"), MustParseISO("USD"), true, "1792-01-01", ""}, |
| 63 |
{r("US"), MustParseISO("USN"), false, "", ""}, |
| 64 |
{r("US"), MustParseISO("USS"), false, "", "2014-03-01"}, |
| 65 |
}, |
| 66 |
}, { |
| 67 |
name: "1800", |
| 68 |
opts: []QueryOption{Date(t1800)}, |
| 69 |
results: []result{ |
| 70 |
{r("CH"), MustParseISO("CHF"), true, "1799-03-17", ""}, |
| 71 |
{r("GB"), MustParseISO("GBP"), true, "1694-07-27", ""}, |
| 72 |
{r("GI"), MustParseISO("GIP"), true, "1713-01-01", ""}, |
| 73 |
// The date for IE and PR seem wrong, so these may be updated at |
| 74 |
// some point causing the tests to fail. |
| 75 |
{r("IE"), MustParseISO("GBP"), true, "1800-01-01", "1922-01-01"}, |
| 76 |
{r("PR"), MustParseISO("ESP"), true, "1800-01-01", "1898-12-10"}, |
| 77 |
{r("US"), MustParseISO("USD"), true, "1792-01-01", ""}, |
| 78 |
}, |
| 79 |
}} |
| 80 |
for _, tc := range testCases { |
| 81 |
n := 0 |
| 82 |
for it := Query(tc.opts...); it.Next(); n++ { |
| 83 |
if n < len(tc.results) { |
| 84 |
got := result{ |
| 85 |
it.Region(), |
| 86 |
it.Unit(), |
| 87 |
it.IsTender(), |
| 88 |
getTime(it.From()), |
| 89 |
getTime(it.To()), |
| 90 |
} |
| 91 |
if got != tc.results[n] { |
| 92 |
t.Errorf("%s:%d: got %v; want %v", tc.name, n, got, tc.results[n]) |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
if n != len(tc.results) { |
| 97 |
t.Errorf("%s: unexpected number of results: got %d; want %d", tc.name, n, len(tc.results)) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func getTime(t time.Time, ok bool) string { |
| 103 |
if !ok { |
| 104 |
return "" |
| 105 |
} |
| 106 |
return t.Format("2006-01-02") |
| 107 |
} |