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 5package getopt 6 7import ( 8 "fmt" 9 "strings" 10 "testing" 11) 12 13var counterTests = []struct { 14 where string 15 in []string 16 c int 17 cnt int 18 err string 19}{ 20 { 21 loc(), 22 []string{}, 23 0, 24 0, 25 "", 26 }, 27 { 28 loc(), 29 []string{"test", "-c", "--cnt"}, 30 1, 31 1, 32 "", 33 }, 34 { 35 loc(), 36 []string{"test", "-cc", "-c", "--cnt", "--cnt"}, 37 3, 38 2, 39 "", 40 }, 41 { 42 loc(), 43 []string{"test", "--c=17", "--cnt=42"}, 44 17, 45 42, 46 "", 47 }, 48 { 49 loc(), 50 []string{"test", "--cnt=false"}, 51 0, 0, 52 "test: not a valid number: false\n", 53 }, 54} 55 56func TestCounter(t *testing.T) { 57 for x, tt := range counterTests { 58 reset() 59 c := Counter('c') 60 cnt := CounterLong("cnt", 0) 61 if strings.Index(tt.where, ":-") > 0 { 62 tt.where = fmt.Sprintf("#%d", x) 63 } 64 65 parse(tt.in) 66 if s := checkError(tt.err); s != "" { 67 t.Errorf("%s: %s", tt.where, s) 68 } 69 if got, want := *c, tt.c; got != want { 70 t.Errorf("%s: got %v, want %v", tt.where, got, want) 71 } 72 if got, want := *cnt, tt.cnt; got != want { 73 t.Errorf("%s: got %v, want %v", tt.where, got, want) 74 } 75 } 76 77 reset() 78 c := 5 79 opt := CounterVar(&c, 'c') 80 parse([]string{"test", "-c"}) 81 if c != 6 { 82 t.Errorf("got %d, want 6", c) 83 } 84 if opt.Count() != 1 { 85 t.Errorf("got %d, want 1", c) 86 } 87 Reset() 88 if c != 5 { 89 t.Errorf("got %d, want 5", c) 90 } 91} 92