pingcap / failpoint

An implementation of failpoints for Golang.
Apache License 2.0
817 stars 63 forks source link

code: support rewrite ExprStmt Init in forStmt #22

Closed amyangfei closed 5 years ago

amyangfei commented 5 years ago

What problem does this PR solve?

initialization statement in for loop could be an ast.ExprStmt, such as following code snippet

package hello

import (
        "fmt"

        "github.com/pingcap/failpoint"
)

type Iterator struct {
        count int
        max   int
}

func (i *Iterator) Begin(fn func()) int {
        i.count = 0
        return i.count
}

func (i *Iterator) Next() int {
        if i.count >= i.max {
                panic("iterator to end")
        }
        i.count++
        return i.count
}

func (i *Iterator) End() bool {
        return i.count == i.max
}

func Hello() {
        iter := &Iterator{max: 10}
        for iter.Begin(func() {
                failpoint.Inject("fptest-in-pkg", func(val failpoint.Value) {
                        fmt.Printf("inject value: %v\n", val)
                })
        }); !iter.End(); {
                i := iter.Next()
                fmt.Printf("get value: %d\n", i)
        }
}

What is changed and how it works?

add support to it

Check List

Tests

codecov[bot] commented 5 years ago

Codecov Report

Merging #22 into master will increase coverage by 0.3053%. The diff coverage is 100%.

@@               Coverage Diff                @@
##             master        #22        +/-   ##
================================================
+ Coverage   86.9213%   87.2266%   +0.3054%     
================================================
  Files             7          7                
  Lines           864        869         +5     
================================================
+ Hits            751        758         +7     
+ Misses           72         70         -2     
  Partials         41         41
amyangfei commented 5 years ago

PTAL @lonng @kennytm

amyangfei commented 5 years ago

PTAL @kennytm