namer_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package namer
  2. import (
  3. "reflect"
  4. "testing"
  5. "go-common/app/tool/gengo/types"
  6. )
  7. func TestNameStrategy(t *testing.T) {
  8. u := types.Universe{}
  9. // Add some types.
  10. base := u.Type(types.Name{Package: "foo/bar", Name: "Baz"})
  11. base.Kind = types.Struct
  12. tmp := u.Type(types.Name{Package: "", Name: "[]bar.Baz"})
  13. tmp.Kind = types.Slice
  14. tmp.Elem = base
  15. tmp = u.Type(types.Name{Package: "", Name: "map[string]bar.Baz"})
  16. tmp.Kind = types.Map
  17. tmp.Key = types.String
  18. tmp.Elem = base
  19. tmp = u.Type(types.Name{Package: "foo/other", Name: "Baz"})
  20. tmp.Kind = types.Struct
  21. tmp.Members = []types.Member{{
  22. Embedded: true,
  23. Type: base,
  24. }}
  25. tmp = u.Type(types.Name{Package: "", Name: "chan Baz"})
  26. tmp.Kind = types.Chan
  27. tmp.Elem = base
  28. u.Type(types.Name{Package: "", Name: "string"})
  29. o := Orderer{NewPublicNamer(0)}
  30. order := o.OrderUniverse(u)
  31. orderedNames := make([]string, len(order))
  32. for i, t := range order {
  33. orderedNames[i] = o.Name(t)
  34. }
  35. expect := []string{"Baz", "Baz", "ChanBaz", "MapStringToBaz", "SliceBaz", "String"}
  36. if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) {
  37. t.Errorf("Wanted %#v, got %#v", e, a)
  38. }
  39. o = Orderer{NewRawNamer("my/package", nil)}
  40. order = o.OrderUniverse(u)
  41. orderedNames = make([]string, len(order))
  42. for i, t := range order {
  43. orderedNames[i] = o.Name(t)
  44. }
  45. expect = []string{"[]bar.Baz", "bar.Baz", "chan bar.Baz", "map[string]bar.Baz", "other.Baz", "string"}
  46. if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) {
  47. t.Errorf("Wanted %#v, got %#v", e, a)
  48. }
  49. o = Orderer{NewRawNamer("foo/bar", nil)}
  50. order = o.OrderUniverse(u)
  51. orderedNames = make([]string, len(order))
  52. for i, t := range order {
  53. orderedNames[i] = o.Name(t)
  54. }
  55. expect = []string{"Baz", "[]Baz", "chan Baz", "map[string]Baz", "other.Baz", "string"}
  56. if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) {
  57. t.Errorf("Wanted %#v, got %#v", e, a)
  58. }
  59. o = Orderer{NewPublicNamer(1)}
  60. order = o.OrderUniverse(u)
  61. orderedNames = make([]string, len(order))
  62. for i, t := range order {
  63. orderedNames[i] = o.Name(t)
  64. }
  65. expect = []string{"BarBaz", "ChanBarBaz", "MapStringToBarBaz", "OtherBaz", "SliceBarBaz", "String"}
  66. if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) {
  67. t.Errorf("Wanted %#v, got %#v", e, a)
  68. }
  69. }