Skip to content

Commit

Permalink
Fix tests and extend README for multi-parameter With, bump nuget
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhailshilkov committed Apr 3, 2017
1 parent d597579 commit 14bc748
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 70 deletions.
131 changes: 76 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,93 @@ https://nuget.org/packages/With.Fody/

## Your Code

public class MyClass
``` cs
public class MyClass
{
public MyClass(int intValue, string stringValue, OtherClass c1, OtherClass c2)
{
public MyClass(int intValue, string stringValue, OtherClass c1, OtherClass c2)
{
this.IntValue = intValue;
this.StringValue = stringValue;
this.C1 = c1;
this.C2 = c2;
}
this.IntValue = intValue;
this.StringValue = stringValue;
this.C1 = c1;
this.C2 = c2;
}

public int IntValue { get; }
public int IntValue { get; }

public string StringValue { get; }
public string StringValue { get; }

public OtherClass C1 { get; }
public OtherClass C1 { get; }

public OtherClass C2 { get; }
public OtherClass C2 { get; }

// Needed for IntelliSense/Resharper support
public MyClass With<T>(T value) => this;
// If two properties have same type, we need to append the property name to With
public MyClass WithC1(OtherClass value) => this;
public MyClass WithC2(OtherClass value) => this;
}
// Needed for IntelliSense/Resharper support
public MyClass With<T>(T value) => this;

// If two properties have same type, we need to append the property name to With
public MyClass WithC1(OtherClass value) => this;
public MyClass WithC2(OtherClass value) => this;

// We can make an explicit version of With for multiple parameters
public MyClass With(int intValue, string stringValue) => this;

// Method name can be more explicit, and parameter names should match property names
public MyClass WithC1andC2(OtherClass c1, OtherClass c2) => this;
}
```

## What gets compiled

public class MyClass
``` cs
public class MyClass
{
public MyClass(int intValue, string stringValue, OtherClass c1, OtherClass c2)
{
this.IntValue = intValue;
this.StringValue = stringValue;
this.C1 = c1;
this.C2 = c2;
}

public int IntValue { get; }

public string StringValue { get; }

public OtherClass C1 { get; }

public OtherClass C2 { get; }

public MyClass With(int value)
{
return new MyClass(value, this.StringValue, this.C1, this.C2);
}

public MyClass With(string value)
{
return new MyClass(this.IntValue, value, this.C1, this.C2);
}

// If two properties have same type, we need to append the property name to With
public MyClass WithC1(OtherClass value)
{
return new MyClass(this.IntValue, this.StringValue, value, this.C2);
}

public MyClass WithC2(OtherClass value)
{
return new MyClass(this.IntValue, this.StringValue, this.C1, value);
}

public MyClass With(int intValue, string stringValue)
{
return new MyClass(intValue, stringValue, this.C1, this.C2);
}

public MyClass WithC1andC2(OtherClass c1, OtherClass c2)
{
public MyClass(int intValue, string stringValue, OtherClass c1, OtherClass c2)
{
this.IntValue = intValue;
this.StringValue = stringValue;
this.C1 = c1;
this.C2 = c2;
}

public int IntValue { get; }

public string StringValue { get; }

public OtherClass C1 { get; }

public OtherClass C2 { get; }

public MyClass With(int value)
{
return new MyClass(value, this.StringValue, this.C1, this.C2);
}

public MyClass With(string value)
{
return new MyClass(this.IntValue, value, this.C1, this.C2);
}

// If two properties have same type, we need to append the property name to With
public MyClass WithC1(OtherClass value)
{
return new MyClass(this.IntValue, this.StringValue, value, this.C2);
}

public MyClass WithC2(OtherClass value)
{
return new MyClass(this.IntValue, this.StringValue, this.C1, value);
}
return new MyClass(this.IntValue, this.StringValue, c1, c2);
}
}
```

## Motivation

Expand Down
30 changes: 15 additions & 15 deletions Tests/WeaverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,19 @@ public void PrimitiveValues_ShortWithIsInjected()
Assert.AreEqual(31231, result3.Value3);

var result4 = instance.With(123, "World");
Assert.AreEqual(123, result1.Value1);
Assert.AreEqual("World", result2.Value2);
Assert.AreEqual(31231, result3.Value3);
Assert.AreEqual(123, result4.Value1);
Assert.AreEqual("World", result4.Value2);
Assert.AreEqual(instance.Value3, result4.Value3);

var result5 = instance.With(123, (long)31231);
Assert.AreEqual(123, result1.Value1);
Assert.AreEqual(instance.Value2, result3.Value2);
Assert.AreEqual(31231, result3.Value3);
Assert.AreEqual(123, result5.Value1);
Assert.AreEqual(instance.Value2, result5.Value2);
Assert.AreEqual(31231, result5.Value3);

var result6 = instance.WithSecondAndThird("World", (long)31231);
Assert.AreEqual(instance.Value1, result2.Value1);
Assert.AreEqual("World", result2.Value2);
Assert.AreEqual(31231, result3.Value3);
Assert.AreEqual(instance.Value1, result6.Value1);
Assert.AreEqual("World", result6.Value2);
Assert.AreEqual(31231, result6.Value3);
}

[Test]
Expand All @@ -199,14 +199,14 @@ public void PropertiesOfSameType_LongNamedWithIsInjected()
Assert.AreEqual(333, result3.Value3);

var result4 = instance.WithValue1Value2(111, 222);
Assert.AreEqual(111, result1.Value1);
Assert.AreEqual(222, result2.Value2);
Assert.AreEqual(instance.Value3, result1.Value3);
Assert.AreEqual(111, result4.Value1);
Assert.AreEqual(222, result4.Value2);
Assert.AreEqual(instance.Value3, result4.Value3);

var result5 = instance.WithSecondAndThird(222, 333);
Assert.AreEqual(instance.Value1, result3.Value1);
Assert.AreEqual(222, result2.Value2);
Assert.AreEqual(333, result3.Value3);
Assert.AreEqual(instance.Value1, result5.Value1);
Assert.AreEqual(222, result5.Value2);
Assert.AreEqual(333, result5.Value3);
}

[Test]
Expand Down

0 comments on commit 14bc748

Please sign in to comment.