Test WebAssembly files for certain properties.
(t *testing.T)
| 505 | |
| 506 | // Test WebAssembly files for certain properties. |
| 507 | func 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 |
nothing calls this directly
no test coverage detected