summaryrefslogtreecommitdiff
path: root/src/pkg/flag/flag_test.go
blob: 59014dcd387f38b596617ed044c6bfea4880619b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package flag_test

import (
	. "flag";
	"testing";
)

var (
	test_bool	= Bool("test_bool", false, "bool value");
	test_int	= Int("test_int", 0, "int value");
	test_int64	= Int64("test_int64", 0, "int64 value");
	test_uint	= Uint("test_uint", 0, "uint value");
	test_uint64	= Uint64("test_uint64", 0, "uint64 value");
	test_string	= String("test_string", "0", "string value");
)

func boolString(s string) string {
	if s == "0" {
		return "false";
	}
	return "true";
}

func TestEverything(t *testing.T) {
	m := make(map[string]*Flag);
	desired := "0";
	visitor := func(f *Flag) {
		if len(f.Name) > 5 && f.Name[0:5] == "test_" {
			m[f.Name] = f;
			ok := false;
			switch {
			case f.Value.String() == desired:
				ok = true;
			case f.Name == "test_bool" && f.Value.String() == boolString(desired):
				ok = true;
			}
			if !ok {
				t.Error("Visit: bad value", f.Value.String(), "for", f.Name);
			}
		}
	};
	VisitAll(visitor);
	if len(m) != 6 {
		t.Error("VisitAll misses some flags");
		for k, v := range m {
			t.Log(k, *v);
		}
	}
	m = make(map[string]*Flag);
	Visit(visitor);
	if len(m) != 0 {
		t.Errorf("Visit sees unset flags");
		for k, v := range m {
			t.Log(k, *v);
		}
	}
	// Now set all flags
	Set("test_bool", "true");
	Set("test_int", "1");
	Set("test_int64", "1");
	Set("test_uint", "1");
	Set("test_uint64", "1");
	Set("test_string", "1");
	desired = "1";
	Visit(visitor);
	if len(m) != 6 {
		t.Error("Visit fails after set");
		for k, v := range m {
			t.Log(k, *v);
		}
	}
}