1// Copyright 2017 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 5package getopt 6 7import ( 8 "fmt" 9 "strings" 10 "testing" 11) 12 13var enumTests = []struct { 14 where string 15 in []string 16 values []string 17 def string 18 out string 19 err string 20}{ 21 { 22 loc(), 23 nil, 24 []string{}, 25 "", 26 "", 27 "", 28 }, 29 { 30 loc(), 31 []string{"test", "-e", "val1"}, 32 []string{"val1", "val2"}, 33 "", 34 "val1", 35 "", 36 }, 37 { 38 loc(), 39 []string{"test", "-e", "val1", "-e", "val2"}, 40 []string{"val1", "val2"}, 41 "", 42 "val2", 43 "", 44 }, 45 { 46 loc(), 47 []string{"test"}, 48 []string{"val1", "val2"}, 49 "val2", 50 "val2", 51 "", 52 }, 53 { 54 loc(), 55 []string{"test", "-e", "val3"}, 56 []string{"val1", "val2"}, 57 "", 58 "", 59 "test: invalid value: val3\n", 60 }, 61} 62 63func TestEnum(t *testing.T) { 64 for x, tt := range enumTests { 65 if strings.Index(tt.where, ":-") > 0 { 66 tt.where = fmt.Sprintf("#%d", x) 67 } 68 69 reset() 70 e := Enum('e', tt.values, tt.def) 71 parse(tt.in) 72 if s := checkError(tt.err); s != "" { 73 t.Errorf("%s: %s", tt.where, s) 74 } 75 if *e != tt.out { 76 t.Errorf("%s: got %v, want %v", tt.where, *e, tt.out) 77 } 78 } 79} 80