aboutsummaryrefslogtreecommitdiff
path: root/tests/gistatic.go
blob: 5798a0c50e07f524e5fa160c2cbbecc23a640848 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package gistatic

import (
	"strings"

	g "gobang"
)



func test_usage() {
	g.TestStart("usage()")

	g.Testing("it writes the usage string to the io.Writer", func() {
		w := strings.Builder{}
		usage("xxx", &w)

		expected := g.Heredoc(`
			Usage: xxx [-o DIRECTORY] [-u CLONE_URL] REPOSITORY...
		`)
		g.TAssertEqual(w.String(), expected)
	})
}

func test_getopt() {
	g.TestStart("getopt()")

	usage := g.Heredoc(`
		Usage: $0 [-o DIRECTORY] [-u CLONE_URL] REPOSITORY...
	`)

	g.Testing("we supress the default error message", func() {
		w := strings.Builder{}
		argv := []string{"$0", "-h"}
		_, rc := getopt(argv, &w)

		g.TAssertEqual(w.String(), usage)
		g.TAssertEqual(rc, 2)
	})

	g.Testing("we get unsupported flag error", func() {
		w := strings.Builder{}
		argv := []string{"$0", "-A"}
		_, rc := getopt(argv, &w)

		const message = "flag provided but not defined: -A\n"
		g.TAssertEqual(w.String(), message + usage)
		g.TAssertEqual(rc, 2)
	})

	g.Testing("we get incorrect use of flag error", func() {
		w1 := strings.Builder{}
		argv1 := []string{"$0", "-u"}
		_, rc1 := getopt(argv1, &w1)

		const message1 = "flag needs an argument: -u\n"
		g.TAssertEqual(w1.String(), message1 + usage)
		g.TAssertEqual(rc1, 2)

		w2 := strings.Builder{}
		argv2 := []string{"$0", "-o"}
		_, rc2 := getopt(argv2, &w2)

		const message2 = "flag needs an argument: -o\n"
		g.TAssertEqual(w2.String(), message2 + usage)
		g.TAssertEqual(rc2, 2)
	})

	g.Testing("the argsT has the flag URL value", func() {
		fn_wdSaved := fn_wd
		fn_wd = func() (string, error) {
			return "virtual working directory", nil
		}

		w := strings.Builder{}
		argv := []string{"$0", "-u", "proto://URL", "a-path"}
		expected := argsT{
			cloneURL: "proto://URL",
			allArgs:  []string{"$0", "-u", "proto://URL", "a-path"},
			subArgs:  []string{"a-path"},
			outdir:   "virtual working directory",
		}

		args, rc := getopt(argv, &w)

		fn_wd = fn_wdSaved

		g.TAssertEqual(w.String(), "")
		g.TAssertEqual(rc, 0)
		g.TAssertEqual(args, expected)
	})
}

func test_run() {
	// FIXME
}



func MainTest() {
	g.Init()

	test_usage()
	test_getopt()
	test_run()
}