summaryrefslogtreecommitdiff
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
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.
-rw-r--r--lib/ini/testdata/struct/embedded_test.txt1
-rw-r--r--lib/ini/testdata/struct/map_test.txt1
-rw-r--r--lib/ini/testdata/struct/slice_of_pointer_test.txt1
-rw-r--r--lib/ini/testdata/struct/slice_of_primitive_test.txt1
-rw-r--r--lib/ini/testdata/struct/slice_of_struct_test.txt1
-rw-r--r--lib/ini/testdata/struct/struct_test.txt1
-rw-r--r--lib/test/data.go16
-rw-r--r--lib/test/data_test.go16
-rw-r--r--lib/test/example_test.go8
9 files changed, 30 insertions, 16 deletions
diff --git a/lib/ini/testdata/struct/embedded_test.txt b/lib/ini/testdata/struct/embedded_test.txt
index 8a5c05a3..10f3c9a4 100644
--- a/lib/ini/testdata/struct/embedded_test.txt
+++ b/lib/ini/testdata/struct/embedded_test.txt
@@ -21,3 +21,4 @@ z = 2.3
[c]
xx = 4
+
diff --git a/lib/ini/testdata/struct/map_test.txt b/lib/ini/testdata/struct/map_test.txt
index 01be7ef1..9d170d61 100644
--- a/lib/ini/testdata/struct/map_test.txt
+++ b/lib/ini/testdata/struct/map_test.txt
@@ -14,3 +14,4 @@ a = 1
a = 1
b = 2
c = 3
+
diff --git a/lib/ini/testdata/struct/slice_of_pointer_test.txt b/lib/ini/testdata/struct/slice_of_pointer_test.txt
index a83802da..5e0c0d26 100644
--- a/lib/ini/testdata/struct/slice_of_pointer_test.txt
+++ b/lib/ini/testdata/struct/slice_of_pointer_test.txt
@@ -56,3 +56,4 @@ int = 1
[slice "ptr_struct"]
string = ptr_struct 1
int = 2
+
diff --git a/lib/ini/testdata/struct/slice_of_primitive_test.txt b/lib/ini/testdata/struct/slice_of_primitive_test.txt
index baefe86f..c44c9dcf 100644
--- a/lib/ini/testdata/struct/slice_of_primitive_test.txt
+++ b/lib/ini/testdata/struct/slice_of_primitive_test.txt
@@ -44,3 +44,4 @@ string = string 1
string = string 2
time = 2021-02-28 03:56:01
time = 2021-02-28 03:56:02
+
diff --git a/lib/ini/testdata/struct/slice_of_struct_test.txt b/lib/ini/testdata/struct/slice_of_struct_test.txt
index 197791bf..47e69595 100644
--- a/lib/ini/testdata/struct/slice_of_struct_test.txt
+++ b/lib/ini/testdata/struct/slice_of_struct_test.txt
@@ -30,3 +30,4 @@ int = 1
[slice "struct"]
string = struct 1
int = 2
+
diff --git a/lib/ini/testdata/struct/struct_test.txt b/lib/ini/testdata/struct/struct_test.txt
index d0c4cbcf..195a5b5a 100644
--- a/lib/ini/testdata/struct/struct_test.txt
+++ b/lib/ini/testdata/struct/struct_test.txt
@@ -55,3 +55,4 @@ int = 3
[section "struct"]
string = struct
int = 1
+
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
}
diff --git a/lib/test/data_test.go b/lib/test/data_test.go
index b6a9f3f0..139599a5 100644
--- a/lib/test/data_test.go
+++ b/lib/test/data_test.go
@@ -33,7 +33,7 @@ func TestData_parse(t *testing.T) {
content: []byte(">>>\n\ninput.\n\n"),
expData: Data{
Input: map[string][]byte{
- `default`: []byte("\ninput.\n\n"),
+ `default`: []byte("\ninput.\n"),
},
},
}, {
@@ -41,7 +41,7 @@ func TestData_parse(t *testing.T) {
content: []byte("<<<\n\noutput.\n\n"),
expData: Data{
Output: map[string][]byte{
- `default`: []byte("\noutput.\n\n"),
+ `default`: []byte("\noutput.\n"),
},
},
}, {
@@ -66,8 +66,8 @@ func TestData_parse(t *testing.T) {
},
Desc: []byte("Desc."),
Input: map[string][]byte{
- "input 1": []byte("1\n"),
- "input 2": []byte("2\n"),
+ "input 1": []byte("1"),
+ "input 2": []byte("2"),
},
},
}, {
@@ -80,8 +80,8 @@ func TestData_parse(t *testing.T) {
Flag: map[string]string{},
Desc: []byte("Desc."),
Output: map[string][]byte{
- "output-1": []byte("1\n\n2\n"),
- "output-2": []byte("3\n\n4\n"),
+ "output-1": []byte("1\n\n2"),
+ "output-2": []byte("3\n\n4"),
},
},
}, {
@@ -93,7 +93,7 @@ func TestData_parse(t *testing.T) {
expData: Data{
Flag: map[string]string{},
Input: map[string][]byte{
- "default": []byte("Input 2.\n"),
+ "default": []byte("Input 2."),
},
},
}, {
@@ -105,7 +105,7 @@ func TestData_parse(t *testing.T) {
),
expData: Data{
Input: map[string][]byte{
- "default": []byte("Input 1.\n<<<\nOutput 1.\n"),
+ "default": []byte("Input 1.\n<<<\nOutput 1."),
},
},
}}
diff --git a/lib/test/example_test.go b/lib/test/example_test.go
index e14d123e..4016fa8b 100644
--- a/lib/test/example_test.go
+++ b/lib/test/example_test.go
@@ -29,11 +29,11 @@ func ExampleLoadDataDir() {
fmt.Printf(" Desc=%s\n", data.Desc)
fmt.Println(" Input")
for name, content = range data.Input {
- fmt.Printf(" %s=%s", name, content)
+ fmt.Printf(" %s=%s\n", name, content)
}
fmt.Println(" Output")
for name, content = range data.Output {
- fmt.Printf(" %s=%s", name, content)
+ fmt.Printf(" %s=%s\n", name, content)
}
}
@@ -82,11 +82,11 @@ func ExampleLoadData() {
fmt.Printf(" Desc=%s\n", data.Desc)
fmt.Println(" Input")
for name, content = range data.Input {
- fmt.Printf(" %s=%s", name, content)
+ fmt.Printf(" %s=%s\n", name, content)
}
fmt.Println(" Output")
for name, content = range data.Output {
- fmt.Printf(" %s=%s", name, content)
+ fmt.Printf(" %s=%s\n", name, content)
}
// Output: