1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package atomic
22
23 import (
24 "bytes"
25 "os"
26 "os/exec"
27 "path/filepath"
28 "reflect"
29 "testing"
30
31 "github.com/stretchr/testify/assert"
32 "github.com/stretchr/testify/require"
33 )
34
35 func TestNocmpComparability(t *testing.T) {
36 tests := []struct {
37 desc string
38 give interface{}
39 comparable bool
40 }{
41 {
42 desc: "nocmp struct",
43 give: nocmp{},
44 },
45 {
46 desc: "struct with nocmp embedded",
47 give: struct{ nocmp }{},
48 },
49 {
50 desc: "pointer to struct with nocmp embedded",
51 give: &struct{ nocmp }{},
52 comparable: true,
53 },
54
55
56 {desc: "Bool", give: Bool{}},
57 {desc: "Duration", give: Duration{}},
58 {desc: "Error", give: Error{}},
59 {desc: "Float64", give: Float64{}},
60 {desc: "Int32", give: Int32{}},
61 {desc: "Int64", give: Int64{}},
62 {desc: "String", give: String{}},
63 {desc: "Uint32", give: Uint32{}},
64 {desc: "Uint64", give: Uint64{}},
65 {desc: "Value", give: Value{}},
66 }
67
68 for _, tt := range tests {
69 t.Run(tt.desc, func(t *testing.T) {
70 typ := reflect.TypeOf(tt.give)
71 assert.Equalf(t, tt.comparable, typ.Comparable(),
72 "type %v comparablity mismatch", typ)
73 })
74 }
75 }
76
77
78 func TestNocmpSize(t *testing.T) {
79 type x struct{ _ int }
80
81 before := reflect.TypeOf(x{}).Size()
82
83 type y struct {
84 _ nocmp
85 _ x
86 }
87
88 after := reflect.TypeOf(y{}).Size()
89
90 assert.Equal(t, before, after,
91 "expected nocmp to have no effect on struct size")
92 }
93
94
95
96
97
98
99
100 func TestNocmpCopy(t *testing.T) {
101 type foo struct{ _ nocmp }
102
103 t.Run("struct copy", func(t *testing.T) {
104 a := foo{}
105 b := a
106 _ = b
107 })
108
109 t.Run("pointer copy", func(t *testing.T) {
110 a := &foo{}
111 b := *a
112 _ = b
113 })
114 }
115
116
117 const _exampleGoMod = `module example.com/nocmp`
118
119 const _badFile = `package atomic
120
121 import "fmt"
122
123 type Int64 struct {
124 nocmp
125
126 v int64
127 }
128
129 func shouldNotCompile() {
130 var x, y Int64
131 fmt.Println(x == y)
132 }
133 `
134
135 func TestNocmpIntegration(t *testing.T) {
136 tempdir := t.TempDir()
137
138 nocmp, err := os.ReadFile("nocmp.go")
139 require.NoError(t, err, "unable to read nocmp.go")
140
141 require.NoError(t,
142 os.WriteFile(filepath.Join(tempdir, "go.mod"), []byte(_exampleGoMod), 0o644),
143 "unable to write go.mod")
144
145 require.NoError(t,
146 os.WriteFile(filepath.Join(tempdir, "nocmp.go"), nocmp, 0o644),
147 "unable to write nocmp.go")
148
149 require.NoError(t,
150 os.WriteFile(filepath.Join(tempdir, "bad.go"), []byte(_badFile), 0o644),
151 "unable to write bad.go")
152
153 var stderr bytes.Buffer
154 cmd := exec.Command("go", "build")
155 cmd.Dir = tempdir
156
157
158 cmd.Env = []string{"HOME=" + filepath.Join(tempdir, "home")}
159 cmd.Stderr = &stderr
160 require.Error(t, cmd.Run(), "bad.go must not compile")
161
162 assert.Contains(t, stderr.String(),
163 "struct containing nocmp cannot be compared")
164 }
165
View as plain text