11package api
22
33import (
4+ "bytes"
45 "context"
56 "testing"
67
@@ -10,6 +11,14 @@ import (
1011 "github.com/oasisprotocol/oasis-core/go/common/quantity"
1112)
1213
14+ func mustAddress (t * testing.T , raw string ) Address {
15+ t .Helper ()
16+
17+ var addr Address
18+ require .NoError (t , addr .UnmarshalText ([]byte (raw )))
19+ return addr
20+ }
21+
1322func TestPrettyPrintCommissionRatePercentage (t * testing.T ) {
1423 require := require .New (t )
1524
@@ -54,3 +63,119 @@ func TestPrettyPrintCommissionScheduleIndexInfixes(t *testing.T) {
5463 require .Equal (t .expectedEmptyInfix , emptyInfix , "obtained empty infix didn't match expected value" )
5564 }
5665}
66+
67+ func TestFormatAddressWith (t * testing.T ) {
68+ require := require .New (t )
69+
70+ addr := mustAddress (t , "oasis1qrydpazemvuwtnp3efm7vmfvg3tde044qg6cxwzx" )
71+ native := addr .String ()
72+
73+ for _ , tc := range []struct {
74+ name string
75+ names AccountNames
76+ expected string
77+ }{
78+ {
79+ name : "nil names" ,
80+ names : nil ,
81+ expected : native ,
82+ },
83+ {
84+ name : "empty names" ,
85+ names : AccountNames {},
86+ expected : native ,
87+ },
88+ {
89+ name : "unknown name" ,
90+ names : AccountNames {"oasis1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpkfh7w" : "ignored" },
91+ expected : native ,
92+ },
93+ {
94+ name : "named address" ,
95+ names : AccountNames {native : "test:bob" },
96+ expected : "test:bob (" + native + ")" ,
97+ },
98+ } {
99+ t .Run (tc .name , func (t * testing.T ) {
100+ require .Equal (tc .expected , FormatAddressWith (tc .names , addr ))
101+ })
102+ }
103+ }
104+
105+ func TestFormatAddress (t * testing.T ) {
106+ require := require .New (t )
107+
108+ addr := mustAddress (t , "oasis1qrydpazemvuwtnp3efm7vmfvg3tde044qg6cxwzx" )
109+ native := addr .String ()
110+
111+ t .Run ("without context names" , func (t * testing.T ) {
112+ require .Equal (native , FormatAddress (context .Background (), addr ))
113+ })
114+
115+ t .Run ("with context names" , func (t * testing.T ) {
116+ ctx := context .WithValue (context .Background (), ContextKeyAccountNames , AccountNames {
117+ native : "test:bob" ,
118+ })
119+ require .Equal ("test:bob (" + native + ")" , FormatAddress (ctx , addr ))
120+ })
121+ }
122+
123+ func TestStakingTxPrettyPrintUsesNamedAddresses (t * testing.T ) {
124+ require := require .New (t )
125+
126+ addr := mustAddress (t , "oasis1qrydpazemvuwtnp3efm7vmfvg3tde044qg6cxwzx" )
127+ native := addr .String ()
128+ amt := * quantity .NewFromUint64 (1 )
129+
130+ ctx := context .WithValue (context .Background (), ContextKeyAccountNames , AccountNames {
131+ native : "test:bob" ,
132+ })
133+
134+ for _ , tc := range []struct {
135+ name string
136+ pretty func (context.Context , * bytes.Buffer )
137+ expected string
138+ }{
139+ {
140+ name : "transfer to" ,
141+ pretty : func (ctx context.Context , buf * bytes.Buffer ) {
142+ Transfer {To : addr , Amount : amt }.PrettyPrint (ctx , "" , buf )
143+ },
144+ expected : "To: test:bob (" + native + ")" ,
145+ },
146+ {
147+ name : "escrow account" ,
148+ pretty : func (ctx context.Context , buf * bytes.Buffer ) {
149+ Escrow {Account : addr , Amount : amt }.PrettyPrint (ctx , "" , buf )
150+ },
151+ expected : "To: test:bob (" + native + ")" ,
152+ },
153+ {
154+ name : "reclaim escrow from" ,
155+ pretty : func (ctx context.Context , buf * bytes.Buffer ) {
156+ ReclaimEscrow {Account : addr , Shares : amt }.PrettyPrint (ctx , "" , buf )
157+ },
158+ expected : "From: test:bob (" + native + ")" ,
159+ },
160+ {
161+ name : "allow beneficiary" ,
162+ pretty : func (ctx context.Context , buf * bytes.Buffer ) {
163+ Allow {Beneficiary : addr , AmountChange : amt }.PrettyPrint (ctx , "" , buf )
164+ },
165+ expected : "Beneficiary: test:bob (" + native + ")" ,
166+ },
167+ {
168+ name : "withdraw from" ,
169+ pretty : func (ctx context.Context , buf * bytes.Buffer ) {
170+ Withdraw {From : addr , Amount : amt }.PrettyPrint (ctx , "" , buf )
171+ },
172+ expected : "From: test:bob (" + native + ")" ,
173+ },
174+ } {
175+ t .Run (tc .name , func (t * testing.T ) {
176+ var buf bytes.Buffer
177+ tc .pretty (ctx , & buf )
178+ require .Contains (buf .String (), tc .expected )
179+ })
180+ }
181+ }
0 commit comments