This file is indexed.

/usr/share/gocode/src/launchpad.net/gocheck/checkers_test.go is in golang-gocheck-dev 0.0~bzr20131118+85-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package gocheck_test

import (
	"errors"
	"launchpad.net/gocheck"
	"reflect"
	"runtime"
)

type CheckersS struct{}

var _ = gocheck.Suite(&CheckersS{})

func testInfo(c *gocheck.C, checker gocheck.Checker, name string, paramNames []string) {
	info := checker.Info()
	if info.Name != name {
		c.Fatalf("Got name %s, expected %s", info.Name, name)
	}
	if !reflect.DeepEqual(info.Params, paramNames) {
		c.Fatalf("Got param names %#v, expected %#v", info.Params, paramNames)
	}
}

func testCheck(c *gocheck.C, checker gocheck.Checker, result bool, error string, params ...interface{}) ([]interface{}, []string) {
	info := checker.Info()
	if len(params) != len(info.Params) {
		c.Fatalf("unexpected param count in test; expected %d got %d", len(info.Params), len(params))
	}
	names := append([]string{}, info.Params...)
	result_, error_ := checker.Check(params, names)
	if result_ != result || error_ != error {
		c.Fatalf("%s.Check(%#v) returned (%#v, %#v) rather than (%#v, %#v)",
			info.Name, params, result_, error_, result, error)
	}
	return params, names
}

func (s *CheckersS) TestComment(c *gocheck.C) {
	bug := gocheck.Commentf("a %d bc", 42)
	comment := bug.CheckCommentString()
	if comment != "a 42 bc" {
		c.Fatalf("Commentf returned %#v", comment)
	}
}

func (s *CheckersS) TestIsNil(c *gocheck.C) {
	testInfo(c, gocheck.IsNil, "IsNil", []string{"value"})

	testCheck(c, gocheck.IsNil, true, "", nil)
	testCheck(c, gocheck.IsNil, false, "", "a")

	testCheck(c, gocheck.IsNil, true, "", (chan int)(nil))
	testCheck(c, gocheck.IsNil, false, "", make(chan int))
	testCheck(c, gocheck.IsNil, true, "", (error)(nil))
	testCheck(c, gocheck.IsNil, false, "", errors.New(""))
	testCheck(c, gocheck.IsNil, true, "", ([]int)(nil))
	testCheck(c, gocheck.IsNil, false, "", make([]int, 1))
	testCheck(c, gocheck.IsNil, false, "", int(0))
}

func (s *CheckersS) TestNotNil(c *gocheck.C) {
	testInfo(c, gocheck.NotNil, "NotNil", []string{"value"})

	testCheck(c, gocheck.NotNil, false, "", nil)
	testCheck(c, gocheck.NotNil, true, "", "a")

	testCheck(c, gocheck.NotNil, false, "", (chan int)(nil))
	testCheck(c, gocheck.NotNil, true, "", make(chan int))
	testCheck(c, gocheck.NotNil, false, "", (error)(nil))
	testCheck(c, gocheck.NotNil, true, "", errors.New(""))
	testCheck(c, gocheck.NotNil, false, "", ([]int)(nil))
	testCheck(c, gocheck.NotNil, true, "", make([]int, 1))
}

func (s *CheckersS) TestNot(c *gocheck.C) {
	testInfo(c, gocheck.Not(gocheck.IsNil), "Not(IsNil)", []string{"value"})

	testCheck(c, gocheck.Not(gocheck.IsNil), false, "", nil)
	testCheck(c, gocheck.Not(gocheck.IsNil), true, "", "a")
}

type simpleStruct struct {
	i int
}

func (s *CheckersS) TestEquals(c *gocheck.C) {
	testInfo(c, gocheck.Equals, "Equals", []string{"obtained", "expected"})

	// The simplest.
	testCheck(c, gocheck.Equals, true, "", 42, 42)
	testCheck(c, gocheck.Equals, false, "", 42, 43)

	// Different native types.
	testCheck(c, gocheck.Equals, false, "", int32(42), int64(42))

	// With nil.
	testCheck(c, gocheck.Equals, false, "", 42, nil)

	// Slices
	testCheck(c, gocheck.Equals, false, "runtime error: comparing uncomparable type []uint8", []byte{1, 2}, []byte{1, 2})

	// Struct values
	testCheck(c, gocheck.Equals, true, "", simpleStruct{1}, simpleStruct{1})
	testCheck(c, gocheck.Equals, false, "", simpleStruct{1}, simpleStruct{2})

	// Struct pointers
	testCheck(c, gocheck.Equals, false, "", &simpleStruct{1}, &simpleStruct{1})
	testCheck(c, gocheck.Equals, false, "", &simpleStruct{1}, &simpleStruct{2})
}

func (s *CheckersS) TestDeepEquals(c *gocheck.C) {
	testInfo(c, gocheck.DeepEquals, "DeepEquals", []string{"obtained", "expected"})

	// The simplest.
	testCheck(c, gocheck.DeepEquals, true, "", 42, 42)
	testCheck(c, gocheck.DeepEquals, false, "", 42, 43)

	// Different native types.
	testCheck(c, gocheck.DeepEquals, false, "", int32(42), int64(42))

	// With nil.
	testCheck(c, gocheck.DeepEquals, false, "", 42, nil)

	// Slices
	testCheck(c, gocheck.DeepEquals, true, "", []byte{1, 2}, []byte{1, 2})
	testCheck(c, gocheck.DeepEquals, false, "", []byte{1, 2}, []byte{1, 3})

	// Struct values
	testCheck(c, gocheck.DeepEquals, true, "", simpleStruct{1}, simpleStruct{1})
	testCheck(c, gocheck.DeepEquals, false, "", simpleStruct{1}, simpleStruct{2})

	// Struct pointers
	testCheck(c, gocheck.DeepEquals, true, "", &simpleStruct{1}, &simpleStruct{1})
	testCheck(c, gocheck.DeepEquals, false, "", &simpleStruct{1}, &simpleStruct{2})
}

func (s *CheckersS) TestHasLen(c *gocheck.C) {
	testInfo(c, gocheck.HasLen, "HasLen", []string{"obtained", "n"})

	testCheck(c, gocheck.HasLen, true, "", "abcd", 4)
	testCheck(c, gocheck.HasLen, true, "", []int{1, 2}, 2)
	testCheck(c, gocheck.HasLen, false, "", []int{1, 2}, 3)

	testCheck(c, gocheck.HasLen, false, "n must be an int", []int{1, 2}, "2")
	testCheck(c, gocheck.HasLen, false, "obtained value type has no length", nil, 2)
}

func (s *CheckersS) TestErrorMatches(c *gocheck.C) {
	testInfo(c, gocheck.ErrorMatches, "ErrorMatches", []string{"value", "regex"})

	testCheck(c, gocheck.ErrorMatches, false, "Error value is nil", nil, "some error")
	testCheck(c, gocheck.ErrorMatches, false, "Value is not an error", 1, "some error")
	testCheck(c, gocheck.ErrorMatches, true, "", errors.New("some error"), "some error")
	testCheck(c, gocheck.ErrorMatches, true, "", errors.New("some error"), "so.*or")

	// Verify params mutation
	params, names := testCheck(c, gocheck.ErrorMatches, false, "", errors.New("some error"), "other error")
	c.Assert(params[0], gocheck.Equals, "some error")
	c.Assert(names[0], gocheck.Equals, "error")
}

func (s *CheckersS) TestMatches(c *gocheck.C) {
	testInfo(c, gocheck.Matches, "Matches", []string{"value", "regex"})

	// Simple matching
	testCheck(c, gocheck.Matches, true, "", "abc", "abc")
	testCheck(c, gocheck.Matches, true, "", "abc", "a.c")

	// Must match fully
	testCheck(c, gocheck.Matches, false, "", "abc", "ab")
	testCheck(c, gocheck.Matches, false, "", "abc", "bc")

	// String()-enabled values accepted
	testCheck(c, gocheck.Matches, true, "", reflect.ValueOf("abc"), "a.c")
	testCheck(c, gocheck.Matches, false, "", reflect.ValueOf("abc"), "a.d")

	// Some error conditions.
	testCheck(c, gocheck.Matches, false, "Obtained value is not a string and has no .String()", 1, "a.c")
	testCheck(c, gocheck.Matches, false, "Can't compile regex: error parsing regexp: missing closing ]: `[c$`", "abc", "a[c")
}

func (s *CheckersS) TestPanics(c *gocheck.C) {
	testInfo(c, gocheck.Panics, "Panics", []string{"function", "expected"})

	// Some errors.
	testCheck(c, gocheck.Panics, false, "Function has not panicked", func() bool { return false }, "BOOM")
	testCheck(c, gocheck.Panics, false, "Function must take zero arguments", 1, "BOOM")

	// Plain strings.
	testCheck(c, gocheck.Panics, true, "", func() { panic("BOOM") }, "BOOM")
	testCheck(c, gocheck.Panics, false, "", func() { panic("KABOOM") }, "BOOM")
	testCheck(c, gocheck.Panics, true, "", func() bool { panic("BOOM") }, "BOOM")

	// Error values.
	testCheck(c, gocheck.Panics, true, "", func() { panic(errors.New("BOOM")) }, errors.New("BOOM"))
	testCheck(c, gocheck.Panics, false, "", func() { panic(errors.New("KABOOM")) }, errors.New("BOOM"))

	type deep struct{ i int }
	// Deep value
	testCheck(c, gocheck.Panics, true, "", func() { panic(&deep{99}) }, &deep{99})

	// Verify params/names mutation
	params, names := testCheck(c, gocheck.Panics, false, "", func() { panic(errors.New("KABOOM")) }, errors.New("BOOM"))
	c.Assert(params[0], gocheck.ErrorMatches, "KABOOM")
	c.Assert(names[0], gocheck.Equals, "panic")

	// Verify a nil panic
	testCheck(c, gocheck.Panics, true, "", func() { panic(nil) }, nil)
	testCheck(c, gocheck.Panics, false, "", func() { panic(nil) }, "NOPE")
}

func (s *CheckersS) TestPanicMatches(c *gocheck.C) {
	testInfo(c, gocheck.PanicMatches, "PanicMatches", []string{"function", "expected"})

	// Error matching.
	testCheck(c, gocheck.PanicMatches, true, "", func() { panic(errors.New("BOOM")) }, "BO.M")
	testCheck(c, gocheck.PanicMatches, false, "", func() { panic(errors.New("KABOOM")) }, "BO.M")

	// Some errors.
	testCheck(c, gocheck.PanicMatches, false, "Function has not panicked", func() bool { return false }, "BOOM")
	testCheck(c, gocheck.PanicMatches, false, "Function must take zero arguments", 1, "BOOM")

	// Plain strings.
	testCheck(c, gocheck.PanicMatches, true, "", func() { panic("BOOM") }, "BO.M")
	testCheck(c, gocheck.PanicMatches, false, "", func() { panic("KABOOM") }, "BOOM")
	testCheck(c, gocheck.PanicMatches, true, "", func() bool { panic("BOOM") }, "BO.M")

	// Verify params/names mutation
	params, names := testCheck(c, gocheck.PanicMatches, false, "", func() { panic(errors.New("KABOOM")) }, "BOOM")
	c.Assert(params[0], gocheck.Equals, "KABOOM")
	c.Assert(names[0], gocheck.Equals, "panic")

	// Verify a nil panic
	testCheck(c, gocheck.PanicMatches, false, "Panic value is not a string or an error", func() { panic(nil) }, "")
}

func (s *CheckersS) TestFitsTypeOf(c *gocheck.C) {
	testInfo(c, gocheck.FitsTypeOf, "FitsTypeOf", []string{"obtained", "sample"})

	// Basic types
	testCheck(c, gocheck.FitsTypeOf, true, "", 1, 0)
	testCheck(c, gocheck.FitsTypeOf, false, "", 1, int64(0))

	// Aliases
	testCheck(c, gocheck.FitsTypeOf, false, "", 1, errors.New(""))
	testCheck(c, gocheck.FitsTypeOf, false, "", "error", errors.New(""))
	testCheck(c, gocheck.FitsTypeOf, true, "", errors.New("error"), errors.New(""))

	// Structures
	testCheck(c, gocheck.FitsTypeOf, false, "", 1, simpleStruct{})
	testCheck(c, gocheck.FitsTypeOf, false, "", simpleStruct{42}, &simpleStruct{})
	testCheck(c, gocheck.FitsTypeOf, true, "", simpleStruct{42}, simpleStruct{})
	testCheck(c, gocheck.FitsTypeOf, true, "", &simpleStruct{42}, &simpleStruct{})

	// Some bad values
	testCheck(c, gocheck.FitsTypeOf, false, "Invalid sample value", 1, interface{}(nil))
	testCheck(c, gocheck.FitsTypeOf, false, "", interface{}(nil), 0)
}

func (s *CheckersS) TestImplements(c *gocheck.C) {
	testInfo(c, gocheck.Implements, "Implements", []string{"obtained", "ifaceptr"})

	var e error
	var re runtime.Error
	testCheck(c, gocheck.Implements, true, "", errors.New(""), &e)
	testCheck(c, gocheck.Implements, false, "", errors.New(""), &re)

	// Some bad values
	testCheck(c, gocheck.Implements, false, "ifaceptr should be a pointer to an interface variable", 0, errors.New(""))
	testCheck(c, gocheck.Implements, false, "ifaceptr should be a pointer to an interface variable", 0, interface{}(nil))
	testCheck(c, gocheck.Implements, false, "", interface{}(nil), &e)
}