testing - Golang test stdout -
testing - Golang test stdout -
i trying test functions print ansi escape codes. e.g.
// print line in color func printlncolor(color string, ...interface{}) { fmt.print("\x1b[31m") fmt.print(a...) fmt.println("\x1b[0m") }
i tried using examples it, don't seem escape codes.
is there way test written stdout?
using fmt.fprint
print io.writer
lets command output written.
var out io.writer = os.stdout func main() { // write stdout printlncolor("foo") buf := &bytes.buffer{} out = buf // write buffer printlncolor("foo") fmt.println(buf.string()) } // print line in color func printlncolor(a ...interface{}) { fmt.fprint(out, "\x1b[31m") fmt.fprint(out, a...) fmt.fprintln(out, "\x1b[0m") }
go play
testing go ansi-escape
Comments
Post a Comment