@@ -2,6 +2,8 @@ package display_test
22
33import (
44 "bytes"
5+ "errors"
6+ "io"
57 "regexp"
68 "strings"
79 "testing"
@@ -64,6 +66,73 @@ func TestDetailsBuilder(t *testing.T) {
6466 })
6567}
6668
69+ func TestRenderMutation (t * testing.T ) {
70+ t .Run ("renders json output when requested" , func (t * testing.T ) {
71+ var out bytes.Buffer
72+ var status bytes.Buffer
73+
74+ err := display .RenderMutation (& out , & status , true , display.MutationResult {
75+ JSONValue : map [string ]string {"status" : "ok" },
76+ SuccessMessage : "Created" ,
77+ Details : []attribute.KeyValue {
78+ attribute .Attribute ("Status" , attribute .Styled ("ok" )),
79+ },
80+ })
81+
82+ require .NoError (t , err )
83+ assert .Equal (t , normalizeOutput ("{\n \" status\" : \" ok\" \n }\n " ), normalizeOutput (out .String ()))
84+ assert .Empty (t , status .String ())
85+ })
86+
87+ t .Run ("renders success message and details in human mode" , func (t * testing.T ) {
88+ var out bytes.Buffer
89+ var status bytes.Buffer
90+
91+ err := display .RenderMutation (& out , & status , false , display.MutationResult {
92+ SuccessMessage : "Created" ,
93+ Details : []attribute.KeyValue {
94+ attribute .Attribute ("Status" , attribute .Styled ("ok" )),
95+ },
96+ })
97+
98+ require .NoError (t , err )
99+ assert .Contains (t , normalizeOutput (status .String ()), "Created" )
100+ assert .Equal (t , normalizeOutput ("Status: ok\n " ), normalizeOutput (out .String ()))
101+ })
102+
103+ t .Run ("prefers custom human renderer when provided" , func (t * testing.T ) {
104+ var out bytes.Buffer
105+ var status bytes.Buffer
106+
107+ err := display .RenderMutation (& out , & status , false , display.MutationResult {
108+ SuccessMessage : "Updated" ,
109+ Details : []attribute.KeyValue {
110+ attribute .Attribute ("Status" , attribute .Styled ("stale" )),
111+ },
112+ RenderHuman : func (w io.Writer ) error {
113+ _ , writeErr := w .Write ([]byte ("custom\n " ))
114+ return writeErr
115+ },
116+ })
117+
118+ require .NoError (t , err )
119+ assert .Contains (t , normalizeOutput (status .String ()), "Updated" )
120+ assert .Equal (t , "custom\n " , out .String ())
121+ })
122+
123+ t .Run ("returns status writer errors" , func (t * testing.T ) {
124+ out := & bytes.Buffer {}
125+ status := failWriter {}
126+
127+ err := display .RenderMutation (out , status , false , display.MutationResult {
128+ SuccessMessage : "Created" ,
129+ })
130+
131+ require .Error (t , err )
132+ assert .ErrorIs (t , err , errWriteFailed )
133+ })
134+ }
135+
67136func TestRenderTable (t * testing.T ) {
68137 t .Run ("renders title and row content" , func (t * testing.T ) {
69138 var out bytes.Buffer
@@ -108,6 +177,14 @@ func TestRenderTableWithOptions(t *testing.T) {
108177
109178var ansiPattern = regexp .MustCompile (`\x1b\[[0-9;]*m` )
110179
180+ var errWriteFailed = errors .New ("write failed" )
181+
182+ type failWriter struct {}
183+
184+ func (failWriter ) Write (_ []byte ) (int , error ) {
185+ return 0 , errWriteFailed
186+ }
187+
111188func normalizeOutput (value string ) string {
112189 value = ansiPattern .ReplaceAllString (value , "" )
113190 lines := strings .Split (strings .ReplaceAll (value , "\r \n " , "\n " ), "\n " )
0 commit comments