Skip to content

Commit

Permalink
fix: terminal pretty printer also prints BigInt's value/amount
Browse files Browse the repository at this point in the history
hide BigInt and assume it is a "Balance" (possible liability)
  • Loading branch information
randomshinichi committed May 27, 2019
1 parent 309363d commit 3f239a1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
18 changes: 12 additions & 6 deletions aeternity/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/aeternity/aepp-sdk-go/generated/models"
"github.com/aeternity/aepp-sdk-go/utils"
)

func times(str string, n int) (out string) {
Expand Down Expand Up @@ -67,13 +68,18 @@ func printIf(title string, v interface{}) {
case reflect.Interface:
p(title, n, v.Elem(), dept)
case reflect.Struct:
PpT(dept, fmt.Sprintf("<%s>", v.Type().Name()))
dept++
for i := 0; i < v.NumField(); i++ {
p("", v.Type().Field(i).Name, v.Field(i), dept)
if v.Type().Name() == "BigInt" {
vc := v.Interface().(utils.BigInt)
PpI(dept, "Balance", vc.Text(10))
} else {
PpT(dept, fmt.Sprintf("<%s>", v.Type().Name()))
dept++
for i := 0; i < v.NumField(); i++ {
p("", v.Type().Field(i).Name, v.Field(i), dept)
}
dept--
PpT(dept, fmt.Sprintf("</%s>", v.Type().Name()))
}
dept--
PpT(dept, fmt.Sprintf("</%s>", v.Type().Name()))
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
p("", "", v.Index(i), dept)
Expand Down
50 changes: 50 additions & 0 deletions aeternity/terminal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package aeternity

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"

"github.com/aeternity/aepp-sdk-go/utils"
)

func Test_printIf_BigIntBalancePrinted(t *testing.T) {
type args struct {
title string
v *utils.BigInt
}
tests := []struct {
name string
args args
}{
{
name: "Test that printIf() recognizes the utils.BigInt special case",
args: args{
title: "Title",
v: utils.NewBigIntFromUint64(1377),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

printIf(tt.args.title, tt.args.v)

w.Close()
out, _ := ioutil.ReadAll(r)
outs := string(out)
os.Stdout = rescueStdout
fmt.Println("DEBUG", outs)

expected := tt.args.v.Text(10)
if !strings.Contains(outs, expected) {
t.Fatalf("The terminal pretty printer printIf did not print out the value of the BigInt, which was %s", expected)
}
})
}
}

0 comments on commit 3f239a1

Please sign in to comment.