MCPcopy Create free account
hub / github.com/codnect/procyon / TestDefaultContainer_RegisterSingleton

Function TestDefaultContainer_RegisterSingleton

component/container_test.go:522–581  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

520}
521
522func TestDefaultContainer_RegisterSingleton(t *testing.T) {
523 testCases := []struct {
524 name string
525 preCondition func(container Container)
526 instanceName string
527 instance any
528
529 wantErr error
530 }{
531 {
532 name: "empty name",
533 instanceName: "",
534 wantErr: errors.New("empty name"),
535 },
536 {
537 name: "nil instance",
538 instanceName: "anyInstanceName",
539 instance: nil,
540 wantErr: errors.New("nil instance"),
541 },
542 {
543 name: "already registered",
544 preCondition: func(container Container) {
545 _ = container.RegisterSingleton("anyInstanceName", AnyComponent{})
546 },
547 instanceName: "anyInstanceName",
548 instance: AnyComponent{},
549 wantErr: ErrInstanceAlreadyExists,
550 },
551 {
552 name: "valid singleton",
553 instanceName: "anyInstanceName",
554 instance: &AnyComponent{},
555 wantErr: nil,
556 },
557 }
558
559 for _, tc := range testCases {
560 t.Run(tc.name, func(t *testing.T) {
561 // given
562 container := NewDefaultContainer()
563
564 if tc.preCondition != nil {
565 tc.preCondition(container)
566 }
567
568 // when
569 err := container.RegisterSingleton(tc.instanceName, tc.instance)
570
571 // then
572 if tc.wantErr != nil {
573 require.Error(t, err)
574 require.EqualError(t, err, tc.wantErr.Error())
575 return
576 }
577
578 assert.NoError(t, err)
579 })

Callers

nothing calls this directly

Calls 3

RegisterSingletonMethod · 0.95
NewDefaultContainerFunction · 0.85
RunMethod · 0.65

Tested by

no test coverage detected