-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathtestmetadata_test.go
More file actions
72 lines (65 loc) · 2.8 KB
/
Copy pathtestmetadata_test.go
File metadata and controls
72 lines (65 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package phonenumbers
// Harness for running the library against upstream libphonenumber's synthetic
// test metadata, the Go analogue of upstream's TestMetadataTestCase + RegionCode.
//
// Upstream's PhoneNumberUtilTest et al. run against resources/PhoneNumberMetadata
// ForTesting.xml — a frozen, hand-authored set of regions with made-up number
// ranges — so test expectations never change when real-world metadata updates.
// We compile that same XML (committed under testdata/) through our builder and
// swap it in via the metadata seam.
//
// Mirrors TestMetadataTestCase: useTestMetadata == setUp's setInstance(test),
// and the registered t.Cleanup == tearDown's setInstance(null). As upstream
// notes, such tests must NOT run in parallel (the active metadata is global).
import (
"os"
"sync"
"testing"
"github.com/nyaruka/phonenumbers/v2/internal/metadatabuilder"
"github.com/nyaruka/phonenumbers/v2/metadata"
"github.com/stretchr/testify/require"
)
// regionCode mirrors upstream's RegionCode constants so ported tests can read
// regionCode.US the way the Java tests read RegionCode.US.
var regionCode = struct {
UN001, AD, AE, AM, AO, AR, AU, BB, BR, BS, BY, CA, CC, CN, CO, CX, DE, FR,
GB, GG, IT, JP, KR, MX, NZ, PL, RE, RU, SE, SG, TA, US, UZ, YT, ZZ string
}{
UN001: "001", AD: "AD", AE: "AE", AM: "AM", AO: "AO", AR: "AR", AU: "AU",
BB: "BB", BR: "BR", BS: "BS", BY: "BY", CA: "CA", CC: "CC", CN: "CN",
CO: "CO", CX: "CX", DE: "DE", FR: "FR", GB: "GB", GG: "GG", IT: "IT",
JP: "JP", KR: "KR", MX: "MX", NZ: "NZ", PL: "PL", RE: "RE", RU: "RU",
SE: "SE", SG: "SG", TA: "TA", US: "US", UZ: "UZ", YT: "YT", ZZ: "ZZ",
}
var (
testMetadataOnce sync.Once
testMetadataContainer *metadata.Container
testMetadataErr error
)
// loadTestMetadataContainer compiles the synthetic test XML once and caches the
// resulting container, following the exact build path cmd/buildmetadata uses for
// the real metadata.
func loadTestMetadataContainer() (*metadata.Container, error) {
testMetadataOnce.Do(func() {
xml, err := os.ReadFile("testdata/PhoneNumberMetadataForTesting.xml")
if err != nil {
testMetadataErr = err
return
}
coll, err := metadatabuilder.BuildPhoneMetadataCollection(xml, false, false, false)
if err != nil {
testMetadataErr = err
return
}
testMetadataContainer, testMetadataErr = metadata.NewContainer(coll, metadatabuilder.BuildCountryCodeToRegionMap(coll))
})
return testMetadataContainer, testMetadataErr
}
// useTestMetadata activates the synthetic test metadata for the duration of the
// test and restores the embedded metadata afterwards (via t.Cleanup, so it runs
// even on a panic/FailNow). Do not call from a test that uses t.Parallel().
func useTestMetadata(t *testing.T) {
mc, err := loadTestMetadataContainer()
require.NoError(t, err)
t.Cleanup(metadata.Use(mc))
}