-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
mocks.go
55 lines (46 loc) · 1.61 KB
/
mocks.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
package text
import (
"fmt"
"strings"
)
// MockTextRetriever is a stub implementation of the `TextRetriever` interface to be used in our unit test suites.
// It stores the expected Content and Err
type MockTextRetriever struct {
Err error
Contents map[string]string
}
func (mtr *MockTextRetriever) ReadLine(location string, line int) (string, error) {
contentLines := strings.Split(
strings.ReplaceAll(mtr.Contents[location], "\r\n", "\n"),
"\n",
)
if len(contentLines) < line {
return "", fmt.Errorf("I/O error: The file %q only contains %d, less than the specified line %d", location, len(contentLines), line)
}
return contentLines[line-1], mtr.Err
}
func (mtr *MockTextRetriever) ReadAll(location string) (string, error) {
return mtr.Contents[location], mtr.Err
}
func (mtr *MockTextRetriever) WriteLineToFile(lineContent, location string, lineNumber int) error {
// No "\r\n" to "\n" replacements here as we want to obtain a joined string with the same line delimiter as before
contentLines := strings.Split(
mtr.Contents[location],
"\n",
)
// If the original line ends by a "\r", add to to the lineContent (which never ends by one)
if strings.HasSuffix(contentLines[lineNumber-1], "\r") {
lineContent = lineContent + "\r"
}
contentLines[lineNumber-1] = lineContent
mtr.Contents[location] = strings.Join(contentLines, "\n")
return mtr.Err
}
func (mtr *MockTextRetriever) WriteToFile(content string, location string) error {
mtr.Contents[location] = content
return mtr.Err
}
func (mtr *MockTextRetriever) FileExists(location string) bool {
_, exists := mtr.Contents[location]
return exists
}