bind/printer: impl io.Reader + test

This commit is contained in:
Sebastien Binet 2015-07-29 08:36:45 +02:00
parent 85a8808e45
commit c8358845a8
3 changed files with 63 additions and 2 deletions

View File

@ -40,12 +40,12 @@ func GenCPython(w io.Writer, fset *token.FileSet, pkg *Package) error {
return err
}
_, err = io.Copy(w, gen.decl.buf)
_, err = io.Copy(w, gen.decl)
if err != nil {
return err
}
_, err = io.Copy(w, gen.impl.buf)
_, err = io.Copy(w, gen.impl)
if err != nil {
return err
}

View File

@ -25,6 +25,10 @@ func (p *printer) writeIndent() error {
return err
}
func (p *printer) Read(b []byte) (n int, err error) {
return p.buf.Read(b)
}
func (p *printer) Write(b []byte) (n int, err error) {
wrote := 0
for len(b) > 0 {

57
bind/printer_test.go Normal file
View File

@ -0,0 +1,57 @@
// Copyright 2015 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bind
import (
"bytes"
"io"
"reflect"
"testing"
)
func TestPrinter(t *testing.T) {
decl := &printer{
buf: new(bytes.Buffer),
indentEach: []byte("\t"),
}
impl := &printer{
buf: new(bytes.Buffer),
indentEach: []byte("\t"),
}
decl.Printf("\ndecl 1\n")
decl.Indent()
decl.Printf(">>> decl-1\n")
decl.Outdent()
impl.Printf("impl 1\n")
decl.Printf("decl 2\n")
impl.Printf("impl 2\n")
out := new(bytes.Buffer)
_, err := io.Copy(out, decl)
if err != nil {
t.Fatalf("error: %v\n", err)
}
_, err = io.Copy(out, impl)
if err != nil {
t.Fatalf("error: %v\n", err)
}
want := `
decl 1
>>> decl-1
decl 2
impl 1
impl 2
`
str := string(out.Bytes())
if !reflect.DeepEqual(str, want) {
t.Fatalf("error:\nwant=%q\ngot =%q\n", want, str)
}
}