(t *testing.T)
| 622 | } |
| 623 | |
| 624 | func TestDefaultContainer_Singleton(t *testing.T) { |
| 625 | anySingleton := &AnyComponent{} |
| 626 | |
| 627 | testCases := []struct { |
| 628 | name string |
| 629 | preCondition func(container Container) |
| 630 | singletonName string |
| 631 | |
| 632 | wantResult bool |
| 633 | wantSingleton any |
| 634 | }{ |
| 635 | { |
| 636 | name: "no singleton", |
| 637 | singletonName: "anySingletonName", |
| 638 | wantResult: false, |
| 639 | }, |
| 640 | { |
| 641 | name: "valid singleton", |
| 642 | preCondition: func(container Container) { |
| 643 | _ = container.RegisterSingleton("anySingletonName", anySingleton) |
| 644 | }, |
| 645 | singletonName: "anySingletonName", |
| 646 | wantResult: true, |
| 647 | wantSingleton: anySingleton, |
| 648 | }, |
| 649 | } |
| 650 | |
| 651 | for _, tc := range testCases { |
| 652 | t.Run(tc.name, func(t *testing.T) { |
| 653 | // given |
| 654 | container := NewDefaultContainer() |
| 655 | |
| 656 | if tc.preCondition != nil { |
| 657 | tc.preCondition(container) |
| 658 | } |
| 659 | |
| 660 | // when |
| 661 | singleton, exists := container.Singleton(tc.singletonName) |
| 662 | |
| 663 | // then |
| 664 | require.Equal(t, tc.wantResult, exists) |
| 665 | require.Equal(t, tc.wantSingleton, singleton) |
| 666 | }) |
| 667 | } |
| 668 | |
| 669 | } |
| 670 | |
| 671 | func TestDefaultContainer_RemoveSingleton(t *testing.T) { |
| 672 | testCases := []struct { |
nothing calls this directly
no test coverage detected