gitea源码

funcinfo_test.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package routing
  4. import (
  5. "fmt"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func Test_shortenFilename(t *testing.T) {
  10. tests := []struct {
  11. filename string
  12. fallback string
  13. expected string
  14. }{
  15. {
  16. "code.gitea.io/routers/common/logger_context.go",
  17. "NO_FALLBACK",
  18. "common/logger_context.go",
  19. },
  20. {
  21. "common/logger_context.go",
  22. "NO_FALLBACK",
  23. "common/logger_context.go",
  24. },
  25. {
  26. "logger_context.go",
  27. "NO_FALLBACK",
  28. "logger_context.go",
  29. },
  30. {
  31. "",
  32. "USE_FALLBACK",
  33. "USE_FALLBACK",
  34. },
  35. }
  36. for _, tt := range tests {
  37. t.Run(fmt.Sprintf("shortenFilename('%s')", tt.filename), func(t *testing.T) {
  38. gotShort := shortenFilename(tt.filename, tt.fallback)
  39. assert.Equal(t, tt.expected, gotShort)
  40. })
  41. }
  42. }
  43. func Test_trimAnonymousFunctionSuffix(t *testing.T) {
  44. tests := []struct {
  45. name string
  46. want string
  47. }{
  48. {
  49. "notAnonymous",
  50. "notAnonymous",
  51. },
  52. {
  53. "anonymous.func1",
  54. "anonymous",
  55. },
  56. {
  57. "notAnonymous.funca",
  58. "notAnonymous.funca",
  59. },
  60. {
  61. "anonymous.func100",
  62. "anonymous",
  63. },
  64. {
  65. "anonymous.func100.func6",
  66. "anonymous.func100",
  67. },
  68. }
  69. for _, tt := range tests {
  70. t.Run(tt.name, func(t *testing.T) {
  71. got := trimAnonymousFunctionSuffix(tt.name)
  72. assert.Equal(t, tt.want, got)
  73. })
  74. }
  75. }