-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
testhelpers_test.go
105 lines (94 loc) · 2.75 KB
/
testhelpers_test.go
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
// This file is part of go-getoptions.
//
// Copyright (C) 2015-2024 David Gamba Rios
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package getoptions
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"testing"
)
func checkError(t *testing.T, got, expected error) {
t.Helper()
if (got == nil && expected != nil) || (got != nil && expected == nil) || (got != nil && expected != nil && !errors.Is(got, expected)) {
t.Errorf("wrong error received: got = '%#v', want '%#v'", got, expected)
}
}
func setupLogging() *bytes.Buffer {
s := ""
buf := bytes.NewBufferString(s)
Logger.SetOutput(buf)
return buf
}
// setupTestLogging - Defines an output for the default Logger and returns a
// function that prints the output if the output is not empty.
//
// Usage:
//
// logTestOutput := setupTestLogging(t)
// defer logTestOutput()
func setupTestLogging(t *testing.T) func() {
s := ""
buf := bytes.NewBufferString(s)
Logger.SetOutput(buf)
return func() {
if len(buf.String()) > 0 {
t.Log("\n" + buf.String())
}
}
}
// Test helper to compare two string outputs and find the first difference
func firstDiff(got, expected string) string {
same := ""
for i, gc := range got {
if len([]rune(expected)) <= i {
return fmt.Sprintf("got:\n%s\nIndex: %d | diff: got '%s' - exp '%s'\n", got, len(expected), got, expected)
}
if gc != []rune(expected)[i] {
return fmt.Sprintf("got:\n%s\nIndex: %d | diff: got '%c' - exp '%c'\n%s\n", got, i, gc, []rune(expected)[i], same)
}
same += string(gc)
}
if len(expected) > len(got) {
return fmt.Sprintf("got:\n%s\nIndex: %d | diff: got '%s' - exp '%s'\n", got, len(got), got, expected)
}
return ""
}
func getNode(tree *programTree, element ...string) (*programTree, error) {
if len(element) == 0 {
return tree, nil
}
if child, ok := tree.ChildCommands[element[0]]; ok {
return getNode(child, element[1:]...)
}
return tree, fmt.Errorf("not found")
}
func stringPT(n *programTree) string {
data, err := json.MarshalIndent(n.str(), "", " ")
if err != nil {
return ""
}
return string(data)
}
func programTreeError(expected, got *programTree) string {
return fmt.Sprintf("expected:\n%s\ngot:\n%s\n", stringPT(expected), stringPT(got))
}
func spewToFileDiff(t *testing.T, expected, got interface{}) string {
return fmt.Sprintf("expected, got: %s %s\n", spewToFile(t, expected, "expected"), spewToFile(t, got, "got"))
}
func spewToFile(t *testing.T, e interface{}, label string) string {
f, err := os.CreateTemp("/tmp/", "spew-")
if err != nil {
t.Fatal(err)
}
defer f.Close()
_, _ = f.WriteString(label + "\n")
fmt.Fprintf(f, "%v\n", e)
return f.Name()
}