...

Source file src/cmd/compile/internal/test/testdata/assert_test.go

Documentation: cmd/compile/internal/test/testdata

     1  // Copyright 2015 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  // Tests type assertion expressions and statements
     6  
     7  package main
     8  
     9  import (
    10  	"runtime"
    11  	"testing"
    12  )
    13  
    14  type (
    15  	S struct{}
    16  	U struct{}
    17  
    18  	I interface {
    19  		F()
    20  	}
    21  )
    22  
    23  var (
    24  	s *S
    25  	u *U
    26  )
    27  
    28  func (s *S) F() {}
    29  func (u *U) F() {}
    30  
    31  func e2t_ssa(e interface{}) *U {
    32  	return e.(*U)
    33  }
    34  
    35  func i2t_ssa(i I) *U {
    36  	return i.(*U)
    37  }
    38  
    39  func testAssertE2TOk(t *testing.T) {
    40  	if got := e2t_ssa(u); got != u {
    41  		t.Errorf("e2t_ssa(u)=%v want %v", got, u)
    42  	}
    43  }
    44  
    45  func testAssertE2TPanic(t *testing.T) {
    46  	var got *U
    47  	defer func() {
    48  		if got != nil {
    49  			t.Errorf("e2t_ssa(s)=%v want nil", got)
    50  		}
    51  		e := recover()
    52  		err, ok := e.(*runtime.TypeAssertionError)
    53  		if !ok {
    54  			t.Errorf("e2t_ssa(s) panic type %T", e)
    55  		}
    56  		want := "interface conversion: interface {} is *main.S, not *main.U"
    57  		if err.Error() != want {
    58  			t.Errorf("e2t_ssa(s) wrong error, want '%s', got '%s'", want, err.Error())
    59  		}
    60  	}()
    61  	got = e2t_ssa(s)
    62  	t.Errorf("e2t_ssa(s) should panic")
    63  
    64  }
    65  
    66  func testAssertI2TOk(t *testing.T) {
    67  	if got := i2t_ssa(u); got != u {
    68  		t.Errorf("i2t_ssa(u)=%v want %v", got, u)
    69  	}
    70  }
    71  
    72  func testAssertI2TPanic(t *testing.T) {
    73  	var got *U
    74  	defer func() {
    75  		if got != nil {
    76  			t.Errorf("i2t_ssa(s)=%v want nil", got)
    77  		}
    78  		e := recover()
    79  		err, ok := e.(*runtime.TypeAssertionError)
    80  		if !ok {
    81  			t.Errorf("i2t_ssa(s) panic type %T", e)
    82  		}
    83  		want := "interface conversion: main.I is *main.S, not *main.U"
    84  		if err.Error() != want {
    85  			t.Errorf("i2t_ssa(s) wrong error, want '%s', got '%s'", want, err.Error())
    86  		}
    87  	}()
    88  	got = i2t_ssa(s)
    89  	t.Errorf("i2t_ssa(s) should panic")
    90  }
    91  
    92  func e2t2_ssa(e interface{}) (*U, bool) {
    93  	u, ok := e.(*U)
    94  	return u, ok
    95  }
    96  
    97  func i2t2_ssa(i I) (*U, bool) {
    98  	u, ok := i.(*U)
    99  	return u, ok
   100  }
   101  
   102  func testAssertE2T2(t *testing.T) {
   103  	if got, ok := e2t2_ssa(u); !ok || got != u {
   104  		t.Errorf("e2t2_ssa(u)=(%v, %v) want (%v, %v)", got, ok, u, true)
   105  	}
   106  	if got, ok := e2t2_ssa(s); ok || got != nil {
   107  		t.Errorf("e2t2_ssa(s)=(%v, %v) want (%v, %v)", got, ok, nil, false)
   108  	}
   109  }
   110  
   111  func testAssertI2T2(t *testing.T) {
   112  	if got, ok := i2t2_ssa(u); !ok || got != u {
   113  		t.Errorf("i2t2_ssa(u)=(%v, %v) want (%v, %v)", got, ok, u, true)
   114  	}
   115  	if got, ok := i2t2_ssa(s); ok || got != nil {
   116  		t.Errorf("i2t2_ssa(s)=(%v, %v) want (%v, %v)", got, ok, nil, false)
   117  	}
   118  }
   119  
   120  // TestTypeAssertion tests type assertions.
   121  func TestTypeAssertion(t *testing.T) {
   122  	testAssertE2TOk(t)
   123  	testAssertE2TPanic(t)
   124  	testAssertI2TOk(t)
   125  	testAssertI2TPanic(t)
   126  	testAssertE2T2(t)
   127  	testAssertI2T2(t)
   128  }
   129  

View as plain text