Skip to content

Commit 355e63d

Browse files
Add integration tests
1 parent be52139 commit 355e63d

16 files changed

+13238
-22
lines changed

src/Pure.DI.Core/Core/Semantic.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ when memberAccessExpressionSyntax.IsKind(SyntaxKind.SimpleMemberAccessExpression
9797
switch (classIdentifierName.Identifier.Text)
9898
{
9999
case nameof(CompositionKind) when IsSpecialType(semanticModel, node, SpecialType.CompositionKind):
100-
if (Enum.TryParse<CompositionKind>(valueStr, out var compositionKindValue))
100+
if (Enum.TryParse<CompositionKind>(valueStr, out var compositionKindValue) && compositionKindValue is T compositionKind)
101101
{
102-
return (T)(object)compositionKindValue;
102+
return compositionKind;
103103
}
104104

105105
break;
106106

107107
case nameof(Lifetime) when IsSpecialType(semanticModel, node, SpecialType.Lifetime):
108-
if (Enum.TryParse<Lifetime>(valueStr, out var lifetimeValue))
108+
if (Enum.TryParse<Lifetime>(valueStr, out var lifetimeValue) && lifetimeValue is T lifetime)
109109
{
110-
return (T)(object)lifetimeValue;
110+
return lifetime;
111111
}
112112

113113
break;
@@ -192,7 +192,10 @@ when memberAccessExpressionSyntax.IsKind(SyntaxKind.SimpleMemberAccessExpression
192192
}
193193

194194
var injectionSite = injectionSiteFactory.CreateInjectionSite(methodArgName.Expression, method, methodArg);
195-
return (T)MdTag.CreateTagOnValue(invocationExpressionSyntax, ImmutableArray.Create(injectionSite));
195+
if (MdTag.CreateTagOnValue(invocationExpressionSyntax, ImmutableArray.Create(injectionSite)) is T tagValue)
196+
{
197+
return tagValue;
198+
}
196199
}
197200

198201
break;
@@ -238,14 +241,14 @@ when memberAccessExpressionSyntax.IsKind(SyntaxKind.SimpleMemberAccessExpression
238241
}
239242

240243
var operation = semanticModel.GetOperation(node);
241-
if (operation?.ConstantValue.Value is T value)
244+
if (operation?.ConstantValue.Value is T val2)
242245
{
243-
return value;
246+
return val2;
244247
}
245248

246-
if (typeof(T) == typeof(object) && operation is ITypeOfOperation { TypeOperand: T val })
249+
if (typeof(T) == typeof(object) && operation is ITypeOfOperation { TypeOperand: T val3 })
247250
{
248-
return val;
251+
return val3;
249252
}
250253
}
251254

tests/Pure.DI.IntegrationTests/AutoBindingTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,14 @@ public static void Main()
218218
}
219219
}
220220
}
221-
""".RunAsync(new Options { LanguageVersion = LanguageVersion.CSharp12 });
221+
""".RunAsync(new Options { LanguageVersion = LanguageVersion.CSharp10 });
222222

223223
// Then
224224
result.Success.ShouldBeTrue(result);
225225
result.StdOut.ShouldBe(["Default"], result);
226226
}
227227

228+
#if ROSLYN4_8_OR_GREATER
228229
[Fact]
229230
public async Task ShouldSupportAutoBindingForClassWithPrimaryConstructor()
230231
{
@@ -273,6 +274,7 @@ public static void Main()
273274
// Then
274275
result.Success.ShouldBeTrue(result);
275276
}
277+
#endif
276278

277279
[Fact]
278280
public async Task ShouldNotSupportAutoBindingForInterface()

tests/Pure.DI.IntegrationTests/BuildersTests.cs

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,4 +1064,235 @@ public static void Main()
10641064
result.Success.ShouldBeTrue(result);
10651065
result.StdOut.ShouldBe(["True"], result);
10661066
}
1067+
1068+
[Fact]
1069+
public async Task ShouldSupportBuilderWithArgs()
1070+
{
1071+
// Given
1072+
1073+
// When
1074+
var result = await """
1075+
using System;
1076+
using Pure.DI;
1077+
#pragma warning disable CS8618
1078+
1079+
namespace Sample
1080+
{
1081+
interface IDependency { string Name { get; } }
1082+
class Dependency: IDependency
1083+
{
1084+
public Dependency(string name) => Name = name;
1085+
public string Name { get; }
1086+
}
1087+
1088+
class Service
1089+
{
1090+
[Ordinal(0)]
1091+
public IDependency Dep { get; set; }
1092+
}
1093+
1094+
static class Setup
1095+
{
1096+
private static void SetupComposition()
1097+
{
1098+
DI.Setup("Composition")
1099+
.Arg<string>("depName")
1100+
.Bind<IDependency>().To<Dependency>()
1101+
.Builder<Service>("BuildUpService");
1102+
}
1103+
}
1104+
1105+
public class Program
1106+
{
1107+
public static void Main()
1108+
{
1109+
var composition = new Composition(depName: "ArgValue");
1110+
var service = new Service();
1111+
composition.BuildUpService(service);
1112+
Console.WriteLine(service.Dep.Name);
1113+
}
1114+
}
1115+
}
1116+
""".RunAsync();
1117+
1118+
// Then
1119+
result.Success.ShouldBeTrue(result);
1120+
result.StdOut.ShouldBe(["ArgValue"], result);
1121+
}
1122+
1123+
[Fact]
1124+
public async Task ShouldSupportBuilderWithLifetimes()
1125+
{
1126+
// Given
1127+
1128+
// When
1129+
var result = await """
1130+
using System;
1131+
using Pure.DI;
1132+
#pragma warning disable CS8618
1133+
1134+
namespace Sample
1135+
{
1136+
interface IDependency {}
1137+
class Dependency: IDependency {}
1138+
1139+
class Service
1140+
{
1141+
[Ordinal(0)]
1142+
public IDependency Dep1 { get; set; }
1143+
1144+
[Ordinal(1)]
1145+
public IDependency Dep2 { get; set; }
1146+
}
1147+
1148+
static class Setup
1149+
{
1150+
private static void SetupComposition()
1151+
{
1152+
DI.Setup("Composition")
1153+
.Bind<IDependency>().As(Lifetime.Singleton).To<Dependency>()
1154+
.Builder<Service>("BuildUpService");
1155+
}
1156+
}
1157+
1158+
public class Program
1159+
{
1160+
public static void Main()
1161+
{
1162+
var composition = new Composition();
1163+
var service1 = new Service();
1164+
composition.BuildUpService(service1);
1165+
1166+
var service2 = new Service();
1167+
composition.BuildUpService(service2);
1168+
1169+
Console.WriteLine(ReferenceEquals(service1.Dep1, service1.Dep2));
1170+
Console.WriteLine(ReferenceEquals(service1.Dep1, service2.Dep1));
1171+
}
1172+
}
1173+
}
1174+
""".RunAsync();
1175+
1176+
// Then
1177+
result.Success.ShouldBeTrue(result);
1178+
result.StdOut.ShouldBe(["True", "True"], result);
1179+
}
1180+
1181+
[Fact]
1182+
public async Task ShouldSupportBuilderWithPartialClasses()
1183+
{
1184+
// Given
1185+
1186+
// When
1187+
var result = await """
1188+
using System;
1189+
using Pure.DI;
1190+
#pragma warning disable CS8618
1191+
1192+
namespace Sample
1193+
{
1194+
interface IDependency {}
1195+
class Dependency: IDependency {}
1196+
1197+
partial class Service
1198+
{
1199+
[Ordinal(0)]
1200+
public IDependency Dep1 { get; set; }
1201+
}
1202+
1203+
partial class Service
1204+
{
1205+
[Ordinal(1)]
1206+
public IDependency Dep2 { get; set; }
1207+
}
1208+
1209+
static class Setup
1210+
{
1211+
private static void SetupComposition()
1212+
{
1213+
DI.Setup("Composition")
1214+
.Bind<IDependency>().To<Dependency>()
1215+
.Builder<Service>("BuildUpService");
1216+
}
1217+
}
1218+
1219+
public class Program
1220+
{
1221+
public static void Main()
1222+
{
1223+
var composition = new Composition();
1224+
var service = new Service();
1225+
composition.BuildUpService(service);
1226+
1227+
Console.WriteLine(service.Dep1 != null);
1228+
Console.WriteLine(service.Dep2 != null);
1229+
}
1230+
}
1231+
}
1232+
""".RunAsync();
1233+
1234+
// Then
1235+
result.Success.ShouldBeTrue(result);
1236+
result.StdOut.ShouldBe(["True", "True"], result);
1237+
}
1238+
1239+
[Fact]
1240+
public async Task ShouldSupportBuilderWithDeepHierarchy()
1241+
{
1242+
// Given
1243+
1244+
// When
1245+
var result = await """
1246+
using System;
1247+
using Pure.DI;
1248+
#pragma warning disable CS8618
1249+
1250+
namespace Sample
1251+
{
1252+
interface IDependency1 {}
1253+
class Dependency1: IDependency1 {}
1254+
1255+
interface IDependency2 { IDependency1 Dep1 { get; } }
1256+
class Dependency2: IDependency2
1257+
{
1258+
public Dependency2(IDependency1 dep1) => Dep1 = dep1;
1259+
public IDependency1 Dep1 { get; }
1260+
}
1261+
1262+
class Service
1263+
{
1264+
[Ordinal(0)]
1265+
public IDependency2 Dep2 { get; set; }
1266+
}
1267+
1268+
static class Setup
1269+
{
1270+
private static void SetupComposition()
1271+
{
1272+
DI.Setup("Composition")
1273+
.Bind<IDependency1>().To<Dependency1>()
1274+
.Bind<IDependency2>().To<Dependency2>()
1275+
.Builder<Service>("BuildUpService");
1276+
}
1277+
}
1278+
1279+
public class Program
1280+
{
1281+
public static void Main()
1282+
{
1283+
var composition = new Composition();
1284+
var service = new Service();
1285+
composition.BuildUpService(service);
1286+
1287+
Console.WriteLine(service.Dep2 != null);
1288+
Console.WriteLine(service.Dep2!.Dep1 != null);
1289+
}
1290+
}
1291+
}
1292+
""".RunAsync();
1293+
1294+
// Then
1295+
result.Success.ShouldBeTrue(result);
1296+
result.StdOut.ShouldBe(["True", "True"], result);
1297+
}
10671298
}

0 commit comments

Comments
 (0)