regutils_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 regutils2
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestSubGroupMatch(t *testing.T) {
  20. type args struct {
  21. pattern string
  22. line string
  23. }
  24. tests := []struct {
  25. name string
  26. args args
  27. want map[string]string
  28. }{
  29. {
  30. name: "normalInput",
  31. args: args{
  32. pattern: `(?P<idx>\d+)\s+(?P<start>\d+)s\s+(?P<end>\d+)s\s+(?P<count>\d+)s`,
  33. line: `1 2048s 314984447s 314982400s ntfs Basic data partition msftdata`,
  34. },
  35. want: map[string]string{
  36. "idx": "1",
  37. "start": "2048",
  38. "end": "314984447",
  39. "count": "314982400",
  40. },
  41. },
  42. {
  43. name: "emptyInput",
  44. args: args{
  45. pattern: `%s+`,
  46. line: ``,
  47. },
  48. want: map[string]string{},
  49. },
  50. }
  51. for _, tt := range tests {
  52. t.Run(tt.name, func(t *testing.T) {
  53. if got := SubGroupMatch(tt.args.pattern, tt.args.line); !reflect.DeepEqual(got, tt.want) {
  54. t.Errorf("SubGroupMatch() = %v, want %v", got, tt.want)
  55. }
  56. })
  57. }
  58. }