diff options
author | Brad Peabody <[email protected]> | 2020-05-23 11:35:37 -0700 |
---|---|---|
committer | Ron Evans <[email protected]> | 2020-05-27 08:43:29 +0200 |
commit | 4918395f88d8f1a38fc421566754dd2acc6e161d (patch) | |
tree | 38ae63e2c11fabbe79bdcd48cd6f1365e3516d12 /tests | |
parent | 67ac4fdd8ebe0b95b0e31aa4e899b324b056b9e6 (diff) | |
download | tinygo-4918395f88d8f1a38fc421566754dd2acc6e161d.tar.gz tinygo-4918395f88d8f1a38fc421566754dd2acc6e161d.zip |
added test for wasm log output
callback case for log test
Diffstat (limited to 'tests')
-rw-r--r-- | tests/wasm/log_test.go | 44 | ||||
-rw-r--r-- | tests/wasm/setup_test.go | 20 | ||||
-rw-r--r-- | tests/wasm/testdata/log.go | 41 |
3 files changed, 101 insertions, 4 deletions
diff --git a/tests/wasm/log_test.go b/tests/wasm/log_test.go new file mode 100644 index 000000000..a9cc1befa --- /dev/null +++ b/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) + } + +} diff --git a/tests/wasm/setup_test.go b/tests/wasm/setup_test.go index 7d5ffa675..a38b42654 100644 --- a/tests/wasm/setup_test.go +++ b/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) } diff --git a/tests/wasm/testdata/log.go b/tests/wasm/testdata/log.go new file mode 100644 index 000000000..c16fe1d01 --- /dev/null +++ b/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") + +} |