Skip to content

Commit 647ab01

Browse files
committed
Add more tests for Golang DevRank
1 parent 074c49e commit 647ab01

File tree

22 files changed

+429
-22
lines changed

22 files changed

+429
-22
lines changed

persper/analytics/go.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def get_graph(self):
4242
r = requests.get(graph_url)
4343
graph_data = r.json()
4444
graph_data['directed'] = True
45-
graph_data['multigraph'] = True
45+
graph_data['multigraph'] = False
4646
return CallCommitGraph(graph_data)
4747

4848
def reset_graph(self):

test/go_test_repo/D/main.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
import(
3+
"fmt"
4+
"math"
5+
)
6+
type a func()
7+
8+
type Vertex struct {
9+
X, Y float64
10+
}
11+
12+
func (v Vertex) Abs() float64 {
13+
return math.Sqrt(v.X*v.X + v.Y*v.Y)
14+
}
15+
func (v *Vertex) Absp() float64{
16+
return math.Sqrt(v.X*v.X + v.Y*v.Y)
17+
}
18+
19+
func funcA () {
20+
fmt.Println("func A is called!")
21+
}
22+
23+
func main() {
24+
a := funcA
25+
a()
26+
v := Vertex{3, 4}
27+
fmt.Println(v.Abs())
28+
p := &Vertex{4,5}
29+
fmt.Println(p.Absp())
30+
}
31+

test/go_test_repo/cg.dot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
digraph go_test_repo {
2-
A -> B -> C;
2+
A -> B -> C -> D;
33
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package sm
2+
3+
func Add(a int, b int) int {
4+
return a + b
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package sm
2+
3+
import "testing"
4+
5+
func TestAdd1(t *testing.T) {
6+
r := Add(1, 2)
7+
8+
if r != 3 {
9+
t.Errorf("Add(1, 2) failed. Got %d, expected 3.", r)
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package sm
2+
3+
import "math"
4+
5+
func Sqrt(i int) int {
6+
v := math.Sqrt(float64(i))
7+
return int(v)
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package sm
2+
3+
import "testing"
4+
5+
func TestSqrt1(t *testing.T) {
6+
v := Sqrt(16)
7+
8+
if v != 4 {
9+
t.Errorf("Sqrt(16) failed. Got %v, expected 4.", v)
10+
}
11+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import ("os"
4+
"fmt"
5+
"strconv"
6+
"gitlab.com/meri.co/test/calcproj/src/simplemath")
7+
8+
var Usage = func() {
9+
fmt.Println("USAGE: calc command [arguments] ...")
10+
fmt.Println("\nThe commands are:\n\tadd\tAddition of two values.\n\tsqrt\tSquare root of a non-negative value.")
11+
}
12+
13+
func main() {
14+
args := os.Args
15+
if args == nil || len(args) < 2 {
16+
Usage()
17+
return
18+
}
19+
20+
switch args[1] {
21+
case "add":
22+
if len(args) != 4 {
23+
fmt.Println("USAGE: calc add <integer1> <integer2>")
24+
return
25+
}
26+
27+
v1, err1 := strconv.Atoi(args[2])
28+
v2, err2 := strconv.Atoi(args[3])
29+
if err1 != nil || err2 != nil {
30+
fmt.Println("USAGE: calc add <integer1> <integer2>")
31+
return
32+
}
33+
34+
ret := sm.Add(v1, v2)
35+
fmt.Println("Result: ", ret)
36+
case "sqrt":
37+
if len(args) != 3 {
38+
fmt.Println("USAGE: calc sqrt <integer>")
39+
return
40+
}
41+
42+
v, err := strconv.Atoi(args[2])
43+
if err != nil {
44+
fmt.Println("USAGE: calc sqrt <integer>")
45+
return
46+
}
47+
48+
ret := sm.Sqrt(v)
49+
fmt.Println("Result: ", ret)
50+
default:
51+
Usage()
52+
}
53+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package sm
2+
3+
func Add(a int, b int) int {
4+
return a + b
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package sm
2+
3+
import "testing"
4+
5+
func TestAdd1(t *testing.T) {
6+
r := Add(1, 2)
7+
8+
if r != 3 {
9+
t.Errorf("Add(1, 2) failed. Got %d, expected 3.", r)
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package sm
2+
3+
import "math"
4+
5+
func Sqrt(i int) int {
6+
v := math.Sqrt(float64(i))
7+
return int(v)
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package sm
2+
3+
import "testing"
4+
5+
func TestSqrt1(t *testing.T) {
6+
v := Sqrt(16)
7+
8+
if v != 4 {
9+
t.Errorf("Sqrt(16) failed. Got %v, expected 4.", v)
10+
}
11+
}

test/go_test_repo_1/cg.dot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
digraph go_test_repo {
2+
A -> B ;
3+
}

test/go_test_repo_2/A/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
import(
3+
"fmt"
4+
)
5+
//This test code is used for test interface function call
6+
type animal interface {
7+
printInfo()
8+
}
9+
10+
type cat int
11+
type dog int
12+
13+
func (c cat) printInfo(){
14+
fmt.Println("a cat")
15+
}
16+
17+
func (d dog) printInfo(){
18+
fmt.Println("a dog")
19+
}
20+
21+
func main() {
22+
var a animal
23+
var c cat
24+
a=c
25+
a.printInfo()
26+
//other type
27+
var d dog
28+
a=d
29+
a.printInfo()
30+
}

test/go_test_repo_2/B/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
import(
3+
"fmt"
4+
)
5+
//This test code is used for test interface function call
6+
type animal interface {
7+
printInfo()
8+
}
9+
10+
type cat int
11+
type dog int
12+
func (c cat) printInfo(){
13+
fmt.Println("a cat")
14+
}
15+
16+
func (c dog) printInfo(){
17+
fmt.Println("a dog")
18+
}
19+
func invoke(a animal){
20+
a.printInfo()
21+
}
22+
func main() {
23+
var c cat
24+
var d dog
25+
//as value convert
26+
invoke(c)
27+
invoke(d)
28+
}

test/go_test_repo_2/cg.dot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
digraph go_test_repo {
2+
A -> B ;
3+
}

test/test_analytics/test_analyzer.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import pytest
33
import subprocess
4+
import shutil
45
from persper.analytics.c import CGraphServer
56
from persper.analytics.analyzer import Analyzer
67
from persper.analytics.graph_server import C_FILENAME_REGEXES
@@ -13,9 +14,13 @@ def az():
1314
repo_path = os.path.join(root_path, 'repos/test_feature_branch')
1415
script_path = os.path.join(root_path, 'tools/repo_creater/create_repo.py')
1516
test_src_path = os.path.join(root_path, 'test/test_feature_branch')
16-
if not os.path.isdir(repo_path):
17-
cmd = '{} {}'.format(script_path, test_src_path)
18-
subprocess.call(cmd, shell=True)
17+
18+
# Always use latest source to create test repo
19+
if os.path.exists(repo_path):
20+
shutil.rmtree(repo_path)
21+
22+
cmd = '{} {}'.format(script_path, test_src_path)
23+
subprocess.call(cmd, shell=True)
1924

2025
return Analyzer(repo_path, CGraphServer(C_FILENAME_REGEXES))
2126

test/test_analytics/test_analyzer_go.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import time
33
import pytest
4+
import shutil
45
import subprocess
56
from persper.analytics.graph_server import GO_FILENAME_REGEXES
67
from persper.analytics.go import GoGraphServer
@@ -25,9 +26,12 @@ def az():
2526
test_src_path = os.path.join(root_path, 'test/go_test_repo')
2627
server_addr = 'http://localhost:%d' % server_port
2728

28-
if not os.path.isdir(repo_path):
29-
cmd = '{} {}'.format(script_path, test_src_path)
30-
subprocess.call(cmd, shell=True)
29+
# Always use latest source to create test repo
30+
if os.path.exists(repo_path):
31+
shutil.rmtree(repo_path)
32+
33+
cmd = '{} {}'.format(script_path, test_src_path)
34+
subprocess.call(cmd, shell=True)
3135

3236
return Analyzer(repo_path, GoGraphServer(server_addr, GO_FILENAME_REGEXES))
3337

@@ -38,6 +42,10 @@ def test_analzyer_go(az):
3842
ccgraph = az.get_graph()
3943

4044
history_truth = {
45+
'D': {'Abs': 6,
46+
'funcA': 0,
47+
'main': 8,
48+
"Absp": 3},
4149
'C': {'Abs': 5,
4250
'funcA': 0,
4351
'funcB': 1,
@@ -78,5 +86,11 @@ def test_analzyer_go(az):
7886
('funcB', 'funcA')
7987
])
8088

81-
all_edges = edges_added_by_A.union(edges_added_by_B).union(edges_added_by_C)
89+
edges_added_by_D = set([
90+
("Absp", "Sqrt"),
91+
("main", "Absp")
92+
])
93+
94+
print(set(az._graph_server.get_graph().edges()))
95+
all_edges = edges_added_by_A.union(edges_added_by_B).union(edges_added_by_C).union(edges_added_by_D)
8296
assert(set(az._graph_server.get_graph().edges()) == all_edges)

0 commit comments

Comments
 (0)