github.com/google/go-github/v49@v49.1.0/github/gitignore_test.go (about) 1 // Copyright 2013 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 "net/http" 12 "testing" 13 14 "github.com/google/go-cmp/cmp" 15 ) 16 17 func TestGitignoresService_List(t *testing.T) { 18 client, mux, _, teardown := setup() 19 defer teardown() 20 21 mux.HandleFunc("/gitignore/templates", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 fmt.Fprint(w, `["C", "Go"]`) 24 }) 25 26 ctx := context.Background() 27 available, _, err := client.Gitignores.List(ctx) 28 if err != nil { 29 t.Errorf("Gitignores.List returned error: %v", err) 30 } 31 32 want := []string{"C", "Go"} 33 if !cmp.Equal(available, want) { 34 t.Errorf("Gitignores.List returned %+v, want %+v", available, want) 35 } 36 37 const methodName = "List" 38 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 39 got, resp, err := client.Gitignores.List(ctx) 40 if got != nil { 41 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 42 } 43 return resp, err 44 }) 45 } 46 47 func TestGitignoresService_Get(t *testing.T) { 48 client, mux, _, teardown := setup() 49 defer teardown() 50 51 mux.HandleFunc("/gitignore/templates/name", func(w http.ResponseWriter, r *http.Request) { 52 testMethod(t, r, "GET") 53 fmt.Fprint(w, `{"name":"Name","source":"template source"}`) 54 }) 55 56 ctx := context.Background() 57 gitignore, _, err := client.Gitignores.Get(ctx, "name") 58 if err != nil { 59 t.Errorf("Gitignores.List returned error: %v", err) 60 } 61 62 want := &Gitignore{Name: String("Name"), Source: String("template source")} 63 if !cmp.Equal(gitignore, want) { 64 t.Errorf("Gitignores.Get returned %+v, want %+v", gitignore, want) 65 } 66 67 const methodName = "Get" 68 testBadOptions(t, methodName, func() (err error) { 69 _, _, err = client.Gitignores.Get(ctx, "\n") 70 return err 71 }) 72 73 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 74 got, resp, err := client.Gitignores.Get(ctx, "name") 75 if got != nil { 76 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 77 } 78 return resp, err 79 }) 80 } 81 82 func TestGitignoresService_Get_invalidTemplate(t *testing.T) { 83 client, _, _, teardown := setup() 84 defer teardown() 85 86 ctx := context.Background() 87 _, _, err := client.Gitignores.Get(ctx, "%") 88 testURLParseError(t, err) 89 } 90 91 func TestGitignore_Marshal(t *testing.T) { 92 testJSONMarshal(t, &Gitignore{}, "{}") 93 94 u := &Gitignore{ 95 Name: String("name"), 96 Source: String("source"), 97 } 98 99 want := `{ 100 "name": "name", 101 "source": "source" 102 }` 103 104 testJSONMarshal(t, u, want) 105 }