Skip to content

Commit 8d908f7

Browse files
committed
add tests
1 parent a506739 commit 8d908f7

5 files changed

Lines changed: 129 additions & 0 deletions

File tree

‎_fixtures/pkg1/pkg1.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package pkg1
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
)
7+
8+
type Astruct struct {
9+
N int
10+
}
11+
12+
func (a *Astruct) UnreachableMethod() {
13+
fmt.Println("UnreachableMethod")
14+
}
15+
16+
func (a *Astruct) ReflectMethodByName(name string) {
17+
reflect.ValueOf(&Astruct{}).MethodByName(name).Call(nil)
18+
}
19+
20+
func (a *Astruct) M() {
21+
fmt.Println("M", a.N)
22+
}
23+

‎_fixtures/reachmeth.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"github.com/aarzilli/whydeadcode/_fixtures/pkg1"
7+
)
8+
9+
var f = (*pkg1.Astruct).ReflectMethodByName
10+
11+
func main() {
12+
f(&pkg1.Astruct{ 10 }, os.Args[1])
13+
}

‎_fixtures/typemethod.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"reflect"
7+
)
8+
9+
type Bstruct struct {
10+
n float64
11+
}
12+
13+
func (b *Bstruct) M() {
14+
fmt.Println("hello world")
15+
}
16+
17+
func F(name string) {
18+
mmeth, _ := reflect.TypeOf(&Bstruct{}).MethodByName(name)
19+
mmeth.Func.Call(nil)
20+
}
21+
22+
func main() {
23+
F(os.Args[1])
24+
}

‎_fixtures/valuemethod.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"reflect"
7+
)
8+
9+
type Bstruct struct {
10+
n float64
11+
}
12+
13+
func (b *Bstruct) M() {
14+
fmt.Println("hello world")
15+
}
16+
17+
func F(name string) {
18+
reflect.ValueOf(&Bstruct{ 2.0 }).MethodByName(name).Call(nil)
19+
}
20+
21+
func main() {
22+
F(os.Args[1])
23+
}

‎main_test.go‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"testing"
10+
)
11+
12+
func TestWhydeadcode(t *testing.T) {
13+
for _, c := range []struct {
14+
fixtureName string
15+
tgt Path
16+
}{
17+
{"valuemethod", Path{"main.F", "main.main"}},
18+
{"typemethod", Path{"main.F", "main.main"}},
19+
{"reachmeth", Path{"github.com/aarzilli/whydeadcode/_fixtures/pkg1.(*Astruct).ReflectMethodByName", "github.com/aarzilli/whydeadcode/_fixtures/pkg1.(*Astruct).ReflectMethodByName·f", "main.f", "main.main"}},
20+
} {
21+
t.Run(c.fixtureName, func(t *testing.T) {
22+
paths, _ := Whydeadcode(buildFixture(t, c.fixtureName))
23+
t.Logf("%q -> %q", c.fixtureName, paths[0])
24+
if len(paths[0]) < len(c.tgt) {
25+
t.Error("output path not long enough")
26+
}
27+
for i := range c.tgt {
28+
if c.tgt[i] != paths[0][i] {
29+
t.Errorf("mismatch at index %d (expected %q got %q)", i, c.tgt[i], paths[0][i])
30+
break
31+
}
32+
}
33+
})
34+
}
35+
}
36+
37+
func buildFixture(t *testing.T, name string) io.Reader {
38+
t.Helper()
39+
cmd := exec.Command("go", "build", "-o", "_debug", "-ldflags=-dumpdep", filepath.Join("_fixtures", name)+".go")
40+
out, err := cmd.CombinedOutput()
41+
if err != nil {
42+
t.Fatalf("compilation failed for %q: %v", name, err)
43+
}
44+
os.Remove("_debug")
45+
return bytes.NewReader(out)
46+
}

0 commit comments

Comments
 (0)