Skip to content

Commit e3703dc

Browse files
committed
internal/export/idna: hoist conformance test
in preparation to deal with the new file format in Unicode 11. Updates golang/go#27945 Change-Id: I424b3891e852c87bea536646a98a96aae2446294 Reviewed-on: https://go-review.googlesource.com/c/text/+/154441 Run-TryBot: Marcel van Lohuizen <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent fe223c5 commit e3703dc

File tree

2 files changed

+70
-64
lines changed

2 files changed

+70
-64
lines changed
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2018 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 idna
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
"testing"
11+
12+
"golang.org/x/text/internal/gen"
13+
"golang.org/x/text/internal/testtext"
14+
"golang.org/x/text/internal/ucd"
15+
)
16+
17+
func TestConformance(t *testing.T) {
18+
testtext.SkipIfNotLong(t)
19+
20+
r := gen.OpenUnicodeFile("idna", "", "IdnaTest.txt")
21+
defer r.Close()
22+
23+
section := "main"
24+
p := ucd.New(r)
25+
transitional := New(Transitional(true), VerifyDNSLength(true), BidiRule(), MapForLookup())
26+
nonTransitional := New(VerifyDNSLength(true), BidiRule(), MapForLookup())
27+
for p.Next() {
28+
// What to test
29+
profiles := []*Profile{}
30+
switch p.String(0) {
31+
case "T":
32+
profiles = append(profiles, transitional)
33+
case "N":
34+
profiles = append(profiles, nonTransitional)
35+
case "B":
36+
profiles = append(profiles, transitional)
37+
profiles = append(profiles, nonTransitional)
38+
}
39+
40+
src := unescape(p.String(1))
41+
42+
wantToUnicode := unescape(p.String(2))
43+
if wantToUnicode == "" {
44+
wantToUnicode = src
45+
}
46+
wantToASCII := unescape(p.String(3))
47+
if wantToASCII == "" {
48+
wantToASCII = wantToUnicode
49+
}
50+
wantErrToUnicode := ""
51+
if strings.HasPrefix(wantToUnicode, "[") {
52+
wantErrToUnicode = wantToUnicode
53+
wantToUnicode = ""
54+
}
55+
wantErrToASCII := ""
56+
if strings.HasPrefix(wantToASCII, "[") {
57+
wantErrToASCII = wantToASCII
58+
wantToASCII = ""
59+
}
60+
61+
// TODO: also do IDNA tests.
62+
// invalidInIDNA2008 := p.String(4) == "NV8"
63+
64+
for _, p := range profiles {
65+
name := fmt.Sprintf("%s:%s", section, p)
66+
doTest(t, p.ToUnicode, name+":ToUnicode", src, wantToUnicode, wantErrToUnicode)
67+
doTest(t, p.ToASCII, name+":ToASCII", src, wantToASCII, wantErrToASCII)
68+
}
69+
}
70+
}

internal/export/idna/idna_test.go

-64
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import (
1010
"strings"
1111
"testing"
1212

13-
"golang.org/x/text/internal/gen"
1413
"golang.org/x/text/internal/testtext"
15-
"golang.org/x/text/internal/ucd"
1614
)
1715

1816
func TestAllocToUnicode(t *testing.T) {
@@ -101,68 +99,6 @@ func doTest(t *testing.T, f func(string) (string, error), name, input, want, err
10199
})
102100
}
103101

104-
func TestConformance(t *testing.T) {
105-
testtext.SkipIfNotLong(t)
106-
107-
r := gen.OpenUnicodeFile("idna", "", "IdnaTest.txt")
108-
defer r.Close()
109-
110-
section := "main"
111-
started := false
112-
p := ucd.New(r, ucd.CommentHandler(func(s string) {
113-
if started {
114-
section = strings.ToLower(strings.Split(s, " ")[0])
115-
}
116-
}))
117-
transitional := New(Transitional(true), VerifyDNSLength(true), BidiRule(), MapForLookup())
118-
nonTransitional := New(VerifyDNSLength(true), BidiRule(), MapForLookup())
119-
for p.Next() {
120-
started = true
121-
122-
// What to test
123-
profiles := []*Profile{}
124-
switch p.String(0) {
125-
case "T":
126-
profiles = append(profiles, transitional)
127-
case "N":
128-
profiles = append(profiles, nonTransitional)
129-
case "B":
130-
profiles = append(profiles, transitional)
131-
profiles = append(profiles, nonTransitional)
132-
}
133-
134-
src := unescape(p.String(1))
135-
136-
wantToUnicode := unescape(p.String(2))
137-
if wantToUnicode == "" {
138-
wantToUnicode = src
139-
}
140-
wantToASCII := unescape(p.String(3))
141-
if wantToASCII == "" {
142-
wantToASCII = wantToUnicode
143-
}
144-
wantErrToUnicode := ""
145-
if strings.HasPrefix(wantToUnicode, "[") {
146-
wantErrToUnicode = wantToUnicode
147-
wantToUnicode = ""
148-
}
149-
wantErrToASCII := ""
150-
if strings.HasPrefix(wantToASCII, "[") {
151-
wantErrToASCII = wantToASCII
152-
wantToASCII = ""
153-
}
154-
155-
// TODO: also do IDNA tests.
156-
// invalidInIDNA2008 := p.String(4) == "NV8"
157-
158-
for _, p := range profiles {
159-
name := fmt.Sprintf("%s:%s", section, p)
160-
doTest(t, p.ToUnicode, name+":ToUnicode", src, wantToUnicode, wantErrToUnicode)
161-
doTest(t, p.ToASCII, name+":ToASCII", src, wantToASCII, wantErrToASCII)
162-
}
163-
}
164-
}
165-
166102
func unescape(s string) string {
167103
s, err := strconv.Unquote(`"` + s + `"`)
168104
if err != nil {

0 commit comments

Comments
 (0)