1+ // ==========================================================================================
2+ // GameFrameX 组织及其衍生项目的版权、商标、专利及其他相关权利
3+ // GameFrameX organization and its derivative projects' copyrights, trademarks, patents, and related rights
4+ // 均受中华人民共和国及相关国际法律法规保护。
5+ // are protected by the laws of the People's Republic of China and relevant international regulations.
6+ //
7+ // 使用本项目须严格遵守相应法律法规及开源许可证之规定。
8+ // Usage of this project must strictly comply with applicable laws, regulations, and open-source licenses.
9+ //
10+ // 本项目采用 MIT 许可证与 Apache License 2.0 双许可证分发,
11+ // This project is dual-licensed under the MIT License and Apache License 2.0,
12+ // 完整许可证文本请参见源代码根目录下的 LICENSE 文件。
13+ // please refer to the LICENSE file in the root directory of the source code for the full license text.
14+ //
15+ // 禁止利用本项目实施任何危害国家安全、破坏社会秩序、
16+ // It is prohibited to use this project to engage in any activities that endanger national security, disrupt social order,
17+ // 侵犯他人合法权益等法律法规所禁止的行为!
18+ // or infringe upon the legitimate rights and interests of others, as prohibited by laws and regulations!
19+ // 因基于本项目二次开发所产生的一切法律纠纷与责任,
20+ // Any legal disputes and liabilities arising from secondary development based on this project
21+ // 本项目组织与贡献者概不承担。
22+ // shall be borne solely by the developer; the project organization and contributors assume no responsibility.
23+ //
24+ // GitHub 仓库:https://github.com/GameFrameX
25+ // GitHub Repository: https://github.com/GameFrameX
26+ // Gitee 仓库:https://gitee.com/GameFrameX
27+ // Gitee Repository: https://gitee.com/GameFrameX
28+ // 官方文档:https://gameframex.doc.alianblank.com/
29+ // Official Documentation: https://gameframex.doc.alianblank.com/
30+ // ==========================================================================================
31+
32+
33+ using System . Linq . Expressions ;
34+ using GameFrameX . Foundation . Extensions ;
35+ using Xunit ;
36+
37+ namespace GameFrameX . Foundation . Tests . Extensions ;
38+
39+ public class ExpressionExtensionTests
40+ {
41+ [ Fact ]
42+ public void And_CombinesTwoExpressions ( )
43+ {
44+ // Arrange
45+ Expression < Func < string , bool > > expr1 = s => s . Length > 5 ;
46+ Expression < Func < string , bool > > expr2 = s => s . StartsWith ( "test" ) ;
47+
48+ // Act
49+ var combined = expr1 . And ( expr2 ) ;
50+
51+ // Assert
52+ Assert . True ( combined . Compile ( ) ( "testing" ) ) ;
53+ Assert . False ( combined . Compile ( ) ( "test" ) ) ;
54+ }
55+
56+ [ Fact ]
57+ public void AndIf_OnlyAppliesWhenConditionIsTrue ( )
58+ {
59+ // Arrange
60+ Expression < Func < string , bool > > expr1 = s => s . Length > 5 ;
61+ Expression < Func < string , bool > > expr2 = s => s . StartsWith ( "test" ) ;
62+ bool condition = true ;
63+
64+ // Act
65+ var result1 = expr1 . AndIf ( ( ) => condition , expr2 ) ;
66+ condition = false ;
67+ var result2 = expr1 . AndIf ( ( ) => condition , expr2 ) ;
68+
69+ // Assert
70+ Assert . True ( result1 . Compile ( ) ( "testing" ) ) ;
71+ Assert . False ( result1 . Compile ( ) ( "test" ) ) ;
72+ Assert . True ( result2 . Compile ( ) ( "longstr" ) ) ; // Only first expression applies
73+ }
74+
75+ [ Fact ]
76+ public void Or_CombinesTwoExpressions ( )
77+ {
78+ // Arrange
79+ Expression < Func < string , bool > > expr1 = s => s . Length > 5 ;
80+ Expression < Func < string , bool > > expr2 = s => s . StartsWith ( "test" ) ;
81+
82+ // Act
83+ var combined = expr1 . Or ( expr2 ) ;
84+
85+ // Assert
86+ Assert . True ( combined . Compile ( ) ( "testing" ) ) ; // Matches both
87+ Assert . True ( combined . Compile ( ) ( "test" ) ) ; // Matches second
88+ Assert . True ( combined . Compile ( ) ( "longstr" ) ) ; // Matches first
89+ Assert . False ( combined . Compile ( ) ( "no" ) ) ; // Matches neither
90+ }
91+
92+ [ Fact ]
93+ public void OrIf_OnlyAppliesWhenConditionIsTrue ( )
94+ {
95+ // Arrange
96+ Expression < Func < string , bool > > expr1 = s => s . Length > 5 ;
97+ Expression < Func < string , bool > > expr2 = s => s . StartsWith ( "test" ) ;
98+ bool condition = true ;
99+
100+ // Act
101+ var result1 = expr1 . OrIf ( ( ) => condition , expr2 ) ;
102+ condition = false ;
103+ var result2 = expr1 . OrIf ( ( ) => condition , expr2 ) ;
104+
105+ // Assert
106+ Assert . True ( result1 . Compile ( ) ( "test" ) ) ; // Second expression applies
107+ Assert . False ( result2 . Compile ( ) ( "test" ) ) ; // Only first expression applies
108+ }
109+
110+ [ Fact ]
111+ public void Not_InvertsExpression ( )
112+ {
113+ // Arrange
114+ Expression < Func < string , bool > > expr = s => s . Length > 5 ;
115+
116+ // Act
117+ var notExpr = expr . Not ( ) ;
118+
119+ // Assert
120+ Assert . True ( notExpr . Compile ( ) ( "test" ) ) ; // Length <= 5
121+ Assert . False ( notExpr . Compile ( ) ( "testing" ) ) ; // Length > 5
122+ }
123+
124+ [ Fact ]
125+ public void All_ThrowArgumentNullException_WhenInputIsNull ( )
126+ {
127+ // Arrange
128+ Expression < Func < string , bool > > expr = s => s . Length > 5 ;
129+ Expression < Func < string , bool > > nullExpr = null ;
130+
131+ // Assert
132+ Assert . Throws < ArgumentNullException > ( ( ) => expr . And ( nullExpr ) ) ;
133+ Assert . Throws < ArgumentNullException > ( ( ) => nullExpr . And ( expr ) ) ;
134+ Assert . Throws < ArgumentNullException > ( ( ) => expr . AndIf ( null , expr ) ) ;
135+ Assert . Throws < ArgumentNullException > ( ( ) => expr . AndIf ( ( ) => true , null ) ) ;
136+ Assert . Throws < ArgumentNullException > ( ( ) => expr . Or ( nullExpr ) ) ;
137+ Assert . Throws < ArgumentNullException > ( ( ) => nullExpr . Or ( expr ) ) ;
138+ Assert . Throws < ArgumentNullException > ( ( ) => expr . OrIf ( null , expr ) ) ;
139+ Assert . Throws < ArgumentNullException > ( ( ) => expr . OrIf ( ( ) => true , null ) ) ;
140+ Assert . Throws < ArgumentNullException > ( ( ) => nullExpr . Not ( ) ) ;
141+ }
142+ }
0 commit comments