github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/gojs/time_test.go (about)

     1  package gojs_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"testing"
     7  
     8  	"github.com/bananabytelabs/wazero/experimental"
     9  	"github.com/bananabytelabs/wazero/experimental/logging"
    10  	"github.com/bananabytelabs/wazero/internal/testing/require"
    11  )
    12  
    13  func Test_time(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	var log bytes.Buffer
    17  	loggingCtx := context.WithValue(testCtx, experimental.FunctionListenerFactoryKey{},
    18  		logging.NewHostLoggingListenerFactory(&log, logging.LogScopeClock))
    19  
    20  	stdout, stderr, err := compileAndRun(loggingCtx, "time", defaultConfig)
    21  
    22  	require.Zero(t, stderr)
    23  	require.NoError(t, err)
    24  	require.Equal(t, `Local
    25  1ms
    26  `, stdout)
    27  
    28  	// To avoid multiple similar assertions, just check three functions we
    29  	// expect were called.
    30  	logString := logString(log)
    31  	require.Contains(t, logString, `==> go.runtime.nanotime1()
    32  <== (nsec=0)`)
    33  	require.Contains(t, logString, `==> go.runtime.walltime()
    34  <== (sec=1640995200,nsec=0)
    35  `)
    36  	require.Contains(t, logString, `==> go.syscall/js.valueCall(Date.getTimezoneOffset())
    37  <== (tz=0)
    38  `)
    39  }