@@ -82,3 +82,44 @@ func TestPrettyPrintToAmount(t *testing.T) {
8282 PrettyPrintToAmount (ctx , "" , & buf , & to , amt )
8383 require .Equal ("To: test:dave (oasis1qrk58a6j2qn065m6p06jgjyt032f7qucy5wqeqpt)\n Amount: <error: ParaTime information not available>\n " , buf .String ())
8484}
85+
86+ func TestFormatNamedAddressWith (t * testing.T ) {
87+ require := require .New (t )
88+
89+ ethHex := "0x60a6321ea71d37102dbf923aae2e08d005c4e403"
90+ ethBytes , err := hex .DecodeString (ethHex [2 :])
91+ require .NoError (err )
92+
93+ addr := NewAddressFromEth (ethBytes )
94+ native := addr .String ()
95+
96+ t .Run ("unknown returns native" , func (_ * testing.T ) {
97+ require .Equal (native , FormatNamedAddressWith (nil , nil , addr ))
98+ require .Equal (native , FormatNamedAddressWith (AccountNames {}, map [string ]string {}, addr ))
99+ })
100+
101+ t .Run ("native fallback when eth unknown" , func (_ * testing.T ) {
102+ names := AccountNames {native : "my" }
103+ require .Equal ("my (" + native + ")" , FormatNamedAddressWith (names , nil , addr ))
104+ require .Equal ("my (" + native + ")" , FormatNamedAddressWith (names , map [string ]string {}, addr ))
105+ require .Equal ("my (" + native + ")" , FormatNamedAddressWith (names , map [string ]string {native : "" }, addr ))
106+ })
107+
108+ t .Run ("eth preferred when known" , func (_ * testing.T ) {
109+ names := AccountNames {native : "my" }
110+ ethMap := map [string ]string {native : ethHex }
111+ require .Equal ("my (" + ethHex + ")" , FormatNamedAddressWith (names , ethMap , addr ))
112+ })
113+
114+ t .Run ("name equals preferred yields preferred" , func (_ * testing.T ) {
115+ names := AccountNames {native : native }
116+ require .Equal (native , FormatNamedAddressWith (names , nil , addr ))
117+ })
118+
119+ t .Run ("ctx wrapper reads maps" , func (_ * testing.T ) {
120+ ctx := context .Background ()
121+ ctx = context .WithValue (ctx , ContextKeyAccountNames , AccountNames {native : "my" })
122+ ctx = context .WithValue (ctx , ContextKeyAccountEthMap , map [string ]string {native : ethHex })
123+ require .Equal ("my (" + ethHex + ")" , FormatNamedAddress (ctx , addr ))
124+ })
125+ }
0 commit comments