Skip to content

Commit b6148f5

Browse files
committed
Copy constructor bug fixed
1 parent e229a8f commit b6148f5

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Tynamix.ObjectFiller;
4+
5+
namespace ObjectFiller.Test
6+
{
7+
[TestClass]
8+
public class CopyConstructorTest
9+
{
10+
11+
[TestMethod]
12+
public void WhenClassWithCopyConstructorIsCreatedNoExceptionShallBeThrown()
13+
{
14+
var f = new Filler<ClassWithCopyConstructorAndNormalConstructor>();
15+
var cc = f.Create();
16+
17+
Assert.IsNotNull(cc);
18+
}
19+
20+
[TestMethod]
21+
[ExpectedException(typeof(InvalidOperationException))]
22+
public void WhenClassWithJustCopyConstructorIsCreatedAExceptionShallBeThrown()
23+
{
24+
var f = new Filler<ClassWithCopyConstructor>();
25+
var cc = f.Create();
26+
27+
}
28+
}
29+
30+
public class ClassWithCopyConstructor
31+
{
32+
33+
public ClassWithCopyConstructor(ClassWithCopyConstructor constructor)
34+
{
35+
Count1 = constructor.Count1;
36+
Count2 = constructor.Count2;
37+
}
38+
39+
public int Count1 { get; set; }
40+
41+
public int Count2 { get; set; }
42+
}
43+
44+
45+
public class ClassWithCopyConstructorAndNormalConstructor
46+
{
47+
public ClassWithCopyConstructorAndNormalConstructor(int count1, int count2)
48+
{
49+
Count1 = count1;
50+
Count2 = count2;
51+
}
52+
53+
public ClassWithCopyConstructorAndNormalConstructor(ClassWithCopyConstructorAndNormalConstructor constructor)
54+
: this(constructor.Count1, constructor.Count2)
55+
{
56+
57+
}
58+
59+
public int Count1 { get; set; }
60+
61+
public int Count2 { get; set; }
62+
}
63+
}

ObjectFiller.Test/ObjectFiller.Test.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
</ItemGroup>
4747
<ItemGroup>
4848
<Compile Include="AddressFillingTest.cs" />
49+
<Compile Include="CopyConstructorTest.cs" />
4950
<Compile Include="CountryNamesPlugin.cs" />
5051
<Compile Include="CityNamesPluginTest.cs" />
5152
<Compile Include="CreateInstanceTest.cs" />

ObjectFiller/Filler.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,13 @@ private object CreateInstanceOfType(Type type, FillerSetupItem currentSetupItem,
536536
if (type.GetConstructors().All(ctor => ctor.GetParameters().Length != 0))
537537
{
538538
IEnumerable<ConstructorInfo> ctorInfos;
539-
if ((ctorInfos = type.GetConstructors().Where(ctr => ctr.GetParameters().Length != 0)).Count() != 0)
539+
if ((ctorInfos = type.GetConstructors().Where(ctr => ctr.GetParameters().Length != 0)).Any())
540540
{
541541
foreach (ConstructorInfo ctorInfo in ctorInfos.OrderBy(x => x.GetParameters().Length))
542542
{
543543
Type[] paramTypes = ctorInfo.GetParameters().Select(p => p.ParameterType).ToArray();
544544

545-
if (paramTypes.All(t => TypeIsValidForObjectFiller(t, currentSetupItem)))
545+
if (paramTypes.All(ctorParamType => TypeIsValidForObjectFiller(ctorParamType, currentSetupItem) && ctorParamType != type))
546546
{
547547
foreach (Type paramType in paramTypes)
548548
{

ObjectFiller/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
3333
// übernehmen, indem Sie "*" eingeben:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.3.6.0")]
36-
[assembly: AssemblyFileVersion("1.4.6.0")]
35+
[assembly: AssemblyVersion("1.3.9.0")]
36+
[assembly: AssemblyFileVersion("1.3.9.0")]
3737

3838
[assembly: InternalsVisibleTo("ObjectFiller.Test")]

0 commit comments

Comments
 (0)