1+ using NUnit . Framework ;
2+ using System ;
3+ using System . Linq . Expressions ;
4+ using TestStack . FluentMVCTesting . Internal ;
5+
6+ namespace TestStack . FluentMVCTesting . Tests . Internal
7+ {
8+ [ TestFixture ]
9+ public class ExpressionInspectorShould
10+ {
11+ [ Test ]
12+ public void Correctly_parse_equality_comparison_with_string_operands ( )
13+ {
14+ Expression < Func < string , bool > > func = text => text == "any" ;
15+ ExpressionInspector sut = new ExpressionInspector ( ) ;
16+ var actual = sut . Inspect ( func ) ;
17+ Assert . AreEqual ( "text => (text == \" any\" )" , actual ) ;
18+ }
19+
20+ [ Test ]
21+ public void Correctly_parse_equality_comparison_with_int_operands ( )
22+ {
23+ Expression < Func < int , bool > > func = number => number == 5 ;
24+ ExpressionInspector sut = new ExpressionInspector ( ) ;
25+ var actual = sut . Inspect ( func ) ;
26+ Assert . AreEqual ( "number => (number == 5)" , actual ) ;
27+ }
28+
29+ [ Test ]
30+ public void Correctly_parse_equality_comparison_with_property_operand ( )
31+ {
32+ Expression < Func < PostViewModel , bool > > func = post => post . Title == "A" ;
33+ ExpressionInspector sut = new ExpressionInspector ( ) ;
34+ var actual = sut . Inspect ( func ) ;
35+ Assert . AreEqual ( "post => (post.Title == \" A\" )" , actual ) ;
36+ }
37+
38+ [ Test ]
39+ public void Correctly_parse_equality_comparison_with_two_property_operands ( )
40+ {
41+ Expression < Func < PostViewModel , bool > > func = post =>
42+ post . Title == post . Slug ;
43+ ExpressionInspector sut = new ExpressionInspector ( ) ;
44+ var actual = sut . Inspect ( func ) ;
45+ Assert . AreEqual ( "post => (post.Title == post.Slug)" , actual ) ;
46+ }
47+
48+ [ Test ]
49+ public void Correctly_parse_equality_comparison_with_captured_constant_operand ( )
50+ {
51+ const int Number = 5 ;
52+ Expression < Func < int , bool > > func = number => number == Number ;
53+ ExpressionInspector sut = new ExpressionInspector ( ) ;
54+ var actual = sut . Inspect ( func ) ;
55+ Assert . AreEqual ( string . Concat ( "number => (number == " , Number , ")" ) , actual ) ;
56+ }
57+
58+ [ Test ]
59+ public void Correctly_parse_inequality_comparison ( )
60+ {
61+ Expression < Func < int , bool > > func = number => number != 5 ;
62+ ExpressionInspector sut = new ExpressionInspector ( ) ;
63+ var actual = sut . Inspect ( func ) ;
64+ Assert . AreEqual ( "number => (number != 5)" , actual ) ;
65+ }
66+
67+ [ Test ]
68+ public void Correctly_parse_relational_comparison ( )
69+ {
70+ Expression < Func < int , bool > > func = number => number < 5 ;
71+ ExpressionInspector sut = new ExpressionInspector ( ) ;
72+ var actual = sut . Inspect ( func ) ;
73+ Assert . AreEqual ( "number => (number < 5)" , actual ) ;
74+ }
75+
76+ [ Test ]
77+ public void Correctly_parse_expression_whose_source_text_contains_parentheses ( )
78+ {
79+ Expression < Func < PostViewModel , bool > > func = post => post . Title . Contains ( "" ) ;
80+ ExpressionInspector sut = new ExpressionInspector ( ) ;
81+ var actual = sut . Inspect ( func ) ;
82+ Assert . AreEqual ( "post => post.Title.Contains(\" \" )" , actual ) ;
83+ }
84+
85+ [ Test ]
86+ public void Correctly_parse_null_coalescing_operator ( )
87+ {
88+ Expression < Func < string , bool > > func =
89+ text => text == ( "" ?? "a" ) ;
90+ ExpressionInspector sut = new ExpressionInspector ( ) ;
91+ var actual = sut . Inspect ( func ) ;
92+ Assert . AreEqual ( "text => (text == (\" \" ?? \" a\" ))" , actual ) ;
93+ }
94+
95+ [ Test ]
96+ public void Correctly_parse_conditional_or_operator ( )
97+ {
98+ Expression < Func < string , bool > > func =
99+ text => text == "any" || text . Length == 3 ;
100+ ExpressionInspector sut = new ExpressionInspector ( ) ;
101+ var actual = sut . Inspect ( func ) ;
102+ Assert . AreEqual ( "text => ((text == \" any\" ) || (text.Length == 3))" , actual ) ;
103+ }
104+
105+ [ Test ]
106+ public void Correctly_parse_two_conditional_or_operators ( )
107+ {
108+ Expression < Func < string , bool > > func =
109+ text => text == "any" || text . Length == 3 || text . Length == 9 ;
110+ ExpressionInspector sut = new ExpressionInspector ( ) ;
111+ var actual = sut . Inspect ( func ) ;
112+ Assert . AreEqual (
113+ "text => (((text == \" any\" ) || (text.Length == 3)) || (text.Length == 9))" , actual ) ;
114+ }
115+
116+ [ Test ]
117+ public void Correctly_parse_conditional_and_operator ( )
118+ {
119+ Expression < Func < string , bool > > func =
120+ text => text == "any" && text . Length == 3 ;
121+ ExpressionInspector sut = new ExpressionInspector ( ) ;
122+ var actual = sut . Inspect ( func ) ;
123+ Assert . AreEqual ( "text => ((text == \" any\" ) && (text.Length == 3))" , actual ) ;
124+ }
125+
126+ [ Test ]
127+ public void Correctly_parse_logical_and_operator ( )
128+ {
129+ Expression < Func < string , bool > > func =
130+ text => text == "any" & text . Length == 3 ;
131+ ExpressionInspector sut = new ExpressionInspector ( ) ;
132+ var actual = sut . Inspect ( func ) ;
133+ Assert . AreEqual ( "text => ((text == \" any\" ) & (text.Length == 3))" , actual ) ;
134+ }
135+
136+ [ Test ]
137+ public void Correctly_parse_logical_or_operator ( )
138+ {
139+ Expression < Func < string , bool > > func =
140+ text => text == "any" | text . Length == 3 ;
141+ ExpressionInspector sut = new ExpressionInspector ( ) ;
142+ var actual = sut . Inspect ( func ) ;
143+ Assert . AreEqual ( "text => ((text == \" any\" ) | (text.Length == 3))" , actual ) ;
144+ }
145+
146+ [ Test ]
147+ public void Not_mistake_property_called_OrElse_for_conditional_or_operator ( )
148+ {
149+ Expression < Func < PostViewModel , bool > > func =
150+ post => post . Title == "" || post . OrElse == "" ;
151+ ExpressionInspector sut = new ExpressionInspector ( ) ;
152+ var actual = sut . Inspect ( func ) ;
153+ Assert . AreEqual ( "post => ((post.Title == \" \" ) || (post.OrElse == \" \" ))" , actual ) ;
154+ }
155+
156+ [ Test ]
157+ public void Correctly_parse_logical_xor_operator ( )
158+ {
159+ Expression < Func < string , bool > > func =
160+ text => text == "any" ^ text . Length == 3 ;
161+ ExpressionInspector sut = new ExpressionInspector ( ) ;
162+ var actual = sut . Inspect ( func ) ;
163+ Assert . AreEqual ( "text => ((text == \" any\" ) ^ (text.Length == 3))" , actual ) ;
164+ }
165+ }
166+
167+ public class PostViewModel
168+ {
169+ public string Title { get ; set ; }
170+ public string Slug { get ; set ; }
171+
172+ public string OrElse { get ; set ; }
173+ }
174+ }
0 commit comments