Created
May 2, 2012 17:16
-
-
Save jamesrcounts/2578385 to your computer and use it in GitHub Desktop.
A patch to handle Write-Only properties in WritePropertiesToString<T>. Are write only properties a good idea? I don't know. Did I find one in legacy code? Yes. Did I write that legacy code? Yes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: ApprovalUtilities.Tests/Utilities/StringUtilitiesTest.cs | |
=================================================================== | |
--- ApprovalUtilities.Tests/Utilities/StringUtilitiesTest.cs (revision 408) | |
+++ ApprovalUtilities.Tests/Utilities/StringUtilitiesTest.cs (working copy) | |
@@ -26,12 +26,34 @@ | |
[TestMethod] | |
public void WritePropertiesToStringTest() | |
{ | |
- var anonymous = new | |
+ var anonymous = new | |
{ | |
SomeString = "Hello", | |
SomeInt = 10 | |
}; | |
Approvals.Verify(anonymous.WritePropertiesToString()); | |
} | |
+ | |
+ [TestMethod] | |
+ public void WriteOnlyPropertyTest() | |
+ { | |
+ var target = new TestingObject() { WriteOnlyString = "Hello", ReadWriteInt = 10 }; | |
+ Approvals.Verify(target.WritePropertiesToString()); | |
+ } | |
+ | |
+ public class TestingObject | |
+ { | |
+ private string _WriteOnlyString; | |
+ | |
+ public string WriteOnlyString | |
+ { | |
+ set | |
+ { | |
+ _WriteOnlyString = value; | |
+ } | |
+ } | |
+ | |
+ public int ReadWriteInt { get; set; } | |
+ } | |
} | |
-} | |
+} | |
\ No newline at end of file | |
Index: ApprovalUtilities.Tests/Utilities/StringUtilitiesTest.WriteOnlyPropertyTest.approved.txt | |
=================================================================== | |
--- ApprovalUtilities.Tests/Utilities/StringUtilitiesTest.WriteOnlyPropertyTest.approved.txt (revision 0) | |
+++ ApprovalUtilities.Tests/Utilities/StringUtilitiesTest.WriteOnlyPropertyTest.approved.txt (working copy) | |
@@ -0,0 +1,4 @@ | |
+TestingObject | |
+{ | |
+ ReadWriteInt: 10 | |
+} | |
Index: ApprovalUtilities/Utilities/StringUtils.cs | |
=================================================================== | |
--- ApprovalUtilities/Utilities/StringUtils.cs (revision 408) | |
+++ ApprovalUtilities/Utilities/StringUtils.cs (working copy) | |
@@ -1,7 +1,7 @@ | |
using System; | |
using System.Collections; | |
+using System.Collections.Generic; | |
using System.Text; | |
-using System.Collections.Generic; | |
namespace ApprovalUtilities.Utilities | |
{ | |
@@ -107,15 +107,19 @@ | |
Type t = typeof(T); | |
var sb = new StringBuilder(); | |
- sb.Append(t.Name + "\r\n{\r\n"); | |
+ sb.AppendLine(t.Name); | |
+ sb.AppendLine("{"); | |
foreach (var p in t.GetProperties()) | |
{ | |
- var propertyValue = p.GetValue(value, new object[0]) ?? "NULL"; | |
- sb.AppendFormat("\t{0}: {1}", p.Name, propertyValue).AppendLine(); | |
+ if (p.CanRead) | |
+ { | |
+ var propertyValue = p.GetValue(value, new object[0]) ?? "NULL"; | |
+ sb.AppendFormat("\t{0}: {1}", p.Name, propertyValue).AppendLine(); | |
+ } | |
} | |
- sb.Append("}\r\n"); | |
+ sb.AppendLine("}"); | |
return sb.ToString(); | |
} | |
- } | |
+ } | |
} | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment