MCPcopy
hub / github.com/tinygo-org/tinygo / TestWebAssembly

Function TestWebAssembly

main_test.go:507–563  ·  view source on GitHub ↗

Test WebAssembly files for certain properties.

(t *testing.T)

Source from the content-addressed store, hash-verified

505
506// Test WebAssembly files for certain properties.
507func TestWebAssembly(t *testing.T) {
508 t.Parallel()
509 type testCase struct {
510 name string
511 target string
512 panicStrategy string
513 imports []string
514 }
515 for _, tc := range []testCase{
516 // Test whether there really are no imports when using -panic=trap. This
517 // tests the bugfix for https://github.com/tinygo-org/tinygo/issues/4161.
518 {name: "panic-default", target: "wasip1", imports: []string{"wasi_snapshot_preview1.fd_write", "wasi_snapshot_preview1.random_get"}},
519 {name: "panic-trap", target: "wasm-unknown", panicStrategy: "trap", imports: []string{}},
520 } {
521 tc := tc
522 t.Run(tc.name, func(t *testing.T) {
523 t.Parallel()
524 tmpdir := t.TempDir()
525 options := optionsFromTarget(tc.target, sema)
526 options.PanicStrategy = tc.panicStrategy
527 config, err := builder.NewConfig(&options)
528 if err != nil {
529 t.Fatal(err)
530 }
531
532 result, err := builder.Build("testdata/trivialpanic.go", ".wasm", tmpdir, config)
533 if err != nil {
534 t.Fatal("failed to build binary:", err)
535 }
536 f, err := os.Open(result.Binary)
537 if err != nil {
538 t.Fatal("could not open output binary:", err)
539 }
540 defer f.Close()
541 module, err := wasm.Parse(f)
542 if err != nil {
543 t.Fatal("could not parse output binary:", err)
544 }
545
546 // Test the list of imports.
547 if tc.imports != nil {
548 var imports []string
549 for _, section := range module.Sections {
550 switch section := section.(type) {
551 case *wasm.SectionImport:
552 for _, symbol := range section.Entries {
553 imports = append(imports, symbol.Module+"."+symbol.Field)
554 }
555 }
556 }
557 if !stringSlicesEqual(imports, tc.imports) {
558 t.Errorf("import list not as expected!\nexpected: %v\nactual: %v", tc.imports, imports)
559 }
560 }
561 })
562 }
563}
564

Callers

nothing calls this directly

Calls 12

NewConfigFunction · 0.92
BuildFunction · 0.92
optionsFromTargetFunction · 0.85
stringSlicesEqualFunction · 0.85
ParallelMethod · 0.80
OpenMethod · 0.80
TempDirMethod · 0.65
FatalMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.65
RunMethod · 0.45
ParseMethod · 0.45

Tested by

no test coverage detected