11using System ;
2- using System . Collections ;
32using System . Collections . Generic ;
43using System . Linq ;
54using System . Linq . Expressions ;
@@ -20,21 +19,20 @@ public ExpressionRewiterTests()
2019 [ Fact ]
2120 public void Repository_Select_Single_Member ( )
2221 {
23- var expression = new Query ( )
22+ var query = new Query ( )
2423 . Repository ( "foo" , "bar" )
2524 . Select ( x => x . Name ) ;
2625
2726 Expression < Func < JObject , string > > expected = data =>
2827 Rewritten . Value . Select ( data [ "data" ] [ "repository" ] , x => x [ "name" ] ) . ToObject < string > ( ) ;
2928
30- var query = expression . Compile ( ) ;
31- Assert . Equal ( expected . ToString ( ) , query . GetResultBuilderExpression ( ) . ToString ( ) ) ;
29+ ExpressionRewriterAssertions . AssertExpressionQueryEqual ( expected , query ) ;
3230 }
3331
3432 [ Fact ]
3533 public void Repository_Select_Multiple_Members ( )
3634 {
37- var expression = new Query ( )
35+ var query = new Query ( )
3836 . Repository ( "foo" , "bar" )
3937 . Select ( x => new { x . Name , x . Description } ) ;
4038
@@ -45,14 +43,13 @@ public void Repository_Select_Multiple_Members()
4543 Description = x [ "description" ] . ToObject < string > ( ) ,
4644 } ) ;
4745
48- var query = expression . Compile ( ) ;
49- Assert . Equal ( expected . ToString ( ) , query . GetResultBuilderExpression ( ) . ToString ( ) ) ;
46+ ExpressionRewriterAssertions . AssertExpressionQueryEqual ( expected , query ) ;
5047 }
5148
5249 [ Fact ]
5350 public void Repository_Issues_Select_Multiple_Members ( )
5451 {
55- var expression = new Query ( )
52+ var query = new Query ( )
5653 . Repository ( "foo" , "bar" )
5754 . Issues ( 10 )
5855 . Nodes
@@ -65,14 +62,13 @@ public void Repository_Issues_Select_Multiple_Members()
6562 Closed = x [ "closed" ] . ToObject < bool > ( ) ,
6663 } ) . ToList ( ) ;
6764
68- var query = expression . Compile ( ) ;
69- Assert . Equal ( expected . ToString ( ) , query . GetResultBuilderExpression ( ) . ToString ( ) ) ;
65+ ExpressionRewriterAssertions . AssertExpressionQueryEqual ( expected , query ) ;
7066 }
7167
7268 [ Fact ]
7369 public void Licence_Conditions_Nested_Selects ( )
7470 {
75- var expression = new Query ( )
71+ var query = new Query ( )
7672 . Licenses
7773 . Select ( x => new
7874 {
@@ -89,14 +85,13 @@ public void Licence_Conditions_Nested_Selects()
8985 Items = Rewritten . List . ToList < string > ( Rewritten . List . Select ( x [ "items" ] , i => i [ "description" ] ) )
9086 } ) . ToList ( ) ;
9187
92- var query = expression . Compile ( ) ;
93- Assert . Equal ( expected . ToString ( ) , query . GetResultBuilderExpression ( ) . ToString ( ) ) ;
88+ ExpressionRewriterAssertions . AssertExpressionQueryEqual ( expected , query ) ;
9489 }
9590
9691 [ Fact ]
9792 public void Licenses_Conditions_Select_ToDictionary ( )
9893 {
99- var expression = new Query ( )
94+ var query = new Query ( )
10095 . Licenses
10196 . Select ( x => new
10297 {
@@ -114,23 +109,20 @@ public void Licenses_Conditions_Select_ToDictionary()
114109 x => new
115110 {
116111 Body = x [ "body" ] . ToObject < string > ( ) ,
117- Items = ( IDictionary < string , object > ) Rewritten . List . Select ( x [ "items" ] , i => new
112+ Items = ( IDictionary < string , string > ) Rewritten . List . Select ( x [ "items" ] , i => new
118113 {
119114 Key = i [ "key" ] . ToObject < string > ( ) ,
120115 Description = i [ "description" ] . ToObject < string > ( )
121116 } ) . ToDictionary ( d => d . Key , d => d . Description )
122117 } ) . ToList ( ) ;
123118
124- var query = expression . Compile ( ) ;
125- var expectedString = expected . ToString ( ) ;
126- var actual = query . GetResultBuilderExpression ( ) . ToString ( ) ;
127- Assert . Equal ( expectedString , actual ) ;
119+ ExpressionRewriterAssertions . AssertExpressionQueryEqual ( expected , query ) ;
128120 }
129121
130122 [ Fact ]
131123 public void Issue_Milestone_Select_Value_Single ( )
132124 {
133- var expression = new Query ( )
125+ var query = new Query ( )
134126 . Repository ( "foo" , "bar" )
135127 . Issue ( 1 )
136128 . Select ( x => new
@@ -143,7 +135,7 @@ public void Issue_Milestone_Select_Value_Single()
143135 } ) ;
144136
145137 Expression < Func < JObject , object > > expected = data =>
146- Rewritten . List . Select (
138+ Rewritten . Value . Select (
147139 data [ "data" ] [ "repository" ] [ "issue" ] ,
148140 x => new
149141 {
@@ -157,14 +149,13 @@ public void Issue_Milestone_Select_Value_Single()
157149 } ) )
158150 } ) ;
159151
160- var query = expression . Compile ( ) ;
161- Assert . Equal ( expected . ToString ( ) , query . GetResultBuilderExpression ( ) . ToString ( ) ) ;
152+ ExpressionRewriterAssertions . AssertExpressionQueryEqual ( expected , query ) ;
162153 }
163154
164155 [ Fact ]
165156 public void Issue_Milestone_Select_Value_SingleOrDefault ( )
166157 {
167- var expression = new Query ( )
158+ var query = new Query ( )
168159 . Repository ( "foo" , "bar" )
169160 . Issue ( 1 )
170161 . Select ( x => new
@@ -177,7 +168,7 @@ public void Issue_Milestone_Select_Value_SingleOrDefault()
177168 } ) ;
178169
179170 Expression < Func < JObject , object > > expected = data =>
180- Rewritten . List . Select (
171+ Rewritten . Value . Select (
181172 data [ "data" ] [ "repository" ] [ "issue" ] ,
182173 x => new
183174 {
@@ -191,14 +182,13 @@ public void Issue_Milestone_Select_Value_SingleOrDefault()
191182 } ) )
192183 } ) ;
193184
194- var query = expression . Compile ( ) ;
195- Assert . Equal ( expected . ToString ( ) , query . GetResultBuilderExpression ( ) . ToString ( ) ) ;
185+ ExpressionRewriterAssertions . AssertExpressionQueryEqual ( expected , query ) ;
196186 }
197187
198188 [ Fact ]
199189 public void Nodes_OfType ( )
200190 {
201- var expression = new Query ( )
191+ var query = new Query ( )
202192 . Nodes ( new [ ] { new ID ( "123" ) } )
203193 . OfType < Issue > ( )
204194 . Select ( x => new
@@ -214,14 +204,13 @@ public void Nodes_OfType()
214204 Body = x [ "body" ] . ToObject < string > ( ) ,
215205 } ) . ToList ( ) ;
216206
217- var query = expression . Compile ( ) ;
218- Assert . Equal ( expected . ToString ( ) , query . GetResultBuilderExpression ( ) . ToString ( ) ) ;
207+ ExpressionRewriterAssertions . AssertExpressionQueryEqual ( expected , query ) ;
219208 }
220209
221210 [ Fact ]
222211 public void Node_OfType ( )
223212 {
224- var expression = new Query ( )
213+ var query = new Query ( )
225214 . Node ( new ID ( "123" ) )
226215 . Cast < Issue > ( )
227216 . Select ( x => x . Body ) ;
@@ -231,14 +220,13 @@ public void Node_OfType()
231220 Rewritten . Interface . Cast ( data [ "data" ] [ "node" ] , "Issue" ) ,
232221 x => x [ "body" ] ) . ToObject < string > ( ) ;
233222
234- var query = expression . Compile ( ) ;
235- Assert . Equal ( expected . ToString ( ) , query . GetResultBuilderExpression ( ) . ToString ( ) ) ;
223+ ExpressionRewriterAssertions . AssertExpressionQueryEqual ( expected , query ) ;
236224 }
237225
238226 [ Fact ]
239227 public void Can_Use_Conditional_With_Null_Result ( )
240228 {
241- var expression = new Query ( )
229+ var query = new Query ( )
242230 . Repository ( "foo" , "bar" )
243231 . Select ( x => ! string . IsNullOrWhiteSpace ( x . Name ) ? x . Name : null ) ;
244232
@@ -247,29 +235,28 @@ public void Can_Use_Conditional_With_Null_Result()
247235 data [ "data" ] [ "repository" ] ,
248236 x => ! string . IsNullOrWhiteSpace ( x [ "name" ] . ToObject < string > ( ) ) ? x [ "name" ] . ToObject < string > ( ) : null ) ;
249237
250- var query = expression . Compile ( ) ;
251- Assert . Equal ( expected . ToString ( ) , query . GetResultBuilderExpression ( ) . ToString ( ) ) ;
238+ ExpressionRewriterAssertions . AssertExpressionQueryEqual ( expected , query ) ;
252239 }
253240
254241 [ Fact ]
255242 public void Can_Use_Conditional_To_Compare_To_Null ( )
256243 {
257- var expression = new Query ( )
244+ var query = new Query ( )
258245 . Repository ( "foo" , "bar" )
259246 . Select ( x => x . Name != null ? x . Name : null ) ;
260247
261- // C# inserts a Convert() around the comparison in the following expression, making the test fail.
262- //
263248 // Expression<Func<JObject, object>> expected = data =>
264249 // Rewritten.Value.Select(
265250 // data["data"]["repository"],
266- // x => x["name"].Type != JTokenType.Null ? x["name"] : null).ToObject<string>();
267- //
268- // Just hardcode the expected output.
269- var expected = "data => Select(data.get_Item(\" data\" ).get_Item(\" repository\" ), x => IIF((x.get_Item(\" name\" ).Type != Null), x.get_Item(\" name\" ).ToObject(), null))" ;
251+ // x => x["name"].Type != JTokenType.Null ? x["name"].ToObject<string>() : null);
252+
253+ var readableString =
254+ "data => Rewritten.Value.Select(data[\" data\" ][\" repository\" ],x => (x[\" name\" ].Type != JTokenType.Null) ? x[\" name\" ].ToObject<string>() : null)" ;
255+
256+ // Expression put through ReadableExpression outputs the following, so I'm using a hard coded string instead
257+ // data => Rewritten.Value.Select(data["data"]["repository"], x => (((int)x["name"].Type) != 10) ? x["name"].ToObject<string>() : null)
270258
271- var query = expression . Compile ( ) ;
272- Assert . Equal ( expected , query . GetResultBuilderExpression ( ) . ToString ( ) ) ;
259+ ExpressionRewriterAssertions . AssertExpressionQueryEqual ( readableString , query ) ;
273260 }
274261 }
275262}
0 commit comments