added test for wasm log output
callback case for log test
Этот коммит содержится в:
родитель
67ac4fdd8e
коммит
4918395f88
3 изменённых файлов: 101 добавлений и 4 удалений
44
tests/wasm/log_test.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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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 {
|
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.
|
// waitLogRe blocks until the log output matches this regular expression
|
||||||
func waitInnerTextTrimEq(sel, innerText string) chromedp.QueryAction {
|
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) {
|
return chromedp.Query(sel, func(s *chromedp.Selector) {
|
||||||
|
|
||||||
|
@ -173,7 +185,7 @@ func waitInnerTextTrimEq(sel, innerText string) chromedp.QueryAction {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nodes, err
|
return nodes, err
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(ret) != innerText {
|
if !re.MatchString(ret) {
|
||||||
// log.Printf("found text: %s", ret)
|
// log.Printf("found text: %s", ret)
|
||||||
return nodes, errors.New("unexpected value: " + ret)
|
return nodes, errors.New("unexpected value: " + ret)
|
||||||
}
|
}
|
||||||
|
|
41
tests/wasm/testdata/log.go
предоставленный
Обычный файл
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")
|
||||||
|
|
||||||
|
}
|
Загрузка…
Создание таблицы
Сослаться в новой задаче