github.com/liquid-dev/text@v0.3.3-liquid/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  package idna
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/liquid-dev/text/internal/gen"
    13  	"github.com/liquid-dev/text/internal/testtext"
    14  	"github.com/liquid-dev/text/internal/ucd"
    15  )
    16  
    17  func TestConformance(t *testing.T) {
    18  	testtext.SkipIfNotLong(t)
    19  
    20  	r := gen.OpenUnicodeFile("idna", "10.0.0", "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  }