github.com/go-enjin/golang-org-x-text@v0.12.1-enjin.2/internal/export/idna/conformance_test.go (about)

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