github.com/secman-team/gh-api@v1.8.2/pkg/cmd/api/template_test.go (about) 1 package api 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/MakeNowJust/heredoc" 12 ) 13 14 func Test_jsonScalarToString(t *testing.T) { 15 tests := []struct { 16 name string 17 input interface{} 18 want string 19 wantErr bool 20 }{ 21 { 22 name: "string", 23 input: "hello", 24 want: "hello", 25 }, 26 { 27 name: "int", 28 input: float64(1234), 29 want: "1234", 30 }, 31 { 32 name: "float", 33 input: float64(12.34), 34 want: "12.34", 35 }, 36 { 37 name: "null", 38 input: nil, 39 want: "", 40 }, 41 { 42 name: "true", 43 input: true, 44 want: "true", 45 }, 46 { 47 name: "false", 48 input: false, 49 want: "false", 50 }, 51 { 52 name: "object", 53 input: map[string]interface{}{}, 54 wantErr: true, 55 }, 56 } 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 got, err := jsonScalarToString(tt.input) 60 if (err != nil) != tt.wantErr { 61 t.Errorf("jsonScalarToString() error = %v, wantErr %v", err, tt.wantErr) 62 return 63 } 64 if got != tt.want { 65 t.Errorf("jsonScalarToString() = %v, want %v", got, tt.want) 66 } 67 }) 68 } 69 } 70 71 func Test_executeTemplate(t *testing.T) { 72 type args struct { 73 json io.Reader 74 template string 75 colorize bool 76 } 77 tests := []struct { 78 name string 79 args args 80 wantW string 81 wantErr bool 82 }{ 83 { 84 name: "color", 85 args: args{ 86 json: strings.NewReader(`{}`), 87 template: `{{color "blue+h" "songs are like tattoos"}}`, 88 colorize: false, 89 }, 90 wantW: "\x1b[0;94msongs are like tattoos\x1b[0m", 91 }, 92 { 93 name: "autocolor enabled", 94 args: args{ 95 json: strings.NewReader(`{}`), 96 template: `{{autocolor "red" "stop"}}`, 97 colorize: true, 98 }, 99 wantW: "\x1b[0;31mstop\x1b[0m", 100 }, 101 { 102 name: "autocolor disabled", 103 args: args{ 104 json: strings.NewReader(`{}`), 105 template: `{{autocolor "red" "go"}}`, 106 colorize: false, 107 }, 108 wantW: "go", 109 }, 110 { 111 name: "timefmt", 112 args: args{ 113 json: strings.NewReader(`{"created_at":"2008-02-25T20:18:33Z"}`), 114 template: `{{.created_at | timefmt "Mon Jan 2, 2006"}}`, 115 colorize: false, 116 }, 117 wantW: "Mon Feb 25, 2008", 118 }, 119 { 120 name: "timeago", 121 args: args{ 122 json: strings.NewReader(fmt.Sprintf(`{"created_at":"%s"}`, time.Now().Add(-5*time.Minute).Format(time.RFC3339))), 123 template: `{{.created_at | timeago}}`, 124 colorize: false, 125 }, 126 wantW: "5 minutes ago", 127 }, 128 { 129 name: "pluck", 130 args: args{ 131 json: strings.NewReader(heredoc.Doc(`[ 132 {"name": "bug"}, 133 {"name": "feature request"}, 134 {"name": "chore"} 135 ]`)), 136 template: `{{range(pluck "name" .)}}{{. | printf "%s\n"}}{{end}}`, 137 colorize: false, 138 }, 139 wantW: "bug\nfeature request\nchore\n", 140 }, 141 { 142 name: "join", 143 args: args{ 144 json: strings.NewReader(`[ "bug", "feature request", "chore" ]`), 145 template: `{{join "\t" .}}`, 146 colorize: false, 147 }, 148 wantW: "bug\tfeature request\tchore", 149 }, 150 } 151 for _, tt := range tests { 152 t.Run(tt.name, func(t *testing.T) { 153 w := &bytes.Buffer{} 154 if err := executeTemplate(w, tt.args.json, tt.args.template, tt.args.colorize); (err != nil) != tt.wantErr { 155 t.Errorf("executeTemplate() error = %v, wantErr %v", err, tt.wantErr) 156 return 157 } 158 if gotW := w.String(); gotW != tt.wantW { 159 t.Errorf("executeTemplate() = %q, want %q", gotW, tt.wantW) 160 } 161 }) 162 } 163 }