github.com/everdrone/grab@v0.1.7-0.20230416223925-40674b995521/internal/utils/error_test.go (about) 1 package utils 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/hashicorp/hcl/v2" 8 ) 9 10 func TestPlural(t *testing.T) { 11 tests := []struct { 12 Singular string 13 Plural string 14 Count int 15 Want string 16 }{ 17 { 18 Singular: "foo", 19 Plural: "bar", 20 Count: 0, 21 Want: "bar", 22 }, 23 { 24 Singular: "foo", 25 Plural: "bar", 26 Count: 1, 27 Want: "foo", 28 }, 29 { 30 Singular: "foo", 31 Plural: "bar", 32 Count: 3, 33 Want: "bar", 34 }, 35 } 36 37 for _, tt := range tests { 38 if got := Plural(tt.Count, tt.Singular, tt.Plural); got != tt.Want { 39 t.Errorf("got: %q, want %q", got, tt.Want) 40 } 41 } 42 } 43 44 func TestPrintDiag(t *testing.T) { 45 tests := []struct { 46 Name string 47 Diag *hcl.Diagnostic 48 Want string 49 }{ 50 { 51 Name: "no subject", 52 Diag: &hcl.Diagnostic{ 53 Severity: DiagError, 54 Summary: "foo", 55 Detail: "bar", 56 }, 57 Want: `╷ Error: foo 58 ╵ bar 59 `, 60 }, 61 { 62 Name: "error with subject", 63 Diag: &hcl.Diagnostic{ 64 Severity: DiagError, 65 Summary: "foo", 66 Detail: "bar", 67 Subject: &hcl.Range{Filename: "foo.hcl", Start: hcl.Pos{Line: 1, Column: 1}, End: hcl.Pos{Line: 1, Column: 2}}, 68 }, 69 Want: `╷ Error: foo 70 │ bar 71 ╵ foo.hcl:1,1-2 72 `, 73 }, 74 { 75 Name: "warning", 76 Diag: &hcl.Diagnostic{ 77 Severity: DiagWarning, 78 Summary: "foo", 79 Detail: "bar", 80 }, 81 Want: `╷ Warning: foo 82 ╵ bar 83 `, 84 }, 85 { 86 Name: "invalid", 87 Diag: &hcl.Diagnostic{ 88 Severity: DiagInvalid, 89 Summary: "foo", 90 Detail: "bar", 91 }, 92 Want: `╷ Invalid: foo 93 ╵ bar 94 `, 95 }, 96 { 97 Name: "info", 98 Diag: &hcl.Diagnostic{ 99 Severity: DiagInfo, 100 Summary: "foo", 101 Detail: "bar", 102 }, 103 Want: `╷ Info: foo 104 ╵ bar 105 `, 106 }, 107 { 108 Name: "debug", 109 Diag: &hcl.Diagnostic{ 110 Severity: DiagDebug, 111 Summary: "foo", 112 Detail: "bar", 113 }, 114 Want: `╷ Debug: foo 115 ╵ bar 116 `, 117 }, 118 } 119 120 for _, tt := range tests { 121 t.Run(tt.Name, func(t *testing.T) { 122 var buf bytes.Buffer 123 PrintDiag(&buf, tt.Diag) 124 if got := buf.String(); got != tt.Want { 125 t.Errorf("got: %s, want %s", got, tt.Want) 126 } 127 }) 128 } 129 }