github.com/haraldrudell/parl@v0.4.176/perrors/errorglue/error-data_test.go (about) 1 /* 2 © 2018–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package errorglue 7 8 import ( 9 "errors" 10 "reflect" 11 "testing" 12 ) 13 14 func TestErrorData(t *testing.T) { 15 //t.Errorf("Logging on") 16 var key, value = "key", "value" 17 var message = "error-message" 18 var eData *errorData 19 var typeName = reflect.TypeOf(eData).String() 20 var m = map[CSFormat]string{ 21 DefaultFormat: message, 22 CodeLocation: "", 23 ShortFormat: message, 24 LongFormat: message + " [" + typeName + "]\n" + key + ": " + value, 25 ShortSuffix: "", 26 LongSuffix: key + ": " + value, 27 } 28 29 var formatAct, formatExp, keyAct, valueAct string 30 var ok bool 31 var err error 32 var err0 = errors.New(message) 33 var hasData ErrorHasData 34 35 // ChainString() KeyValue() 36 var _ *errorData 37 38 err = NewErrorData(err0, key, value) 39 40 // err should be ErrorHasData 41 hasData, ok = err.(ErrorHasData) 42 if !ok { 43 t.Fatalf("NewErrorData not ErrorHasData") 44 } 45 46 // err should be eData 47 eData, ok = err.(*errorData) 48 if !ok { 49 t.Fatalf("NewErrorData not ErrorHasData") 50 } 51 52 // KeyValue should match 53 keyAct, valueAct = hasData.KeyValue() 54 if keyAct != key { 55 t.Errorf("key %q exp %q", keyAct, key) 56 } 57 if valueAct != value { 58 t.Errorf("value %q exp %q", valueAct, value) 59 } 60 61 for _, csFormat := range csFormatList { 62 if formatExp, ok = m[csFormat]; !ok { 63 t.Errorf("no expected value for format: %s", csFormat) 64 } 65 formatAct = eData.ChainString(csFormat) 66 67 // DefaultFormat: "error-message" 68 // CodeLocation: "" 69 // ShortFormat: "error-message" 70 // LongFormat: "error-message [*errorglue.errorData]\nkey: value" 71 // ShortSuffix: "" 72 // LongSuffix: "key: value" 73 t.Logf("%s: %q", csFormat, formatAct) 74 75 // ChainString should match 76 if formatAct != formatExp { 77 t.Errorf("FAIL %s:\n%q exp\n%q", 78 csFormat, formatAct, formatExp, 79 ) 80 } 81 } 82 }