Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / cache / serialization_test.go
1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
4
5 package cache
6
7 import (
8         "reflect"
9         "testing"
10 )
11
12 type Struct1 struct {
13         X int
14 }
15
16 func (s Struct1) Method1() {}
17
18 type Interface1 interface {
19         Method1()
20 }
21
22 var (
23         struct1                    = Struct1{1}
24         ptrStruct                  = &Struct1{2}
25         emptyIface     interface{} = Struct1{3}
26         iface1         Interface1  = Struct1{4}
27         sliceStruct                = []Struct1{{5}, {6}, {7}}
28         ptrSliceStruct             = []*Struct1{{8}, {9}, {10}}
29
30         valueMap = map[string]interface{}{
31                 "bytes":          []byte{0x61, 0x62, 0x63, 0x64},
32                 "string":         "string",
33                 "bool":           true,
34                 "int":            5,
35                 "int8":           int8(5),
36                 "int16":          int16(5),
37                 "int32":          int32(5),
38                 "int64":          int64(5),
39                 "uint":           uint(5),
40                 "uint8":          uint8(5),
41                 "uint16":         uint16(5),
42                 "uint32":         uint32(5),
43                 "uint64":         uint64(5),
44                 "float32":        float32(5),
45                 "float64":        float64(5),
46                 "array":          [5]int{1, 2, 3, 4, 5},
47                 "slice":          []int{1, 2, 3, 4, 5},
48                 "emptyIf":        emptyIface,
49                 "Iface1":         iface1,
50                 "map":            map[string]string{"foo": "bar"},
51                 "ptrStruct":      ptrStruct,
52                 "struct1":        struct1,
53                 "sliceStruct":    sliceStruct,
54                 "ptrSliceStruct": ptrSliceStruct,
55         }
56 )
57
58 // Test passing all kinds of data between serialize and deserialize.
59 func TestRoundTrip(t *testing.T) {
60         for _, expected := range valueMap {
61                 bytes, err := Serialize(expected)
62                 if err != nil {
63                         t.Error(err)
64                         continue
65                 }
66
67                 ptrActual := reflect.New(reflect.TypeOf(expected)).Interface()
68                 err = Deserialize(bytes, ptrActual)
69                 if err != nil {
70                         t.Error(err)
71                         continue
72                 }
73
74                 actual := reflect.ValueOf(ptrActual).Elem().Interface()
75                 if !reflect.DeepEqual(expected, actual) {
76                         t.Errorf("(expected) %T %v != %T %v (actual)", expected, expected, actual, actual)
77                 }
78         }
79 }
80
81 func zeroMap(arg map[string]interface{}) map[string]interface{} {
82         result := map[string]interface{}{}
83         for key, value := range arg {
84                 result[key] = reflect.Zero(reflect.TypeOf(value)).Interface()
85         }
86         return result
87 }