Skip to content

Commit d3b337b

Browse files
committed
Added parameters to configure Fody.ToString output
@otebos
1 parent e5c58d4 commit d3b337b

8 files changed

Lines changed: 364 additions & 54 deletions

File tree

README.md

Lines changed: 110 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ The `Update-Package Fody` is required since NuGet always defaults to the oldest,
3333
Add `<ToString/>` to [FodyWeavers.xml](https://github.com/Fody/Fody#add-fodyweaversxml)
3434

3535
```xml
36-
<?xml version="1.0" encoding="utf-8" ?>
3736
<Weavers>
3837
<ToString/>
3938
</Weavers>
@@ -42,37 +41,122 @@ Add `<ToString/>` to [FodyWeavers.xml](https://github.com/Fody/Fody#add-fodyweav
4241

4342
## Your Code
4443

45-
[ToString]
46-
class TestClass
47-
{
48-
public int Foo { get; set; }
49-
50-
public double Bar { get; set; }
51-
52-
[IgnoreDuringToString]
53-
public string Baz { get; set; }
54-
}
44+
```csharp
45+
[ToString]
46+
class TestClass
47+
{
48+
public int Foo { get; set; }
49+
50+
public double Bar { get; set; }
51+
52+
[IgnoreDuringToString]
53+
public string Baz { get; set; }
54+
}
55+
```
5556

5657

5758
## What gets compiled
5859

59-
class TestClass
60+
```csharp
61+
class TestClass
62+
{
63+
public int Foo { get; set; }
64+
65+
public double Bar { get; set; }
66+
67+
public string Baz { get; set; }
68+
69+
public override string ToString()
6070
{
61-
public int Foo { get; set; }
62-
63-
public double Bar { get; set; }
64-
65-
public string Baz { get; set; }
66-
67-
public override string ToString()
68-
{
69-
return string.Format(
70-
CultureInfo.InvariantCulture,
71-
"{{T: TestClass, Foo: {0}, Bar: {1}}}",
72-
this.Foo,
73-
this.Bar);
74-
}
71+
return string.Format(
72+
CultureInfo.InvariantCulture,
73+
"{{T: TestClass, Foo: {0}, Bar: {1}}}",
74+
Foo,
75+
Bar);
7576
}
77+
}
78+
```
79+
80+
81+
## Options
82+
83+
84+
### PropertyNameToValueSeparator
85+
86+
Default: `: `
87+
88+
For example:
89+
90+
```xml
91+
<Weavers>
92+
<ToString PropertyNameToValueSeparator="->"/>
93+
</Weavers>
94+
```
95+
96+
97+
### PropertiesSeparator
98+
99+
Default: `, `
100+
101+
For example:
102+
103+
```xml
104+
<Weavers>
105+
<ToString PropertiesSeparator=". "/>
106+
</Weavers>
107+
```
108+
109+
110+
### WrapWithBrackets
111+
112+
Default: `true`
113+
114+
For example:
115+
116+
```xml
117+
<Weavers>
118+
<ToString WrapWithBrackets="false"/>
119+
</Weavers>
120+
```
121+
122+
123+
### WriteTypeName
124+
125+
Default: `true`
126+
127+
For example:
128+
129+
```xml
130+
<Weavers>
131+
<ToString WriteTypeName="false"/>
132+
</Weavers>
133+
```
134+
135+
136+
### ListStart
137+
138+
Default: `[`
139+
140+
For example:
141+
142+
```xml
143+
<Weavers>
144+
<ToString ListStart="("/>
145+
</Weavers>
146+
```
147+
148+
149+
### ListEnd
150+
151+
Default: `]`
152+
153+
For example:
154+
155+
```xml
156+
<Weavers>
157+
<ToString ListEnd=")"/>
158+
</Weavers>
159+
```
76160

77161

78162
## Icon

Tests/AttributesConfiguration.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class AttributesConfiguration
2+
{
3+
public string PropertyNameToValueSeparator;
4+
public string PropertiesSeparator;
5+
public bool? WrapWithBrackets;
6+
public bool? WriteTypeName;
7+
public string ListStart;
8+
public string ListEnd;
9+
}

Tests/AttributesTests.cs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using System;
2+
using System.Reflection;
3+
using Fody;
4+
using Xunit;
5+
6+
public class AttributesTests
7+
{
8+
string PropertyNameToValueSeparator = "$%^%$";
9+
string PropertiesSeparator = "$@#@$";
10+
bool WrapWithBrackets = false;
11+
bool WriteTypeName = false;
12+
string ListStart = "---[[[";
13+
string ListEnd = "]]]---";
14+
15+
public Assembly PrepareAssembly(string name, AttributesConfiguration configuration)
16+
{
17+
var config = TestHelper.PrepareConfig(configuration);
18+
var weavingTask = new ModuleWeaver()
19+
{
20+
Config = config
21+
22+
};
23+
var testResult = weavingTask.ExecuteTestRun("AssemblyToProcess.dll");
24+
return testResult.Assembly;
25+
}
26+
27+
[Fact]
28+
public void NormalClassTest_ShouldUseCustomPropertyNameToValueSeparator()
29+
{
30+
var assembly = PrepareAssembly("test1", new AttributesConfiguration { PropertyNameToValueSeparator = PropertyNameToValueSeparator });
31+
32+
var type = assembly.GetType("NormalClass");
33+
dynamic instance = Activator.CreateInstance(type);
34+
instance.X = 1;
35+
instance.Y = "2";
36+
instance.Z = 4.5;
37+
instance.V = 'C';
38+
39+
var result = instance.ToString();
40+
41+
Assert.Equal(
42+
string.Format("{{T{0}\"NormalClass\", X{0}1, Y{0}\"2\", Z{0}4.5, V{0}\"C\"}}", PropertyNameToValueSeparator),
43+
result);
44+
}
45+
46+
[Fact]
47+
public void NormalClassTest_ShouldUseCustomPropertiesSeparator()
48+
{
49+
var assembly = PrepareAssembly("test2", new AttributesConfiguration { PropertiesSeparator = PropertiesSeparator });
50+
51+
var type = assembly.GetType("NormalClass");
52+
dynamic instance = Activator.CreateInstance(type);
53+
instance.X = 1;
54+
instance.Y = "2";
55+
instance.Z = 4.5;
56+
instance.V = 'C';
57+
58+
var result = instance.ToString();
59+
60+
Assert.Equal(
61+
string.Format("{{T: \"NormalClass\"{0}X: 1{0}Y: \"2\"{0}Z: 4.5{0}V: \"C\"}}", PropertiesSeparator),
62+
result);
63+
}
64+
65+
[Fact]
66+
public void NormalClassTest_ShouldNotWrapInBrackets()
67+
{
68+
var assembly = PrepareAssembly("test3", new AttributesConfiguration { WrapWithBrackets = WrapWithBrackets });
69+
70+
var type = assembly.GetType("NormalClass");
71+
dynamic instance = Activator.CreateInstance(type);
72+
instance.X = 1;
73+
instance.Y = "2";
74+
instance.Z = 4.5;
75+
instance.V = 'C';
76+
77+
var result = instance.ToString();
78+
79+
Assert.Equal(
80+
"T: \"NormalClass\", X: 1, Y: \"2\", Z: 4.5, V: \"C\"",
81+
result);
82+
}
83+
84+
[Fact]
85+
public void NormalClassTest_ShouldNotWriteClassName()
86+
{
87+
var assembly = PrepareAssembly("test4", new AttributesConfiguration { WriteTypeName = WriteTypeName });
88+
89+
var type = assembly.GetType("NormalClass");
90+
dynamic instance = Activator.CreateInstance(type);
91+
instance.X = 1;
92+
instance.Y = "2";
93+
instance.Z = 4.5;
94+
instance.V = 'C';
95+
96+
var result = instance.ToString();
97+
98+
Assert.Equal(
99+
"{X: 1, Y: \"2\", Z: 4.5, V: \"C\"}",
100+
result);
101+
}
102+
103+
[Fact]
104+
public void NormalClassTest_ShouldStartListWithCustomSeparator()
105+
{
106+
var assembly = PrepareAssembly("test5", new AttributesConfiguration { ListStart = ListStart });
107+
108+
var type = assembly.GetType("IntCollection");
109+
dynamic instance = Activator.CreateInstance(type);
110+
instance.Collection = new[] { 1, 2, 3, 4, 5, 6 };
111+
instance.Count = 2;
112+
113+
var result = instance.ToString();
114+
115+
var expected = $"{{T: \"IntCollection\", Count: 2, Collection: {ListStart}1, 2, 3, 4, 5, 6]}}";
116+
117+
Assert.Equal(expected, result);
118+
}
119+
120+
[Fact]
121+
public void NormalClassTest_ShouldEndListWithCustomSeparator()
122+
{
123+
var assembly = PrepareAssembly("test6", new AttributesConfiguration { ListEnd = ListEnd });
124+
125+
var type = assembly.GetType("IntCollection");
126+
dynamic instance = Activator.CreateInstance(type);
127+
instance.Collection = new[] { 1, 2, 3, 4, 5, 6 };
128+
instance.Count = 2;
129+
130+
var result = instance.ToString();
131+
132+
var expected = $"{{T: \"IntCollection\", Count: 2, Collection: [1, 2, 3, 4, 5, 6{ListEnd}}}";
133+
134+
Assert.Equal(expected, result);
135+
}
136+
}

Tests/TestHelper.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Text;
2+
using System.Xml.Linq;
3+
4+
static class TestHelper
5+
{
6+
public static XElement PrepareConfig(AttributesConfiguration configuration)
7+
{
8+
var configXml = new StringBuilder();
9+
configXml.Append("<ToString ");
10+
if (!string.IsNullOrEmpty(configuration.PropertyNameToValueSeparator))
11+
{
12+
configXml.AppendFormat("PropertyNameToValueSeparator=\"{0}\" ", configuration.PropertyNameToValueSeparator);
13+
}
14+
15+
if (!string.IsNullOrEmpty(configuration.PropertiesSeparator))
16+
{
17+
configXml.AppendFormat("PropertiesSeparator=\"{0}\" ", configuration.PropertiesSeparator);
18+
}
19+
20+
if (configuration.WrapWithBrackets.HasValue)
21+
{
22+
configXml.AppendFormat("WrapWithBrackets=\"{0}\" ", configuration.WrapWithBrackets);
23+
}
24+
25+
if (configuration.WriteTypeName.HasValue)
26+
{
27+
configXml.AppendFormat("WriteTypeName=\"{0}\" ", configuration.WriteTypeName);
28+
}
29+
30+
if (!string.IsNullOrEmpty(configuration.ListStart))
31+
{
32+
configXml.AppendFormat("ListStart=\"{0}\" ", configuration.ListStart);
33+
}
34+
35+
if (!string.IsNullOrEmpty(configuration.ListEnd))
36+
{
37+
configXml.AppendFormat("ListEnd=\"{0}\" ", configuration.ListEnd);
38+
}
39+
40+
configXml.Append("/>");
41+
42+
return XElement.Parse(configXml.ToString());
43+
}
44+
}

Tests/Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88
<ItemGroup>
99
<Reference Include="Microsoft.CSharp" />
10-
<PackageReference Include="FodyHelpers" Version="3.2.9" />
10+
<PackageReference Include="FodyHelpers" Version="3.2.10" />
1111
<PackageReference Include="xunit" Version="2.4.0" />
1212
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
1313
<ProjectReference Include="..\ToString.Fody\ToString.Fody.csproj" />

0 commit comments

Comments
 (0)