|
| 1 | +using ObjectSemantics.NET.Tests.MoqModels; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace ObjectSemantics.NET.Tests |
| 6 | +{ |
| 7 | + public class ExpressionFunctionTests |
| 8 | + { |
| 9 | + [Fact] |
| 10 | + public void Should_Sum_Collection_Path() |
| 11 | + { |
| 12 | + OrderEmailModel model = new OrderEmailModel |
| 13 | + { |
| 14 | + Customer = new OrderCustomer |
| 15 | + { |
| 16 | + Payments = new List<OrderPayment> |
| 17 | + { |
| 18 | + new OrderPayment { Amount = 1000 }, |
| 19 | + new OrderPayment { Amount = 2000.75m }, |
| 20 | + new OrderPayment { Amount = 999.25m } |
| 21 | + } |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + string result = model.Map("{{ __sum(Customer.Payments.Amount):N2 }}"); |
| 26 | + Assert.Equal("4,000.00", result); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void Should_Average_Collection_Path_With_Single_Underscore() |
| 31 | + { |
| 32 | + OrderEmailModel model = new OrderEmailModel |
| 33 | + { |
| 34 | + Customer = new OrderCustomer |
| 35 | + { |
| 36 | + Payments = new List<OrderPayment> |
| 37 | + { |
| 38 | + new OrderPayment { PaidAmount = 300 }, |
| 39 | + new OrderPayment { PaidAmount = 900 }, |
| 40 | + new OrderPayment { PaidAmount = 1200 } |
| 41 | + } |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + string result = model.Map("{{ _avg(Customer.Payments.PaidAmount):N2 }}"); |
| 46 | + Assert.Equal("800.00", result); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void Should_Calculate_Property_Expression() |
| 51 | + { |
| 52 | + OrderEmailModel model = new OrderEmailModel |
| 53 | + { |
| 54 | + PaidAmount = 10000, |
| 55 | + Customer = new OrderCustomer |
| 56 | + { |
| 57 | + CreditLimit = 4500 |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + string result = model.Map("{{ __calc(PaidAmount - Customer.CreditLimit):N2 }}"); |
| 62 | + Assert.Equal("5,500.00", result); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void Should_Calculate_Expression_Including_Parentheses_And_Decimals() |
| 67 | + { |
| 68 | + OrderEmailModel model = new OrderEmailModel |
| 69 | + { |
| 70 | + Subtotal = 1000, |
| 71 | + Tax = 160 |
| 72 | + }; |
| 73 | + |
| 74 | + string result = model.Map("{{ __calc((Subtotal + Tax) * 0.5):N2 }}"); |
| 75 | + Assert.Equal("580.00", result); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void Should_Calculate_Count_Min_And_Max_Aggregates() |
| 80 | + { |
| 81 | + OrderEmailModel model = new OrderEmailModel |
| 82 | + { |
| 83 | + Customer = new OrderCustomer |
| 84 | + { |
| 85 | + Payments = new List<OrderPayment> |
| 86 | + { |
| 87 | + new OrderPayment { Amount = 700 }, |
| 88 | + new OrderPayment { Amount = 200 }, |
| 89 | + new OrderPayment { Amount = 1100 } |
| 90 | + } |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + string template = "{{ __count(Customer.Payments.Amount) }}|{{ __min(Customer.Payments.Amount):N2 }}|{{ __max(Customer.Payments.Amount):N2 }}"; |
| 95 | + string result = model.Map(template); |
| 96 | + Assert.Equal("3|200.00|1,100.00", result); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public void Should_Calculate_Per_Row_Expression_Inside_Loop() |
| 101 | + { |
| 102 | + OrderEmailModel model = new OrderEmailModel |
| 103 | + { |
| 104 | + Items = new List<OrderLineItem> |
| 105 | + { |
| 106 | + new OrderLineItem { Quantity = 2, UnitPrice = 350 }, |
| 107 | + new OrderLineItem { Quantity = 3, UnitPrice = 100.5m } |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + string template = "{{ #foreach(Items) }}[{{ __calc(Quantity * UnitPrice):N2 }}]{{ #endforeach }}"; |
| 112 | + string result = model.Map(template); |
| 113 | + |
| 114 | + Assert.Equal("[700.00][301.50]", result); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public void Should_Render_Empty_When_Calc_Expression_Is_Invalid() |
| 119 | + { |
| 120 | + OrderEmailModel model = new OrderEmailModel |
| 121 | + { |
| 122 | + PaidAmount = 5000, |
| 123 | + Customer = new OrderCustomer |
| 124 | + { |
| 125 | + CreditLimit = 1000 |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + string result = model.Map("A{{ __calc(PaidAmount - UnknownValue):N2 }}B"); |
| 130 | + Assert.Equal("AB", result); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public void Should_Render_Empty_For_Unknown_Aggregate_Path() |
| 135 | + { |
| 136 | + OrderEmailModel model = new OrderEmailModel |
| 137 | + { |
| 138 | + Customer = new OrderCustomer |
| 139 | + { |
| 140 | + Payments = new List<OrderPayment> |
| 141 | + { |
| 142 | + new OrderPayment { Amount = 100 } |
| 143 | + } |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + string result = model.Map("{{ __sum(Customer.UnknownPayments.Amount) }}|{{ __avg(Customer.UnknownPayments.Amount) }}|{{ __count(Customer.UnknownPayments.Amount) }}|{{ __min(Customer.UnknownPayments.Amount) }}|{{ __max(Customer.UnknownPayments.Amount) }}"); |
| 148 | + Assert.Equal("||||", result); |
| 149 | + } |
| 150 | + |
| 151 | + [Fact] |
| 152 | + public void Should_Return_Zero_For_Aggregates_When_Source_Is_Null() |
| 153 | + { |
| 154 | + OrderEmailModel model = new OrderEmailModel |
| 155 | + { |
| 156 | + Customer = null |
| 157 | + }; |
| 158 | + |
| 159 | + string result = model.Map("{{ __sum(Customer.Payments.Amount) }}|{{ __avg(Customer.Payments.Amount) }}|{{ __count(Customer.Payments.Amount) }}|{{ __min(Customer.Payments.Amount) }}|{{ __max(Customer.Payments.Amount) }}"); |
| 160 | + Assert.Equal("0|0|0|0|0", result); |
| 161 | + } |
| 162 | + |
| 163 | + [Fact] |
| 164 | + public void Should_Return_Zero_When_Calc_Uses_Null_Operand_Path() |
| 165 | + { |
| 166 | + OrderEmailModel model = new OrderEmailModel |
| 167 | + { |
| 168 | + PaidAmount = 5000, |
| 169 | + Customer = null |
| 170 | + }; |
| 171 | + |
| 172 | + string result = model.Map("A{{ __calc(PaidAmount - Customer.CreditLimit) }}B"); |
| 173 | + Assert.Equal("A0B", result); |
| 174 | + } |
| 175 | + |
| 176 | + [Fact] |
| 177 | + public void Should_Support_Expressions_Without_Number_Format() |
| 178 | + { |
| 179 | + OrderEmailModel model = new OrderEmailModel |
| 180 | + { |
| 181 | + PaidAmount = 10000, |
| 182 | + Customer = new OrderCustomer |
| 183 | + { |
| 184 | + CreditLimit = 4500, |
| 185 | + Payments = new List<OrderPayment> |
| 186 | + { |
| 187 | + new OrderPayment { Amount = 1000 }, |
| 188 | + new OrderPayment { Amount = 2000 }, |
| 189 | + new OrderPayment { Amount = 1500 } |
| 190 | + } |
| 191 | + } |
| 192 | + }; |
| 193 | + |
| 194 | + string result = model.Map("{{ __sum(Customer.Payments.Amount) }}|{{ __calc(PaidAmount - Customer.CreditLimit) }}"); |
| 195 | + Assert.Equal("4500|5500", result); |
| 196 | + } |
| 197 | + |
| 198 | + [Fact] |
| 199 | + public void Should_Render_Empty_When_Sum_Path_Is_String_Based() |
| 200 | + { |
| 201 | + OrderEmailModel model = new OrderEmailModel |
| 202 | + { |
| 203 | + Items = new List<OrderLineItem> |
| 204 | + { |
| 205 | + new OrderLineItem { Name = "Keyboard" }, |
| 206 | + new OrderLineItem { Name = "Mouse" } |
| 207 | + } |
| 208 | + }; |
| 209 | + |
| 210 | + string result = model.Map("X{{ __sum(Items.Name) }}Y"); |
| 211 | + Assert.Equal("XY", result); |
| 212 | + } |
| 213 | + |
| 214 | + [Fact] |
| 215 | + public void Should_Render_Empty_When_Sum_Path_Is_Date_Based() |
| 216 | + { |
| 217 | + OrderEmailModel model = new OrderEmailModel |
| 218 | + { |
| 219 | + OrderDate = new System.DateTime(2026, 03, 07) |
| 220 | + }; |
| 221 | + |
| 222 | + string result = model.Map("X{{ __sum(OrderDate) }}Y"); |
| 223 | + Assert.Equal("XY", result); |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments