@@ -2,6 +2,8 @@ package display_test
22
33import (
44 "bytes"
5+ "errors"
6+ "io"
57 "regexp"
68 "strings"
79 "testing"
@@ -37,6 +39,73 @@ func TestDataList(t *testing.T) {
3739 })
3840}
3941
42+ func TestRenderMutation (t * testing.T ) {
43+ t .Run ("renders json output when requested" , func (t * testing.T ) {
44+ var out bytes.Buffer
45+ var status bytes.Buffer
46+
47+ err := display .RenderMutation (& out , & status , true , display.MutationResult {
48+ JSONValue : map [string ]string {"status" : "ok" },
49+ SuccessMessage : "Created" ,
50+ Details : []attribute.KeyValue {
51+ attribute .Attribute ("Status" , attribute .Styled ("ok" )),
52+ },
53+ })
54+
55+ require .NoError (t , err )
56+ assert .Equal (t , normalizeOutput ("{\n \" status\" : \" ok\" \n }\n " ), normalizeOutput (out .String ()))
57+ assert .Empty (t , status .String ())
58+ })
59+
60+ t .Run ("renders success message and details in human mode" , func (t * testing.T ) {
61+ var out bytes.Buffer
62+ var status bytes.Buffer
63+
64+ err := display .RenderMutation (& out , & status , false , display.MutationResult {
65+ SuccessMessage : "Created" ,
66+ Details : []attribute.KeyValue {
67+ attribute .Attribute ("Status" , attribute .Styled ("ok" )),
68+ },
69+ })
70+
71+ require .NoError (t , err )
72+ assert .Contains (t , normalizeOutput (status .String ()), "Created" )
73+ assert .Equal (t , normalizeOutput ("Status: ok\n " ), normalizeOutput (out .String ()))
74+ })
75+
76+ t .Run ("prefers custom human renderer when provided" , func (t * testing.T ) {
77+ var out bytes.Buffer
78+ var status bytes.Buffer
79+
80+ err := display .RenderMutation (& out , & status , false , display.MutationResult {
81+ SuccessMessage : "Updated" ,
82+ Details : []attribute.KeyValue {
83+ attribute .Attribute ("Status" , attribute .Styled ("stale" )),
84+ },
85+ RenderHuman : func (w io.Writer ) error {
86+ _ , writeErr := w .Write ([]byte ("custom\n " ))
87+ return writeErr
88+ },
89+ })
90+
91+ require .NoError (t , err )
92+ assert .Contains (t , normalizeOutput (status .String ()), "Updated" )
93+ assert .Equal (t , "custom\n " , out .String ())
94+ })
95+
96+ t .Run ("returns status writer errors" , func (t * testing.T ) {
97+ out := & bytes.Buffer {}
98+ status := failWriter {}
99+
100+ err := display .RenderMutation (out , status , false , display.MutationResult {
101+ SuccessMessage : "Created" ,
102+ })
103+
104+ require .Error (t , err )
105+ assert .ErrorIs (t , err , errWriteFailed )
106+ })
107+ }
108+
40109func TestRenderTable (t * testing.T ) {
41110 t .Run ("renders title and row content" , func (t * testing.T ) {
42111 var out bytes.Buffer
@@ -65,6 +134,14 @@ func TestRenderTableWithOptionsSupportsEmptyTextWithoutTitle(t *testing.T) {
65134
66135var ansiPattern = regexp .MustCompile (`\x1b\[[0-9;]*m` )
67136
137+ var errWriteFailed = errors .New ("write failed" )
138+
139+ type failWriter struct {}
140+
141+ func (failWriter ) Write (_ []byte ) (int , error ) {
142+ return 0 , errWriteFailed
143+ }
144+
68145func normalizeOutput (value string ) string {
69146 value = ansiPattern .ReplaceAllString (value , "" )
70147 lines := strings .Split (strings .ReplaceAll (value , "\r \n " , "\n " ), "\n " )
0 commit comments