Skip to content

Commit

Permalink
chore(gnovm): move invalid labels tests to tests/files/ (#2058)
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl authored Jun 3, 2024
1 parent f1e46e3 commit 9329980
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 86 deletions.
86 changes: 0 additions & 86 deletions gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,92 +42,6 @@ func BenchmarkStringLargeData(b *testing.B) {
}
}

func TestRunInvalidLabels(t *testing.T) {
tests := []struct {
code string
output string
}{
{
code: `
package test
func main(){}
func invalidLabel() {
FirstLoop:
for i := 0; i < 10; i++ {
}
for i := 0; i < 10; i++ {
break FirstLoop
}
}
`,
output: `cannot find branch label "FirstLoop"`,
},
{
code: `
package test
func main(){}
func undefinedLabel() {
for i := 0; i < 10; i++ {
break UndefinedLabel
}
}
`,
output: `label UndefinedLabel undefined`,
},
{
code: `
package test
func main(){}
func labelOutsideScope() {
for i := 0; i < 10; i++ {
continue FirstLoop
}
FirstLoop:
for i := 0; i < 10; i++ {
}
}
`,
output: `cannot find branch label "FirstLoop"`,
},
{
code: `
package test
func main(){}
func invalidLabelStatement() {
if true {
break InvalidLabel
}
}
`,
output: `label InvalidLabel undefined`,
},
}

for n, s := range tests {
n := n
t.Run(fmt.Sprintf("%v\n", n), func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
es := fmt.Sprintf("%v\n", r)
if !strings.Contains(es, s.output) {
t.Fatalf("invalid label test: `%v` missing expected error: %+v got: %v\n", n, s.output, es)
}
} else {
t.Fatalf("invalid label test: `%v` should have failed but didn't\n", n)
}
}()

m := NewMachine("test", nil)
nn := MustParseFile("main.go", s.code)
m.RunFiles(nn)
m.RunMain()
})
}
}

func TestBuiltinIdentifiersShadowing(t *testing.T) {
t.Parallel()
tests := map[string]string{}
Expand Down
14 changes: 14 additions & 0 deletions gnovm/tests/files/invalid_labels0.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

func main() {}
func invalidLabel() {
FirstLoop:
for i := 0; i < 10; i++ {
}
for i := 0; i < 10; i++ {
break FirstLoop
}
}

// Error:
// main/files/invalid_labels0.gno:9: cannot find branch label "FirstLoop"
12 changes: 12 additions & 0 deletions gnovm/tests/files/invalid_labels1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func main() {}

func undefinedLabel() {
for i := 0; i < 10; i++ {
break UndefinedLabel
}
}

// Error:
// files/invalid_labels1.gno:7:9: label UndefinedLabel undefined
15 changes: 15 additions & 0 deletions gnovm/tests/files/invalid_labels2.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

func main() {}

func labelOutsideScope() {
for i := 0; i < 10; i++ {
continue FirstLoop
}
FirstLoop:
for i := 0; i < 10; i++ {
}
}

// Error:
// main/files/invalid_labels2.gno:7: cannot find branch label "FirstLoop"
12 changes: 12 additions & 0 deletions gnovm/tests/files/invalid_labels3.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func main() {}

func invalidLabelStatement() {
if true {
break InvalidLabel
}
}

// Error:
// files/invalid_labels3.gno:7:9: label InvalidLabel undefined

0 comments on commit 9329980

Please sign in to comment.