4
4
package scale
5
5
6
6
import (
7
+ "bytes"
7
8
"math/big"
8
9
"reflect"
9
10
"strings"
10
11
"testing"
12
+
13
+ "github.com/stretchr/testify/assert"
14
+ "github.com/stretchr/testify/require"
11
15
)
12
16
17
+ func Test_NewEncoder (t * testing.T ) {
18
+ t .Parallel ()
19
+
20
+ cache .Lock ()
21
+ defer cache .Unlock ()
22
+
23
+ writer := bytes .NewBuffer (nil )
24
+ encoder := NewEncoder (writer )
25
+
26
+ expectedEncoder := & Encoder {
27
+ encodeState : encodeState {
28
+ Writer : writer ,
29
+ fieldScaleIndicesCache : cache ,
30
+ },
31
+ }
32
+
33
+ assert .Equal (t , expectedEncoder , encoder )
34
+ }
35
+
36
+ func Test_Encoder_Encode (t * testing.T ) {
37
+ t .Parallel ()
38
+
39
+ buffer := bytes .NewBuffer (nil )
40
+ encoder := NewEncoder (buffer )
41
+
42
+ err := encoder .Encode (uint16 (1 ))
43
+ require .NoError (t , err )
44
+
45
+ err = encoder .Encode (uint8 (2 ))
46
+ require .NoError (t , err )
47
+
48
+ array := [2 ]byte {4 , 5 }
49
+ err = encoder .Encode (array )
50
+ require .NoError (t , err )
51
+
52
+ type T struct {
53
+ Array [2 ]byte
54
+ }
55
+
56
+ someStruct := T {Array : [2 ]byte {6 , 7 }}
57
+ err = encoder .Encode (someStruct )
58
+ require .NoError (t , err )
59
+
60
+ structSlice := []T {{Array : [2 ]byte {8 , 9 }}}
61
+ err = encoder .Encode (structSlice )
62
+ require .NoError (t , err )
63
+
64
+ written := buffer .Bytes ()
65
+ expectedWritten := []byte {
66
+ 1 , 0 ,
67
+ 2 ,
68
+ 4 , 5 ,
69
+ 6 , 7 ,
70
+ 4 , 8 , 9 ,
71
+ }
72
+ assert .Equal (t , expectedWritten , written )
73
+ }
74
+
13
75
type test struct {
14
76
name string
15
77
in interface {}
@@ -869,12 +931,15 @@ type MyStructWithPrivate struct {
869
931
func Test_encodeState_encodeFixedWidthInteger (t * testing.T ) {
870
932
for _ , tt := range fixedWidthIntegerTests {
871
933
t .Run (tt .name , func (t * testing.T ) {
872
- es := & encodeState {}
934
+ buffer := bytes .NewBuffer (nil )
935
+ es := & encodeState {
936
+ Writer : buffer ,
937
+ }
873
938
if err := es .marshal (tt .in ); (err != nil ) != tt .wantErr {
874
939
t .Errorf ("encodeState.encodeFixedWidthInt() error = %v, wantErr %v" , err , tt .wantErr )
875
940
}
876
- if ! reflect .DeepEqual (es . Buffer .Bytes (), tt .want ) {
877
- t .Errorf ("encodeState.encodeFixedWidthInt() = %v, want %v" , es . Buffer .Bytes (), tt .want )
941
+ if ! reflect .DeepEqual (buffer .Bytes (), tt .want ) {
942
+ t .Errorf ("encodeState.encodeFixedWidthInt() = %v, want %v" , buffer .Bytes (), tt .want )
878
943
}
879
944
})
880
945
}
@@ -883,12 +948,15 @@ func Test_encodeState_encodeFixedWidthInteger(t *testing.T) {
883
948
func Test_encodeState_encodeVariableWidthIntegers (t * testing.T ) {
884
949
for _ , tt := range variableWidthIntegerTests {
885
950
t .Run (tt .name , func (t * testing.T ) {
886
- es := & encodeState {}
951
+ buffer := bytes .NewBuffer (nil )
952
+ es := & encodeState {
953
+ Writer : buffer ,
954
+ }
887
955
if err := es .marshal (tt .in ); (err != nil ) != tt .wantErr {
888
956
t .Errorf ("encodeState.encodeFixedWidthInt() error = %v, wantErr %v" , err , tt .wantErr )
889
957
}
890
- if ! reflect .DeepEqual (es . Buffer .Bytes (), tt .want ) {
891
- t .Errorf ("encodeState.encodeFixedWidthInt() = %v, want %v" , es . Buffer .Bytes (), tt .want )
958
+ if ! reflect .DeepEqual (buffer .Bytes (), tt .want ) {
959
+ t .Errorf ("encodeState.encodeFixedWidthInt() = %v, want %v" , buffer .Bytes (), tt .want )
892
960
}
893
961
})
894
962
}
@@ -897,12 +965,15 @@ func Test_encodeState_encodeVariableWidthIntegers(t *testing.T) {
897
965
func Test_encodeState_encodeBigInt (t * testing.T ) {
898
966
for _ , tt := range bigIntTests {
899
967
t .Run (tt .name , func (t * testing.T ) {
900
- es := & encodeState {}
968
+ buffer := bytes .NewBuffer (nil )
969
+ es := & encodeState {
970
+ Writer : buffer ,
971
+ }
901
972
if err := es .marshal (tt .in ); (err != nil ) != tt .wantErr {
902
973
t .Errorf ("encodeState.encodeBigInt() error = %v, wantErr %v" , err , tt .wantErr )
903
974
}
904
- if ! reflect .DeepEqual (es . Buffer .Bytes (), tt .want ) {
905
- t .Errorf ("encodeState.encodeBigInt() = %v, want %v" , es . Buffer .Bytes (), tt .want )
975
+ if ! reflect .DeepEqual (buffer .Bytes (), tt .want ) {
976
+ t .Errorf ("encodeState.encodeBigInt() = %v, want %v" , buffer .Bytes (), tt .want )
906
977
}
907
978
})
908
979
}
@@ -911,12 +982,15 @@ func Test_encodeState_encodeBigInt(t *testing.T) {
911
982
func Test_encodeState_encodeUint128 (t * testing.T ) {
912
983
for _ , tt := range uint128Tests {
913
984
t .Run (tt .name , func (t * testing.T ) {
914
- es := & encodeState {}
985
+ buffer := bytes .NewBuffer (nil )
986
+ es := & encodeState {
987
+ Writer : buffer ,
988
+ }
915
989
if err := es .marshal (tt .in ); (err != nil ) != tt .wantErr {
916
990
t .Errorf ("encodeState.encodeUin128() error = %v, wantErr %v" , err , tt .wantErr )
917
991
}
918
- if ! reflect .DeepEqual (es . Buffer .Bytes (), tt .want ) {
919
- t .Errorf ("encodeState.encodeUin128() = %v, want %v" , es . Buffer .Bytes (), tt .want )
992
+ if ! reflect .DeepEqual (buffer .Bytes (), tt .want ) {
993
+ t .Errorf ("encodeState.encodeUin128() = %v, want %v" , buffer .Bytes (), tt .want )
920
994
}
921
995
})
922
996
}
@@ -925,12 +999,16 @@ func Test_encodeState_encodeUint128(t *testing.T) {
925
999
func Test_encodeState_encodeBytes (t * testing.T ) {
926
1000
for _ , tt := range stringTests {
927
1001
t .Run (tt .name , func (t * testing.T ) {
928
- es := & encodeState {}
1002
+
1003
+ buffer := bytes .NewBuffer (nil )
1004
+ es := & encodeState {
1005
+ Writer : buffer ,
1006
+ }
929
1007
if err := es .marshal (tt .in ); (err != nil ) != tt .wantErr {
930
1008
t .Errorf ("encodeState.encodeBytes() error = %v, wantErr %v" , err , tt .wantErr )
931
1009
}
932
- if ! reflect .DeepEqual (es . Buffer .Bytes (), tt .want ) {
933
- t .Errorf ("encodeState.encodeBytes() = %v, want %v" , es . Buffer .Bytes (), tt .want )
1010
+ if ! reflect .DeepEqual (buffer .Bytes (), tt .want ) {
1011
+ t .Errorf ("encodeState.encodeBytes() = %v, want %v" , buffer .Bytes (), tt .want )
934
1012
}
935
1013
})
936
1014
}
@@ -939,12 +1017,16 @@ func Test_encodeState_encodeBytes(t *testing.T) {
939
1017
func Test_encodeState_encodeBool (t * testing.T ) {
940
1018
for _ , tt := range boolTests {
941
1019
t .Run (tt .name , func (t * testing.T ) {
942
- es := & encodeState {}
1020
+
1021
+ buffer := bytes .NewBuffer (nil )
1022
+ es := & encodeState {
1023
+ Writer : buffer ,
1024
+ }
943
1025
if err := es .marshal (tt .in ); (err != nil ) != tt .wantErr {
944
1026
t .Errorf ("encodeState.encodeBool() error = %v, wantErr %v" , err , tt .wantErr )
945
1027
}
946
- if ! reflect .DeepEqual (es . Buffer .Bytes (), tt .want ) {
947
- t .Errorf ("encodeState.encodeBool() = %v, want %v" , es . Buffer .Bytes (), tt .want )
1028
+ if ! reflect .DeepEqual (buffer .Bytes (), tt .want ) {
1029
+ t .Errorf ("encodeState.encodeBool() = %v, want %v" , buffer .Bytes (), tt .want )
948
1030
}
949
1031
})
950
1032
}
@@ -953,12 +1035,16 @@ func Test_encodeState_encodeBool(t *testing.T) {
953
1035
func Test_encodeState_encodeStruct (t * testing.T ) {
954
1036
for _ , tt := range structTests {
955
1037
t .Run (tt .name , func (t * testing.T ) {
956
- es := & encodeState {fieldScaleIndicesCache : cache }
1038
+ buffer := bytes .NewBuffer (nil )
1039
+ es := & encodeState {
1040
+ Writer : buffer ,
1041
+ fieldScaleIndicesCache : cache ,
1042
+ }
957
1043
if err := es .marshal (tt .in ); (err != nil ) != tt .wantErr {
958
1044
t .Errorf ("encodeState.encodeStruct() error = %v, wantErr %v" , err , tt .wantErr )
959
1045
}
960
- if ! reflect .DeepEqual (es . Buffer .Bytes (), tt .want ) {
961
- t .Errorf ("encodeState.encodeStruct() = %v, want %v" , es . Buffer .Bytes (), tt .want )
1046
+ if ! reflect .DeepEqual (buffer .Bytes (), tt .want ) {
1047
+ t .Errorf ("encodeState.encodeStruct() = %v, want %v" , buffer .Bytes (), tt .want )
962
1048
}
963
1049
})
964
1050
}
@@ -967,12 +1053,16 @@ func Test_encodeState_encodeStruct(t *testing.T) {
967
1053
func Test_encodeState_encodeSlice (t * testing.T ) {
968
1054
for _ , tt := range sliceTests {
969
1055
t .Run (tt .name , func (t * testing.T ) {
970
- es := & encodeState {fieldScaleIndicesCache : cache }
1056
+ buffer := bytes .NewBuffer (nil )
1057
+ es := & encodeState {
1058
+ Writer : buffer ,
1059
+ fieldScaleIndicesCache : cache ,
1060
+ }
971
1061
if err := es .marshal (tt .in ); (err != nil ) != tt .wantErr {
972
1062
t .Errorf ("encodeState.encodeSlice() error = %v, wantErr %v" , err , tt .wantErr )
973
1063
}
974
- if ! reflect .DeepEqual (es . Buffer .Bytes (), tt .want ) {
975
- t .Errorf ("encodeState.encodeSlice() = %v, want %v" , es . Buffer .Bytes (), tt .want )
1064
+ if ! reflect .DeepEqual (buffer .Bytes (), tt .want ) {
1065
+ t .Errorf ("encodeState.encodeSlice() = %v, want %v" , buffer .Bytes (), tt .want )
976
1066
}
977
1067
})
978
1068
}
@@ -981,12 +1071,16 @@ func Test_encodeState_encodeSlice(t *testing.T) {
981
1071
func Test_encodeState_encodeArray (t * testing.T ) {
982
1072
for _ , tt := range arrayTests {
983
1073
t .Run (tt .name , func (t * testing.T ) {
984
- es := & encodeState {fieldScaleIndicesCache : cache }
1074
+ buffer := bytes .NewBuffer (nil )
1075
+ es := & encodeState {
1076
+ Writer : buffer ,
1077
+ fieldScaleIndicesCache : cache ,
1078
+ }
985
1079
if err := es .marshal (tt .in ); (err != nil ) != tt .wantErr {
986
1080
t .Errorf ("encodeState.encodeArray() error = %v, wantErr %v" , err , tt .wantErr )
987
1081
}
988
- if ! reflect .DeepEqual (es . Buffer .Bytes (), tt .want ) {
989
- t .Errorf ("encodeState.encodeArray() = %v, want %v" , es . Buffer .Bytes (), tt .want )
1082
+ if ! reflect .DeepEqual (buffer .Bytes (), tt .want ) {
1083
+ t .Errorf ("encodeState.encodeArray() = %v, want %v" , buffer .Bytes (), tt .want )
990
1084
}
991
1085
})
992
1086
}
@@ -1007,12 +1101,16 @@ func Test_marshal_optionality(t *testing.T) {
1007
1101
}
1008
1102
for _ , tt := range ptrTests {
1009
1103
t .Run (tt .name , func (t * testing.T ) {
1010
- es := & encodeState {fieldScaleIndicesCache : cache }
1104
+ buffer := bytes .NewBuffer (nil )
1105
+ es := & encodeState {
1106
+ Writer : buffer ,
1107
+ fieldScaleIndicesCache : cache ,
1108
+ }
1011
1109
if err := es .marshal (tt .in ); (err != nil ) != tt .wantErr {
1012
1110
t .Errorf ("encodeState.encodeFixedWidthInt() error = %v, wantErr %v" , err , tt .wantErr )
1013
1111
}
1014
- if ! reflect .DeepEqual (es . Buffer .Bytes (), tt .want ) {
1015
- t .Errorf ("encodeState.encodeFixedWidthInt() = %v, want %v" , es . Buffer .Bytes (), tt .want )
1112
+ if ! reflect .DeepEqual (buffer .Bytes (), tt .want ) {
1113
+ t .Errorf ("encodeState.encodeFixedWidthInt() = %v, want %v" , buffer .Bytes (), tt .want )
1016
1114
}
1017
1115
})
1018
1116
}
@@ -1043,12 +1141,16 @@ func Test_marshal_optionality_nil_cases(t *testing.T) {
1043
1141
}
1044
1142
for _ , tt := range ptrTests {
1045
1143
t .Run (tt .name , func (t * testing.T ) {
1046
- es := & encodeState {fieldScaleIndicesCache : cache }
1144
+ buffer := bytes .NewBuffer (nil )
1145
+ es := & encodeState {
1146
+ Writer : buffer ,
1147
+ fieldScaleIndicesCache : cache ,
1148
+ }
1047
1149
if err := es .marshal (tt .in ); (err != nil ) != tt .wantErr {
1048
1150
t .Errorf ("encodeState.encodeFixedWidthInt() error = %v, wantErr %v" , err , tt .wantErr )
1049
1151
}
1050
- if ! reflect .DeepEqual (es . Buffer .Bytes (), tt .want ) {
1051
- t .Errorf ("encodeState.encodeFixedWidthInt() = %v, want %v" , es . Buffer .Bytes (), tt .want )
1152
+ if ! reflect .DeepEqual (buffer .Bytes (), tt .want ) {
1153
+ t .Errorf ("encodeState.encodeFixedWidthInt() = %v, want %v" , buffer .Bytes (), tt .want )
1052
1154
}
1053
1155
})
1054
1156
}
0 commit comments