-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_test.go
161 lines (135 loc) · 4.49 KB
/
io_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2024 Highlander Paiva. All rights reserved.
// Use of this source code is governed by the MIT License that can be found in
// the LICENSE file.
package goaoc
import (
"bytes"
"errors"
"io"
"os"
"reflect"
"strings"
"testing"
)
func mockEnv(args []string, input string, output io.Writer) Env {
return Env{
Stdin: bytes.NewBufferString(input),
Stdout: output,
Args: args,
}
}
type failingWriter struct{}
func (f *failingWriter) Write(_ []byte) (n int, err error) {
return 0, errors.New("write failed")
}
func TestRead(t *testing.T) {
testCases := []struct {
name string
env Env
expect string
expectErr string
}{
{"PartFromFlag", mockEnv([]string{"-part=1"}, "", new(bytes.Buffer)), "1", ""},
{"PartFromEnv", mockEnv([]string{}, "", new(bytes.Buffer)), "2", ""},
{"PartFromStdin", mockEnv([]string{}, "1\n", new(bytes.Buffer)), "1", ""},
{"PartFromStdinFailStdout", mockEnv([]string{}, "1\n", &failingWriter{}), "1", "write failed"},
{"PartFromStdinFailEmpty", mockEnv([]string{}, "", new(bytes.Buffer)), "", "failed to read input: no part specified, please provide a valid part"},
{"FlagProvidedButNotDefined", mockEnv([]string{"--test"}, "0", new(bytes.Buffer)), "", "failed to read input: flag provided but not defined: -test"},
{"FlagProvidedButNotDefinedFailedStdout", mockEnv([]string{"--test"}, "0", &failingWriter{}), "", "failed to read input: flag provided but not defined: -test"},
{"EmptyRead", mockEnv([]string{}, "", new(bytes.Buffer)), "", ""},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
manager := DefaultConsoleManager{Env: tc.env}
if tc.name == "PartFromEnv" {
_ = os.Setenv("GOAOC_CHALLENGE_PART", "2")
defer func() {
err := os.Unsetenv("GOAOC_CHALLENGE_PART")
if err != nil {
t.Fatalf("Unexpected error while unsetting environment variable: %v", err)
}
}()
}
part, err := manager.Read("part")
if tc.name == "EmptyRead" {
part, err = manager.Read("")
}
if tc.expectErr != "" {
if err == nil || err.Error() != tc.expectErr {
t.Fatalf("Expected error '%s', but got: %v", tc.expectErr, err)
}
} else {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if part != tc.expect {
t.Errorf("Expected part %s, but got %s", tc.expect, part)
}
}
})
}
}
func TestToClipboard(t *testing.T) {
env := mockEnv([]string{}, "", new(bytes.Buffer))
manager := DefaultConsoleManager{Env: env}
testCases := []struct {
name string
output string
}{
{"Working", "Copied to clipboard: test value"},
{"Deactivated", ""},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_ = os.Setenv("GOAOC_DISABLE_COPY_CLIPBOARD", "false")
if tc.name == "Deactivated" {
_ = os.Setenv("GOAOC_DISABLE_COPY_CLIPBOARD", "true")
}
toClipboard("test value", env.Stdout)
output := manager.Env.Stdout.(*bytes.Buffer).String()
if !strings.Contains(output, tc.output) {
t.Errorf("Expected clipboard message, but got: %s", output)
}
})
}
}
func TestOutput(t *testing.T) {
env := mockEnv([]string{}, "", new(bytes.Buffer))
manager := DefaultConsoleManager{Env: env}
_ = os.Setenv("GOAOC_DISABLE_COPY_CLIPBOARD", "false")
err := manager.Write("42")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
output := env.Stdout.(*bytes.Buffer).String()
expectedOutput := "The challenge result is 42\nCopied to clipboard: 42\n"
if output != expectedOutput {
t.Errorf("Expected output '%s', but got '%s'", expectedOutput, output)
}
}
func TestSelectPartErrors(t *testing.T) {
_ = os.Unsetenv("GOAOC_CHALLENGE_PART")
mockEnv := mockEnv([]string{}, "", new(bytes.Buffer))
manager := DefaultConsoleManager{Env: mockEnv}
_, err := manager.Read("part")
if err == nil || err.Error() != "failed to read input: no part specified, please provide a valid part" {
t.Fatalf("Expected 'failed to read input: no part specified, please provide a valid part' error, but got: %v", err)
}
}
func TestOutputWriterFails(t *testing.T) {
manager := DefaultConsoleManager{
Env: Env{
Stdout: &failingWriter{},
},
}
err := manager.Write("42")
if err == nil || err.Error() != "failed to write input: write failed" {
t.Fatalf("Expected 'failed to write input: write failed' error, but got: %v", err)
}
}
func TestNewConsoleManager(t *testing.T) {
manager := NewConsoleManager()
if !reflect.DeepEqual(manager.Env, defaultConsoleEnv) {
t.Errorf("expected Stdin to be %v, but got %v", os.Stdin, manager.Env.Stdin)
}
}