1
+ // Copyright 2022 The Gogs Authors. All rights reserved.
2
+ // Use of this source code is governed by a MIT-style
3
+ // license that can be found in the LICENSE file.
4
+
1
5
package git
2
6
3
7
import (
@@ -6,94 +10,130 @@ import (
6
10
"github.com/stretchr/testify/assert"
7
11
)
8
12
9
- func TestRepoGrepSimple (t * testing.T ) {
10
- pattern := "programmingPoints"
11
- expect := []GrepResult {
12
- {
13
- TreeID : "HEAD" , Path : "src/Main.groovy" , Line : 7 , Column : 5 , Text : "int programmingPoints = 10" ,
14
- },
15
- {
16
- TreeID : "HEAD" , Path : "src/Main.groovy" , Line : 10 , Column : 33 , Text : `println "${name} has at least ${programmingPoints} programming points."` ,
17
- },
18
- {
19
- TreeID : "HEAD" , Path : "src/Main.groovy" , Line : 11 , Column : 12 , Text : `println "${programmingPoints} squared is ${square(programmingPoints)}"` ,
20
- },
21
- {
22
- TreeID : "HEAD" , Path : "src/Main.groovy" , Line : 12 , Column : 12 , Text : `println "${programmingPoints} divided by 2 bonus points is ${divide(programmingPoints, 2)}"` ,
23
- },
24
- {
25
- TreeID : "HEAD" , Path : "src/Main.groovy" , Line : 13 , Column : 12 , Text : `println "${programmingPoints} minus 7 bonus points is ${subtract(programmingPoints, 7)}"` ,
26
- },
27
- {
28
- TreeID : "HEAD" , Path : "src/Main.groovy" , Line : 14 , Column : 12 , Text : `println "${programmingPoints} plus 3 bonus points is ${sum(programmingPoints, 3)}"` ,
13
+ func TestRepository_Grep_Simple (t * testing.T ) {
14
+ want := []* GrepResult {
15
+ {
16
+ Tree : "HEAD" ,
17
+ Path : "src/Main.groovy" ,
18
+ Line : 7 ,
19
+ Column : 5 ,
20
+ Text : "int programmingPoints = 10" ,
21
+ }, {
22
+ Tree : "HEAD" ,
23
+ Path : "src/Main.groovy" ,
24
+ Line : 10 ,
25
+ Column : 33 ,
26
+ Text : `println "${name} has at least ${programmingPoints} programming points."` ,
27
+ }, {
28
+ Tree : "HEAD" ,
29
+ Path : "src/Main.groovy" ,
30
+ Line : 11 ,
31
+ Column : 12 ,
32
+ Text : `println "${programmingPoints} squared is ${square(programmingPoints)}"` ,
33
+ }, {
34
+ Tree : "HEAD" ,
35
+ Path : "src/Main.groovy" ,
36
+ Line : 12 ,
37
+ Column : 12 ,
38
+ Text : `println "${programmingPoints} divided by 2 bonus points is ${divide(programmingPoints, 2)}"` ,
39
+ }, {
40
+ Tree : "HEAD" ,
41
+ Path : "src/Main.groovy" ,
42
+ Line : 13 ,
43
+ Column : 12 ,
44
+ Text : `println "${programmingPoints} minus 7 bonus points is ${subtract(programmingPoints, 7)}"` ,
45
+ }, {
46
+ Tree : "HEAD" ,
47
+ Path : "src/Main.groovy" ,
48
+ Line : 14 ,
49
+ Column : 12 ,
50
+ Text : `println "${programmingPoints} plus 3 bonus points is ${sum(programmingPoints, 3)}"` ,
29
51
},
30
52
}
31
- results , err := testrepo .Grep (pattern )
32
- assert .NoError (t , err )
33
- for i , result := range results {
34
- assert .Equal (t , expect [i ], * result )
35
- }
53
+ got := testrepo .Grep ("programmingPoints" )
54
+ assert .Equal (t , want , got )
36
55
}
37
56
38
- func TestRepoGrepIgnoreCase (t * testing.T ) {
39
- pattern := "Hello"
40
- expect := []GrepResult {
41
- {
42
- TreeID :
"HEAD" ,
Path :
"README.txt" ,
Line :
9 ,
Column :
36 ,
Text :
"* [email protected] :matthewmccullough/hellogitworld.git" ,
43
- },
44
- {
45
- TreeID : "HEAD" , Path : "README.txt" , Line : 10 , Column : 38 , Text : "* git://github.com/matthewmccullough/hellogitworld.git" ,
46
- },
47
- {
48
- TreeID :
"HEAD" ,
Path :
"README.txt" ,
Line :
11 ,
Column :
58 ,
Text :
"* https://[email protected] /matthewmccullough/hellogitworld.git" ,
49
- },
50
- {
51
- TreeID : "HEAD" , Path : "src/Main.groovy" , Line : 9 , Column : 10 , Text : `println "Hello ${name}"` ,
52
- },
53
- {
54
- TreeID : "HEAD" , Path : "src/main/java/com/github/App.java" , Line : 4 , Column : 4 , Text : " * Hello again" ,
55
- },
56
- {
57
- TreeID : "HEAD" , Path : "src/main/java/com/github/App.java" , Line : 5 , Column : 4 , Text : " * Hello world!" ,
58
- },
59
- {
60
- TreeID : "HEAD" , Path : "src/main/java/com/github/App.java" , Line : 6 , Column : 4 , Text : " * Hello" ,
61
- },
62
- {
63
- TreeID : "HEAD" , Path : "src/main/java/com/github/App.java" , Line : 13 , Column : 30 , Text : ` System.out.println( "Hello World!" );` ,
57
+ func TestRepository_Grep_IgnoreCase (t * testing.T ) {
58
+ want := []* GrepResult {
59
+ {
60
+ Tree : "HEAD" ,
61
+ Path : "README.txt" ,
62
+ Line : 9 ,
63
+ Column : 36 ,
64
+ Text :
"* [email protected] :matthewmccullough/hellogitworld.git" ,
65
+ }, {
66
+ Tree : "HEAD" ,
67
+ Path : "README.txt" ,
68
+ Line : 10 ,
69
+ Column : 38 ,
70
+ Text : "* git://github.com/matthewmccullough/hellogitworld.git" ,
71
+ }, {
72
+ Tree : "HEAD" ,
73
+ Path : "README.txt" ,
74
+ Line : 11 ,
75
+ Column : 58 ,
76
+ Text :
"* https://[email protected] /matthewmccullough/hellogitworld.git" ,
77
+ }, {
78
+ Tree : "HEAD" ,
79
+ Path : "src/Main.groovy" ,
80
+ Line : 9 ,
81
+ Column : 10 ,
82
+ Text : `println "Hello ${name}"` ,
83
+ }, {
84
+ Tree : "HEAD" ,
85
+ Path : "src/main/java/com/github/App.java" ,
86
+ Line : 4 ,
87
+ Column : 4 ,
88
+ Text : " * Hello again" ,
89
+ }, {
90
+ Tree : "HEAD" ,
91
+ Path : "src/main/java/com/github/App.java" ,
92
+ Line : 5 ,
93
+ Column : 4 ,
94
+ Text : " * Hello world!" ,
95
+ }, {
96
+ Tree : "HEAD" ,
97
+ Path : "src/main/java/com/github/App.java" ,
98
+ Line : 6 ,
99
+ Column : 4 ,
100
+ Text : " * Hello" ,
101
+ }, {
102
+ Tree : "HEAD" ,
103
+ Path : "src/main/java/com/github/App.java" ,
104
+ Line : 13 ,
105
+ Column : 30 ,
106
+ Text : ` System.out.println( "Hello World!" );` ,
64
107
},
65
108
}
66
- results , err := testrepo .Grep (pattern , GrepOptions {IgnoreCase : true })
67
- assert .NoError (t , err )
68
- for i , result := range results {
69
- assert .Equal (t , expect [i ], * result )
70
- }
109
+ got := testrepo .Grep ("Hello" , GrepOptions {IgnoreCase : true })
110
+ assert .Equal (t , want , got )
71
111
}
72
112
73
- func TestRepoGrepRegex (t * testing.T ) {
74
- pattern := "Hello\\ sW\\ w+"
75
- expect := []GrepResult {
113
+ func TestRepository_Grep_ExtendedRegexp (t * testing.T ) {
114
+ want := []* GrepResult {
76
115
{
77
- TreeID : "HEAD" , Path : "src/main/java/com/github/App.java" , Line : 13 , Column : 30 , Text : ` System.out.println( "Hello World!" );` ,
116
+ Tree : "HEAD" ,
117
+ Path : "src/main/java/com/github/App.java" ,
118
+ Line : 5 ,
119
+ Column : 4 ,
120
+ Text : ` * Hello world!` ,
78
121
},
79
122
}
80
- results , err := testrepo .Grep (pattern , GrepOptions {ExtendedRegex : true })
81
- assert .NoError (t , err )
82
- for i , result := range results {
83
- assert .Equal (t , expect [i ], * result )
84
- }
123
+ got := testrepo .Grep (`Hello \w+` , GrepOptions {ExtendedRegexp : true })
124
+ assert .Equal (t , want , got )
85
125
}
86
126
87
- func TestRepoGrepWord (t * testing.T ) {
88
- pattern := "Hello\\ sW\\ w+"
89
- expect := []GrepResult {
127
+ func TestRepository_Grep_WordRegexp (t * testing.T ) {
128
+ want := []* GrepResult {
90
129
{
91
- TreeID : "HEAD" , Path : "src/main/java/com/github/App.java" , Line : 13 , Column : 36 , Text : ` System.out.println( "Hello World!" );` ,
130
+ Tree : "HEAD" ,
131
+ Path : "src/main/java/com/github/App.java" ,
132
+ Line : 5 ,
133
+ Column : 10 ,
134
+ Text : ` * Hello world!` ,
92
135
},
93
136
}
94
- results , err := testrepo .Grep (pattern , GrepOptions {WordMatch : true })
95
- assert .NoError (t , err )
96
- for i , result := range results {
97
- assert .Equal (t , expect [i ], * result )
98
- }
137
+ got := testrepo .Grep ("world" , GrepOptions {WordRegexp : true })
138
+ assert .Equal (t , want , got )
99
139
}
0 commit comments