github.com/tomwright/dasel@v1.27.3/error_test.go (about) 1 package dasel_test 2 3 import ( 4 "errors" 5 "fmt" 6 "reflect" 7 "testing" 8 9 "github.com/tomwright/dasel" 10 ) 11 12 func TestErrorMessages(t *testing.T) { 13 tests := []struct { 14 In error 15 Out string 16 }{ 17 {In: dasel.ErrMissingPreviousNode, Out: "missing previous node"}, 18 {In: &dasel.UnknownComparisonOperatorErr{Operator: "<"}, Out: "unknown comparison operator: <"}, 19 {In: &dasel.InvalidIndexErr{Index: "1"}, Out: "invalid index: 1"}, 20 {In: &dasel.UnsupportedSelector{Selector: "..."}, Out: "selector is not supported here: ..."}, 21 {In: &dasel.UnsupportedTypeForSelector{ 22 Value: reflect.ValueOf(map[string]interface{}{}), 23 Selector: dasel.Selector{ 24 Raw: ".a.b.c", 25 Current: ".a", 26 Remaining: ".b.c", 27 Type: "INDEX", 28 Index: 1, 29 }, 30 }, Out: "selector [type:INDEX selector:.a.b.c] does not support value: [kind:map type:map[string]interface {}] map[]"}, 31 {In: &dasel.ValueNotFound{ 32 Selector: ".name", 33 }, Out: "no value found for selector: .name: <invalid reflect.Value>"}, 34 {In: &dasel.ValueNotFound{ 35 Selector: ".name", 36 PreviousValue: reflect.ValueOf(map[string]interface{}{}), 37 }, Out: "no value found for selector: .name: map[]"}, 38 {In: &dasel.UnexpectedPreviousNilValue{Selector: ".name"}, Out: "previous value is nil: .name"}, 39 {In: &dasel.UnhandledCheckType{Value: ""}, Out: "unhandled check type: string"}, 40 } 41 42 for _, testCase := range tests { 43 tc := testCase 44 t.Run("ErrorMessage", func(t *testing.T) { 45 if exp, got := tc.Out, tc.In.Error(); exp != got { 46 t.Errorf("expected %s, got %s", exp, got) 47 } 48 }) 49 } 50 } 51 52 func TestErrorsIs(t *testing.T) { 53 type args struct { 54 Err error 55 Target error 56 } 57 58 tests := []struct { 59 In args 60 Out bool 61 }{ 62 { 63 In: args{ 64 Err: &dasel.UnknownComparisonOperatorErr{}, 65 Target: &dasel.UnknownComparisonOperatorErr{}, 66 }, 67 Out: true, 68 }, 69 { 70 In: args{ 71 Err: fmt.Errorf("some error: %w", &dasel.UnknownComparisonOperatorErr{}), 72 Target: &dasel.UnknownComparisonOperatorErr{}, 73 }, 74 Out: true, 75 }, 76 { 77 In: args{ 78 Err: errors.New("some error"), 79 Target: &dasel.UnknownComparisonOperatorErr{}, 80 }, 81 Out: false, 82 }, 83 { 84 In: args{ 85 Err: &dasel.InvalidIndexErr{}, 86 Target: &dasel.InvalidIndexErr{}, 87 }, 88 Out: true, 89 }, 90 { 91 In: args{ 92 Err: fmt.Errorf("some error: %w", &dasel.InvalidIndexErr{}), 93 Target: &dasel.InvalidIndexErr{}, 94 }, 95 Out: true, 96 }, 97 { 98 In: args{ 99 Err: errors.New("some error"), 100 Target: &dasel.InvalidIndexErr{}, 101 }, 102 Out: false, 103 }, 104 { 105 In: args{ 106 Err: &dasel.UnsupportedSelector{}, 107 Target: &dasel.UnsupportedSelector{}, 108 }, 109 Out: true, 110 }, 111 { 112 In: args{ 113 Err: fmt.Errorf("some error: %w", &dasel.UnsupportedSelector{}), 114 Target: &dasel.UnsupportedSelector{}, 115 }, 116 Out: true, 117 }, 118 { 119 In: args{ 120 Err: errors.New("some error"), 121 Target: &dasel.UnsupportedSelector{}, 122 }, 123 Out: false, 124 }, 125 { 126 In: args{ 127 Err: &dasel.UnsupportedTypeForSelector{}, 128 Target: &dasel.UnsupportedTypeForSelector{}, 129 }, 130 Out: true, 131 }, 132 { 133 In: args{ 134 Err: fmt.Errorf("some error: %w", &dasel.UnsupportedTypeForSelector{}), 135 Target: &dasel.UnsupportedTypeForSelector{}, 136 }, 137 Out: true, 138 }, 139 { 140 In: args{ 141 Err: errors.New("some error"), 142 Target: &dasel.UnsupportedTypeForSelector{}, 143 }, 144 Out: false, 145 }, 146 { 147 In: args{ 148 Err: &dasel.ValueNotFound{}, 149 Target: &dasel.ValueNotFound{}, 150 }, 151 Out: true, 152 }, 153 { 154 In: args{ 155 Err: fmt.Errorf("some error: %w", &dasel.ValueNotFound{}), 156 Target: &dasel.ValueNotFound{}, 157 }, 158 Out: true, 159 }, 160 { 161 In: args{ 162 Err: errors.New("some error"), 163 Target: &dasel.ValueNotFound{}, 164 }, 165 Out: false, 166 }, 167 { 168 In: args{ 169 Err: &dasel.UnexpectedPreviousNilValue{}, 170 Target: &dasel.UnexpectedPreviousNilValue{}, 171 }, 172 Out: true, 173 }, 174 { 175 In: args{ 176 Err: fmt.Errorf("some error: %w", &dasel.UnexpectedPreviousNilValue{}), 177 Target: &dasel.UnexpectedPreviousNilValue{}, 178 }, 179 Out: true, 180 }, 181 { 182 In: args{ 183 Err: errors.New("some error"), 184 Target: &dasel.UnexpectedPreviousNilValue{}, 185 }, 186 Out: false, 187 }, 188 { 189 In: args{ 190 Err: &dasel.UnhandledCheckType{}, 191 Target: &dasel.UnhandledCheckType{}, 192 }, 193 Out: true, 194 }, 195 { 196 In: args{ 197 Err: fmt.Errorf("some error: %w", &dasel.UnhandledCheckType{}), 198 Target: &dasel.UnhandledCheckType{}, 199 }, 200 Out: true, 201 }, 202 { 203 In: args{ 204 Err: errors.New("some error"), 205 Target: &dasel.UnhandledCheckType{}, 206 }, 207 Out: false, 208 }, 209 } 210 211 for _, testCase := range tests { 212 tc := testCase 213 t.Run("ErrorMessage", func(t *testing.T) { 214 if exp, got := tc.Out, errors.Is(tc.In.Err, tc.In.Target); exp != got { 215 t.Errorf("expected %v, got %v", exp, got) 216 } 217 }) 218 } 219 }