| 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 stringTests = []struct { |
| 10 | where string |
| 11 | in []string |
| 12 | sout string |
| 13 | optout string |
| 14 | err string |
| 15 | }{ |
| 16 | { |
| 17 | loc(), |
| 18 | []string{}, |
| 19 | "one", |
| 20 | "two", |
| 21 | "", |
| 22 | }, |
| 23 | { |
| 24 | loc(), |
| 25 | []string{"test", "-s"}, |
| 26 | "one", |
| 27 | "two", |
| 28 | "test: missing parameter for -s\n", |
| 29 | }, |
| 30 | { |
| 31 | loc(), |
| 32 | []string{"test", "--opt"}, |
| 33 | "one", |
| 34 | "two", |
| 35 | "test: missing parameter for --opt\n", |
| 36 | }, |
| 37 | { |
| 38 | loc(), |
| 39 | []string{"test", "-svalue", "--opt=option"}, |
| 40 | "value", |
| 41 | "option", |
| 42 | "", |
| 43 | }, |
| 44 | { |
| 45 | loc(), |
| 46 | []string{"test", "-s", "value", "--opt", "option"}, |
| 47 | "value", |
| 48 | "option", |
| 49 | "", |
| 50 | }, |
| 51 | { |
| 52 | loc(), |
| 53 | []string{"test", "-swrong", "--opt=wrong", "-s", "value", "--opt", "option"}, |
| 54 | "value", |
| 55 | "option", |
| 56 | "", |
| 57 | }, |
| 58 | } |
| 59 | |
| 60 | func TestString(t *testing.T) { |
| 61 | for _, tt := range stringTests { |
| 62 | reset() |
| 63 | s := String('s', "one") |
| 64 | opt := StringLong("opt", 0, "two") |
| 65 | |
| 66 | parse(tt.in) |
| 67 | if s := checkError(tt.err); s != "" { |
| 68 | t.Errorf("%s: %s", tt.where, s) |
| 69 | } |
| 70 | if *s != tt.sout { |
| 71 | t.Errorf("%s: got s = %q, want %q", tt.where, *s, tt.sout) |
| 72 | } |
| 73 | if *opt != tt.optout { |
| 74 | t.Errorf("%s: got opt = %q, want %q", tt.where, *opt, tt.optout) |
| 75 | } |
| 76 | } |
| 77 | } |