Browse Source

added test for wasm log output

callback case for log test
pull/1113/head
Brad Peabody 5 years ago
committed by Ron Evans
parent
commit
4918395f88
  1. 44
      tests/wasm/log_test.go
  2. 20
      tests/wasm/setup_test.go
  3. 41
      tests/wasm/testdata/log.go

44
tests/wasm/log_test.go

@ -0,0 +1,44 @@
// +build go1.14
package wasm
import (
"testing"
"time"
"github.com/chromedp/chromedp"
)
func TestLog(t *testing.T) {
t.Parallel()
err := run("tinygo build -o " + wasmTmpDir + "/log.wasm -target wasm testdata/log.go")
if err != nil {
t.Fatal(err)
}
ctx, cancel := chromectx(5 * time.Second)
defer cancel()
var log1 string
err = chromedp.Run(ctx,
chromedp.Navigate("http://localhost:8826/run?file=log.wasm"),
chromedp.Sleep(time.Second),
chromedp.InnerHTML("#log", &log1),
waitLogRe(`^..../../.. ..:..:.. log 1
..../../.. ..:..:.. log 2
..../../.. ..:..:.. log 3
println 4
fmt.Println 5
..../../.. ..:..:.. log 6
in func 1
..../../.. ..:..:.. in func 2
$`),
)
t.Logf("log1: %s", log1)
if err != nil {
t.Fatal(err)
}
}

20
tests/wasm/setup_test.go

@ -10,6 +10,7 @@ import (
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"testing"
"time"
@ -145,12 +146,23 @@ if (wasmSupported) {
}
// waitLog blocks until the log output equals the text provided (ignoring whitespace before and after)
func waitLog(logText string) chromedp.QueryAction {
return waitInnerTextTrimEq("#log", logText)
return waitInnerTextTrimEq("#log", strings.TrimSpace(logText))
}
// waitInnerTextTrimEq will wait for the innerText of the specified element to match a specific string after whitespace trimming.
func waitInnerTextTrimEq(sel, innerText string) chromedp.QueryAction {
// waitLogRe blocks until the log output matches this regular expression
func waitLogRe(restr string) chromedp.QueryAction {
return waitInnerTextMatch("#log", regexp.MustCompile(restr))
}
// waitInnerTextTrimEq will wait for the innerText of the specified element to match a specific text pattern (ignoring whitespace before and after)
func waitInnerTextTrimEq(sel string, innerText string) chromedp.QueryAction {
return waitInnerTextMatch(sel, regexp.MustCompile(`^\s*`+regexp.QuoteMeta(innerText)+`\s*$`))
}
// waitInnerTextMatch will wait for the innerText of the specified element to match a specific regexp pattern
func waitInnerTextMatch(sel string, re *regexp.Regexp) chromedp.QueryAction {
return chromedp.Query(sel, func(s *chromedp.Selector) {
@ -173,7 +185,7 @@ func waitInnerTextTrimEq(sel, innerText string) chromedp.QueryAction {
if err != nil {
return nodes, err
}
if strings.TrimSpace(ret) != innerText {
if !re.MatchString(ret) {
// log.Printf("found text: %s", ret)
return nodes, errors.New("unexpected value: " + ret)
}

41
tests/wasm/testdata/log.go

@ -0,0 +1,41 @@
package main
import (
"fmt"
"log"
"syscall/js"
)
func main() {
// try various log and other output directly
log.Println("log 1")
log.Print("log 2")
log.Printf("log %d\n", 3)
println("println 4")
fmt.Println("fmt.Println 5")
log.Printf("log %s", "6")
// now set up some log output in a button click callback
js.Global().
Get("document").
Call("querySelector", "#main").
Set("innerHTML", `<button id="testbtn">Test</button>`)
js.Global().
Get("document").
Call("querySelector", "#testbtn").
Call("addEventListener", "click",
js.FuncOf(func(this js.Value, args []js.Value) interface{} {
println("in func 1")
log.Printf("in func 2")
return nil
}))
// click the button
js.Global().
Get("document").
Call("querySelector", "#testbtn").
Call("click")
}
Loading…
Cancel
Save