| 1 |
package inject_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/rand" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/facebookgo/ensure" |
| 11 |
"github.com/facebookgo/inject" |
| 12 |
|
| 13 |
injecttesta "github.com/facebookgo/inject/injecttesta" |
| 14 |
injecttestb "github.com/facebookgo/inject/injecttestb" |
| 15 |
) |
| 16 |
|
| 17 |
func init() { |
| 18 |
|
| 19 |
rand.Seed(time.Now().UnixNano()) |
| 20 |
} |
| 21 |
|
| 22 |
type Answerable interface { |
| 23 |
Answer() int |
| 24 |
} |
| 25 |
|
| 26 |
type TypeAnswerStruct struct { |
| 27 |
answer int |
| 28 |
private int |
| 29 |
} |
| 30 |
|
| 31 |
func (t *TypeAnswerStruct) Answer() int { |
| 32 |
return t.answer |
| 33 |
} |
| 34 |
|
| 35 |
type TypeNestedStruct struct { |
| 36 |
A *TypeAnswerStruct `inject:""` |
| 37 |
} |
| 38 |
|
| 39 |
func (t *TypeNestedStruct) Answer() int { |
| 40 |
return t.A.Answer() |
| 41 |
} |
| 42 |
|
| 43 |
func TestRequireTag(t *testing.T) { |
| 44 |
var v struct { |
| 45 |
A *TypeAnswerStruct |
| 46 |
B *TypeNestedStruct `inject:""` |
| 47 |
} |
| 48 |
|
| 49 |
if err := inject.Populate(&v); err != nil { |
| 50 |
t.Fatal(err) |
| 51 |
} |
| 52 |
if v.A != nil { |
| 53 |
t.Fatal("v.A is not nil") |
| 54 |
} |
| 55 |
if v.B == nil { |
| 56 |
t.Fatal("v.B is nil") |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
type TypeWithNonPointerInject struct { |
| 61 |
A int `inject:""` |
| 62 |
} |
| 63 |
|
| 64 |
func TestErrorOnNonPointerInject(t *testing.T) { |
| 65 |
var a TypeWithNonPointerInject |
| 66 |
err := inject.Populate(&a) |
| 67 |
if err == nil { |
| 68 |
t.Fatalf("expected error for %+v", a) |
| 69 |
} |
| 70 |
|
| 71 |
const msg = "found inject tag on unsupported field A in type *inject_test.TypeWithNonPointerInject" |
| 72 |
if err.Error() != msg { |
| 73 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
type TypeWithNonPointerStructInject struct { |
| 78 |
A *int `inject:""` |
| 79 |
} |
| 80 |
|
| 81 |
func TestErrorOnNonPointerStructInject(t *testing.T) { |
| 82 |
var a TypeWithNonPointerStructInject |
| 83 |
err := inject.Populate(&a) |
| 84 |
if err == nil { |
| 85 |
t.Fatalf("expected error for %+v", a) |
| 86 |
} |
| 87 |
|
| 88 |
const msg = "found inject tag on unsupported field A in type *inject_test.TypeWithNonPointerStructInject" |
| 89 |
if err.Error() != msg { |
| 90 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestInjectSimple(t *testing.T) { |
| 95 |
var v struct { |
| 96 |
A *TypeAnswerStruct `inject:""` |
| 97 |
B *TypeNestedStruct `inject:""` |
| 98 |
} |
| 99 |
|
| 100 |
if err := inject.Populate(&v); err != nil { |
| 101 |
t.Fatal(err) |
| 102 |
} |
| 103 |
if v.A == nil { |
| 104 |
t.Fatal("v.A is nil") |
| 105 |
} |
| 106 |
if v.B == nil { |
| 107 |
t.Fatal("v.B is nil") |
| 108 |
} |
| 109 |
if v.B.A == nil { |
| 110 |
t.Fatal("v.B.A is nil") |
| 111 |
} |
| 112 |
if v.A != v.B.A { |
| 113 |
t.Fatal("got different instances of A") |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func TestDoesNotOverwrite(t *testing.T) { |
| 118 |
a := &TypeAnswerStruct{} |
| 119 |
var v struct { |
| 120 |
A *TypeAnswerStruct `inject:""` |
| 121 |
B *TypeNestedStruct `inject:""` |
| 122 |
} |
| 123 |
v.A = a |
| 124 |
if err := inject.Populate(&v); err != nil { |
| 125 |
t.Fatal(err) |
| 126 |
} |
| 127 |
if v.A != a { |
| 128 |
t.Fatal("original A was lost") |
| 129 |
} |
| 130 |
if v.B == nil { |
| 131 |
t.Fatal("v.B is nil") |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func TestPrivate(t *testing.T) { |
| 136 |
var v struct { |
| 137 |
A *TypeAnswerStruct `inject:"private"` |
| 138 |
B *TypeNestedStruct `inject:""` |
| 139 |
} |
| 140 |
|
| 141 |
if err := inject.Populate(&v); err != nil { |
| 142 |
t.Fatal(err) |
| 143 |
} |
| 144 |
if v.A == nil { |
| 145 |
t.Fatal("v.A is nil") |
| 146 |
} |
| 147 |
if v.B == nil { |
| 148 |
t.Fatal("v.B is nil") |
| 149 |
} |
| 150 |
if v.B.A == nil { |
| 151 |
t.Fatal("v.B.A is nil") |
| 152 |
} |
| 153 |
if v.A == v.B.A { |
| 154 |
t.Fatal("got the same A") |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
type TypeWithJustColon struct { |
| 159 |
A *TypeAnswerStruct `inject:` |
| 160 |
} |
| 161 |
|
| 162 |
func TestTagWithJustColon(t *testing.T) { |
| 163 |
var a TypeWithJustColon |
| 164 |
err := inject.Populate(&a) |
| 165 |
if err == nil { |
| 166 |
t.Fatalf("expected error for %+v", a) |
| 167 |
} |
| 168 |
|
| 169 |
const msg = "unexpected tag format `inject:` for field A in type *inject_test.TypeWithJustColon" |
| 170 |
if err.Error() != msg { |
| 171 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
type TypeWithOpenQuote struct { |
| 176 |
A *TypeAnswerStruct `inject:"` |
| 177 |
} |
| 178 |
|
| 179 |
func TestTagWithOpenQuote(t *testing.T) { |
| 180 |
var a TypeWithOpenQuote |
| 181 |
err := inject.Populate(&a) |
| 182 |
if err == nil { |
| 183 |
t.Fatalf("expected error for %+v", a) |
| 184 |
} |
| 185 |
|
| 186 |
const msg = "unexpected tag format `inject:\"` for field A in type *inject_test.TypeWithOpenQuote" |
| 187 |
if err.Error() != msg { |
| 188 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func TestProvideWithFields(t *testing.T) { |
| 193 |
var g inject.Graph |
| 194 |
a := &TypeAnswerStruct{} |
| 195 |
err := g.Provide(&inject.Object{Value: a, Fields: map[string]*inject.Object{}}) |
| 196 |
ensure.NotNil(t, err) |
| 197 |
ensure.DeepEqual(t, err.Error(), "fields were specified on object *inject_test.TypeAnswerStruct when it was provided") |
| 198 |
} |
| 199 |
|
| 200 |
func TestProvideNonPointer(t *testing.T) { |
| 201 |
var g inject.Graph |
| 202 |
var i int |
| 203 |
err := g.Provide(&inject.Object{Value: i}) |
| 204 |
if err == nil { |
| 205 |
t.Fatal("expected error") |
| 206 |
} |
| 207 |
|
| 208 |
const msg = "expected unnamed object value to be a pointer to a struct but got type int with value 0" |
| 209 |
if err.Error() != msg { |
| 210 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
func TestProvideNonPointerStruct(t *testing.T) { |
| 215 |
var g inject.Graph |
| 216 |
var i *int |
| 217 |
err := g.Provide(&inject.Object{Value: i}) |
| 218 |
if err == nil { |
| 219 |
t.Fatal("expected error") |
| 220 |
} |
| 221 |
|
| 222 |
const msg = "expected unnamed object value to be a pointer to a struct but got type *int with value <nil>" |
| 223 |
if err.Error() != msg { |
| 224 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
func TestProvideTwoOfTheSame(t *testing.T) { |
| 229 |
var g inject.Graph |
| 230 |
a := TypeAnswerStruct{} |
| 231 |
err := g.Provide(&inject.Object{Value: &a}) |
| 232 |
if err != nil { |
| 233 |
t.Fatal(err) |
| 234 |
} |
| 235 |
|
| 236 |
err = g.Provide(&inject.Object{Value: &a}) |
| 237 |
if err == nil { |
| 238 |
t.Fatal("expected error") |
| 239 |
} |
| 240 |
|
| 241 |
const msg = "provided two unnamed instances of type *github.com/facebookgo/inject_test.TypeAnswerStruct" |
| 242 |
if err.Error() != msg { |
| 243 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func TestProvideTwoOfTheSameWithPopulate(t *testing.T) { |
| 248 |
a := TypeAnswerStruct{} |
| 249 |
err := inject.Populate(&a, &a) |
| 250 |
if err == nil { |
| 251 |
t.Fatal("expected error") |
| 252 |
} |
| 253 |
|
| 254 |
const msg = "provided two unnamed instances of type *github.com/facebookgo/inject_test.TypeAnswerStruct" |
| 255 |
if err.Error() != msg { |
| 256 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
func TestProvideTwoWithTheSameName(t *testing.T) { |
| 261 |
var g inject.Graph |
| 262 |
const name = "foo" |
| 263 |
a := TypeAnswerStruct{} |
| 264 |
err := g.Provide(&inject.Object{Value: &a, Name: name}) |
| 265 |
if err != nil { |
| 266 |
t.Fatal(err) |
| 267 |
} |
| 268 |
|
| 269 |
err = g.Provide(&inject.Object{Value: &a, Name: name}) |
| 270 |
if err == nil { |
| 271 |
t.Fatal("expected error") |
| 272 |
} |
| 273 |
|
| 274 |
const msg = "provided two instances named foo" |
| 275 |
if err.Error() != msg { |
| 276 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
func TestNamedInstanceWithDependencies(t *testing.T) { |
| 281 |
var g inject.Graph |
| 282 |
a := &TypeNestedStruct{} |
| 283 |
if err := g.Provide(&inject.Object{Value: a, Name: "foo"}); err != nil { |
| 284 |
t.Fatal(err) |
| 285 |
} |
| 286 |
|
| 287 |
var c struct { |
| 288 |
A *TypeNestedStruct `inject:"foo"` |
| 289 |
} |
| 290 |
if err := g.Provide(&inject.Object{Value: &c}); err != nil { |
| 291 |
t.Fatal(err) |
| 292 |
} |
| 293 |
|
| 294 |
if err := g.Populate(); err != nil { |
| 295 |
t.Fatal(err) |
| 296 |
} |
| 297 |
|
| 298 |
if c.A.A == nil { |
| 299 |
t.Fatal("c.A.A was not injected") |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
func TestTwoNamedInstances(t *testing.T) { |
| 304 |
var g inject.Graph |
| 305 |
a := &TypeAnswerStruct{} |
| 306 |
b := &TypeAnswerStruct{} |
| 307 |
if err := g.Provide(&inject.Object{Value: a, Name: "foo"}); err != nil { |
| 308 |
t.Fatal(err) |
| 309 |
} |
| 310 |
|
| 311 |
if err := g.Provide(&inject.Object{Value: b, Name: "bar"}); err != nil { |
| 312 |
t.Fatal(err) |
| 313 |
} |
| 314 |
|
| 315 |
var c struct { |
| 316 |
A *TypeAnswerStruct `inject:"foo"` |
| 317 |
B *TypeAnswerStruct `inject:"bar"` |
| 318 |
} |
| 319 |
if err := g.Provide(&inject.Object{Value: &c}); err != nil { |
| 320 |
t.Fatal(err) |
| 321 |
} |
| 322 |
|
| 323 |
if err := g.Populate(); err != nil { |
| 324 |
t.Fatal(err) |
| 325 |
} |
| 326 |
|
| 327 |
if c.A != a { |
| 328 |
t.Fatal("did not find expected c.A") |
| 329 |
} |
| 330 |
if c.B != b { |
| 331 |
t.Fatal("did not find expected c.B") |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
type TypeWithMissingNamed struct { |
| 336 |
A *TypeAnswerStruct `inject:"foo"` |
| 337 |
} |
| 338 |
|
| 339 |
func TestTagWithMissingNamed(t *testing.T) { |
| 340 |
var a TypeWithMissingNamed |
| 341 |
err := inject.Populate(&a) |
| 342 |
if err == nil { |
| 343 |
t.Fatalf("expected error for %+v", a) |
| 344 |
} |
| 345 |
|
| 346 |
const msg = "did not find object named foo required by field A in type *inject_test.TypeWithMissingNamed" |
| 347 |
if err.Error() != msg { |
| 348 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
func TestCompleteProvides(t *testing.T) { |
| 353 |
var g inject.Graph |
| 354 |
var v struct { |
| 355 |
A *TypeAnswerStruct `inject:""` |
| 356 |
} |
| 357 |
if err := g.Provide(&inject.Object{Value: &v, Complete: true}); err != nil { |
| 358 |
t.Fatal(err) |
| 359 |
} |
| 360 |
|
| 361 |
if err := g.Populate(); err != nil { |
| 362 |
t.Fatal(err) |
| 363 |
} |
| 364 |
if v.A != nil { |
| 365 |
t.Fatal("v.A was not nil") |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
func TestCompleteNamedProvides(t *testing.T) { |
| 370 |
var g inject.Graph |
| 371 |
var v struct { |
| 372 |
A *TypeAnswerStruct `inject:""` |
| 373 |
} |
| 374 |
if err := g.Provide(&inject.Object{Value: &v, Complete: true, Name: "foo"}); err != nil { |
| 375 |
t.Fatal(err) |
| 376 |
} |
| 377 |
|
| 378 |
if err := g.Populate(); err != nil { |
| 379 |
t.Fatal(err) |
| 380 |
} |
| 381 |
if v.A != nil { |
| 382 |
t.Fatal("v.A was not nil") |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
type TypeInjectInterfaceMissing struct { |
| 387 |
Answerable Answerable `inject:""` |
| 388 |
} |
| 389 |
|
| 390 |
func TestInjectInterfaceMissing(t *testing.T) { |
| 391 |
var v TypeInjectInterfaceMissing |
| 392 |
err := inject.Populate(&v) |
| 393 |
if err == nil { |
| 394 |
t.Fatal("did not find expected error") |
| 395 |
} |
| 396 |
|
| 397 |
const msg = "found no assignable value for field Answerable in type *inject_test.TypeInjectInterfaceMissing" |
| 398 |
if err.Error() != msg { |
| 399 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
type TypeInjectInterface struct { |
| 404 |
Answerable Answerable `inject:""` |
| 405 |
A *TypeAnswerStruct `inject:""` |
| 406 |
} |
| 407 |
|
| 408 |
func TestInjectInterface(t *testing.T) { |
| 409 |
var v TypeInjectInterface |
| 410 |
if err := inject.Populate(&v); err != nil { |
| 411 |
t.Fatal(err) |
| 412 |
} |
| 413 |
if v.Answerable == nil || v.Answerable != v.A { |
| 414 |
t.Fatalf( |
| 415 |
"expected the same but got Answerable = %T %+v / A = %T %+v", |
| 416 |
v.Answerable, |
| 417 |
v.Answerable, |
| 418 |
v.A, |
| 419 |
v.A, |
| 420 |
) |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
type TypeWithInvalidNamedType struct { |
| 425 |
A *TypeNestedStruct `inject:"foo"` |
| 426 |
} |
| 427 |
|
| 428 |
func TestInvalidNamedInstanceType(t *testing.T) { |
| 429 |
var g inject.Graph |
| 430 |
a := &TypeAnswerStruct{} |
| 431 |
if err := g.Provide(&inject.Object{Value: a, Name: "foo"}); err != nil { |
| 432 |
t.Fatal(err) |
| 433 |
} |
| 434 |
|
| 435 |
var c TypeWithInvalidNamedType |
| 436 |
if err := g.Provide(&inject.Object{Value: &c}); err != nil { |
| 437 |
t.Fatal(err) |
| 438 |
} |
| 439 |
|
| 440 |
err := g.Populate() |
| 441 |
if err == nil { |
| 442 |
t.Fatal("did not find expected error") |
| 443 |
} |
| 444 |
|
| 445 |
const msg = "object named foo of type *inject_test.TypeNestedStruct is not assignable to field A (*inject_test.TypeAnswerStruct) in type *inject_test.TypeWithInvalidNamedType" |
| 446 |
if err.Error() != msg { |
| 447 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
type TypeWithInjectOnPrivateField struct { |
| 452 |
a *TypeAnswerStruct `inject:""` |
| 453 |
} |
| 454 |
|
| 455 |
func TestInjectOnPrivateField(t *testing.T) { |
| 456 |
var a TypeWithInjectOnPrivateField |
| 457 |
err := inject.Populate(&a) |
| 458 |
if err == nil { |
| 459 |
t.Fatal("did not find expected error") |
| 460 |
} |
| 461 |
|
| 462 |
const msg = "inject requested on unexported field a in type *inject_test.TypeWithInjectOnPrivateField" |
| 463 |
if err.Error() != msg { |
| 464 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
type TypeWithInjectOnPrivateInterfaceField struct { |
| 469 |
a Answerable `inject:""` |
| 470 |
} |
| 471 |
|
| 472 |
func TestInjectOnPrivateInterfaceField(t *testing.T) { |
| 473 |
var a TypeWithInjectOnPrivateField |
| 474 |
err := inject.Populate(&a) |
| 475 |
if err == nil { |
| 476 |
t.Fatal("did not find expected error") |
| 477 |
} |
| 478 |
|
| 479 |
const msg = "inject requested on unexported field a in type *inject_test.TypeWithInjectOnPrivateField" |
| 480 |
if err.Error() != msg { |
| 481 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
type TypeInjectPrivateInterface struct { |
| 486 |
Answerable Answerable `inject:"private"` |
| 487 |
B *TypeNestedStruct `inject:""` |
| 488 |
} |
| 489 |
|
| 490 |
func TestInjectPrivateInterface(t *testing.T) { |
| 491 |
var v TypeInjectPrivateInterface |
| 492 |
err := inject.Populate(&v) |
| 493 |
if err == nil { |
| 494 |
t.Fatal("did not find expected error") |
| 495 |
} |
| 496 |
|
| 497 |
const msg = "found private inject tag on interface field Answerable in type *inject_test.TypeInjectPrivateInterface" |
| 498 |
if err.Error() != msg { |
| 499 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
type TypeInjectTwoSatisfyInterface struct { |
| 504 |
Answerable Answerable `inject:""` |
| 505 |
A *TypeAnswerStruct `inject:""` |
| 506 |
B *TypeNestedStruct `inject:""` |
| 507 |
} |
| 508 |
|
| 509 |
func TestInjectTwoSatisfyInterface(t *testing.T) { |
| 510 |
var v TypeInjectTwoSatisfyInterface |
| 511 |
err := inject.Populate(&v) |
| 512 |
if err == nil { |
| 513 |
t.Fatal("did not find expected error") |
| 514 |
} |
| 515 |
|
| 516 |
const msg = "found two assignable values for field Answerable in type *inject_test.TypeInjectTwoSatisfyInterface. one type *inject_test.TypeAnswerStruct with value &{0 0} and another type *inject_test.TypeNestedStruct with value" |
| 517 |
if !strings.HasPrefix(err.Error(), msg) { |
| 518 |
t.Fatalf("expected prefix:\n%s\nactual:\n%s", msg, err.Error()) |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
type TypeInjectNamedTwoSatisfyInterface struct { |
| 523 |
Answerable Answerable `inject:""` |
| 524 |
A *TypeAnswerStruct `inject:""` |
| 525 |
B *TypeNestedStruct `inject:""` |
| 526 |
} |
| 527 |
|
| 528 |
func TestInjectNamedTwoSatisfyInterface(t *testing.T) { |
| 529 |
var g inject.Graph |
| 530 |
var v TypeInjectNamedTwoSatisfyInterface |
| 531 |
if err := g.Provide(&inject.Object{Name: "foo", Value: &v}); err != nil { |
| 532 |
t.Fatal(err) |
| 533 |
} |
| 534 |
|
| 535 |
err := g.Populate() |
| 536 |
if err == nil { |
| 537 |
t.Fatal("was expecting error") |
| 538 |
} |
| 539 |
|
| 540 |
const msg = "found two assignable values for field Answerable in type *inject_test.TypeInjectNamedTwoSatisfyInterface. one type *inject_test.TypeAnswerStruct with value &{0 0} and another type *inject_test.TypeNestedStruct with value" |
| 541 |
if !strings.HasPrefix(err.Error(), msg) { |
| 542 |
t.Fatalf("expected prefix:\n%s\nactual:\n%s", msg, err.Error()) |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
type TypeWithInjectNamedOnPrivateInterfaceField struct { |
| 547 |
a Answerable `inject:""` |
| 548 |
} |
| 549 |
|
| 550 |
func TestInjectNamedOnPrivateInterfaceField(t *testing.T) { |
| 551 |
var g inject.Graph |
| 552 |
var v TypeWithInjectNamedOnPrivateInterfaceField |
| 553 |
if err := g.Provide(&inject.Object{Name: "foo", Value: &v}); err != nil { |
| 554 |
t.Fatal(err) |
| 555 |
} |
| 556 |
|
| 557 |
err := g.Populate() |
| 558 |
if err == nil { |
| 559 |
t.Fatal("was expecting error") |
| 560 |
} |
| 561 |
|
| 562 |
const msg = "inject requested on unexported field a in type *inject_test.TypeWithInjectNamedOnPrivateInterfaceField" |
| 563 |
if err.Error() != msg { |
| 564 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
type TypeWithNonPointerNamedInject struct { |
| 569 |
A int `inject:"foo"` |
| 570 |
} |
| 571 |
|
| 572 |
func TestErrorOnNonPointerNamedInject(t *testing.T) { |
| 573 |
var g inject.Graph |
| 574 |
if err := g.Provide(&inject.Object{Name: "foo", Value: 42}); err != nil { |
| 575 |
t.Fatal(err) |
| 576 |
} |
| 577 |
|
| 578 |
var v TypeWithNonPointerNamedInject |
| 579 |
if err := g.Provide(&inject.Object{Value: &v}); err != nil { |
| 580 |
t.Fatal(err) |
| 581 |
} |
| 582 |
|
| 583 |
if err := g.Populate(); err != nil { |
| 584 |
t.Fatal(err) |
| 585 |
} |
| 586 |
|
| 587 |
if v.A != 42 { |
| 588 |
t.Fatalf("expected v.A = 42 but got %d", v.A) |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
func TestInjectInline(t *testing.T) { |
| 593 |
var v struct { |
| 594 |
Inline struct { |
| 595 |
A *TypeAnswerStruct `inject:""` |
| 596 |
B *TypeNestedStruct `inject:""` |
| 597 |
} `inject:"inline"` |
| 598 |
} |
| 599 |
|
| 600 |
if err := inject.Populate(&v); err != nil { |
| 601 |
t.Fatal(err) |
| 602 |
} |
| 603 |
if v.Inline.A == nil { |
| 604 |
t.Fatal("v.Inline.A is nil") |
| 605 |
} |
| 606 |
if v.Inline.B == nil { |
| 607 |
t.Fatal("v.Inline.B is nil") |
| 608 |
} |
| 609 |
if v.Inline.B.A == nil { |
| 610 |
t.Fatal("v.Inline.B.A is nil") |
| 611 |
} |
| 612 |
if v.Inline.A != v.Inline.B.A { |
| 613 |
t.Fatal("got different instances of A") |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
func TestInjectInlineOnPointer(t *testing.T) { |
| 618 |
var v struct { |
| 619 |
Inline *struct { |
| 620 |
A *TypeAnswerStruct `inject:""` |
| 621 |
B *TypeNestedStruct `inject:""` |
| 622 |
} `inject:""` |
| 623 |
} |
| 624 |
|
| 625 |
if err := inject.Populate(&v); err != nil { |
| 626 |
t.Fatal(err) |
| 627 |
} |
| 628 |
if v.Inline.A == nil { |
| 629 |
t.Fatal("v.Inline.A is nil") |
| 630 |
} |
| 631 |
if v.Inline.B == nil { |
| 632 |
t.Fatal("v.Inline.B is nil") |
| 633 |
} |
| 634 |
if v.Inline.B.A == nil { |
| 635 |
t.Fatal("v.Inline.B.A is nil") |
| 636 |
} |
| 637 |
if v.Inline.A != v.Inline.B.A { |
| 638 |
t.Fatal("got different instances of A") |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
func TestInjectInvalidInline(t *testing.T) { |
| 643 |
var v struct { |
| 644 |
A *TypeAnswerStruct `inject:"inline"` |
| 645 |
} |
| 646 |
|
| 647 |
err := inject.Populate(&v) |
| 648 |
if err == nil { |
| 649 |
t.Fatal("was expecting an error") |
| 650 |
} |
| 651 |
|
| 652 |
const msg = `inline requested on non inlined field A in type *struct { A *inject_test.TypeAnswerStruct "inject:\"inline\"" }` |
| 653 |
if err.Error() != msg { |
| 654 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
func TestInjectInlineMissing(t *testing.T) { |
| 659 |
var v struct { |
| 660 |
Inline struct { |
| 661 |
B *TypeNestedStruct `inject:""` |
| 662 |
} `inject:""` |
| 663 |
} |
| 664 |
|
| 665 |
err := inject.Populate(&v) |
| 666 |
if err == nil { |
| 667 |
t.Fatal("was expecting an error") |
| 668 |
} |
| 669 |
|
| 670 |
const msg = `inline struct on field Inline in type *struct { Inline struct { B *inject_test.TypeNestedStruct "inject:\"\"" } "inject:\"\"" } requires an explicit "inline" tag` |
| 671 |
if err.Error() != msg { |
| 672 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
type TypeWithInlineStructWithPrivate struct { |
| 677 |
Inline struct { |
| 678 |
A *TypeAnswerStruct `inject:""` |
| 679 |
B *TypeNestedStruct `inject:""` |
| 680 |
} `inject:"private"` |
| 681 |
} |
| 682 |
|
| 683 |
func TestInjectInlinePrivate(t *testing.T) { |
| 684 |
var v TypeWithInlineStructWithPrivate |
| 685 |
err := inject.Populate(&v) |
| 686 |
if err == nil { |
| 687 |
t.Fatal("was expecting an error") |
| 688 |
} |
| 689 |
|
| 690 |
const msg = "cannot use private inject on inline struct on field Inline in type *inject_test.TypeWithInlineStructWithPrivate" |
| 691 |
if err.Error() != msg { |
| 692 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
type TypeWithStructValue struct { |
| 697 |
Inline TypeNestedStruct `inject:"inline"` |
| 698 |
} |
| 699 |
|
| 700 |
func TestInjectWithStructValue(t *testing.T) { |
| 701 |
var v TypeWithStructValue |
| 702 |
if err := inject.Populate(&v); err != nil { |
| 703 |
t.Fatal(err) |
| 704 |
} |
| 705 |
if v.Inline.A == nil { |
| 706 |
t.Fatal("v.Inline.A is nil") |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
type TypeWithNonpointerStructValue struct { |
| 711 |
Inline TypeNestedStruct `inject:"inline"` |
| 712 |
} |
| 713 |
|
| 714 |
func TestInjectWithNonpointerStructValue(t *testing.T) { |
| 715 |
var v TypeWithNonpointerStructValue |
| 716 |
var g inject.Graph |
| 717 |
if err := g.Provide(&inject.Object{Value: &v}); err != nil { |
| 718 |
t.Fatal(err) |
| 719 |
} |
| 720 |
if err := g.Populate(); err != nil { |
| 721 |
t.Fatal(err) |
| 722 |
} |
| 723 |
if v.Inline.A == nil { |
| 724 |
t.Fatal("v.Inline.A is nil") |
| 725 |
} |
| 726 |
n := len(g.Objects()) |
| 727 |
if n != 3 { |
| 728 |
t.Fatalf("expected 3 object in graph, got %d", n) |
| 729 |
} |
| 730 |
|
| 731 |
} |
| 732 |
|
| 733 |
func TestPrivateIsFollowed(t *testing.T) { |
| 734 |
var v struct { |
| 735 |
A *TypeNestedStruct `inject:"private"` |
| 736 |
} |
| 737 |
|
| 738 |
if err := inject.Populate(&v); err != nil { |
| 739 |
t.Fatal(err) |
| 740 |
} |
| 741 |
if v.A.A == nil { |
| 742 |
t.Fatal("v.A.A is nil") |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
func TestDoesNotOverwriteInterface(t *testing.T) { |
| 747 |
a := &TypeAnswerStruct{} |
| 748 |
var v struct { |
| 749 |
A Answerable `inject:""` |
| 750 |
B *TypeNestedStruct `inject:""` |
| 751 |
} |
| 752 |
v.A = a |
| 753 |
if err := inject.Populate(&v); err != nil { |
| 754 |
t.Fatal(err) |
| 755 |
} |
| 756 |
if v.A != a { |
| 757 |
t.Fatal("original A was lost") |
| 758 |
} |
| 759 |
if v.B == nil { |
| 760 |
t.Fatal("v.B is nil") |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
func TestInterfaceIncludingPrivate(t *testing.T) { |
| 765 |
var v struct { |
| 766 |
A Answerable `inject:""` |
| 767 |
B *TypeNestedStruct `inject:"private"` |
| 768 |
C *TypeAnswerStruct `inject:""` |
| 769 |
} |
| 770 |
if err := inject.Populate(&v); err != nil { |
| 771 |
t.Fatal(err) |
| 772 |
} |
| 773 |
if v.A == nil { |
| 774 |
t.Fatal("v.A is nil") |
| 775 |
} |
| 776 |
if v.B == nil { |
| 777 |
t.Fatal("v.B is nil") |
| 778 |
} |
| 779 |
if v.C == nil { |
| 780 |
t.Fatal("v.C is nil") |
| 781 |
} |
| 782 |
if v.A != v.C { |
| 783 |
t.Fatal("v.A != v.C") |
| 784 |
} |
| 785 |
if v.A == v.B { |
| 786 |
t.Fatal("v.A == v.B") |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
func TestInjectMap(t *testing.T) { |
| 791 |
var v struct { |
| 792 |
A map[string]int `inject:"private"` |
| 793 |
} |
| 794 |
if err := inject.Populate(&v); err != nil { |
| 795 |
t.Fatal(err) |
| 796 |
} |
| 797 |
if v.A == nil { |
| 798 |
t.Fatal("v.A is nil") |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
type TypeInjectWithMapWithoutPrivate struct { |
| 803 |
A map[string]int `inject:""` |
| 804 |
} |
| 805 |
|
| 806 |
func TestInjectMapWithoutPrivate(t *testing.T) { |
| 807 |
var v TypeInjectWithMapWithoutPrivate |
| 808 |
err := inject.Populate(&v) |
| 809 |
if err == nil { |
| 810 |
t.Fatalf("expected error for %+v", v) |
| 811 |
} |
| 812 |
|
| 813 |
const msg = "inject on map field A in type *inject_test.TypeInjectWithMapWithoutPrivate must be named or private" |
| 814 |
if err.Error() != msg { |
| 815 |
t.Fatalf("expected:\n%s\nactual:\n%s", msg, err.Error()) |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
type TypeForObjectString struct { |
| 820 |
A *TypeNestedStruct `inject:"foo"` |
| 821 |
B *TypeNestedStruct `inject:""` |
| 822 |
} |
| 823 |
|
| 824 |
func TestObjectString(t *testing.T) { |
| 825 |
var g inject.Graph |
| 826 |
a := &TypeNestedStruct{} |
| 827 |
if err := g.Provide(&inject.Object{Value: a, Name: "foo"}); err != nil { |
| 828 |
t.Fatal(err) |
| 829 |
} |
| 830 |
|
| 831 |
var c TypeForObjectString |
| 832 |
if err := g.Provide(&inject.Object{Value: &c}); err != nil { |
| 833 |
t.Fatal(err) |
| 834 |
} |
| 835 |
|
| 836 |
if err := g.Populate(); err != nil { |
| 837 |
t.Fatal(err) |
| 838 |
} |
| 839 |
|
| 840 |
var actual []string |
| 841 |
for _, o := range g.Objects() { |
| 842 |
actual = append(actual, fmt.Sprint(o)) |
| 843 |
} |
| 844 |
|
| 845 |
ensure.SameElements(t, actual, []string{ |
| 846 |
"*inject_test.TypeForObjectString", |
| 847 |
"*inject_test.TypeNestedStruct", |
| 848 |
"*inject_test.TypeNestedStruct named foo", |
| 849 |
"*inject_test.TypeAnswerStruct", |
| 850 |
}) |
| 851 |
} |
| 852 |
|
| 853 |
type TypeForGraphObjects struct { |
| 854 |
TypeNestedStruct `inject:"inline"` |
| 855 |
A *TypeNestedStruct `inject:"foo"` |
| 856 |
E struct { |
| 857 |
B *TypeNestedStruct `inject:""` |
| 858 |
} `inject:"inline"` |
| 859 |
} |
| 860 |
|
| 861 |
func TestGraphObjects(t *testing.T) { |
| 862 |
var g inject.Graph |
| 863 |
err := g.Provide( |
| 864 |
&inject.Object{Value: &TypeNestedStruct{}, Name: "foo"}, |
| 865 |
&inject.Object{Value: &TypeForGraphObjects{}}, |
| 866 |
) |
| 867 |
ensure.Nil(t, err) |
| 868 |
ensure.Nil(t, g.Populate()) |
| 869 |
|
| 870 |
var actual []string |
| 871 |
for _, o := range g.Objects() { |
| 872 |
actual = append(actual, fmt.Sprint(o)) |
| 873 |
} |
| 874 |
|
| 875 |
ensure.SameElements(t, actual, []string{ |
| 876 |
"*inject_test.TypeAnswerStruct", |
| 877 |
"*inject_test.TypeForGraphObjects", |
| 878 |
"*inject_test.TypeNestedStruct named foo", |
| 879 |
"*inject_test.TypeNestedStruct", |
| 880 |
`*struct { B *inject_test.TypeNestedStruct "inject:\"\"" }`, |
| 881 |
}) |
| 882 |
} |
| 883 |
|
| 884 |
type logger struct { |
| 885 |
Expected []string |
| 886 |
T testing.TB |
| 887 |
next int |
| 888 |
} |
| 889 |
|
| 890 |
func (l *logger) Debugf(f string, v ...interface{}) { |
| 891 |
actual := fmt.Sprintf(f, v...) |
| 892 |
if l.next == len(l.Expected) { |
| 893 |
l.T.Fatalf(`unexpected log "%s"`, actual) |
| 894 |
} |
| 895 |
expected := l.Expected[l.next] |
| 896 |
if actual != expected { |
| 897 |
l.T.Fatalf(`expected log "%s" got "%s"`, expected, actual) |
| 898 |
} |
| 899 |
l.next++ |
| 900 |
} |
| 901 |
|
| 902 |
type TypeForLoggingInterface interface { |
| 903 |
Foo() |
| 904 |
} |
| 905 |
|
| 906 |
type TypeForLoggingCreated struct{} |
| 907 |
|
| 908 |
func (t TypeForLoggingCreated) Foo() {} |
| 909 |
|
| 910 |
type TypeForLoggingEmbedded struct { |
| 911 |
TypeForLoggingCreated *TypeForLoggingCreated `inject:""` |
| 912 |
TypeForLoggingInterface TypeForLoggingInterface `inject:""` |
| 913 |
TypeForLoggingCreatedNamed *TypeForLoggingCreated `inject:"name_for_logging"` |
| 914 |
Map map[string]string `inject:"private"` |
| 915 |
} |
| 916 |
|
| 917 |
type TypeForLogging struct { |
| 918 |
TypeForLoggingEmbedded `inject:"inline"` |
| 919 |
TypeForLoggingCreated *TypeForLoggingCreated `inject:""` |
| 920 |
} |
| 921 |
|
| 922 |
func TestInjectLogging(t *testing.T) { |
| 923 |
g := inject.Graph{ |
| 924 |
Logger: &logger{ |
| 925 |
Expected: []string{ |
| 926 |
"provided *inject_test.TypeForLoggingCreated named name_for_logging", |
| 927 |
"provided *inject_test.TypeForLogging", |
| 928 |
"provided embedded *inject_test.TypeForLoggingEmbedded", |
| 929 |
"created *inject_test.TypeForLoggingCreated", |
| 930 |
"assigned newly created *inject_test.TypeForLoggingCreated to field TypeForLoggingCreated in *inject_test.TypeForLogging", |
| 931 |
"assigned existing *inject_test.TypeForLoggingCreated to field TypeForLoggingCreated in *inject_test.TypeForLoggingEmbedded", |
| 932 |
"assigned *inject_test.TypeForLoggingCreated named name_for_logging to field TypeForLoggingCreatedNamed in *inject_test.TypeForLoggingEmbedded", |
| 933 |
"made map for field Map in *inject_test.TypeForLoggingEmbedded", |
| 934 |
"assigned existing *inject_test.TypeForLoggingCreated to interface field TypeForLoggingInterface in *inject_test.TypeForLoggingEmbedded", |
| 935 |
}, |
| 936 |
T: t, |
| 937 |
}, |
| 938 |
} |
| 939 |
var v TypeForLogging |
| 940 |
|
| 941 |
err := g.Provide( |
| 942 |
&inject.Object{Value: &TypeForLoggingCreated{}, Name: "name_for_logging"}, |
| 943 |
&inject.Object{Value: &v}, |
| 944 |
) |
| 945 |
if err != nil { |
| 946 |
t.Fatal(err) |
| 947 |
} |
| 948 |
if err := g.Populate(); err != nil { |
| 949 |
t.Fatal(err) |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
type TypeForNamedWithUnnamedDepSecond struct{} |
| 954 |
|
| 955 |
type TypeForNamedWithUnnamedDepFirst struct { |
| 956 |
TypeForNamedWithUnnamedDepSecond *TypeForNamedWithUnnamedDepSecond `inject:""` |
| 957 |
} |
| 958 |
|
| 959 |
type TypeForNamedWithUnnamed struct { |
| 960 |
TypeForNamedWithUnnamedDepFirst *TypeForNamedWithUnnamedDepFirst `inject:""` |
| 961 |
} |
| 962 |
|
| 963 |
func TestForNamedWithUnnamed(t *testing.T) { |
| 964 |
var g inject.Graph |
| 965 |
var v TypeForNamedWithUnnamed |
| 966 |
|
| 967 |
err := g.Provide( |
| 968 |
&inject.Object{Value: &v, Name: "foo"}, |
| 969 |
) |
| 970 |
if err != nil { |
| 971 |
t.Fatal(err) |
| 972 |
} |
| 973 |
if err := g.Populate(); err != nil { |
| 974 |
t.Fatal(err) |
| 975 |
} |
| 976 |
if v.TypeForNamedWithUnnamedDepFirst == nil { |
| 977 |
t.Fatal("expected TypeForNamedWithUnnamedDepFirst to be populated") |
| 978 |
} |
| 979 |
if v.TypeForNamedWithUnnamedDepFirst.TypeForNamedWithUnnamedDepSecond == nil { |
| 980 |
t.Fatal("expected TypeForNamedWithUnnamedDepSecond to be populated") |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
func TestForSameNameButDifferentPackage(t *testing.T) { |
| 985 |
var g inject.Graph |
| 986 |
err := g.Provide( |
| 987 |
&inject.Object{Value: &injecttesta.Foo{}}, |
| 988 |
&inject.Object{Value: &injecttestb.Foo{}}, |
| 989 |
) |
| 990 |
if err != nil { |
| 991 |
t.Fatal(err) |
| 992 |
} |
| 993 |
if err := g.Populate(); err != nil { |
| 994 |
t.Fatal(err) |
| 995 |
} |
| 996 |
} |