github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/natives/src/encoding/gob/gob_test.go (about)

     1  //go:build js
     2  // +build js
     3  
     4  package gob
     5  
     6  import (
     7  	"bytes"
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  // TODO: TestEndToEnd override can be removed once the bug with Marr field is fixed.
    13  func TestEndToEnd(t *testing.T) {
    14  	type T2 struct {
    15  		T string
    16  	}
    17  	type T3 struct {
    18  		X float64
    19  		Z *int
    20  	}
    21  	type T1 struct {
    22  		A, B, C  int
    23  		M        map[string]*float64
    24  		M2       map[int]T3
    25  		Mstring  map[string]string
    26  		Mintptr  map[int]*int
    27  		Mcomp    map[complex128]complex128
    28  		Marr     map[[2]string][2]*float64
    29  		EmptyMap map[string]int // to check that we receive a non-nil map.
    30  		N        *[3]float64
    31  		Strs     *[2]string
    32  		Int64s   *[]int64
    33  		RI       complex64
    34  		S        string
    35  		Y        []byte
    36  		T        *T2
    37  	}
    38  	pi := 3.14159
    39  	e := 2.71828
    40  	two := 2.0
    41  	meaning := 42
    42  	fingers := 5
    43  	s1 := "string1"
    44  	s2 := "string2"
    45  	var comp1 complex128 = complex(1.0, 1.0)
    46  	var comp2 complex128 = complex(1.0, 1.0)
    47  	var arr1 [2]string
    48  	arr1[0] = s1
    49  	arr1[1] = s2
    50  	var arr2 [2]string
    51  	arr2[0] = s2
    52  	arr2[1] = s1
    53  	var floatArr1 [2]*float64
    54  	floatArr1[0] = &pi
    55  	floatArr1[1] = &e
    56  	var floatArr2 [2]*float64
    57  	floatArr2[0] = &e
    58  	floatArr2[1] = &two
    59  	t1 := &T1{
    60  		A:       17,
    61  		B:       18,
    62  		C:       -5,
    63  		M:       map[string]*float64{"pi": &pi, "e": &e},
    64  		M2:      map[int]T3{4: {X: pi, Z: &meaning}, 10: {X: e, Z: &fingers}},
    65  		Mstring: map[string]string{"pi": "3.14", "e": "2.71"},
    66  		Mintptr: map[int]*int{meaning: &fingers, fingers: &meaning},
    67  		Mcomp:   map[complex128]complex128{comp1: comp2, comp2: comp1},
    68  		// TODO: Fix this problem:
    69  		// 	TypeError: dst.$set is not a function
    70  		// 	    at typedmemmove (/github.com/gopherjs/gopherjs/reflect.go:487:3)
    71  		// Marr:     map[[2]string][2]*float64{arr1: floatArr1, arr2: floatArr2},
    72  		EmptyMap: make(map[string]int),
    73  		N:        &[3]float64{1.5, 2.5, 3.5},
    74  		Strs:     &[2]string{s1, s2},
    75  		Int64s:   &[]int64{77, 89, 123412342134},
    76  		RI:       17 - 23i,
    77  		S:        "Now is the time",
    78  		Y:        []byte("hello, sailor"),
    79  		T:        &T2{"this is T2"},
    80  	}
    81  	b := new(bytes.Buffer)
    82  	err := NewEncoder(b).Encode(t1)
    83  	if err != nil {
    84  		t.Error("encode:", err)
    85  	}
    86  	var _t1 T1
    87  	err = NewDecoder(b).Decode(&_t1)
    88  	if err != nil {
    89  		t.Fatal("decode:", err)
    90  	}
    91  	if !reflect.DeepEqual(t1, &_t1) {
    92  		t.Errorf("encode expected %v got %v", *t1, _t1)
    93  	}
    94  	// Be absolutely sure the received map is non-nil.
    95  	if t1.EmptyMap == nil {
    96  		t.Errorf("nil map sent")
    97  	}
    98  	if _t1.EmptyMap == nil {
    99  		t.Errorf("nil map received")
   100  	}
   101  }
   102  
   103  func TestTypeRace(t *testing.T) {
   104  	// encoding/gob currently uses nosync. This test uses sync.WaitGroup and
   105  	// cannot succeed when nosync is used.
   106  	t.Skip("using nosync")
   107  }