This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
gocov_test.go
138 lines (124 loc) · 3.98 KB
/
gocov_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) 2012 The Gocov Authors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
package gocov
import "testing"
func registerPackage(name string) *Package {
return &Package{Name: name}
}
func registerFunction(p *Package, name, file string, startOffset, endOffset int) *Function {
f := &Function{Name: name, File: file, Start: startOffset, End: endOffset}
p.Functions = append(p.Functions, f)
return f
}
func registerStatement(f *Function, startOffset, endOffset int) *Statement {
s := &Statement{Start: startOffset, End: endOffset}
f.Statements = append(f.Statements, s)
return s
}
func TestAccumulatePackage(t *testing.T) {
p1_1 := registerPackage("p1")
p1_2 := registerPackage("p1")
p2 := registerPackage("p2")
p3 := registerPackage("p1")
registerFunction(p3, "f", "file.go", 0, 1)
p4 := registerPackage("p1")
registerFunction(p4, "f", "file.go", 1, 2)
var tests = [...]struct {
a, b *Package
expectPass bool
}{
// Should work: everything is the same.
{p1_1, p1_2, true},
// Should fail: name is different.
{p1_1, p2, false},
// Should fail: numbers of functions are different.
{p1_1, p3, false},
// Should fail: functions are different.
{p3, p4, false},
}
for _, test := range tests {
err := test.a.Accumulate(test.b)
if test.expectPass {
if err != nil {
t.Error(err)
}
} else {
if err == nil {
t.Error("Expected an error")
}
}
}
}
func TestAccumulateFunction(t *testing.T) {
p := registerPackage("p1")
f1_1 := registerFunction(p, "f1", "file.go", 0, 1)
f1_2 := registerFunction(p, "f1", "file.go", 0, 1)
f2 := registerFunction(p, "f2", "file.go", 0, 1)
f3 := registerFunction(p, "f1", "file2.go", 0, 1)
f4 := registerFunction(p, "f1", "file.go", 2, 3)
f5 := registerFunction(p, "f1", "file.go", 0, 1)
registerStatement(f5, 0, 1)
f6 := registerFunction(p, "f1", "file.go", 0, 1)
registerStatement(f6, 2, 3)
var tests = [...]struct {
a, b *Function
expectPass bool
}{
// Should work: everything is the same.
{f1_1, f1_2, true},
// Should fail: names are different.
{f1_1, f2, false},
// Should fail: files are different.
{f1_1, f3, false},
// Should fail: ranges are different.
{f1_1, f4, false},
// Should fail: numbers of statements are different.
{f1_1, f5, false},
// Should fail: all the same, except statement values.
{f5, f6, false},
}
for _, test := range tests {
err := test.a.Accumulate(test.b)
if test.expectPass {
if err != nil {
t.Error(err)
}
} else {
if err == nil {
t.Error("Expected an error")
}
}
}
}
func TestAccumulateStatement(t *testing.T) {
p := registerPackage("p1")
f := registerFunction(p, "f1", "file.go", 0, 1)
s1_1 := registerStatement(f, 0, 1)
s1_2 := registerStatement(f, 0, 1)
s2 := registerStatement(f, 2, 3)
// Should work: ranges are the same.
if err := s1_1.Accumulate(s1_2); err != nil {
t.Error(err)
}
// Should fail: ranges are not the same.
if err := s1_1.Accumulate(s2); err == nil {
t.Errorf("Expected an error")
}
}