spec_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package test
  14. import "testing"
  15. // Test the tests!
  16. // make one expected arg
  17. func oneExp(s ...string) [][]string {
  18. return [][]string{s}
  19. }
  20. // make actual args
  21. func act(s ...string) []string {
  22. return s
  23. }
  24. func TestArgumentsMatchExpected(t *testing.T) {
  25. type args struct {
  26. expectedArgs [][]string
  27. actualArgs []string
  28. }
  29. tests := []struct {
  30. name string
  31. args args
  32. wantErr bool
  33. }{
  34. {"-f ; ok", args{oneExp("-h"), act("-h")}, false},
  35. {"--flag=val ; ok", args{oneExp("--show=all"), act("--show=all")}, false},
  36. {"--flag val ; ok", args{oneExp("--show", "all"), act("--show", "all")}, false},
  37. {"-fval notfound", args{oneExp("-Ohere"), act("-ohere")}, true},
  38. {"--flag val ; no flag", args{oneExp("--debug", "3"), act("--iterations", "3")}, true},
  39. {"--flag val ; no val", args{oneExp("--debug", "3"), act("--debug", "4")}, true},
  40. {"--flag val ; out of order", args{oneExp("-d", "3"), act("3", "-d")}, true},
  41. {"empty expected arg", args{oneExp(""), act("-h")}, true},
  42. {"empty actual", args{oneExp("-h"), act("")}, true},
  43. {"extra actuals", args{oneExp("-h"), act("-h", "-v")}, true},
  44. {"complicated ; ok", args{
  45. [][]string{{"-h"}, {"-vvv"}, {"-d", "3"}, {"--name=kit"}, {"--name", "sammy"}},
  46. []string{"-h", "-vvv", "-d", "3", "--name=kit", "--name", "sammy"}}, false},
  47. {"complicated ; missing", args{
  48. [][]string{{"-h"}, {"-vvv"}, {"-d", "3"}, {"--name=kit"}, {"--name", "sammy"}},
  49. []string{"-h", "-vvv", "-d", "3", "--name", "sammy"}}, true},
  50. {"complicated ; extra actuals", args{
  51. [][]string{{"-h"}, {"-vvv"}, {"-d", "3"}, {"--name=kit"}, {"--name", "sammy"}},
  52. []string{"-h", "-vvv", "--i-am=extra", "-d", "3", "--name", "sammy"}}, true},
  53. {"complicated ; double instance", args{
  54. [][]string{{"-h"}, {"-vvv"}, {"-d", "3"}, {"--name=kit"}, {"--name", "sammy"}},
  55. []string{"-h", "-vvv", "-vvv", "-d", "3", "--name", "sammy"}}, true},
  56. }
  57. for _, tt := range tests {
  58. t.Run(tt.name, func(t *testing.T) {
  59. if err := ArgumentsMatchExpected(tt.args.actualArgs, tt.args.expectedArgs); (err != nil) != tt.wantErr {
  60. t.Errorf("ArgumentsMatchExpected() error = %v, wantErr %v", err, tt.wantErr)
  61. }
  62. })
  63. }
  64. }