github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/tests/alias_test.go (about) 1 package tests 2 3 import ( 4 "testing" 5 ) 6 7 type foo struct { 8 a int 9 } 10 11 func Test1(t *testing.T) { 12 calls := 0 13 bar := func() *foo { 14 calls++ 15 return &foo{42} 16 } 17 q := &bar().a 18 if calls != 1 { 19 t.Error("Should've been a call") 20 } 21 *q = 40 22 if calls != 1 { 23 t.Error("Wrong number of calls: ", calls, ", should be 1") 24 } 25 if *q != 40 { 26 t.Error("*q != 40") 27 } 28 } 29 30 func Test2(t *testing.T) { 31 f := foo{} 32 p := &f.a 33 f = foo{} 34 f.a = 4 35 if *p != 4 { 36 t.Error("*p != 4") 37 } 38 } 39 40 func Test3(t *testing.T) { 41 f := foo{} 42 p := &f 43 f = foo{4} 44 if p.a != 4 { 45 t.Error("p.a != 4") 46 } 47 } 48 49 func Test4(t *testing.T) { 50 f := struct { 51 a struct { 52 b int 53 } 54 }{} 55 p := &f.a 56 q := &p.b 57 r := &(*p).b 58 *r = 4 59 p = nil 60 if *r != 4 { 61 t.Error("*r != 4") 62 } 63 if *q != 4 { 64 t.Error("*q != 4") 65 } 66 } 67 68 func Test5(t *testing.T) { 69 f := struct { 70 a [3]int 71 }{[3]int{6, 6, 6}} 72 s := f.a[:] 73 f.a = [3]int{4, 4, 4} 74 if s[1] != 4 { 75 t.Error("s[1] != 4") 76 } 77 }