aboutsummaryrefslogtreecommitdiff
path: root/lib/test/data.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-07-25 01:54:57 +0700
committerShulhan <ms@kilabit.info>2022-07-25 01:54:57 +0700
commit6c72cd80c30f62885809ac874e9869fdb0a1aaaa (patch)
tree7dec5f53e306a565d995cb330b42a2ede1817726 /lib/test/data.go
parenteb932c941bd3456f5b0635641274853e062916e7 (diff)
downloadpakakeh.go-6c72cd80c30f62885809ac874e9869fdb0a1aaaa.tar.xz
lib/test: truncate the last new line at the end of input and output
Given the following input and output, >>> input <<< output EOF The input and output content always have new line at the end. This may cause unexpected input or output. If input or output content expecting new line at the end, add two empty lines at the end of it.
Diffstat (limited to 'lib/test/data.go')
-rw-r--r--lib/test/data.go16
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/test/data.go b/lib/test/data.go
index becca630..5e1d4eed 100644
--- a/lib/test/data.go
+++ b/lib/test/data.go
@@ -55,6 +55,11 @@ var (
// An output can have a name, if its empty it will be set to "default".
// An output also can be defined multiple times, with different names.
//
+// All of both input and output content will have one new line truncated at
+// the end.
+// If they expecting new line at the end, add two empty lines at the end of
+// it.
+//
// # Example
//
// The following code illustrate how to use Data when writing test.
@@ -281,12 +286,12 @@ func (data *Data) parseFlag(content []byte) {
data.Flag[string(bkey)] = string(bval)
}
-func (data *Data) parseInputOutput(lines [][]byte) (name string, content []byte, n int) {
+func (data *Data) parseInputOutput(lines [][]byte) (name string, content []byte, x int) {
var (
line []byte
bname []byte
bufContent bytes.Buffer
- x int
+ n int
isPrevEmpty bool
)
@@ -310,8 +315,7 @@ func (data *Data) parseInputOutput(lines [][]byte) (name string, content []byte,
}
if isPrevEmpty {
if bytes.HasPrefix(line, prefixInput) || bytes.HasPrefix(line, prefixOutput) {
- content = bufContent.Bytes()
- return name, content, x
+ break
}
bufContent.WriteByte('\n')
}
@@ -321,6 +325,10 @@ func (data *Data) parseInputOutput(lines [][]byte) (name string, content []byte,
}
content = bufContent.Bytes()
+ n = len(content) - 1
+ if n > 0 && content[n] == '\n' {
+ content = content[:n]
+ }
return name, content, x
}