@@ -14,7 +14,7 @@ import (
1414 "github.com/google/go-cmp/cmp"
1515)
1616
17- func TestRepositoriesService_GetRulesForBranch (t * testing.T ) {
17+ func TestRepositoriesService_ListRulesForBranch (t * testing.T ) {
1818 t .Parallel ()
1919 client , mux , _ := setup (t )
2020
@@ -40,9 +40,9 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
4040 })
4141
4242 ctx := t .Context ()
43- rules , _ , err := client .Repositories .GetRulesForBranch (ctx , "o" , "repo" , "branch" , nil )
43+ rules , _ , err := client .Repositories .ListRulesForBranch (ctx , "o" , "repo" , "branch" , nil )
4444 if err != nil {
45- t .Errorf ("Repositories.GetRulesForBranch returned error: %v" , err )
45+ t .Errorf ("Repositories.ListRulesForBranch returned error: %v" , err )
4646 }
4747
4848 want := & BranchRules {
@@ -51,19 +51,89 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
5151 }
5252
5353 if ! cmp .Equal (rules , want ) {
54- t .Errorf ("Repositories.GetRulesForBranch returned %+v, want %+v" , rules , want )
54+ t .Errorf ("Repositories.ListRulesForBranch returned %+v, want %+v" , rules , want )
5555 }
5656
57- const methodName = "GetRulesForBranch "
57+ const methodName = "ListRulesForBranch "
5858 testNewRequestAndDoFailure (t , methodName , client , func () (* Response , error ) {
59- got , resp , err := client .Repositories .GetRulesForBranch (ctx , "o" , "repo" , "branch" , nil )
59+ got , resp , err := client .Repositories .ListRulesForBranch (ctx , "o" , "repo" , "branch" , nil )
6060 if got != nil {
6161 t .Errorf ("testNewRequestAndDoFailure %v = %#v, want nil" , methodName , got )
6262 }
6363 return resp , err
6464 })
6565}
6666
67+ func TestRepositoriesService_ListRulesForBranchIter (t * testing.T ) {
68+ t .Parallel ()
69+ client , mux , _ := setup (t )
70+ var callNum int
71+ mux .HandleFunc ("/" , func (w http.ResponseWriter , _ * http.Request ) {
72+ callNum ++
73+ switch callNum {
74+ case 1 :
75+ w .Header ().Set ("Link" , `<https://api.github.com/?page=1>; rel="next"` )
76+ fmt .Fprint (w , `[{"type":"creation"},{"type":"deletion"},{"type":"update"}]` )
77+ case 2 :
78+ fmt .Fprint (w , `[{"type":"creation"},{"type":"deletion"},{"type":"update"},{"type":"workflows"}]` )
79+ case 3 , 5 :
80+ fmt .Fprint (w , `[{"type":"creation"},{"type":"deletion"}]` )
81+ case 4 :
82+ w .WriteHeader (http .StatusNotFound )
83+ }
84+ })
85+
86+ iter := client .Repositories .ListRulesForBranchIter (t .Context (), "o" , "r" , "b" , nil )
87+ var gotItems int
88+ for _ , err := range iter {
89+ gotItems ++
90+ if err != nil {
91+ t .Errorf ("Unexpected error: %v" , err )
92+ }
93+ }
94+ if want := 7 ; gotItems != want {
95+ t .Errorf ("client.Repositories.ListRulesForBranchIter call 1 got %v items; want %v" , gotItems , want )
96+ }
97+
98+ opts := & ListOptions {}
99+ iter = client .Repositories .ListRulesForBranchIter (t .Context (), "o" , "r" , "b" , opts )
100+ gotItems = 0
101+ for _ , err := range iter {
102+ gotItems ++
103+ if err != nil {
104+ t .Errorf ("Unexpected error: %v" , err )
105+ }
106+ }
107+ if want := 2 ; gotItems != want {
108+ t .Errorf ("client.Repositories.ListRulesForBranchIter call 2 got %v items; want %v" , gotItems , want )
109+ }
110+
111+ iter = client .Repositories .ListRulesForBranchIter (t .Context (), "o" , "r" , "b" , nil )
112+ gotItems = 0
113+ for _ , err := range iter {
114+ gotItems ++
115+ if err == nil {
116+ t .Error ("expected error; got nil" )
117+ }
118+ }
119+ if gotItems != 1 {
120+ t .Errorf ("client.Repositories.ListRulesForBranchIter call 3 got %v items; want 1 (an error)" , gotItems )
121+ }
122+
123+ iter = client .Repositories .ListRulesForBranchIter (t .Context (), "o" , "r" , "b" , nil )
124+ gotItems = 0
125+ iter (func (_ any , err error ) bool {
126+ gotItems ++
127+ if err != nil {
128+ t .Errorf ("Unexpected error: %v" , err )
129+ }
130+ return false
131+ })
132+ if gotItems != 1 {
133+ t .Errorf ("client.Repositories.ListRulesForBranchIter call 4 got %v items; want 1 (an error)" , gotItems )
134+ }
135+ }
136+
67137func TestRepositoriesService_UpdateRuleset_OmitZero_Nil (t * testing.T ) {
68138 t .Parallel ()
69139 client , mux , _ := setup (t )
@@ -132,7 +202,7 @@ func TestRepositoriesService_UpdateRuleset_OmitZero_EmptySlice(t *testing.T) {
132202 }
133203}
134204
135- func TestRepositoriesService_GetRulesForBranch_ListOptions (t * testing.T ) {
205+ func TestRepositoriesService_ListRulesForBranch_ListOptions (t * testing.T ) {
136206 t .Parallel ()
137207 client , mux , _ := setup (t )
138208
@@ -152,27 +222,27 @@ func TestRepositoriesService_GetRulesForBranch_ListOptions(t *testing.T) {
152222
153223 opts := & ListOptions {Page : 2 , PerPage : 35 }
154224 ctx := t .Context ()
155- rules , _ , err := client .Repositories .GetRulesForBranch (ctx , "o" , "repo" , "branch" , opts )
225+ rules , _ , err := client .Repositories .ListRulesForBranch (ctx , "o" , "repo" , "branch" , opts )
156226 if err != nil {
157- t .Errorf ("Repositories.GetRulesForBranch returned error: %v" , err )
227+ t .Errorf ("Repositories.ListRulesForBranch returned error: %v" , err )
158228 }
159229
160230 want := & BranchRules {
161231 Creation : []* BranchRuleMetadata {{RulesetID : 42069 }},
162232 }
163233
164234 if ! cmp .Equal (rules , want ) {
165- t .Errorf ("Repositories.GetRulesForBranch returned %+v, want %+v" , rules , want )
235+ t .Errorf ("Repositories.ListRulesForBranch returned %+v, want %+v" , rules , want )
166236 }
167237
168- const methodName = "GetRulesForBranch "
238+ const methodName = "ListRulesForBranch "
169239 testBadOptions (t , methodName , func () (err error ) {
170- _ , _ , err = client .Repositories .GetRulesForBranch (ctx , "\n " , "\n " , "\n " , opts )
240+ _ , _ , err = client .Repositories .ListRulesForBranch (ctx , "\n " , "\n " , "\n " , opts )
171241 return err
172242 })
173243
174244 testNewRequestAndDoFailure (t , methodName , client , func () (* Response , error ) {
175- got , resp , err := client .Repositories .GetRulesForBranch (ctx , "o" , "repo" , "branch" , opts )
245+ got , resp , err := client .Repositories .ListRulesForBranch (ctx , "o" , "repo" , "branch" , opts )
176246 if got != nil {
177247 t .Errorf ("testNewRequestAndDoFailure %v = %#v, want nil" , methodName , got )
178248 }
0 commit comments