test main

master
bel 2022-07-10 17:16:29 -06:00
parent 215625a482
commit 683bd62b87
2 changed files with 36 additions and 3 deletions

26
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"bytes"
"errors" "errors"
"flag" "flag"
"io" "io"
@ -9,6 +10,8 @@ import (
) )
var ( var (
stdin io.Reader = os.Stdin
stdout io.Writer = os.Stdout
columnDelimiter string columnDelimiter string
pageDelimiter string pageDelimiter string
minimumInstances int minimumInstances int
@ -19,20 +22,26 @@ func main() {
flag.StringVar(&columnDelimiter, "d", " ", "column delimiter character") flag.StringVar(&columnDelimiter, "d", " ", "column delimiter character")
flag.IntVar(&minimumInstances, "m", 2, "minimum non-leading instances of delimiter to break a column") flag.IntVar(&minimumInstances, "m", 2, "minimum non-leading instances of delimiter to break a column")
flag.Parse() flag.Parse()
if err := _main(); err != nil {
panic(err)
}
}
r := bufio.NewReader(os.Stdin) func _main() error {
r := bufio.NewReader(stdin)
for { for {
b, err := readPage(r) b, err := readPage(r)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
panic(err) return err
} }
if err := putPage(b); err != nil { if err := putPage(b); err != nil {
panic(err) return err
} }
if err == io.EOF { if err == io.EOF {
break break
} }
} }
return nil
} }
func readPage(r *bufio.Reader) ([]byte, error) { func readPage(r *bufio.Reader) ([]byte, error) {
@ -48,5 +57,16 @@ func _readPage(d byte, r *bufio.Reader) ([]byte, error) {
} }
func putPage(b []byte) error { func putPage(b []byte) error {
return _putPage(stdout, b)
}
func _putPage(w io.Writer, b []byte) error {
for _, line := range lines(b) {
_ = line
}
return errors.New("not impl") return errors.New("not impl")
} }
func lines(b []byte) [][]byte {
return bytes.Split(b, []byte{'\n'})
}

View File

@ -2,12 +2,25 @@ package main
import ( import (
"bufio" "bufio"
"bytes"
"fmt" "fmt"
"io" "io"
"strings" "strings"
"testing" "testing"
) )
func Test_Main(t *testing.T) {
stdin = strings.NewReader("\ta a a\t\tb b b\na a\t\tb b\fc c\td d\nc c\td.")
stdout = bytes.NewBuffer(nil)
columnDelimiter = "\t"
pageDelimiter = "\f"
minimumInstances = 1
if err := _main(); err != nil {
t.Error(err)
}
t.Errorf("%s", stdout.(*bytes.Buffer).Bytes())
}
func TestReadPage(t *testing.T) { func TestReadPage(t *testing.T) {
cases := map[string]struct { cases := map[string]struct {
input string input string