| 1 | // Copyright 2023 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package interp_test |
| 6 | |
| 7 | import ( |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "golang.org/x/tools/internal/testenv" |
| 14 | ) |
| 15 | |
| 16 | func TestIssue69298(t *testing.T) { |
| 17 | testenv.NeedsGo1Point(t, 23) |
| 18 | |
| 19 | goroot := makeGoroot(t) |
| 20 | run(t, filepath.Join("testdata", "fixedbugs", "issue69298.go"), goroot) |
| 21 | } |
| 22 | |
| 23 | func TestRangeFunc(t *testing.T) { |
| 24 | testenv.NeedsGo1Point(t, 23) |
| 25 | |
| 26 | goroot := makeGoroot(t) |
| 27 | out := run(t, filepath.Join("testdata", "rangefunc.go"), goroot) |
| 28 | |
| 29 | // Check the output of the tests. |
| 30 | const ( |
| 31 | RERR_DONE = "Saw expected panic: yield function called after range loop exit" |
| 32 | RERR_MISSING = "Saw expected panic: iterator call did not preserve panic" |
| 33 | RERR_EXHAUSTED = RERR_DONE // ssa does not distinguish. Same message as RERR_DONE. |
| 34 | |
| 35 | CERR_DONE = "Saw expected panic: checked rangefunc error: loop iteration after body done" |
| 36 | CERR_EXHAUSTED = "Saw expected panic: checked rangefunc error: loop iteration after iterator exit" |
| 37 | CERR_MISSING = "Saw expected panic: checked rangefunc error: loop iterator swallowed panic" |
| 38 | |
| 39 | panickyIterMsg = "Saw expected panic: Panicky iterator panicking" |
| 40 | ) |
| 41 | expected := map[string][]string{ |
| 42 | // rangefunc.go |
| 43 | "TestCheck": {"i = 45", CERR_DONE}, |
| 44 | "TestCooperativeBadOfSliceIndex": {RERR_EXHAUSTED, "i = 36"}, |
| 45 | "TestCooperativeBadOfSliceIndexCheck": {CERR_EXHAUSTED, "i = 36"}, |
| 46 | "TestTrickyIterAll": {"i = 36", RERR_EXHAUSTED}, |
| 47 | "TestTrickyIterOne": {"i = 1", RERR_EXHAUSTED}, |
| 48 | "TestTrickyIterZero": {"i = 0", RERR_EXHAUSTED}, |
| 49 | "TestTrickyIterZeroCheck": {"i = 0", CERR_EXHAUSTED}, |
| 50 | "TestTrickyIterEcho": { |
| 51 | "first loop i=0", |
| 52 | "first loop i=1", |
| 53 | "first loop i=3", |
| 54 | "first loop i=6", |
| 55 | "i = 10", |
| 56 | "second loop i=0", |
| 57 | RERR_EXHAUSTED, |
| 58 | "end i=0", |
| 59 | }, |
| 60 | "TestTrickyIterEcho2": { |
| 61 | "k=0,x=1,i=0", |
| 62 | "k=0,x=2,i=1", |
| 63 | "k=0,x=3,i=3", |
| 64 | "k=0,x=4,i=6", |
| 65 | "i = 10", |
| 66 | "k=1,x=1,i=0", |
| 67 | RERR_EXHAUSTED, |
| 68 | "end i=1", |
| 69 | }, |
| 70 | "TestBreak1": {"[1 2 -1 1 2 -2 1 2 -3]"}, |
| 71 | "TestBreak2": {"[1 2 -1 1 2 -2 1 2 -3]"}, |
| 72 | "TestContinue": {"[-1 1 2 -2 1 2 -3 1 2 -4]"}, |
| 73 | "TestBreak3": {"[100 10 2 4 200 10 2 4 20 2 4 300 10 2 4 20 2 4 30]"}, |
| 74 | "TestBreak1BadA": {"[1 2 -1 1 2 -2 1 2 -3]", RERR_DONE}, |
| 75 | "TestBreak1BadB": {"[1 2]", RERR_DONE}, |
| 76 | "TestMultiCont0": {"[1000 10 2 4 2000]"}, |
| 77 | "TestMultiCont1": {"[1000 10 2 4]", RERR_DONE}, |
| 78 | "TestMultiCont2": {"[1000 10 2 4]", RERR_DONE}, |
| 79 | "TestMultiCont3": {"[1000 10 2 4]", RERR_DONE}, |
| 80 | "TestMultiBreak0": {"[1000 10 2 4]", RERR_DONE}, |
| 81 | "TestMultiBreak1": {"[1000 10 2 4]", RERR_DONE}, |
| 82 | "TestMultiBreak2": {"[1000 10 2 4]", RERR_DONE}, |
| 83 | "TestMultiBreak3": {"[1000 10 2 4]", RERR_DONE}, |
| 84 | "TestPanickyIterator1": {panickyIterMsg}, |
| 85 | "TestPanickyIterator1Check": {panickyIterMsg}, |
| 86 | "TestPanickyIterator2": {RERR_MISSING}, |
| 87 | "TestPanickyIterator2Check": {CERR_MISSING}, |
| 88 | "TestPanickyIterator3": {"[100 10 1 2 200 10 1 2]"}, |
| 89 | "TestPanickyIterator3Check": {"[100 10 1 2 200 10 1 2]"}, |
| 90 | "TestPanickyIterator4": {RERR_MISSING}, |
| 91 | "TestPanickyIterator4Check": {CERR_MISSING}, |
| 92 | "TestVeryBad1": {"[1 10]"}, |
| 93 | "TestVeryBad2": {"[1 10]"}, |
| 94 | "TestVeryBadCheck": {"[1 10]"}, |
| 95 | "TestOk": {"[1 10]"}, |
| 96 | "TestBreak1BadDefer": {RERR_DONE, "[1 2 -1 1 2 -2 1 2 -3 -30 -20 -10]"}, |
| 97 | "TestReturns": {"[-1 1 2 -10]", "[-1 1 2 -10]", RERR_DONE, "[-1 1 2 -10]", RERR_DONE}, |
| 98 | "TestGotoA": {"testGotoA1[-1 1 2 -2 1 2 -3 1 2 -4 -30 -20 -10]", "testGotoA2[-1 1 2 -2 1 2 -3 1 2 -4 -30 -20 -10]", RERR_DONE, "testGotoA3[-1 1 2 -10]", RERR_DONE}, |
| 99 | "TestGotoB": {"testGotoB1[-1 1 2 999 -10]", "testGotoB2[-1 1 2 -10]", RERR_DONE, "testGotoB3[-1 1 2 -10]", RERR_DONE}, |
| 100 | "TestPanicReturns": { |
| 101 | "Got expected 'f return'", |
| 102 | "Got expected 'g return'", |
| 103 | "Got expected 'h return'", |
| 104 | "Got expected 'k return'", |
| 105 | "Got expected 'j return'", |
| 106 | "Got expected 'm return'", |
| 107 | "Got expected 'n return and n closure return'", |
| 108 | }, |
| 109 | } |
| 110 | got := make(map[string][]string) |
| 111 | for ln := range strings.SplitSeq(out, "\n") { |
| 112 | if n, m, ok := strings.Cut(ln, " \t "); ok { |
| 113 | got[n] = append(got[n], m) |
| 114 | } |
| 115 | } |
| 116 | for n, es := range expected { |
| 117 | if gs := got[n]; !reflect.DeepEqual(es, gs) { |
| 118 | t.Errorf("Output of test %s did not match expected output %v. got %v", n, es, gs) |
| 119 | } |
| 120 | } |
| 121 | for n, gs := range got { |
| 122 | if expected[n] == nil { |
| 123 | t.Errorf("No expected output for test %s. got %v", n, gs) |
| 124 | } |
| 125 | } |
| 126 | } |