4
4
using System . Diagnostics ;
5
5
using System . Linq ;
6
6
using System . Reflection ;
7
+ using System . Runtime . Serialization ;
7
8
using System . Text ;
8
9
using NUnit . Framework ;
9
10
10
11
namespace ServiceStack . Text . Tests
11
12
{
12
- public class Module
13
- {
14
- public Module ( )
15
- {
16
- ExtendedData = new Dictionary < string , object > ( ) ;
17
- }
18
-
19
- public string Name { get ; set ; }
20
- public string Version { get ; set ; }
21
- public IDictionary < string , object > ExtendedData { get ; set ; }
22
- }
23
-
24
- public class StackFrame
25
- {
26
- public StackFrame ( )
27
- {
28
- ExtendedData = new Dictionary < string , object > ( ) ;
29
- Parameters = new Collection < Parameter > ( ) ;
30
- }
31
-
32
- public string FileName { get ; set ; }
33
- public int LineNumber { get ; set ; }
34
- public int Column { get ; set ; }
35
- public IDictionary < string , object > ExtendedData { get ; set ; }
36
- public string Type { get ; set ; }
37
- public string Namespace { get ; set ; }
38
- public Module Module { get ; set ; }
39
- public string Method { get ; set ; }
40
- public ICollection < Parameter > Parameters { get ; set ; }
41
- }
42
-
43
- public class Parameter
44
- {
45
- public Parameter ( )
46
- {
47
- ExtendedData = new Dictionary < string , object > ( ) ;
48
- }
49
-
50
- public string Name { get ; set ; }
51
- public string Type { get ; set ; }
52
- public IDictionary < string , object > ExtendedData { get ; set ; }
53
- }
54
-
55
- public class Error
56
- {
57
- public Error ( )
58
- {
59
- ExtendedData = new Dictionary < string , object > ( ) ;
60
- Tags = new HashSet < string > ( ) ;
61
- StackTrace = new Collection < StackFrame > ( ) ;
62
- }
63
-
64
- public string Id { get ; set ; }
65
- public string Message { get ; set ; }
66
- public string Type { get ; set ; }
67
- public Module Module { get ; set ; }
68
- public string Description { get ; set ; }
69
- public DateTime OccurrenceDate { get ; set ; }
70
- public string Code { get ; set ; }
71
- public IDictionary < string , object > ExtendedData { get ; set ; }
72
- public HashSet < string > Tags { get ; set ; }
73
-
74
- public Error Inner { get ; set ; }
75
-
76
- public ICollection < StackFrame > StackTrace { get ; set ; }
77
- public string Contact { get ; set ; }
78
- public string Notes { get ; set ; }
79
- }
80
-
81
-
82
- [ TestFixture ]
83
- public class CyclicalDependencyTests : TestBase
84
- {
85
- [ Test ]
86
- public void Can_serialize_Error ( )
87
- {
88
- var dto = new Error {
89
- Id = "Id" ,
90
- Message = "Message" ,
91
- Type = "Type" ,
92
- Description = "Description" ,
93
- OccurrenceDate = new DateTime ( 2012 , 01 , 01 ) ,
94
- Code = "Code" ,
95
- ExtendedData = new Dictionary < string , object > { { "Key" , "Value" } } ,
96
- Tags = new HashSet < string > { "C#" , "ruby" } ,
97
- Inner = new Error {
98
- Id = "Id2" ,
99
- Message = "Message2" ,
100
- ExtendedData = new Dictionary < string , object > { { "InnerKey" , "InnerValue" } } ,
101
- Module = new Module {
102
- Name = "Name" ,
103
- Version = "v1.0"
104
- } ,
105
- StackTrace = new Collection < StackFrame > {
106
- new StackFrame {
107
- Column = 1 ,
108
- Module = new Module {
109
- Name = "StackTrace.Name" ,
110
- Version = "StackTrace.v1.0"
111
- } ,
112
- ExtendedData = new Dictionary < string , object > { { "StackTraceKey" , "StackTraceValue" } } ,
113
- FileName = "FileName" ,
114
- Type = "Type" ,
115
- LineNumber = 1 ,
116
- Method = "Method" ,
117
- Namespace = "Namespace" ,
118
- Parameters = new Collection < Parameter > {
119
- new Parameter { Name = "Parameter" , Type = "ParameterType" } ,
120
- }
121
- }
122
- }
123
- } ,
124
- Contact = "Contact" ,
125
- Notes = "Notes" ,
126
- } ;
127
-
128
- var from = Serialize ( dto , includeXml : false ) ;
129
- Console . WriteLine ( from . Dump ( ) ) ;
130
-
131
- Assert . That ( from . Id , Is . EqualTo ( dto . Id ) ) ;
132
- Assert . That ( from . Message , Is . EqualTo ( dto . Message ) ) ;
133
- Assert . That ( from . Type , Is . EqualTo ( dto . Type ) ) ;
134
- Assert . That ( from . Description , Is . EqualTo ( dto . Description ) ) ;
135
- Assert . That ( from . OccurrenceDate , Is . EqualTo ( dto . OccurrenceDate ) ) ;
136
- Assert . That ( from . Code , Is . EqualTo ( dto . Code ) ) ;
137
-
138
- Assert . That ( from . Inner . Id , Is . EqualTo ( dto . Inner . Id ) ) ;
139
- Assert . That ( from . Inner . Message , Is . EqualTo ( dto . Inner . Message ) ) ;
140
- Assert . That ( from . Inner . ExtendedData [ "InnerKey" ] , Is . EqualTo ( dto . Inner . ExtendedData [ "InnerKey" ] ) ) ;
141
- Assert . That ( from . Inner . Module . Name , Is . EqualTo ( dto . Inner . Module . Name ) ) ;
142
- Assert . That ( from . Inner . Module . Version , Is . EqualTo ( dto . Inner . Module . Version ) ) ;
143
-
144
- var actualStack = from . Inner . StackTrace . First ( ) ;
145
- var expectedStack = dto . Inner . StackTrace . First ( ) ;
146
- Assert . That ( actualStack . Column , Is . EqualTo ( expectedStack . Column ) ) ;
147
- Assert . That ( actualStack . FileName , Is . EqualTo ( expectedStack . FileName ) ) ;
148
- Assert . That ( actualStack . Type , Is . EqualTo ( expectedStack . Type ) ) ;
149
- Assert . That ( actualStack . LineNumber , Is . EqualTo ( expectedStack . LineNumber ) ) ;
150
- Assert . That ( actualStack . Method , Is . EqualTo ( expectedStack . Method ) ) ;
151
-
152
- Assert . That ( from . Contact , Is . EqualTo ( dto . Contact ) ) ;
153
- Assert . That ( from . Notes , Is . EqualTo ( dto . Notes ) ) ;
154
- }
13
+ public class Module
14
+ {
15
+ public Module ( )
16
+ {
17
+ ExtendedData = new Dictionary < string , object > ( ) ;
18
+ }
19
+
20
+ public string Name { get ; set ; }
21
+ public string Version { get ; set ; }
22
+ public IDictionary < string , object > ExtendedData { get ; set ; }
23
+ }
24
+
25
+ public class StackFrame
26
+ {
27
+ public StackFrame ( )
28
+ {
29
+ ExtendedData = new Dictionary < string , object > ( ) ;
30
+ Parameters = new Collection < Parameter > ( ) ;
31
+ }
32
+
33
+ public string FileName { get ; set ; }
34
+ public int LineNumber { get ; set ; }
35
+ public int Column { get ; set ; }
36
+ public IDictionary < string , object > ExtendedData { get ; set ; }
37
+ public string Type { get ; set ; }
38
+ public string Namespace { get ; set ; }
39
+ public Module Module { get ; set ; }
40
+ public string Method { get ; set ; }
41
+ public ICollection < Parameter > Parameters { get ; set ; }
42
+ }
43
+
44
+ public class Parameter
45
+ {
46
+ public Parameter ( )
47
+ {
48
+ ExtendedData = new Dictionary < string , object > ( ) ;
49
+ }
50
+
51
+ public string Name { get ; set ; }
52
+ public string Type { get ; set ; }
53
+ public IDictionary < string , object > ExtendedData { get ; set ; }
54
+ }
55
+
56
+ public class Error
57
+ {
58
+ public Error ( )
59
+ {
60
+ ExtendedData = new Dictionary < string , object > ( ) ;
61
+ Tags = new HashSet < string > ( ) ;
62
+ StackTrace = new Collection < StackFrame > ( ) ;
63
+ }
64
+
65
+ public string Id { get ; set ; }
66
+ public string Message { get ; set ; }
67
+ public string Type { get ; set ; }
68
+ public Module Module { get ; set ; }
69
+ public string Description { get ; set ; }
70
+ public DateTime OccurrenceDate { get ; set ; }
71
+ public string Code { get ; set ; }
72
+ public IDictionary < string , object > ExtendedData { get ; set ; }
73
+ public HashSet < string > Tags { get ; set ; }
74
+
75
+ public Error Inner { get ; set ; }
76
+
77
+ public ICollection < StackFrame > StackTrace { get ; set ; }
78
+ public string Contact { get ; set ; }
79
+ public string Notes { get ; set ; }
80
+ }
81
+
82
+
83
+ [ TestFixture ]
84
+ public class CyclicalDependencyTests : TestBase
85
+ {
86
+ [ Test ]
87
+ public void Can_serialize_Error ( )
88
+ {
89
+ var dto = new Error
90
+ {
91
+ Id = "Id" ,
92
+ Message = "Message" ,
93
+ Type = "Type" ,
94
+ Description = "Description" ,
95
+ OccurrenceDate = new DateTime ( 2012 , 01 , 01 ) ,
96
+ Code = "Code" ,
97
+ ExtendedData = new Dictionary < string , object > { { "Key" , "Value" } } ,
98
+ Tags = new HashSet < string > { "C#" , "ruby" } ,
99
+ Inner = new Error
100
+ {
101
+ Id = "Id2" ,
102
+ Message = "Message2" ,
103
+ ExtendedData = new Dictionary < string , object > { { "InnerKey" , "InnerValue" } } ,
104
+ Module = new Module
105
+ {
106
+ Name = "Name" ,
107
+ Version = "v1.0"
108
+ } ,
109
+ StackTrace = new Collection < StackFrame > {
110
+ new StackFrame {
111
+ Column = 1 ,
112
+ Module = new Module {
113
+ Name = "StackTrace.Name" ,
114
+ Version = "StackTrace.v1.0"
115
+ } ,
116
+ ExtendedData = new Dictionary < string , object > { { "StackTraceKey" , "StackTraceValue" } } ,
117
+ FileName = "FileName" ,
118
+ Type = "Type" ,
119
+ LineNumber = 1 ,
120
+ Method = "Method" ,
121
+ Namespace = "Namespace" ,
122
+ Parameters = new Collection < Parameter > {
123
+ new Parameter { Name = "Parameter" , Type = "ParameterType" } ,
124
+ }
125
+ }
126
+ }
127
+ } ,
128
+ Contact = "Contact" ,
129
+ Notes = "Notes" ,
130
+ } ;
131
+
132
+ var from = Serialize ( dto , includeXml : false ) ;
133
+ Console . WriteLine ( from . Dump ( ) ) ;
134
+
135
+ Assert . That ( from . Id , Is . EqualTo ( dto . Id ) ) ;
136
+ Assert . That ( from . Message , Is . EqualTo ( dto . Message ) ) ;
137
+ Assert . That ( from . Type , Is . EqualTo ( dto . Type ) ) ;
138
+ Assert . That ( from . Description , Is . EqualTo ( dto . Description ) ) ;
139
+ Assert . That ( from . OccurrenceDate , Is . EqualTo ( dto . OccurrenceDate ) ) ;
140
+ Assert . That ( from . Code , Is . EqualTo ( dto . Code ) ) ;
141
+
142
+ Assert . That ( from . Inner . Id , Is . EqualTo ( dto . Inner . Id ) ) ;
143
+ Assert . That ( from . Inner . Message , Is . EqualTo ( dto . Inner . Message ) ) ;
144
+ Assert . That ( from . Inner . ExtendedData [ "InnerKey" ] , Is . EqualTo ( dto . Inner . ExtendedData [ "InnerKey" ] ) ) ;
145
+ Assert . That ( from . Inner . Module . Name , Is . EqualTo ( dto . Inner . Module . Name ) ) ;
146
+ Assert . That ( from . Inner . Module . Version , Is . EqualTo ( dto . Inner . Module . Version ) ) ;
147
+
148
+ var actualStack = from . Inner . StackTrace . First ( ) ;
149
+ var expectedStack = dto . Inner . StackTrace . First ( ) ;
150
+ Assert . That ( actualStack . Column , Is . EqualTo ( expectedStack . Column ) ) ;
151
+ Assert . That ( actualStack . FileName , Is . EqualTo ( expectedStack . FileName ) ) ;
152
+ Assert . That ( actualStack . Type , Is . EqualTo ( expectedStack . Type ) ) ;
153
+ Assert . That ( actualStack . LineNumber , Is . EqualTo ( expectedStack . LineNumber ) ) ;
154
+ Assert . That ( actualStack . Method , Is . EqualTo ( expectedStack . Method ) ) ;
155
+
156
+ Assert . That ( from . Contact , Is . EqualTo ( dto . Contact ) ) ;
157
+ Assert . That ( from . Notes , Is . EqualTo ( dto . Notes ) ) ;
158
+ }
155
159
156
160
class person
157
161
{
158
162
public string name { get ; set ; }
159
163
public person teacher { get ; set ; }
160
164
}
161
165
162
- [ Test ]
163
- public void Can_limit_cyclical_dependencies ( )
164
- {
165
- using ( JsConfig . With ( maxDepth : 4 ) )
166
- {
166
+ [ Test ]
167
+ public void Can_limit_cyclical_dependencies ( )
168
+ {
169
+ using ( JsConfig . With ( maxDepth : 4 ) )
170
+ {
167
171
var p = new person ( ) ;
168
172
p . teacher = new person { name = "sam" , teacher = p } ;
169
173
p . name = "bob" ;
@@ -172,7 +176,5 @@ public void Can_limit_cyclical_dependencies()
172
176
p . ToJson ( ) . Print ( ) ;
173
177
}
174
178
}
175
-
176
-
177
- }
179
+ }
178
180
}
0 commit comments