(t *testing.T)
| 1062 | } |
| 1063 | |
| 1064 | func TestDefaultContainer_ResolveAs(t *testing.T) { |
| 1065 | var testCases = []struct { |
| 1066 | name string |
| 1067 | ctx context.Context |
| 1068 | preCondition func(container Container) |
| 1069 | instanceName string |
| 1070 | instanceType reflect.Type |
| 1071 | |
| 1072 | wantErr error |
| 1073 | }{ |
| 1074 | { |
| 1075 | name: "empty name", |
| 1076 | wantErr: errors.New("empty name"), |
| 1077 | instanceName: "", |
| 1078 | }, |
| 1079 | { |
| 1080 | name: "nil type", |
| 1081 | wantErr: errors.New("nil type"), |
| 1082 | instanceName: "anyInstanceName", |
| 1083 | instanceType: nil, |
| 1084 | }, |
| 1085 | { |
| 1086 | name: "assignable type", |
| 1087 | preCondition: func(container Container) { |
| 1088 | _ = container.RegisterSingleton("anyInstanceName", AnyComponent{}) |
| 1089 | }, |
| 1090 | instanceName: "anyInstanceName", |
| 1091 | instanceType: reflect.TypeFor[AnyComponent](), |
| 1092 | wantErr: nil, |
| 1093 | }, |
| 1094 | { |
| 1095 | name: "not assignable type", |
| 1096 | preCondition: func(container Container) { |
| 1097 | _ = container.RegisterSingleton("anyInstanceName", AnyComponent{}) |
| 1098 | }, |
| 1099 | instanceName: "anyInstanceName", |
| 1100 | instanceType: reflect.TypeFor[AnotherComponent](), |
| 1101 | wantErr: errors.New("component \"anyInstanceName\" is not assignable to component.AnotherComponent"), |
| 1102 | }, |
| 1103 | } |
| 1104 | |
| 1105 | for _, tc := range testCases { |
| 1106 | t.Run(tc.name, func(t *testing.T) { |
| 1107 | // given |
| 1108 | container := NewDefaultContainer() |
| 1109 | if tc.preCondition != nil { |
| 1110 | tc.preCondition(container) |
| 1111 | } |
| 1112 | |
| 1113 | result, err := container.ResolveAs(tc.ctx, tc.instanceName, tc.instanceType) |
| 1114 | // when |
| 1115 | |
| 1116 | // then |
| 1117 | if tc.wantErr != nil { |
| 1118 | require.Error(t, err) |
| 1119 | require.EqualError(t, err, tc.wantErr.Error()) |
| 1120 | return |
| 1121 | } |
nothing calls this directly
no test coverage detected