@@ -32,6 +32,36 @@ func TestWordCounterError(t *testing.T) {
3232 wantMsg : "failed to read file: /path/to/file: permission denied" ,
3333 wantType : wcg .ErrorTypeFileRead ,
3434 },
35+ {
36+ name : "File write error" ,
37+ err : wcg .NewFileWriteError ("/path/to/output" , errors .New ("disk full" )),
38+ wantMsg : "failed to write file: /path/to/output: disk full" ,
39+ wantType : wcg .ErrorTypeFileWrite ,
40+ },
41+ {
42+ name : "Invalid path error" ,
43+ err : wcg .NewInvalidPathError ("/invalid/path" , errors .New ("invalid characters" )),
44+ wantMsg : "invalid path: /invalid/path: invalid characters" ,
45+ wantType : wcg .ErrorTypeInvalidPath ,
46+ },
47+ {
48+ name : "Pattern match error" ,
49+ err : wcg .NewPatternMatchError ("*.{" , errors .New ("invalid regex" )),
50+ wantMsg : "invalid pattern: *.{: invalid regex" ,
51+ wantType : wcg .ErrorTypePatternMatch ,
52+ },
53+ {
54+ name : "Export error" ,
55+ err : wcg .NewExportError ("CSV export" , errors .New ("encoding error" )),
56+ wantMsg : "export failed: CSV export: encoding error" ,
57+ wantType : wcg .ErrorTypeExport ,
58+ },
59+ {
60+ name : "Server error" ,
61+ err : wcg .NewServerError ("failed to start server" , errors .New ("port in use" )),
62+ wantMsg : "failed to start server: port in use" ,
63+ wantType : wcg .ErrorTypeServer ,
64+ },
3565 }
3666
3767 for _ , tt := range tests {
@@ -67,3 +97,52 @@ func TestWordCounterError_Unwrap(t *testing.T) {
6797 t .Errorf ("Expected unwrapped error to be %v, got %v" , cause , unwrapped )
6898 }
6999}
100+
101+ func TestErrorConstructorsWithContext (t * testing.T ) {
102+ tests := []struct {
103+ name string
104+ err * wcg.WordCounterError
105+ wantContext map [string ]any
106+ }{
107+ {
108+ name : "File write error with path context" ,
109+ err : wcg .NewFileWriteError ("/output/file.txt" , errors .New ("disk full" )),
110+ wantContext : map [string ]any {
111+ "path" : "/output/file.txt" ,
112+ },
113+ },
114+ {
115+ name : "Invalid path error with path context" ,
116+ err : wcg .NewInvalidPathError ("/invalid/path" , errors .New ("invalid chars" )),
117+ wantContext : map [string ]any {
118+ "path" : "/invalid/path" ,
119+ },
120+ },
121+ {
122+ name : "Pattern match error with pattern context" ,
123+ err : wcg .NewPatternMatchError ("*.{" , errors .New ("invalid regex" )),
124+ wantContext : map [string ]any {
125+ "pattern" : "*.{" ,
126+ },
127+ },
128+ {
129+ name : "Export error with operation context" ,
130+ err : wcg .NewExportError ("Excel export" , errors .New ("format error" )),
131+ wantContext : map [string ]any {
132+ "operation" : "Excel export" ,
133+ },
134+ },
135+ }
136+
137+ for _ , tt := range tests {
138+ t .Run (tt .name , func (t * testing.T ) {
139+ for key , expectedValue := range tt .wantContext {
140+ if actualValue , exists := tt .err .Context [key ]; ! exists {
141+ t .Errorf ("Expected context key %s to exist" , key )
142+ } else if actualValue != expectedValue {
143+ t .Errorf ("Expected context[%s] = %v, got %v" , key , expectedValue , actualValue )
144+ }
145+ }
146+ })
147+ }
148+ }
0 commit comments