github.com/haraldrudell/parl@v0.4.176/preflect/function-name_test.go (about) 1 /* 2 © 2024–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package preflect 7 8 import ( 9 "errors" 10 "os" 11 "strings" 12 "testing" 13 14 "github.com/haraldrudell/parl" 15 "github.com/haraldrudell/parl/perrors" 16 "github.com/haraldrudell/parl/pruntime" 17 ) 18 19 // funcName is a top-level func fixture 20 func funcName() {} 21 22 func TestFunctionName(t *testing.T) { 23 //t.Error("logging on") 24 var expFuncName = "funcName" 25 var expAnonymous = "TestFunctionName.func" 26 var expMethod = "Close-fm" 27 28 var codeLocation *pruntime.CodeLocation 29 var err error 30 var anonymous = func() {} 31 32 // top-level function should work 33 codeLocation, err = FuncName(funcName) 34 // FuncName(funcName top-level func) → cL: File: "/opt/sw/parl/preflect/function-name_test.go" Line: 18 FuncName: "github.com/haraldrudell/parl/preflect.funcName" err: “OK” 35 t.Logf("FuncName(funcName top-level func) → cL: %s err: “%s”", 36 codeLocation.Dump(), perrors.Short(err), 37 ) 38 if err != nil { 39 t.Errorf("FAIL FuncName top-level function err: %s", perrors.Short(err)) 40 } 41 if codeLocation.FuncIdentifier() != expFuncName { 42 t.Errorf("FAIL FuncName top-level function %q exp %q", codeLocation.FuncIdentifier(), expFuncName) 43 } 44 45 // anonymous function should work 46 codeLocation, err = FuncName(anonymous) 47 // FuncName(anonymous func) → name: File: "/opt/sw/parl/preflect/function-name_test.go" Line: 25 FuncName: "github.com/haraldrudell/parl/preflect.TestFunctionName.func1" err: “OK” 48 t.Logf("FuncName(anonymous func) → name: %s err: “%s”", 49 codeLocation.Dump(), perrors.Short(err), 50 ) 51 if err != nil { 52 t.Errorf("FAIL FuncName anonymous function err: %s", perrors.Short(err)) 53 } 54 if !strings.HasPrefix(codeLocation.FuncIdentifier(), expAnonymous) { 55 t.Errorf("FAIL FuncName anonymous prefix bad: %q exp %q", codeLocation.FuncIdentifier(), expAnonymous) 56 } 57 58 // method should work 59 codeLocation, err = FuncName(os.Stdout.Close) 60 // FuncName(t.Run method) → name: File: "<autogenerated>" Line: 1 FuncName: "testing.(*T).Run-fm" err: “OK” 61 t.Logf("FuncName(t.Run method) → name: %s err: “%s”", 62 codeLocation.Dump(), perrors.Short(err), 63 ) 64 if err != nil { 65 t.Errorf("FAIL FuncName method err: %s", perrors.Short(err)) 66 } 67 if codeLocation.FuncIdentifier() != expMethod { 68 t.Errorf("FAIL FuncName method %q exp %q", codeLocation.FuncIdentifier(), expMethod) 69 } 70 71 // funcOrMethod nil should return error 72 codeLocation, err = FuncName(nil) 73 // FuncName(nil) → name: "" err: 74 // “preflect.FuncName funcOrMethod cannot be nil at preflect.FuncName()-function-name.go:20” 75 t.Logf("FuncName(nil) → name: %q err: “%s”", 76 codeLocation, perrors.Short(err), 77 ) 78 if !errors.Is(err, parl.ErrNil) { 79 t.Errorf("FAIL FuncName nil did not return error: actual %q err: %s", 80 codeLocation, perrors.Short(err), 81 ) 82 } 83 84 // funcOrMethod not function should return error 85 codeLocation, err = FuncName(0) 86 // FuncName(0) → name: "" err: 87 // “preflect.FuncName funcOrMethod not func: int at preflect.FuncName()-function-name.go:26” 88 t.Logf("FuncName(0) → name: %q err: “%s”", 89 codeLocation, perrors.Short(err), 90 ) 91 if err == nil { 92 t.Errorf("FAIL FuncName(0) did not return error") 93 } 94 }