...

Source file src/cmd/compile/internal/types2/typeset_test.go

Documentation: cmd/compile/internal/types2

     1  // Copyright 2021 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 types2
     6  
     7  import (
     8  	"cmd/compile/internal/syntax"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestInvalidTypeSet(t *testing.T) {
    14  	if !invalidTypeSet.IsEmpty() {
    15  		t.Error("invalidTypeSet is not empty")
    16  	}
    17  }
    18  
    19  func TestTypeSetString(t *testing.T) {
    20  	for body, want := range map[string]string{
    21  		"{}":            "𝓤",
    22  		"{int}":         "{int}",
    23  		"{~int}":        "{~int}",
    24  		"{int|string}":  "{int | string}",
    25  		"{int; string}": "∅",
    26  
    27  		"{comparable}":              "{comparable}",
    28  		"{comparable; int}":         "{int}",
    29  		"{~int; comparable}":        "{~int}",
    30  		"{int|string; comparable}":  "{int | string}",
    31  		"{comparable; int; string}": "∅",
    32  
    33  		"{m()}":                         "{func (p.T).m()}",
    34  		"{m1(); m2() int }":             "{func (p.T).m1(); func (p.T).m2() int}",
    35  		"{error}":                       "{func (error).Error() string}",
    36  		"{m(); comparable}":             "{comparable; func (p.T).m()}",
    37  		"{m1(); comparable; m2() int }": "{comparable; func (p.T).m1(); func (p.T).m2() int}",
    38  		"{comparable; error}":           "{comparable; func (error).Error() string}",
    39  
    40  		"{m(); comparable; int|float32|string}": "{func (p.T).m(); int | float32 | string}",
    41  		"{m1(); int; m2(); comparable }":        "{func (p.T).m1(); func (p.T).m2(); int}",
    42  
    43  		"{E}; type E interface{}":           "𝓤",
    44  		"{E}; type E interface{int;string}": "∅",
    45  		"{E}; type E interface{comparable}": "{comparable}",
    46  	} {
    47  		// parse
    48  		errh := func(error) {} // dummy error handler so that parsing continues in presence of errors
    49  		src := "package p; type T interface" + body
    50  		file, err := syntax.Parse(nil, strings.NewReader(src), errh, nil, 0)
    51  		if err != nil {
    52  			t.Fatalf("%s: %v (invalid test case)", body, err)
    53  		}
    54  
    55  		// type check
    56  		var conf Config
    57  		pkg, err := conf.Check(file.PkgName.Value, []*syntax.File{file}, nil)
    58  		if err != nil {
    59  			t.Fatalf("%s: %v (invalid test case)", body, err)
    60  		}
    61  
    62  		// lookup T
    63  		obj := pkg.scope.Lookup("T")
    64  		if obj == nil {
    65  			t.Fatalf("%s: T not found (invalid test case)", body)
    66  		}
    67  		T, ok := under(obj.Type()).(*Interface)
    68  		if !ok {
    69  			t.Fatalf("%s: %v is not an interface (invalid test case)", body, obj)
    70  		}
    71  
    72  		// verify test case
    73  		got := T.typeSet().String()
    74  		if got != want {
    75  			t.Errorf("%s: got %s; want %s", body, got, want)
    76  		}
    77  	}
    78  }
    79  
    80  // TODO(gri) add more tests
    81  

View as plain text