naming_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package genopenapi
  2. import "testing"
  3. func TestNaming(t *testing.T) {
  4. type expectedNames struct {
  5. fqn, legacy, simple, pkg string
  6. }
  7. messageNameToExpected := map[string]expectedNames{
  8. ".A": {"A", "A", "A", "A"},
  9. ".a.B.C": {"a.B.C", "aBC", "B.C", "B.C"},
  10. ".a.D.C": {"a.D.C", "aDC", "D.C", "D.C"},
  11. ".a.E.F": {"a.E.F", "aEF", "a.E.F", "a.E.F"},
  12. ".b.E.F": {"b.E.F", "bEF", "b.E.F", "b.E.F"},
  13. ".c.G.H": {"c.G.H", "GH", "H", "G.H"},
  14. }
  15. allMessageNames := make([]string, 0, len(messageNameToExpected))
  16. for msgName := range messageNameToExpected {
  17. allMessageNames = append(allMessageNames, msgName)
  18. }
  19. t.Run("fqn", func(t *testing.T) {
  20. uniqueNames := resolveNamesFQN(allMessageNames)
  21. for _, msgName := range allMessageNames {
  22. expected := messageNameToExpected[msgName].fqn
  23. actual := uniqueNames[msgName]
  24. if expected != actual {
  25. t.Errorf("fqn unique name %q does not match expected name %q", actual, expected)
  26. }
  27. }
  28. })
  29. t.Run("legacy", func(t *testing.T) {
  30. uniqueNames := resolveNamesLegacy(allMessageNames)
  31. for _, msgName := range allMessageNames {
  32. expected := messageNameToExpected[msgName].legacy
  33. actual := uniqueNames[msgName]
  34. if expected != actual {
  35. t.Errorf("legacy unique name %q does not match expected name %q", actual, expected)
  36. }
  37. }
  38. })
  39. t.Run("simple", func(t *testing.T) {
  40. uniqueNames := resolveNamesSimple(allMessageNames)
  41. for _, msgName := range allMessageNames {
  42. expected := messageNameToExpected[msgName].simple
  43. actual := uniqueNames[msgName]
  44. if expected != actual {
  45. t.Errorf("simple unique name %q does not match expected name %q", actual, expected)
  46. }
  47. }
  48. })
  49. t.Run("package", func(t *testing.T) {
  50. uniqueNames := resolveNamesPackage(allMessageNames)
  51. for _, msgName := range allMessageNames {
  52. expected := messageNameToExpected[msgName].pkg
  53. actual := uniqueNames[msgName]
  54. if expected != actual {
  55. t.Errorf("package unique name %q does not match expected name %q", actual, expected)
  56. }
  57. }
  58. })
  59. }