| 1 | // Copyright 2013 Google Inc. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package getopt |
| 6 | |
| 7 | import "testing" |
| 8 | |
| 9 | var listTests = []struct { |
| 10 | where string |
| 11 | in []string |
| 12 | l, list []string |
| 13 | err string |
| 14 | }{ |
| 15 | { |
| 16 | loc(), |
| 17 | []string{}, |
| 18 | nil, nil, |
| 19 | "", |
| 20 | }, |
| 21 | { |
| 22 | loc(), |
| 23 | []string{"test", "-l", "one"}, |
| 24 | []string{"one"}, nil, |
| 25 | "", |
| 26 | }, |
| 27 | { |
| 28 | loc(), |
| 29 | []string{"test", "-lone", "-ltwo"}, |
| 30 | []string{"one", "two"}, nil, |
| 31 | "", |
| 32 | }, |
| 33 | { |
| 34 | loc(), |
| 35 | []string{"test", "--list", "one"}, |
| 36 | nil, []string{"one"}, |
| 37 | "", |
| 38 | }, |
| 39 | { |
| 40 | loc(), |
| 41 | []string{"test", "--list=one", "--list=two"}, |
| 42 | nil, []string{"one", "two"}, |
| 43 | "", |
| 44 | }, |
| 45 | { |
| 46 | loc(), |
| 47 | []string{"test", "--list=one,two"}, |
| 48 | nil, []string{"one", "two"}, |
| 49 | "", |
| 50 | }, |
| 51 | } |
| 52 | |
| 53 | func TestList(t *testing.T) { |
| 54 | for _, tt := range listTests { |
| 55 | reset() |
| 56 | l := List('l') |
| 57 | list := ListLong("list", 0) |
| 58 | |
| 59 | parse(tt.in) |
| 60 | if s := checkError(tt.err); s != "" { |
| 61 | t.Errorf("%s: %s", tt.where, s) |
| 62 | } |
| 63 | if badSlice(*l, tt.l) { |
| 64 | t.Errorf("%s: got s = %q, want %q", tt.where, *l, tt.l) |
| 65 | } |
| 66 | if badSlice(*list, tt.list) { |
| 67 | t.Errorf("%s: got s = %q, want %q", tt.where, *list, tt.list) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestDefaultList(t *testing.T) { |
| 73 | reset() |
| 74 | list := []string{"d1", "d2", "d3"} |
| 75 | ListVar(&list, 'l') |
| 76 | parse([]string{"test"}) |
| 77 | |
| 78 | want := []string{"d1", "d2", "d3"} |
| 79 | if badSlice(list, want) { |
| 80 | t.Errorf("got s = %q, want %q", list, want) |
| 81 | } |
| 82 | |
| 83 | parse([]string{"test", "-l", "one"}) |
| 84 | want = []string{"one"} |
| 85 | if badSlice(list, want) { |
| 86 | t.Errorf("got s = %q, want %q", list, want) |
| 87 | } |
| 88 | |
| 89 | parse([]string{"test", "-l", "two"}) |
| 90 | want = []string{"one", "two"} |
| 91 | if badSlice(list, want) { |
| 92 | t.Errorf("got s = %q, want %q", list, want) |
| 93 | } |
| 94 | Lookup('l').Reset() |
| 95 | want = []string{"d1", "d2", "d3"} |
| 96 | if badSlice(list, want) { |
| 97 | t.Errorf("got s = %q, want %q", list, want) |
| 98 | } |
| 99 | } |