github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/test_utils_test.go (about) 1 // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls 2 // 3 // Copyright 2020 Stafi Protocol 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package types_test 18 19 import ( 20 "bytes" 21 "fmt" 22 "reflect" 23 "testing" 24 25 "github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale" 26 . "github.com/stafiprotocol/go-substrate-rpc-client/types" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 type encodedLengthAssert struct { 31 input interface{} 32 expected int 33 } 34 35 func assertEqual(t *testing.T, a interface{}, b interface{}) { 36 if reflect.DeepEqual(a, b) { 37 return 38 } 39 t.Errorf("Received %#v (type %v), expected %#v (type %v)", a, reflect.TypeOf(a), b, reflect.TypeOf(b)) 40 } 41 42 func assertRoundtrip(t *testing.T, value interface{}) { 43 var buffer = bytes.Buffer{} 44 err := scale.NewEncoder(&buffer).Encode(value) 45 assert.NoError(t, err) 46 target := reflect.New(reflect.TypeOf(value)) 47 err = scale.NewDecoder(&buffer).Decode(target.Interface()) 48 assert.NoError(t, err) 49 assertEqual(t, target.Elem().Interface(), value) 50 } 51 52 func assertEncodedLength(t *testing.T, encodedLengthAsserts []encodedLengthAssert) { 53 for _, test := range encodedLengthAsserts { 54 result, err := EncodedLength(test.input) 55 if err != nil { 56 t.Errorf("Encoded length error for input %v: %v\n", test.input, err) 57 } 58 if result != test.expected { 59 t.Errorf("Fail, input %v, expected %v, result %v\n", test.input, test.expected, result) 60 } 61 } 62 } 63 64 type encodingAssert struct { 65 input interface{} 66 expected []byte 67 } 68 69 func assertEncode(t *testing.T, encodingAsserts []encodingAssert) { 70 for _, test := range encodingAsserts { 71 result, err := EncodeToBytes(test.input) 72 if err != nil { 73 t.Errorf("Encoding error for input %v: %v\n", test.input, err) 74 } 75 if !bytes.Equal(result, test.expected) { 76 t.Errorf("Fail, input %v, expected %#x, result %#x\n", test.input, test.expected, result) 77 } 78 } 79 } 80 81 type decodingAssert struct { 82 input []byte 83 expected interface{} 84 } 85 86 func assertDecode(t *testing.T, decodingAsserts []decodingAssert) { 87 for _, test := range decodingAsserts { 88 target := reflect.New(reflect.TypeOf(test.expected)) 89 err := DecodeFromBytes(test.input, target.Interface()) 90 if err != nil { 91 t.Errorf("Encoding error for input %v: %v\n", test.input, err) 92 } 93 assertEqual(t, target.Elem().Interface(), test.expected) 94 } 95 } 96 97 type hashAssert struct { 98 input interface{} 99 expected []byte 100 } 101 102 func assertHash(t *testing.T, hashAsserts []hashAssert) { 103 for _, test := range hashAsserts { 104 result, err := GetHash(test.input) 105 if err != nil { 106 t.Errorf("Hash error for input %v: %v\n", test.input, err) 107 } 108 if !bytes.Equal(result[:], test.expected) { 109 t.Errorf("Fail, input %v, expected %#x, result %#x\n", test.input, test.expected, result) 110 } 111 } 112 } 113 114 type encodeToHexAssert struct { 115 input interface{} 116 expected string 117 } 118 119 func assertEncodeToHex(t *testing.T, encodeToHexAsserts []encodeToHexAssert) { 120 for _, test := range encodeToHexAsserts { 121 result, err := EncodeToHexString(test.input) 122 if err != nil { 123 t.Errorf("Hex error for input %v: %v\n", test.input, err) 124 } 125 if result != test.expected { 126 t.Errorf("Fail, input %v, expected %v, result %v\n", test.input, test.expected, result) 127 } 128 } 129 } 130 131 type stringAssert struct { 132 input interface{} 133 expected string 134 } 135 136 func assertString(t *testing.T, stringAsserts []stringAssert) { 137 for _, test := range stringAsserts { 138 result := fmt.Sprintf("%v", test.input) 139 if result != test.expected { 140 t.Errorf("Fail, input %v, expected %v, result %v\n", test.input, test.expected, result) 141 } 142 } 143 } 144 145 type eqAssert struct { 146 input interface{} 147 other interface{} 148 expected bool 149 } 150 151 func assertEq(t *testing.T, eqAsserts []eqAssert) { 152 for _, test := range eqAsserts { 153 result := Eq(test.input, test.other) 154 if result != test.expected { 155 t.Errorf("Fail, input %v, other %v, expected %v, result %v\n", test.input, test.other, test.expected, result) 156 } 157 } 158 }