namepattern_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 stringutils2
  15. import (
  16. "testing"
  17. )
  18. func TestParseNamePattern2(t *testing.T) {
  19. cases := []struct {
  20. input string
  21. match string
  22. pattern string
  23. patternLen int
  24. offset int
  25. charType byte
  26. }{
  27. {
  28. input: "testimg###",
  29. match: "testimg%",
  30. pattern: "testimg%03d",
  31. patternLen: 3,
  32. offset: 0,
  33. charType: RepChar,
  34. },
  35. {
  36. input: "testimg###66#",
  37. match: "testimg%",
  38. pattern: "testimg%03d",
  39. patternLen: 3,
  40. offset: 66,
  41. charType: RepChar,
  42. },
  43. {
  44. input: "testimg###ab#",
  45. match: "testimg%",
  46. pattern: "testimg%03d",
  47. patternLen: 3,
  48. offset: 0,
  49. charType: RepChar,
  50. },
  51. {
  52. input: "testimg",
  53. match: "testimg-%",
  54. pattern: "testimg-%d",
  55. patternLen: 0,
  56. offset: 0,
  57. },
  58. {
  59. input: "testimg???",
  60. match: "testimg%",
  61. pattern: "testimg%s",
  62. patternLen: 3,
  63. offset: 0,
  64. charType: RandomChar,
  65. },
  66. }
  67. for _, c := range cases {
  68. m, p, pl, o, ch := ParseNamePattern2(c.input)
  69. if m != c.match || p != c.pattern || pl != c.patternLen || o != c.offset || ch != c.charType {
  70. t.Errorf("match got %s want %s, pattern got %s want %s, patternLen got %d want %d, offset got %d want %d, charType got %c want %c", m, c.match, p, c.pattern, pl, c.patternLen, o, c.offset, ch, c.charType)
  71. }
  72. }
  73. }