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