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+ }
0 commit comments