Debian Codegraph @

github/codesofconduct_test.go

raw
1 // Copyright 2023 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 TestCodesOfConductService_List(t *testing.T) {
18 client, mux, _, teardown := setup()
19 defer teardown()
20
21 mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) {
22 testMethod(t, r, "GET")
23 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview)
24 fmt.Fprint(w, `[{
25 "key": "key",
26 "name": "name",
27 "url": "url"}
28 ]`)
29 })
30
31 ctx := context.Background()
32 cs, _, err := client.ListCodesOfConduct(ctx)
33 assertNilError(t, err)
34
35 want := []*CodeOfConduct{
36 {
37 Key: String("key"),
38 Name: String("name"),
39 URL: String("url"),
40 }}
41 if !cmp.Equal(want, cs) {
42 t.Errorf("returned %+v, want %+v", cs, want)
43 }
44
45 const methodName = "List"
46 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
47 got, resp, err := client.CodesOfConduct.List(ctx)
48 if got != nil {
49 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
50 }
51 return resp, err
52 })
53 }
54
55 func TestCodesOfConductService_Get(t *testing.T) {
56 client, mux, _, teardown := setup()
57 defer teardown()
58
59 mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) {
60 testMethod(t, r, "GET")
61 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview)
62 fmt.Fprint(w, `{
63 "key": "key",
64 "name": "name",
65 "url": "url",
66 "body": "body"}`,
67 )
68 })
69
70 ctx := context.Background()
71 coc, _, err := client.GetCodeOfConduct(ctx, "k")
72 assertNilError(t, err)
73
74 want := &CodeOfConduct{
75 Key: String("key"),
76 Name: String("name"),
77 URL: String("url"),
78 Body: String("body"),
79 }
80 if !cmp.Equal(want, coc) {
81 t.Errorf("returned %+v, want %+v", coc, want)
82 }
83
84 const methodName = "Get"
85 testBadOptions(t, methodName, func() (err error) {
86 _, _, err = client.CodesOfConduct.Get(ctx, "\n")
87 return err
88 })
89
90 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
91 got, resp, err := client.CodesOfConduct.Get(ctx, "k")
92 if got != nil {
93 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
94 }
95 return resp, err
96 })
97 }
98
99 func TestCodeOfConduct_Marshal(t *testing.T) {
100 testJSONMarshal(t, &CodeOfConduct{}, "{}")
101
102 a := &CodeOfConduct{
103 Name: String("name"),
104 Key: String("key"),
105 URL: String("url"),
106 Body: String("body"),
107 }
108
109 want := `{
110 "name": "name",
111 "key": "key",
112 "url": "url",
113 "body": "body"
114 }`
115
116 testJSONMarshal(t, a, want)
117 }