-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package struct2ts | ||
|
||
import "io" | ||
|
||
type tabScanner struct { | ||
output io.Writer | ||
tabs []byte | ||
} | ||
|
||
func (ts *tabScanner) Write(bs []byte) (n int, err error) { | ||
p := 0 | ||
var rn int | ||
for pos, b := range bs { | ||
switch b { | ||
case '\n': | ||
rn, err = ts.output.Write(bs[p : pos+1]) | ||
n += rn | ||
if err != nil { | ||
return | ||
} | ||
_, err = ts.output.Write(ts.tabs) | ||
if err != nil { | ||
return | ||
} | ||
p = pos + 1 | ||
} | ||
} | ||
rn, err = ts.output.Write(bs[p:]) | ||
return n + rn, err | ||
} | ||
|
||
func newTabScanner(w io.Writer, tabs string) io.Writer { | ||
return &tabScanner{ | ||
output: w, | ||
tabs: []byte(tabs), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package struct2ts | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestTabScanner_Write(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
Input struct { | ||
Tabs string | ||
Buffers []string | ||
} | ||
Expected struct { | ||
Output string | ||
n *int | ||
} | ||
}{ | ||
{ | ||
"One new line mid string - one input, 3 spaces", | ||
struct { | ||
Tabs string | ||
Buffers []string | ||
}{Tabs: " ", Buffers: []string{"Hello how are you?\nI'm fine thanks!"}}, | ||
struct { | ||
Output string | ||
n *int | ||
}{Output: "Hello how are you?\n I'm fine thanks!"}, | ||
}, | ||
{ | ||
"One new line end string - one input, 3 spaces", | ||
struct { | ||
Tabs string | ||
Buffers []string | ||
}{Tabs: " ", Buffers: []string{"Hello how are you?\n"}}, | ||
struct { | ||
Output string | ||
n *int | ||
}{Output: "Hello how are you?\n "}, | ||
}, | ||
{ | ||
"One new line start string - one input, 3 spaces", | ||
struct { | ||
Tabs string | ||
Buffers []string | ||
}{Tabs: " ", Buffers: []string{"\nHello how are you?"}}, | ||
struct { | ||
Output string | ||
n *int | ||
}{Output: "\n Hello how are you?"}, | ||
}, | ||
{ | ||
"empty string", | ||
struct { | ||
Tabs string | ||
Buffers []string | ||
}{Tabs: " ", Buffers: []string{""}}, | ||
struct { | ||
Output string | ||
n *int | ||
}{Output: ""}, | ||
}, | ||
{ | ||
"Just a new line", | ||
struct { | ||
Tabs string | ||
Buffers []string | ||
}{Tabs: " ", Buffers: []string{"\n"}}, | ||
struct { | ||
Output string | ||
n *int | ||
}{Output: "\n "}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
outputBuffer := bytes.NewBuffer(nil) | ||
ts := newTabScanner(outputBuffer, test.Input.Tabs) | ||
for _, s := range test.Input.Buffers { | ||
_, err := ts.Write([]byte(s)) | ||
if err != nil { | ||
t.Error("Buffer write error", err) | ||
} | ||
} | ||
if outputBuffer.String() != test.Expected.Output { | ||
t.Log("Output buffer doesn't match. Got ") | ||
t.Log(outputBuffer.String()) | ||
t.Log("expected") | ||
t.Log(test.Expected.Output) | ||
t.Fail() | ||
} | ||
}) | ||
} | ||
} |