gitee.com/quant1x/pkg@v0.2.8/goja_nodejs/util/module_test.go (about) 1 package util 2 3 import ( 4 "bytes" 5 "testing" 6 7 "gitee.com/quant1x/pkg/goja" 8 "gitee.com/quant1x/pkg/goja_nodejs/require" 9 ) 10 11 func TestUtil_Format(t *testing.T) { 12 vm := goja.New() 13 util := New(vm) 14 15 var b bytes.Buffer 16 util.Format(&b, "Test: %% %д %s %d, %j", vm.ToValue("string"), vm.ToValue(42), vm.NewObject()) 17 18 if res := b.String(); res != "Test: % %д string 42, {}" { 19 t.Fatalf("Unexpected result: '%s'", res) 20 } 21 } 22 23 func TestUtil_Format_NoArgs(t *testing.T) { 24 vm := goja.New() 25 util := New(vm) 26 27 var b bytes.Buffer 28 util.Format(&b, "Test: %s %d, %j") 29 30 if res := b.String(); res != "Test: %s %d, %j" { 31 t.Fatalf("Unexpected result: '%s'", res) 32 } 33 } 34 35 func TestUtil_Format_LessArgs(t *testing.T) { 36 vm := goja.New() 37 util := New(vm) 38 39 var b bytes.Buffer 40 util.Format(&b, "Test: %s %d, %j", vm.ToValue("string"), vm.ToValue(42)) 41 42 if res := b.String(); res != "Test: string 42, %j" { 43 t.Fatalf("Unexpected result: '%s'", res) 44 } 45 } 46 47 func TestUtil_Format_MoreArgs(t *testing.T) { 48 vm := goja.New() 49 util := New(vm) 50 51 var b bytes.Buffer 52 util.Format(&b, "Test: %s %d, %j", vm.ToValue("string"), vm.ToValue(42), vm.NewObject(), vm.ToValue(42.42)) 53 54 if res := b.String(); res != "Test: string 42, {} 42.42" { 55 t.Fatalf("Unexpected result: '%s'", res) 56 } 57 } 58 59 func TestJSNoArgs(t *testing.T) { 60 vm := goja.New() 61 new(require.Registry).Enable(vm) 62 63 if util, ok := require.Require(vm, "util").(*goja.Object); ok { 64 if format, ok := goja.AssertFunction(util.Get("format")); ok { 65 res, err := format(util) 66 if err != nil { 67 t.Fatal(err) 68 } 69 if v := res.Export(); v != "" { 70 t.Fatalf("Unexpected result: %v", v) 71 } 72 } 73 } 74 }