From c8358845a8f759081e2d09154784435669f341b3 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Wed, 29 Jul 2015 08:36:45 +0200 Subject: [PATCH] bind/printer: impl io.Reader + test --- bind/bind.go | 4 ++-- bind/printer.go | 4 ++++ bind/printer_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 bind/printer_test.go diff --git a/bind/bind.go b/bind/bind.go index b8a2b15..0d56827 100644 --- a/bind/bind.go +++ b/bind/bind.go @@ -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 } diff --git a/bind/printer.go b/bind/printer.go index 0b3e058..786c27a 100644 --- a/bind/printer.go +++ b/bind/printer.go @@ -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 { diff --git a/bind/printer_test.go b/bind/printer_test.go new file mode 100644 index 0000000..c533f39 --- /dev/null +++ b/bind/printer_test.go @@ -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) + } + +}