organization_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package identity
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestJoinLabel(t *testing.T) {
  20. cases := []struct {
  21. segs []string
  22. want string
  23. }{
  24. {
  25. segs: []string{"L1", "L2", "L3"},
  26. want: "L1/L2/L3",
  27. },
  28. {
  29. segs: []string{"L1/", "L2", "L3"},
  30. want: "L1/L2/L3",
  31. },
  32. {
  33. segs: []string{"L1/ ", "/L2", "/L3/"},
  34. want: "L1/L2/L3",
  35. },
  36. {
  37. segs: []string{"L1/ ", "/L2", "/L3/", "H4/H5"},
  38. want: "L1/L2/L3/H4\\/H5",
  39. },
  40. }
  41. for _, c := range cases {
  42. got := JoinLabels(c.segs...)
  43. if got != c.want {
  44. t.Errorf("got %s want %s", got, c.want)
  45. }
  46. }
  47. }
  48. func TestSplitLabel(t *testing.T) {
  49. cases := []struct {
  50. in string
  51. want []string
  52. }{
  53. {
  54. in: "L1/L2/L3",
  55. want: []string{"L1", "L2", "L3"},
  56. },
  57. {
  58. in: "L1/L2//L3",
  59. want: []string{"L1", "L2", "L3"},
  60. },
  61. {
  62. in: "/L1/L2/L3/",
  63. want: []string{"L1", "L2", "L3"},
  64. },
  65. {
  66. in: "L1/L2/L3/H4\\/H5",
  67. want: []string{"L1", "L2", "L3", "H4/H5"},
  68. },
  69. }
  70. for _, c := range cases {
  71. got := SplitLabel(c.in)
  72. if !reflect.DeepEqual(got, c.want) {
  73. t.Errorf("want %s got %s", c.want, got)
  74. }
  75. }
  76. }