Skip to content

Commit 9ba67d7

Browse files
authored
Merge pull request dop251#60 from ganigeorgiev/console-std-printer
Update the console module to behave similarly as in Node.js
2 parents 0788764 + 69dc599 commit 9ba67d7

File tree

3 files changed

+69
-33
lines changed

3 files changed

+69
-33
lines changed

console/module.go

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package console
22

33
import (
4-
"log"
5-
64
"github.com/dop251/goja"
75
"github.com/dop251/goja_nodejs/require"
86
"github.com/dop251/goja_nodejs/util"
@@ -22,16 +20,6 @@ type Printer interface {
2220
Error(string)
2321
}
2422

25-
type PrinterFunc func(s string)
26-
27-
func (p PrinterFunc) Log(s string) { p(s) }
28-
29-
func (p PrinterFunc) Warn(s string) { p(s) }
30-
31-
func (p PrinterFunc) Error(s string) { p(s) }
32-
33-
var defaultPrinter Printer = PrinterFunc(func(s string) { log.Print(s) })
34-
3523
func (c *Console) log(p func(string)) func(goja.FunctionCall) goja.Value {
3624
return func(call goja.FunctionCall) goja.Value {
3725
if format, ok := goja.AssertFunction(c.util.Get("format")); ok {
@@ -50,7 +38,7 @@ func (c *Console) log(p func(string)) func(goja.FunctionCall) goja.Value {
5038
}
5139

5240
func Require(runtime *goja.Runtime, module *goja.Object) {
53-
requireWithPrinter(defaultPrinter)(runtime, module)
41+
requireWithPrinter(defaultStdPrinter)(runtime, module)
5442
}
5543

5644
func RequireWithPrinter(printer Printer) require.ModuleLoader {
@@ -70,6 +58,8 @@ func requireWithPrinter(printer Printer) require.ModuleLoader {
7058
o.Set("log", c.log(c.printer.Log))
7159
o.Set("error", c.log(c.printer.Error))
7260
o.Set("warn", c.log(c.printer.Warn))
61+
o.Set("info", c.log(c.printer.Log))
62+
o.Set("debug", c.log(c.printer.Log))
7363
}
7464
}
7565

console/module_test.go

+28-20
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,25 @@ func TestConsole(t *testing.T) {
2828
if _, err := vm.RunString("console.warn('')"); err != nil {
2929
t.Fatal("console.warn() error", err)
3030
}
31+
32+
if _, err := vm.RunString("console.info('')"); err != nil {
33+
t.Fatal("console.info() error", err)
34+
}
35+
36+
if _, err := vm.RunString("console.debug('')"); err != nil {
37+
t.Fatal("console.debug() error", err)
38+
}
3139
}
3240

3341
func TestConsoleWithPrinter(t *testing.T) {
34-
vm := goja.New()
42+
var stdoutStr, stderrStr string
43+
44+
printer := StdPrinter{
45+
StdoutPrint: func(s string) { stdoutStr += s },
46+
StderrPrint: func(s string) { stderrStr += s },
47+
}
3548

36-
var lastPrint string
37-
printer := PrinterFunc(func(s string) {
38-
lastPrint = s
39-
})
49+
vm := goja.New()
4050

4151
registry := new(require.Registry)
4252
registry.Enable(vm)
@@ -47,24 +57,22 @@ func TestConsoleWithPrinter(t *testing.T) {
4757
t.Fatal("console not found")
4858
}
4959

50-
if _, err := vm.RunString("console.log('log')"); err != nil {
51-
t.Fatal("console.log() error", err)
52-
}
53-
if lastPrint != "log" {
54-
t.Fatal("lastPrint not 'log'", lastPrint)
60+
_, err := vm.RunString(`
61+
console.log('a')
62+
console.error('b')
63+
console.warn('c')
64+
console.debug('d')
65+
console.info('e')
66+
`)
67+
if err != nil {
68+
t.Fatal(err)
5569
}
5670

57-
if _, err := vm.RunString("console.error('error')"); err != nil {
58-
t.Fatal("console.error() error", err)
59-
}
60-
if lastPrint != "error" {
61-
t.Fatal("lastPrint not 'error'", lastPrint)
71+
if want := "ade"; stdoutStr != want {
72+
t.Fatalf("Unexpected stdout output: got %q, want %q", stdoutStr, want)
6273
}
6374

64-
if _, err := vm.RunString("console.warn('warn')"); err != nil {
65-
t.Fatal("console.warn() error", err)
66-
}
67-
if lastPrint != "warn" {
68-
t.Fatal("lastPrint not 'warn'", lastPrint)
75+
if want := "bc"; stderrStr != want {
76+
t.Fatalf("Unexpected stderr output: got %q, want %q", stderrStr, want)
6977
}
7078
}

console/std_printer.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package console
2+
3+
import (
4+
"log"
5+
"os"
6+
)
7+
8+
var (
9+
stderrLogger = log.Default() // the default logger output to stderr
10+
stdoutLogger = log.New(os.Stdout, "", log.LstdFlags)
11+
12+
defaultStdPrinter Printer = &StdPrinter{
13+
StdoutPrint: func(s string) { stdoutLogger.Print(s) },
14+
StderrPrint: func(s string) { stderrLogger.Print(s) },
15+
}
16+
)
17+
18+
// StdPrinter implements the console.Printer interface
19+
// that prints to the stdout or stderr.
20+
type StdPrinter struct {
21+
StdoutPrint func(s string)
22+
StderrPrint func(s string)
23+
}
24+
25+
// Log prints s to the stdout.
26+
func (p StdPrinter) Log(s string) {
27+
p.StdoutPrint(s)
28+
}
29+
30+
// Warn prints s to the stderr.
31+
func (p StdPrinter) Warn(s string) {
32+
p.StderrPrint(s)
33+
}
34+
35+
// Error prints s to the stderr.
36+
func (p StdPrinter) Error(s string) {
37+
p.StderrPrint(s)
38+
}

0 commit comments

Comments
 (0)