Skip to content

Commit

Permalink
refactor: BigInt newtype has a working Set(), allowing UnmarshalJSON(…
Browse files Browse the repository at this point in the history
…) to work properly
  • Loading branch information
randomshinichi committed Jun 4, 2019
1 parent 54fce99 commit 6ef4866
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
9 changes: 6 additions & 3 deletions integration_test/spend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integrationtest

import (
"fmt"
"math/big"
"testing"

"github.com/aeternity/aepp-sdk-go/aeternity"
Expand All @@ -22,9 +23,10 @@ func TestSpendTx(t *testing.T) {
expected := new(big.Int)
bobState, err := node.APIGetAccount(bob.Address)
if err != nil {
expected.Set(amount.Int)
expected.Set(amount)
} else {
expected.Add(bobState.Balance.Int, amount.Int)
bS := big.Int(bobState.Balance)
expected.Add(&bS, amount)
}

ttl, nonce, err := node.GetTTLNonce(sender, aeternity.Config.Client.TTL)
Expand All @@ -50,8 +52,9 @@ func TestSpendTx(t *testing.T) {
}
}
delay(getBobsAccount)
b := big.Int(bobState.Balance)

if bobState.Balance.Cmp(expected.Int) != 0 {
if expected.Cmp(&b) != 0 {
t.Fatalf("Bob should have %v, but has %v instead", expected.String(), bobState.Balance.String())
}
}
24 changes: 22 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func AskPassword(question string) (password string, err error) {
}

// BigInt is an alias for math/big.Int, for use with Swagger generated code.
// Even though it has some corresponding methods, convert it as soon as possible into big.Int.
type BigInt big.Int

// String casts BigInt into big.Int and uses its String method.
Expand Down Expand Up @@ -178,8 +179,27 @@ func (b *BigInt) LargerOrEqualToZero() bool {

// UnmarshalJSON casts BigInt into big.Int and uses its UnmarshalJSON method.
func (b *BigInt) UnmarshalJSON(text []byte) error {
bc := big.Int(*b)
return bc.UnmarshalJSON(text)
bc := new(big.Int)
err := bc.UnmarshalJSON(text)
if err != nil {
return err
}
b.Set(bc)
return nil
}

// Set makes a BigInt equal to a given big.Int.
func (b *BigInt) Set(i *big.Int) *BigInt {
iB := BigInt(*i)
*b = iB
return b
}

// Cmp compares two BigInts just like big.Int
func (b *BigInt) Cmp(i *BigInt) int {
b2 := big.Int(*b)
i2 := big.Int(*i)
return b2.Cmp(&i2)
}

// NewBigIntFromString returns a new math/big.Int from a string representation
Expand Down
28 changes: 28 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,31 @@ func TestBigIntLargerThanZero(t *testing.T) {
}
}
}

func TestBigInt_UnmarshalJSON(t *testing.T) {
type args struct {
text []byte
}
tests := []struct {
name string
b BigInt
args args
wantErr bool
}{{
name: "Deserialize this",
args: args{[]byte("1600000000000000000129127208515966861305")},
b: BigInt(*RequireBigIntFromString("1600000000000000000129127208515966861305")),
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &BigInt{}
if err := b.UnmarshalJSON(tt.args.text); (err != nil) != tt.wantErr {
t.Errorf("BigInt.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
if b.Cmp(&tt.b) != 0 {
t.Errorf("The values are not the same: b: %s, want: %s", b.String(), tt.b.String())
}
})
}
}

0 comments on commit 6ef4866

Please sign in to comment.