Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kanishkatn committed May 25, 2023
1 parent f5a0dc1 commit ac94da6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 29 deletions.
58 changes: 35 additions & 23 deletions pkg/scale/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,18 @@ func Test_decodeState_decodeStruct(t *testing.T) {
if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr {
t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr)
}
var diff string
if tt.out != nil {
diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in))
} else {
diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(big.Int{}, tt.in, VDTValue2{}, MyStructWithIgnore{}))
}
if diff != "" {
t.Errorf("decodeState.unmarshal() = %s", diff)

// assert response only if we aren't expecting an error
if !tt.wantErr {
var diff string
if tt.out != nil {
diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in))
} else {
diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(big.Int{}, tt.in, VDTValue2{}, MyStructWithIgnore{}))
}
if diff != "" {
t.Errorf("decodeState.unmarshal() = %s", diff)
}
}
})
}
Expand Down Expand Up @@ -294,20 +298,24 @@ func Test_unmarshal_optionality(t *testing.T) {
t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr)
return
}
var diff string
if tt.out != nil {
diff = cmp.Diff(
reflect.ValueOf(dst).Elem().Interface(),
reflect.ValueOf(tt.out).Interface(),
cmpopts.IgnoreUnexported(tt.in))
} else {
diff = cmp.Diff(
reflect.ValueOf(dst).Elem().Interface(),
reflect.ValueOf(tt.in).Interface(),
cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{}))
}
if diff != "" {
t.Errorf("decodeState.unmarshal() = %s", diff)

// assert response only if we aren't expecting an error
if !tt.wantErr {
var diff string
if tt.out != nil {
diff = cmp.Diff(
reflect.ValueOf(dst).Elem().Interface(),
reflect.ValueOf(tt.out).Interface(),
cmpopts.IgnoreUnexported(tt.in))
} else {
diff = cmp.Diff(
reflect.ValueOf(dst).Elem().Interface(),
reflect.ValueOf(tt.in).Interface(),
cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{}))
}
if diff != "" {
t.Errorf("decodeState.unmarshal() = %s", diff)
}
}
}
})
Expand All @@ -325,7 +333,11 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) {
// ignore out, since we are testing nil case
// out: t.out,
}
ptrTest.want = []byte{0x00}

// for error cases, we don't need to modify the input since we need it to fail
if !t.wantErr {
ptrTest.want = []byte{0x00}
}

temp := reflect.New(reflect.TypeOf(t.in))
// create a new pointer to type of temp
Expand Down
33 changes: 27 additions & 6 deletions pkg/scale/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,15 @@ var (
},
want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01},
},
{
name: "struct_{[]byte,_int32}_with_invalid_tag",
in: &struct {
Foo []byte `scale:"1,invalid"`
}{
Foo: []byte{0x01},
},
wantErr: true,
},
{
name: "struct_{[]byte,_int32,_bool}",
in: struct {
Expand Down Expand Up @@ -1073,8 +1082,12 @@ func Test_encodeState_encodeStruct(t *testing.T) {
if err := es.marshal(tt.in); (err != nil) != tt.wantErr {
t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(buffer.Bytes(), tt.want) {
t.Errorf("encodeState.encodeStruct() = %v, want %v", buffer.Bytes(), tt.want)

// we don't need this check for error cases
if !tt.wantErr {
if !reflect.DeepEqual(buffer.Bytes(), tt.want) {
t.Errorf("encodeState.encodeStruct() = %v, want %v", buffer.Bytes(), tt.want)
}
}
})
}
Expand Down Expand Up @@ -1182,8 +1195,12 @@ func Test_marshal_optionality(t *testing.T) {
if err := es.marshal(tt.in); (err != nil) != tt.wantErr {
t.Errorf("encodeState.encodeFixedWidthInt() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(buffer.Bytes(), tt.want) {
t.Errorf("encodeState.encodeFixedWidthInt() = %v, want %v", buffer.Bytes(), tt.want)

// if we expect an error, we do not need to check the result
if !tt.wantErr {
if !reflect.DeepEqual(buffer.Bytes(), tt.want) {
t.Errorf("encodeState.encodeFixedWidthInt() = %v, want %v", buffer.Bytes(), tt.want)
}
}
})
}
Expand All @@ -1196,8 +1213,12 @@ func Test_marshal_optionality_nil_cases(t *testing.T) {
ptrTest := test{
name: t.name,
// in: t.in,
wantErr: t.wantErr,
want: t.want,

// these do not matter since we are testing nil cases
// and the want is being set to empty byte slice below
// marshal will not error out for nil cases since it never hits a type's marshal method
//wantErr: t.wantErr,
//want: t.want,
}
// create a new pointer to new zero value of t.in
temp := reflect.New(reflect.TypeOf(t.in))
Expand Down

0 comments on commit ac94da6

Please sign in to comment.