Expressions.vala 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  1. using Invercargill;
  2. using Invercargill.DataStructures;
  3. using Invercargill.Expressions;
  4. class ExprTestPerson : Object {
  5. public string name { get; set; }
  6. public int age { get; set; }
  7. public int rank { get; set; }
  8. public bool is_important { get; set; }
  9. public ExprTestPerson(string name, int age, int rank = 0, bool is_important = false) {
  10. this.name = name;
  11. this.age = age;
  12. this.rank = rank;
  13. this.is_important = is_important;
  14. }
  15. }
  16. class ExprTestContainer : Object {
  17. public int min_rank { get; set; }
  18. public Enumerable<ExprTestPerson> values { get; set; }
  19. public ExprTestContainer(int min_rank, Enumerable<ExprTestPerson> values) {
  20. this.min_rank = min_rank;
  21. this.values = values;
  22. }
  23. }
  24. void expression_tests() {
  25. // ==================== Literal Tests ====================
  26. Test.add_func("/invercargill/expressions/literal_int", () => {
  27. var expr = new LiteralExpression(new NativeElement<int>(42));
  28. var props = new PropertyDictionary();
  29. var context = new EvaluationContext(props);
  30. var result = expr.evaluate(context);
  31. assert(result != null);
  32. int value;
  33. assert(result.try_get_as<int>(out value));
  34. assert(value == 42);
  35. });
  36. Test.add_func("/invercargill/expressions/literal_string", () => {
  37. var expr = new LiteralExpression(new NativeElement<string>("hello"));
  38. var props = new PropertyDictionary();
  39. var context = new EvaluationContext(props);
  40. var result = expr.evaluate(context);
  41. assert(result != null);
  42. string value;
  43. assert(result.try_get_as<string>(out value));
  44. assert(value == "hello");
  45. });
  46. Test.add_func("/invercargill/expressions/literal_bool", () => {
  47. var expr = new LiteralExpression(new NativeElement<bool>(true));
  48. var props = new PropertyDictionary();
  49. var context = new EvaluationContext(props);
  50. var result = expr.evaluate(context);
  51. assert(result != null);
  52. bool value;
  53. assert(result.try_get_as<bool>(out value));
  54. assert(value == true);
  55. });
  56. Test.add_func("/invercargill/expressions/literal_null", () => {
  57. var expr = new LiteralExpression(new NullElement());
  58. var props = new PropertyDictionary();
  59. var context = new EvaluationContext(props);
  60. var result = expr.evaluate(context);
  61. assert(result != null);
  62. assert(result.is_null());
  63. });
  64. // ==================== Variable Tests ====================
  65. Test.add_func("/invercargill/expressions/variable", () => {
  66. var expr = new VariableExpression("x");
  67. var props = new PropertyDictionary();
  68. props.set("x", new NativeElement<int>(10));
  69. var context = new EvaluationContext(props);
  70. var result = expr.evaluate(context);
  71. assert(result != null);
  72. int value;
  73. assert(result.try_get_as<int>(out value));
  74. assert(value == 10);
  75. });
  76. // ==================== Binary Comparison Tests ====================
  77. Test.add_func("/invercargill/expressions/binary_equals", () => {
  78. var expr = new BinaryExpression(
  79. new LiteralExpression(new NativeElement<int>(5)),
  80. new LiteralExpression(new NativeElement<int>(5)),
  81. BinaryOperator.EQUAL
  82. );
  83. var props = new PropertyDictionary();
  84. var context = new EvaluationContext(props);
  85. var result = expr.evaluate(context);
  86. assert(result != null);
  87. bool value;
  88. assert(result.try_get_as<bool>(out value));
  89. assert(value == true);
  90. });
  91. Test.add_func("/invercargill/expressions/binary_not_equals", () => {
  92. var expr = new BinaryExpression(
  93. new LiteralExpression(new NativeElement<int>(5)),
  94. new LiteralExpression(new NativeElement<int>(3)),
  95. BinaryOperator.NOT_EQUAL
  96. );
  97. var props = new PropertyDictionary();
  98. var context = new EvaluationContext(props);
  99. var result = expr.evaluate(context);
  100. assert(result != null);
  101. bool value;
  102. assert(result.try_get_as<bool>(out value));
  103. assert(value == true);
  104. });
  105. Test.add_func("/invercargill/expressions/binary_greater_than", () => {
  106. var expr = new BinaryExpression(
  107. new LiteralExpression(new NativeElement<int>(5)),
  108. new LiteralExpression(new NativeElement<int>(3)),
  109. BinaryOperator.GREATER_THAN
  110. );
  111. var props = new PropertyDictionary();
  112. var context = new EvaluationContext(props);
  113. var result = expr.evaluate(context);
  114. assert(result != null);
  115. bool value;
  116. assert(result.try_get_as<bool>(out value));
  117. assert(value == true);
  118. });
  119. Test.add_func("/invercargill/expressions/binary_less_than", () => {
  120. var expr = new BinaryExpression(
  121. new LiteralExpression(new NativeElement<int>(3)),
  122. new LiteralExpression(new NativeElement<int>(5)),
  123. BinaryOperator.LESS_THAN
  124. );
  125. var props = new PropertyDictionary();
  126. var context = new EvaluationContext(props);
  127. var result = expr.evaluate(context);
  128. assert(result != null);
  129. bool value;
  130. assert(result.try_get_as<bool>(out value));
  131. assert(value == true);
  132. });
  133. // ==================== Binary Logical Tests ====================
  134. Test.add_func("/invercargill/expressions/binary_and", () => {
  135. var expr = new BinaryExpression(
  136. new LiteralExpression(new NativeElement<bool>(true)),
  137. new LiteralExpression(new NativeElement<bool>(true)),
  138. BinaryOperator.AND
  139. );
  140. var props = new PropertyDictionary();
  141. var context = new EvaluationContext(props);
  142. var result = expr.evaluate(context);
  143. assert(result != null);
  144. bool value;
  145. assert(result.try_get_as<bool>(out value));
  146. assert(value == true);
  147. });
  148. Test.add_func("/invercargill/expressions/binary_or", () => {
  149. var expr = new BinaryExpression(
  150. new LiteralExpression(new NativeElement<bool>(false)),
  151. new LiteralExpression(new NativeElement<bool>(true)),
  152. BinaryOperator.OR
  153. );
  154. var props = new PropertyDictionary();
  155. var context = new EvaluationContext(props);
  156. var result = expr.evaluate(context);
  157. assert(result != null);
  158. bool value;
  159. assert(result.try_get_as<bool>(out value));
  160. assert(value == true);
  161. });
  162. // ==================== Binary Arithmetic Tests ====================
  163. Test.add_func("/invercargill/expressions/binary_arithmetic_add", () => {
  164. var expr = new BinaryExpression(
  165. new LiteralExpression(new NativeElement<int>(5)),
  166. new LiteralExpression(new NativeElement<int>(3)),
  167. BinaryOperator.ADD
  168. );
  169. var props = new PropertyDictionary();
  170. var context = new EvaluationContext(props);
  171. var result = expr.evaluate(context);
  172. assert(result != null);
  173. int64? value;
  174. assert(result.try_get_as<int64?>(out value));
  175. assert(value == 8);
  176. });
  177. Test.add_func("/invercargill/expressions/binary_arithmetic_subtract", () => {
  178. var expr = new BinaryExpression(
  179. new LiteralExpression(new NativeElement<int>(5)),
  180. new LiteralExpression(new NativeElement<int>(3)),
  181. BinaryOperator.SUBTRACT
  182. );
  183. var props = new PropertyDictionary();
  184. var context = new EvaluationContext(props);
  185. var result = expr.evaluate(context);
  186. assert(result != null);
  187. int64? value;
  188. assert(result.try_get_as<int64?>(out value));
  189. assert(value == 2);
  190. });
  191. Test.add_func("/invercargill/expressions/binary_arithmetic_multiply", () => {
  192. var expr = new BinaryExpression(
  193. new LiteralExpression(new NativeElement<int>(5)),
  194. new LiteralExpression(new NativeElement<int>(3)),
  195. BinaryOperator.MULTIPLY
  196. );
  197. var props = new PropertyDictionary();
  198. var context = new EvaluationContext(props);
  199. var result = expr.evaluate(context);
  200. assert(result != null);
  201. int64? value;
  202. assert(result.try_get_as<int64?>(out value));
  203. assert(value == 15);
  204. });
  205. Test.add_func("/invercargill/expressions/binary_arithmetic_divide", () => {
  206. var expr = new BinaryExpression(
  207. new LiteralExpression(new NativeElement<int>(15)),
  208. new LiteralExpression(new NativeElement<int>(3)),
  209. BinaryOperator.DIVIDE
  210. );
  211. var props = new PropertyDictionary();
  212. var context = new EvaluationContext(props);
  213. var result = expr.evaluate(context);
  214. assert(result != null);
  215. int64? value;
  216. assert(result.try_get_as<int64?>(out value));
  217. assert(value == 5);
  218. });
  219. Test.add_func("/invercargill/expressions/binary_arithmetic_modulo", () => {
  220. var expr = new BinaryExpression(
  221. new LiteralExpression(new NativeElement<int>(17)),
  222. new LiteralExpression(new NativeElement<int>(5)),
  223. BinaryOperator.MODULO
  224. );
  225. var props = new PropertyDictionary();
  226. var context = new EvaluationContext(props);
  227. var result = expr.evaluate(context);
  228. assert(result != null);
  229. int64? value;
  230. assert(result.try_get_as<int64?>(out value));
  231. assert(value == 2);
  232. });
  233. // ==================== Unary Tests ====================
  234. Test.add_func("/invercargill/expressions/unary_negate", () => {
  235. var expr = new UnaryExpression(
  236. UnaryOperator.NEGATE,
  237. new LiteralExpression(new NativeElement<int>(5))
  238. );
  239. var props = new PropertyDictionary();
  240. var context = new EvaluationContext(props);
  241. var result = expr.evaluate(context);
  242. assert(result != null);
  243. int64? value;
  244. assert(result.try_get_as<int64?>(out value));
  245. assert(value == -5);
  246. });
  247. Test.add_func("/invercargill/expressions/unary_not", () => {
  248. var expr = new UnaryExpression(
  249. UnaryOperator.NOT,
  250. new LiteralExpression(new NativeElement<bool>(false))
  251. );
  252. var props = new PropertyDictionary();
  253. var context = new EvaluationContext(props);
  254. var result = expr.evaluate(context);
  255. assert(result != null);
  256. bool value;
  257. assert(result.try_get_as<bool>(out value));
  258. assert(value == true);
  259. });
  260. // ==================== Ternary Tests ====================
  261. Test.add_func("/invercargill/expressions/ternary_true", () => {
  262. var expr = new TernaryExpression(
  263. new LiteralExpression(new NativeElement<bool>(true)),
  264. new LiteralExpression(new NativeElement<string>("yes")),
  265. new LiteralExpression(new NativeElement<string>("no"))
  266. );
  267. var props = new PropertyDictionary();
  268. var context = new EvaluationContext(props);
  269. var result = expr.evaluate(context);
  270. assert(result != null);
  271. string value;
  272. assert(result.try_get_as<string>(out value));
  273. assert(value == "yes");
  274. });
  275. Test.add_func("/invercargill/expressions/ternary_false", () => {
  276. var expr = new TernaryExpression(
  277. new LiteralExpression(new NativeElement<bool>(false)),
  278. new LiteralExpression(new NativeElement<string>("yes")),
  279. new LiteralExpression(new NativeElement<string>("no"))
  280. );
  281. var props = new PropertyDictionary();
  282. var context = new EvaluationContext(props);
  283. var result = expr.evaluate(context);
  284. assert(result != null);
  285. string value;
  286. assert(result.try_get_as<string>(out value));
  287. assert(value == "no");
  288. });
  289. // ==================== Property Access Tests ====================
  290. Test.add_func("/invercargill/expressions/property_access", () => {
  291. var person = new ExprTestPerson("Alice", 30);
  292. var expr = new PropertyExpression(
  293. new VariableExpression("p"),
  294. "name"
  295. );
  296. var props = new PropertyDictionary();
  297. props.set("p", new NativeElement<ExprTestPerson?>(person));
  298. var context = new EvaluationContext(props);
  299. var result = expr.evaluate(context);
  300. assert(result != null);
  301. string value;
  302. assert(result.try_get_as<string>(out value));
  303. assert(value == "Alice");
  304. });
  305. Test.add_func("/invercargill/expressions/nested_property_access", () => {
  306. var person = new ExprTestPerson("Bob", 25);
  307. var expr = new PropertyExpression(
  308. new VariableExpression("p"),
  309. "age"
  310. );
  311. var props = new PropertyDictionary();
  312. props.set("p", new NativeElement<ExprTestPerson>(person));
  313. var context = new EvaluationContext(props);
  314. var result = expr.evaluate(context);
  315. assert(result != null);
  316. int value;
  317. assert(result.try_get_as<int>(out value));
  318. assert(value == 25);
  319. });
  320. // ==================== Bracketed Tests ====================
  321. Test.add_func("/invercargill/expressions/bracketed_expression", () => {
  322. // (5 + 3) * 2 = 16
  323. var inner = new BracketedExpression(
  324. new BinaryExpression(
  325. new LiteralExpression(new NativeElement<int>(5)),
  326. new LiteralExpression(new NativeElement<int>(3)),
  327. BinaryOperator.ADD
  328. )
  329. );
  330. var expr = new BinaryExpression(
  331. inner,
  332. new LiteralExpression(new NativeElement<int>(2)),
  333. BinaryOperator.MULTIPLY
  334. );
  335. var props = new PropertyDictionary();
  336. var context = new EvaluationContext(props);
  337. var result = expr.evaluate(context);
  338. assert(result != null);
  339. int64? value;
  340. assert(result.try_get_as<int64?>(out value));
  341. assert(value == 16);
  342. });
  343. // ==================== Complex Expression Tests ====================
  344. Test.add_func("/invercargill/expressions/complex_expression", () => {
  345. // a > 5 && b < 10
  346. var expr = new BinaryExpression(
  347. new BinaryExpression(
  348. new VariableExpression("a"),
  349. new LiteralExpression(new NativeElement<int>(5)),
  350. BinaryOperator.GREATER_THAN
  351. ),
  352. new BinaryExpression(
  353. new VariableExpression("b"),
  354. new LiteralExpression(new NativeElement<int>(10)),
  355. BinaryOperator.LESS_THAN
  356. ),
  357. BinaryOperator.AND
  358. );
  359. var props = new PropertyDictionary();
  360. props.set("a", new NativeElement<int>(7));
  361. props.set("b", new NativeElement<int>(8));
  362. var context = new EvaluationContext(props);
  363. var result = expr.evaluate(context);
  364. assert(result != null);
  365. bool value;
  366. assert(result.try_get_as<bool>(out value));
  367. assert(value == true);
  368. });
  369. // ==================== Lambda Tests ====================
  370. Test.add_func("/invercargill/expressions/lambda_basic", () => {
  371. // x => x > 5
  372. var body = new BinaryExpression(
  373. new VariableExpression("x"),
  374. new LiteralExpression(new NativeElement<int>(5)),
  375. BinaryOperator.GREATER_THAN
  376. );
  377. var expr = new LambdaExpression("x", body);
  378. var props = new PropertyDictionary();
  379. var context = new EvaluationContext(props);
  380. var result = expr.evaluate(context);
  381. assert(result != null);
  382. var lambda = result as LambdaElement;
  383. assert(lambda != null);
  384. assert(lambda.get_lambda().parameter_name == "x");
  385. });
  386. // ==================== Function Call Tests ====================
  387. Test.add_func("/invercargill/expressions/function_call_where", () => {
  388. // Create an enumerable of test persons
  389. var persons = new Series<ExprTestPerson>();
  390. persons.add(new ExprTestPerson("Alice", 30, 5));
  391. persons.add(new ExprTestPerson("Bob", 25, 3));
  392. persons.add(new ExprTestPerson("Charlie", 35, 7));
  393. // persons.where(p => p.rank > 4)
  394. var lambda_body = new BinaryExpression(
  395. new PropertyExpression(new VariableExpression("p"), "rank"),
  396. new LiteralExpression(new NativeElement<int>(4)),
  397. BinaryOperator.GREATER_THAN
  398. );
  399. var lambda = new LambdaExpression("p", lambda_body);
  400. var args = new Series<Expression>();
  401. args.add(lambda);
  402. var expr = new FunctionCallExpression(
  403. new LiteralExpression(new NativeElement<Enumerable<ExprTestPerson>?>(persons)),
  404. "where",
  405. args
  406. );
  407. var props = new PropertyDictionary();
  408. var context = new EvaluationContext(props);
  409. var result = expr.evaluate(context);
  410. assert(result != null);
  411. Elements elements;
  412. assert(result.try_get_as<Elements>(out elements));
  413. // Should have 2 elements (Alice with rank 5, Charlie with rank 7)
  414. var count = 0;
  415. elements.iterate(e => count++);
  416. assert(count == 2);
  417. });
  418. Test.add_func("/invercargill/expressions/function_call_select", () => {
  419. // Create an enumerable of test persons
  420. var persons = new Series<ExprTestPerson>();
  421. persons.add(new ExprTestPerson("Alice", 30));
  422. persons.add(new ExprTestPerson("Bob", 25));
  423. // persons.select(p => p.name)
  424. var lambda_body = new PropertyExpression(new VariableExpression("p"), "name");
  425. var lambda = new LambdaExpression("p", lambda_body);
  426. var args = new Series<Expression>();
  427. args.add(lambda);
  428. var expr = new FunctionCallExpression(
  429. new LiteralExpression(new NativeElement<Enumerable<ExprTestPerson>>(persons)),
  430. "select",
  431. args
  432. );
  433. var props = new PropertyDictionary();
  434. var context = new EvaluationContext(props);
  435. var result = expr.evaluate(context);
  436. assert(result != null);
  437. Elements elements;
  438. assert(result.try_get_as<Elements>(out elements));
  439. // Should have 2 string elements
  440. var names = new Series<string>();
  441. elements.iterate(e => {
  442. string? name;
  443. if(e.try_get_as<string>(out name)) {
  444. names.add(name);
  445. }
  446. });
  447. assert(names.length == 2);
  448. // Check names using iterate
  449. var idx = 0;
  450. names.iterate(n => {
  451. if (idx == 0) assert(n == "Alice");
  452. if (idx == 1) assert(n == "Bob");
  453. idx++;
  454. });
  455. });
  456. Test.add_func("/invercargill/expressions/function_call_first", () => {
  457. // Create an enumerable of test persons
  458. var persons = new Series<ExprTestPerson>();
  459. persons.add(new ExprTestPerson("Alice", 30));
  460. persons.add(new ExprTestPerson("Bob", 25));
  461. // persons.first()
  462. var expr = new FunctionCallExpression(
  463. new LiteralExpression(new NativeElement<Enumerable<ExprTestPerson>>(persons)),
  464. "first"
  465. );
  466. var props = new PropertyDictionary();
  467. var context = new EvaluationContext(props);
  468. var result = expr.evaluate(context);
  469. assert(result != null);
  470. ExprTestPerson? person;
  471. assert(result.try_get_as<ExprTestPerson?>(out person));
  472. assert(person.name == "Alice");
  473. });
  474. Test.add_func("/invercargill/expressions/function_call_count", () => {
  475. // Create an enumerable of test persons
  476. var persons = new Series<ExprTestPerson>();
  477. persons.add(new ExprTestPerson("Alice", 30));
  478. persons.add(new ExprTestPerson("Bob", 25));
  479. persons.add(new ExprTestPerson("Charlie", 35));
  480. // persons.count()
  481. var expr = new FunctionCallExpression(
  482. new LiteralExpression(new NativeElement<Enumerable<ExprTestPerson>>(persons)),
  483. "count"
  484. );
  485. var props = new PropertyDictionary();
  486. var context = new EvaluationContext(props);
  487. var result = expr.evaluate(context);
  488. assert(result != null);
  489. int64? count;
  490. assert(result.try_get_as<int64?>(out count));
  491. assert(count == 3);
  492. });
  493. Test.add_func("/invercargill/expressions/function_call_any", () => {
  494. // Create an enumerable of test persons
  495. var persons = new Series<ExprTestPerson>();
  496. persons.add(new ExprTestPerson("Alice", 30, 5));
  497. persons.add(new ExprTestPerson("Bob", 25, 3));
  498. // persons.any(p => p.rank > 4)
  499. var lambda_body = new BinaryExpression(
  500. new PropertyExpression(new VariableExpression("p"), "rank"),
  501. new LiteralExpression(new NativeElement<int>(4)),
  502. BinaryOperator.GREATER_THAN
  503. );
  504. var lambda = new LambdaExpression("p", lambda_body);
  505. var args = new Series<Expression>();
  506. args.add(lambda);
  507. var expr = new FunctionCallExpression(
  508. new LiteralExpression(new NativeElement<Enumerable<ExprTestPerson>?>(persons)),
  509. "any",
  510. args
  511. );
  512. var props = new PropertyDictionary();
  513. var context = new EvaluationContext(props);
  514. var result = expr.evaluate(context);
  515. assert(result != null);
  516. bool any;
  517. assert(result.try_get_as<bool>(out any));
  518. assert(any == true);
  519. });
  520. // ==================== Chained Function Tests ====================
  521. Test.add_func("/invercargill/expressions/chained_where_first", () => {
  522. // Create an enumerable of test persons
  523. var persons = new Series<ExprTestPerson>();
  524. persons.add(new ExprTestPerson("Alice", 30, 5));
  525. persons.add(new ExprTestPerson("Bob", 25, 3));
  526. persons.add(new ExprTestPerson("Charlie", 35, 7));
  527. // persons.where(p => p.rank > 4).first()
  528. var lambda_body = new BinaryExpression(
  529. new PropertyExpression(new VariableExpression("p"), "rank"),
  530. new LiteralExpression(new NativeElement<int>(4)),
  531. BinaryOperator.GREATER_THAN
  532. );
  533. var lambda = new LambdaExpression("p", lambda_body);
  534. var args = new Series<Expression>();
  535. args.add(lambda);
  536. var where_call = new FunctionCallExpression(
  537. new LiteralExpression(new NativeElement<Enumerable<ExprTestPerson>?>(persons)),
  538. "where",
  539. args
  540. );
  541. var first_call = new FunctionCallExpression(
  542. where_call,
  543. "first"
  544. );
  545. var props = new PropertyDictionary();
  546. var context = new EvaluationContext(props);
  547. var result = first_call.evaluate(context);
  548. assert(result != null);
  549. ExprTestPerson? person;
  550. assert(result.try_get_as<ExprTestPerson?>(out person));
  551. assert(person.name == "Alice"); // First with rank > 4
  552. });
  553. // ==================== Closure Tests ====================
  554. Test.add_func("/invercargill/expressions/closure_capture", () => {
  555. // Test that lambda can capture variables from outer scope
  556. // threshold = 4
  557. // persons.where(p => p.rank > threshold)
  558. var persons = new Series<ExprTestPerson>();
  559. persons.add(new ExprTestPerson("Alice", 30, 5));
  560. persons.add(new ExprTestPerson("Bob", 25, 3));
  561. persons.add(new ExprTestPerson("Charlie", 35, 7));
  562. var threshold = 4;
  563. // Create context with threshold
  564. var props = new PropertyDictionary();
  565. props.set("threshold", new NativeElement<int>(threshold));
  566. var parent_context = new EvaluationContext(props);
  567. // Lambda body: p.rank > threshold
  568. var lambda_body = new BinaryExpression(
  569. new PropertyExpression(new VariableExpression("p"), "rank"),
  570. new VariableExpression("threshold"),
  571. BinaryOperator.GREATER_THAN
  572. );
  573. var lambda = new LambdaExpression("p", lambda_body);
  574. var args = new Series<Expression>();
  575. args.add(lambda);
  576. var expr = new FunctionCallExpression(
  577. new LiteralExpression(new NativeElement<Enumerable<ExprTestPerson>?>(persons)),
  578. "where",
  579. args
  580. );
  581. var result = expr.evaluate(parent_context);
  582. assert(result != null);
  583. Elements elements;
  584. assert(result.try_get_as<Elements>(out elements));
  585. // Should have 2 elements (Alice with rank 5, Charlie with rank 7)
  586. var count = 0;
  587. elements.iterate(e => count++);
  588. assert(count == 2);
  589. });
  590. // ==================== Parser Tests ====================
  591. Test.add_func("/invercargill/expressions/parser_literal_int", () => {
  592. var expr = ExpressionParser.parse("42");
  593. var props = new PropertyDictionary();
  594. var context = new EvaluationContext(props);
  595. var result = expr.evaluate(context);
  596. assert(result != null);
  597. int64? value;
  598. assert(result.try_get_as<int64?>(out value));
  599. assert(value == 42);
  600. });
  601. Test.add_func("/invercargill/expressions/parser_literal_string", () => {
  602. var expr = ExpressionParser.parse("\"hello\"");
  603. var props = new PropertyDictionary();
  604. var context = new EvaluationContext(props);
  605. var result = expr.evaluate(context);
  606. assert(result != null);
  607. string value;
  608. assert(result.try_get_as<string>(out value));
  609. assert(value == "hello");
  610. });
  611. Test.add_func("/invercargill/expressions/parser_literal_bool", () => {
  612. var expr = ExpressionParser.parse("true");
  613. var props = new PropertyDictionary();
  614. var context = new EvaluationContext(props);
  615. var result = expr.evaluate(context);
  616. assert(result != null);
  617. bool value;
  618. assert(result.try_get_as<bool>(out value));
  619. assert(value == true);
  620. });
  621. Test.add_func("/invercargill/expressions/parser_variable", () => {
  622. var expr = ExpressionParser.parse("x");
  623. var props = new PropertyDictionary();
  624. props.set("x", new NativeElement<int>(10));
  625. var context = new EvaluationContext(props);
  626. var result = expr.evaluate(context);
  627. assert(result != null);
  628. int value;
  629. assert(result.try_get_as<int>(out value));
  630. assert(value == 10);
  631. });
  632. Test.add_func("/invercargill/expressions/parser_binary_equals", () => {
  633. var expr = ExpressionParser.parse("5 == 5");
  634. var props = new PropertyDictionary();
  635. var context = new EvaluationContext(props);
  636. var result = expr.evaluate(context);
  637. assert(result != null);
  638. bool value;
  639. assert(result.try_get_as<bool>(out value));
  640. assert(value == true);
  641. });
  642. Test.add_func("/invercargill/expressions/parser_binary_arithmetic", () => {
  643. var expr = ExpressionParser.parse("5 + 3");
  644. var props = new PropertyDictionary();
  645. var context = new EvaluationContext(props);
  646. var result = expr.evaluate(context);
  647. assert(result != null);
  648. int64? value;
  649. assert(result.try_get_as<int64?>(out value));
  650. assert(value == 8);
  651. });
  652. Test.add_func("/invercargill/expressions/parser_binary_logical", () => {
  653. var expr = ExpressionParser.parse("true && false");
  654. var props = new PropertyDictionary();
  655. var context = new EvaluationContext(props);
  656. var result = expr.evaluate(context);
  657. assert(result != null);
  658. bool value;
  659. assert(result.try_get_as<bool>(out value));
  660. assert(value == false);
  661. });
  662. Test.add_func("/invercargill/expressions/parser_ternary", () => {
  663. var expr = ExpressionParser.parse("true ? \"yes\" : \"no\"");
  664. var props = new PropertyDictionary();
  665. var context = new EvaluationContext(props);
  666. var result = expr.evaluate(context);
  667. assert(result != null);
  668. string value;
  669. assert(result.try_get_as<string>(out value));
  670. assert(value == "yes");
  671. });
  672. Test.add_func("/invercargill/expressions/parser_property_access", () => {
  673. var person = new ExprTestPerson("Alice", 30);
  674. var expr = ExpressionParser.parse("p.name");
  675. var props = new PropertyDictionary();
  676. props.set("p", new NativeElement<ExprTestPerson?>(person));
  677. var context = new EvaluationContext(props);
  678. var result = expr.evaluate(context);
  679. assert(result != null);
  680. string value;
  681. assert(result.try_get_as<string>(out value));
  682. assert(value == "Alice");
  683. });
  684. Test.add_func("/invercargill/expressions/parser_bracketed", () => {
  685. // (5 + 3) * 2 = 16
  686. var expr = ExpressionParser.parse("(5 + 3) * 2");
  687. var props = new PropertyDictionary();
  688. var context = new EvaluationContext(props);
  689. var result = expr.evaluate(context);
  690. assert(result != null);
  691. int64? value;
  692. assert(result.try_get_as<int64?>(out value));
  693. assert(value == 16);
  694. });
  695. Test.add_func("/invercargill/expressions/parser_unary_negate", () => {
  696. var expr = ExpressionParser.parse("-5");
  697. var props = new PropertyDictionary();
  698. var context = new EvaluationContext(props);
  699. var result = expr.evaluate(context);
  700. assert(result != null);
  701. int64? value;
  702. assert(result.try_get_as<int64?>(out value));
  703. assert(value == -5);
  704. });
  705. Test.add_func("/invercargill/expressions/parser_unary_not", () => {
  706. var expr = ExpressionParser.parse("!false");
  707. var props = new PropertyDictionary();
  708. var context = new EvaluationContext(props);
  709. var result = expr.evaluate(context);
  710. assert(result != null);
  711. bool value;
  712. assert(result.try_get_as<bool>(out value));
  713. assert(value == true);
  714. });
  715. Test.add_func("/invercargill/expressions/parser_lambda", () => {
  716. var expr = ExpressionParser.parse("x => x > 5");
  717. var props = new PropertyDictionary();
  718. var context = new EvaluationContext(props);
  719. var result = expr.evaluate(context);
  720. assert(result != null);
  721. var lambda = result as LambdaElement;
  722. assert(lambda != null);
  723. assert(lambda.get_lambda().parameter_name == "x");
  724. });
  725. Test.add_func("/invercargill/expressions/parser_complex", () => {
  726. // a > 5 && b < 10
  727. var expr = ExpressionParser.parse("a > 5 && b < 10");
  728. var props = new PropertyDictionary();
  729. props.set("a", new NativeElement<int>(7));
  730. props.set("b", new NativeElement<int>(8));
  731. var context = new EvaluationContext(props);
  732. var result = expr.evaluate(context);
  733. assert(result != null);
  734. bool value;
  735. assert(result.try_get_as<bool>(out value));
  736. assert(value == true);
  737. });
  738. Test.add_func("/invercargill/expressions/parser_operator_precedence", () => {
  739. // 2 + 3 * 4 = 14 (not 20)
  740. var expr = ExpressionParser.parse("2 + 3 * 4");
  741. var props = new PropertyDictionary();
  742. var context = new EvaluationContext(props);
  743. var result = expr.evaluate(context);
  744. assert(result != null);
  745. int64? value;
  746. assert(result.try_get_as<int64?>(out value));
  747. assert(value == 14);
  748. });
  749. Test.add_func("/invercargill/expressions/parser_comparison_chain", () => {
  750. // Test chained comparisons with proper precedence
  751. var expr = ExpressionParser.parse("5 < 10 && 10 > 5");
  752. var props = new PropertyDictionary();
  753. var context = new EvaluationContext(props);
  754. var result = expr.evaluate(context);
  755. assert(result != null);
  756. bool value;
  757. assert(result.try_get_as<bool>(out value));
  758. assert(value == true);
  759. });
  760. // ==================== Lot Literal Tests ====================
  761. Test.add_func("/invercargill/expressions/lot_literal_empty", () => {
  762. var expr = new LotLiteralExpression(new Expression[0]);
  763. var props = new PropertyDictionary();
  764. var context = new EvaluationContext(props);
  765. var result = expr.evaluate(context);
  766. assert(result != null);
  767. Enumerable<Object?>? lot;
  768. assert(result.try_get_as<Enumerable<Object?>>(out lot));
  769. assert(lot != null);
  770. var count = 0;
  771. foreach (var item in lot) {
  772. count++;
  773. }
  774. assert(count == 0);
  775. });
  776. Test.add_func("/invercargill/expressions/lot_literal_integers", () => {
  777. var elements = new Expression[3];
  778. elements[0] = new LiteralExpression(new NativeElement<int>(1));
  779. elements[1] = new LiteralExpression(new NativeElement<int>(2));
  780. elements[2] = new LiteralExpression(new NativeElement<int>(3));
  781. var expr = new LotLiteralExpression(elements);
  782. var props = new PropertyDictionary();
  783. var context = new EvaluationContext(props);
  784. var result = expr.evaluate(context);
  785. assert(result != null);
  786. Enumerable<int>? lot;
  787. assert(result.try_get_as<Enumerable<int>?>(out lot));
  788. assert(lot != null);
  789. var values = new Series<int>();
  790. foreach (var item in lot) {
  791. values.add(item);
  792. }
  793. assert(values.length == 3);
  794. var idx = 0;
  795. foreach (var val in values) {
  796. if (idx == 0) assert(val == 1);
  797. else if (idx == 1) assert(val == 2);
  798. else if (idx == 2) assert(val == 3);
  799. idx++;
  800. }
  801. });
  802. Test.add_func("/invercargill/expressions/lot_literal_strings", () => {
  803. var elements = new Expression[3];
  804. elements[0] = new LiteralExpression(new NativeElement<string>("a"));
  805. elements[1] = new LiteralExpression(new NativeElement<string>("b"));
  806. elements[2] = new LiteralExpression(new NativeElement<string>("c"));
  807. var expr = new LotLiteralExpression(elements);
  808. var props = new PropertyDictionary();
  809. var context = new EvaluationContext(props);
  810. var result = expr.evaluate(context);
  811. assert(result != null);
  812. Enumerable<string?>? lot;
  813. assert(result.try_get_as<Enumerable<string?>>(out lot));
  814. assert(lot != null);
  815. var values = new Series<string?>();
  816. foreach (var item in lot) {
  817. values.add(item);
  818. }
  819. assert(values.length == 3);
  820. });
  821. Test.add_func("/invercargill/expressions/lot_literal_parser", () => {
  822. var expr = ExpressionParser.parse("[1, 2, 3]");
  823. var props = new PropertyDictionary();
  824. var context = new EvaluationContext(props);
  825. var result = expr.evaluate(context);
  826. assert(result != null);
  827. Enumerable<int>? lot;
  828. assert(result.try_get_as<Enumerable<int>?>(out lot));
  829. assert(lot != null);
  830. var count = 0;
  831. foreach (var item in lot) {
  832. count++;
  833. }
  834. assert(count == 3);
  835. });
  836. // ==================== Iterate Function Tests ====================
  837. Test.add_func("/invercargill/expressions/iterate_range", () => {
  838. var accessor = new IterateFunctionAccessor();
  839. var args = new Series<Element>();
  840. args.add(new NativeElement<int>(0));
  841. args.add(new NativeElement<int>(5));
  842. var result = accessor.call_function("range", args, new EvaluationContext(new PropertyDictionary()));
  843. assert(result != null);
  844. Enumerable<int>? range;
  845. assert(result.try_get_as<Enumerable<int>?>(out range));
  846. assert(range != null);
  847. var values = new Series<int>();
  848. foreach (var item in range) {
  849. values.add(item);
  850. }
  851. assert(values.length == 5);
  852. });
  853. Test.add_func("/invercargill/expressions/iterate_single", () => {
  854. var accessor = new IterateFunctionAccessor();
  855. var args = new Series<Element>();
  856. args.add(new NativeElement<int>(42));
  857. var result = accessor.call_function("single", args, new EvaluationContext(new PropertyDictionary()));
  858. assert(result != null);
  859. Enumerable<int>? single;
  860. assert(result.try_get_as<Enumerable<int>?>(out single));
  861. assert(single != null);
  862. var count = 0;
  863. foreach (var item in single) {
  864. assert(item == 42);
  865. count++;
  866. }
  867. assert(count == 1);
  868. });
  869. Test.add_func("/invercargill/expressions/iterate_nothing", () => {
  870. var accessor = new IterateFunctionAccessor();
  871. var args = new Series<Element>();
  872. var result = accessor.call_function("nothing", args, new EvaluationContext(new PropertyDictionary()));
  873. assert(result != null);
  874. Enumerable<Object?>? empty;
  875. assert(result.try_get_as<Enumerable<Object?>>(out empty));
  876. assert(empty != null);
  877. var count = 0;
  878. foreach (var item in empty) {
  879. count++;
  880. }
  881. assert(count == 0);
  882. });
  883. Test.add_func("/invercargill/expressions/iterate_range_with_step", () => {
  884. var accessor = new IterateFunctionAccessor();
  885. var args = new Series<Element>();
  886. args.add(new NativeElement<int>(0));
  887. args.add(new NativeElement<int>(10));
  888. args.add(new NativeElement<int>(2));
  889. var result = accessor.call_function("range", args, new EvaluationContext(new PropertyDictionary()));
  890. assert(result != null);
  891. Enumerable<int>? range;
  892. assert(result.try_get_as<Enumerable<int>?>(out range));
  893. assert(range != null);
  894. var values = new Series<int>();
  895. foreach (var item in range) {
  896. values.add(item);
  897. }
  898. assert(values.length == 5); // 0, 2, 4, 6, 8
  899. });
  900. Test.add_func("/invercargill/expressions/global_functions_element", () => {
  901. var accessor = new IterateFunctionAccessor();
  902. var global_element = new GlobalFunctionsElement(accessor);
  903. var props = new PropertyDictionary();
  904. props.set("Iterate", global_element);
  905. var context = new EvaluationContext(props);
  906. // Get the variable
  907. var expr = new VariableExpression("Iterate");
  908. var result = expr.evaluate(context);
  909. assert(result != null);
  910. GlobalFunctionsElement? retrieved;
  911. assert(result.try_get_as<GlobalFunctionsElement?>(out retrieved));
  912. assert(retrieved != null);
  913. assert(retrieved.accessor is IterateFunctionAccessor);
  914. });
  915. // ==================== Global Format Function Tests ====================
  916. Test.add_func("/invercargill/expressions/format_string", () => {
  917. // format("Hello, {{name}}!")
  918. var expr = ExpressionParser.parse("format(\"Hello, {{name}}!\")");
  919. var props = new PropertyDictionary();
  920. props.set("name", new NativeElement<string>("World"));
  921. var context = new EvaluationContext(props);
  922. var result = expr.evaluate(context);
  923. assert(result != null);
  924. string value;
  925. assert(result.try_get_as<string>(out value));
  926. assert(value == "Hello, World!");
  927. });
  928. Test.add_func("/invercargill/expressions/format_integer", () => {
  929. // format("Count: {{count}}")
  930. var expr = ExpressionParser.parse("format(\"Count: {{count}}\")");
  931. var props = new PropertyDictionary();
  932. props.set("count", new NativeElement<int>(42));
  933. var context = new EvaluationContext(props);
  934. var result = expr.evaluate(context);
  935. assert(result != null);
  936. string value;
  937. assert(result.try_get_as<string>(out value));
  938. assert(value == "Count: 42");
  939. });
  940. Test.add_func("/invercargill/expressions/format_multiple", () => {
  941. // format("{{category}} #{{id}}: {{title}}")
  942. var expr = ExpressionParser.parse("format(\"{{category}} #{{id}}: {{title}}\")");
  943. var props = new PropertyDictionary();
  944. props.set("category", new NativeElement<string>("Item"));
  945. props.set("id", new NativeElement<int>(123));
  946. props.set("title", new NativeElement<string>("Test Product"));
  947. var context = new EvaluationContext(props);
  948. var result = expr.evaluate(context);
  949. assert(result != null);
  950. string value;
  951. assert(result.try_get_as<string>(out value));
  952. assert(value == "Item #123: Test Product");
  953. });
  954. Test.add_func("/invercargill/expressions/format_float", () => {
  955. // format("Price: {{price}}")
  956. var expr = ExpressionParser.parse("format(\"Price: {{price}}\")");
  957. var props = new PropertyDictionary();
  958. props.set("price", new NativeElement<double?>(19.99));
  959. var context = new EvaluationContext(props);
  960. var result = expr.evaluate(context);
  961. assert(result != null);
  962. string value;
  963. assert(result.try_get_as<string>(out value));
  964. assert(value == "Price: 19.99");
  965. });
  966. Test.add_func("/invercargill/expressions/format_hex", () => {
  967. // Note: Handlebars format doesn't support hex formatting natively
  968. // Using stringify to convert the value
  969. var expr = ExpressionParser.parse("format(\"Hex: {{value}}\")");
  970. var props = new PropertyDictionary();
  971. props.set("value", new NativeElement<int>(255));
  972. var context = new EvaluationContext(props);
  973. var result = expr.evaluate(context);
  974. assert(result != null);
  975. string value;
  976. assert(result.try_get_as<string>(out value));
  977. assert(value == "Hex: 255");
  978. });
  979. Test.add_func("/invercargill/expressions/format_percent", () => {
  980. // Test that \% produces literal % (escape sequence)
  981. var expr = ExpressionParser.parse("format(\"100\\% complete\")");
  982. var props = new PropertyDictionary();
  983. var context = new EvaluationContext(props);
  984. var result = expr.evaluate(context);
  985. assert(result != null);
  986. string value;
  987. assert(result.try_get_as<string>(out value));
  988. assert(value == "100% complete");
  989. });
  990. Test.add_func("/invercargill/expressions/format_no_args", () => {
  991. // format("Just a string")
  992. var expr = ExpressionParser.parse("format(\"Just a string\")");
  993. var props = new PropertyDictionary();
  994. var context = new EvaluationContext(props);
  995. var result = expr.evaluate(context);
  996. assert(result != null);
  997. string value;
  998. assert(result.try_get_as<string>(out value));
  999. assert(value == "Just a string");
  1000. });
  1001. // ==================== Nested Property Access Parser Tests ====================
  1002. Test.add_func("/invercargill/expressions/parser_nested_property_simple", () => {
  1003. // Test: p.name (simple property access via parser - same as existing test but confirms it works)
  1004. var person = new ExprTestPerson("Alice", 30);
  1005. var expr = ExpressionParser.parse("p.name");
  1006. var props = new PropertyDictionary();
  1007. props.set("p", new NativeElement<ExprTestPerson?>(person));
  1008. var context = new EvaluationContext(props);
  1009. var result = expr.evaluate(context);
  1010. assert(result != null);
  1011. string value;
  1012. assert(result.try_get_as<string>(out value));
  1013. assert(value == "Alice");
  1014. });
  1015. Test.add_func("/invercargill/expressions/parser_nested_property_deep", () => {
  1016. // Test: container.min_rank (accessing property on a container object)
  1017. var persons = new Series<ExprTestPerson>();
  1018. persons.add(new ExprTestPerson("Alice", 30, 5));
  1019. var container = new ExprTestContainer(3, persons);
  1020. var expr = ExpressionParser.parse("container.min_rank");
  1021. var props = new PropertyDictionary();
  1022. props.set("container", new NativeElement<ExprTestContainer?>(container));
  1023. var context = new EvaluationContext(props);
  1024. var result = expr.evaluate(context);
  1025. assert(result != null);
  1026. int value;
  1027. assert(result.try_get_as<int>(out value));
  1028. assert(value == 3);
  1029. });
  1030. Test.add_func("/invercargill/expressions/parser_nested_property_with_comparison", () => {
  1031. // Test: p.age > 18 (property access in comparison)
  1032. var person = new ExprTestPerson("Bob", 25);
  1033. var expr = ExpressionParser.parse("p.age > 18");
  1034. var props = new PropertyDictionary();
  1035. props.set("p", new NativeElement<ExprTestPerson?>(person));
  1036. var context = new EvaluationContext(props);
  1037. var result = expr.evaluate(context);
  1038. assert(result != null);
  1039. bool value;
  1040. assert(result.try_get_as<bool>(out value));
  1041. assert(value == true);
  1042. });
  1043. Test.add_func("/invercargill/expressions/parser_nested_property_in_lambda", () => {
  1044. // Test: items.where(i => i.rank > 2) - nested property in lambda
  1045. var persons = new Series<ExprTestPerson>();
  1046. persons.add(new ExprTestPerson("Alice", 30, 5));
  1047. persons.add(new ExprTestPerson("Bob", 25, 1));
  1048. persons.add(new ExprTestPerson("Charlie", 35, 3));
  1049. var expr = ExpressionParser.parse("items.where(i => i.rank > 2)");
  1050. var props = new PropertyDictionary();
  1051. props.set("items", new NativeElement<Enumerable<ExprTestPerson>?>(persons));
  1052. var context = new EvaluationContext(props);
  1053. var result = expr.evaluate(context);
  1054. assert(result != null);
  1055. Elements elements;
  1056. assert(result.try_get_as<Elements>(out elements));
  1057. var count = 0;
  1058. elements.iterate(e => count++);
  1059. assert(count == 2); // Alice (rank 5) and Charlie (rank 3)
  1060. });
  1061. Test.add_func("/invercargill/expressions/parser_nested_property_chained_functions", () => {
  1062. // Test: items.where(i => i.rank > 2).first().name
  1063. var persons = new Series<ExprTestPerson>();
  1064. persons.add(new ExprTestPerson("Alice", 30, 5));
  1065. persons.add(new ExprTestPerson("Bob", 25, 1));
  1066. persons.add(new ExprTestPerson("Charlie", 35, 3));
  1067. var expr = ExpressionParser.parse("items.where(i => i.rank > 2).first().name");
  1068. var props = new PropertyDictionary();
  1069. props.set("items", new NativeElement<Enumerable<ExprTestPerson>?>(persons));
  1070. var context = new EvaluationContext(props);
  1071. var result = expr.evaluate(context);
  1072. assert(result != null);
  1073. string value;
  1074. assert(result.try_get_as<string>(out value));
  1075. assert(value == "Alice"); // First item with rank > 2
  1076. });
  1077. Test.add_func("/invercargill/expressions/parser_nested_property_closure", () => {
  1078. // Test: items.where(i => i.rank > container.min_rank) - closure with nested property
  1079. var persons = new Series<ExprTestPerson>();
  1080. persons.add(new ExprTestPerson("Alice", 30, 5));
  1081. persons.add(new ExprTestPerson("Bob", 25, 1));
  1082. persons.add(new ExprTestPerson("Charlie", 35, 3));
  1083. var container = new ExprTestContainer(2, persons);
  1084. var expr = ExpressionParser.parse("container.values.where(i => i.rank > container.min_rank)");
  1085. var props = new PropertyDictionary();
  1086. props.set("container", new NativeElement<ExprTestContainer?>(container));
  1087. var context = new EvaluationContext(props);
  1088. var result = expr.evaluate(context);
  1089. assert(result != null);
  1090. Elements elements;
  1091. assert(result.try_get_as<Elements>(out elements));
  1092. var count = 0;
  1093. elements.iterate(e => count++);
  1094. assert(count == 2); // Alice (rank 5) and Charlie (rank 3), both > min_rank (2)
  1095. });
  1096. Test.add_func("/invercargill/expressions/parser_nested_property_ternary", () => {
  1097. // Test: p.age >= 18 ? p.name + " is adult" : p.name + " is minor"
  1098. var person = new ExprTestPerson("Bob", 25);
  1099. var expr = ExpressionParser.parse("p.age >= 18 ? p.name + \" is adult\" : p.name + \" is minor\"");
  1100. var props = new PropertyDictionary();
  1101. props.set("p", new NativeElement<ExprTestPerson?>(person));
  1102. var context = new EvaluationContext(props);
  1103. var result = expr.evaluate(context);
  1104. assert(result != null);
  1105. string value;
  1106. assert(result.try_get_as<string>(out value));
  1107. assert(value == "Bob is adult");
  1108. });
  1109. Test.add_func("/invercargill/expressions/parser_nested_property_arithmetic", () => {
  1110. // Test: p.age + 5 (property in arithmetic)
  1111. var person = new ExprTestPerson("Alice", 30);
  1112. var expr = ExpressionParser.parse("p.age + 5");
  1113. var props = new PropertyDictionary();
  1114. props.set("p", new NativeElement<ExprTestPerson?>(person));
  1115. var context = new EvaluationContext(props);
  1116. var result = expr.evaluate(context);
  1117. assert(result != null);
  1118. int64? value;
  1119. assert(result.try_get_as<int64?>(out value));
  1120. assert(value == 35);
  1121. });
  1122. Test.add_func("/invercargill/expressions/parser_nested_property_boolean", () => {
  1123. // Test: p.is_important (boolean property)
  1124. var person = new ExprTestPerson("Important", 30, 5, true);
  1125. var expr = ExpressionParser.parse("p.is_important");
  1126. var props = new PropertyDictionary();
  1127. props.set("p", new NativeElement<ExprTestPerson?>(person));
  1128. var context = new EvaluationContext(props);
  1129. var result = expr.evaluate(context);
  1130. assert(result != null);
  1131. bool value;
  1132. assert(result.try_get_as<bool>(out value));
  1133. assert(value == true);
  1134. });
  1135. Test.add_func("/invercargill/expressions/parser_nested_property_complex_expression", () => {
  1136. // Complex expression from the requirements:
  1137. // a.values.where(s => s.rank > a.min_rank).first().is_important
  1138. var persons = new Series<ExprTestPerson>();
  1139. persons.add(new ExprTestPerson("Alice", 30, 1, false));
  1140. persons.add(new ExprTestPerson("Bob", 25, 5, true));
  1141. persons.add(new ExprTestPerson("Charlie", 35, 3, false));
  1142. var container = new ExprTestContainer(2, persons);
  1143. var expr = ExpressionParser.parse("a.values.where(s => s.rank > a.min_rank).first().is_important");
  1144. var props = new PropertyDictionary();
  1145. props.set("a", new NativeElement<ExprTestContainer?>(container));
  1146. var context = new EvaluationContext(props);
  1147. var result = expr.evaluate(context);
  1148. assert(result != null);
  1149. bool value;
  1150. assert(result.try_get_as<bool>(out value));
  1151. assert(value == true); // Bob has rank 5 > min_rank 2, and is_important = true
  1152. });
  1153. // ==================== String Property Accessor Tests ====================
  1154. Test.add_func("/invercargill/expressions/string_length", () => {
  1155. var expr = ExpressionParser.parse("s.length");
  1156. var props = new PropertyDictionary();
  1157. props.set("s", new NativeElement<string>("hello"));
  1158. var context = new EvaluationContext(props);
  1159. var result = expr.evaluate(context);
  1160. assert(result != null);
  1161. int value;
  1162. assert(result.try_get_as<int>(out value));
  1163. assert(value == 5);
  1164. });
  1165. Test.add_func("/invercargill/expressions/string_empty", () => {
  1166. var expr = ExpressionParser.parse("s.empty");
  1167. var props = new PropertyDictionary();
  1168. props.set("s", new NativeElement<string>(""));
  1169. var context = new EvaluationContext(props);
  1170. var result = expr.evaluate(context);
  1171. assert(result != null);
  1172. bool value;
  1173. assert(result.try_get_as<bool>(out value));
  1174. assert(value == true);
  1175. });
  1176. Test.add_func("/invercargill/expressions/string_is_null", () => {
  1177. var expr = ExpressionParser.parse("s.is_null");
  1178. var props = new PropertyDictionary();
  1179. props.set("s", new NullElement());
  1180. var context = new EvaluationContext(props);
  1181. var result = expr.evaluate(context);
  1182. assert(result != null);
  1183. bool value;
  1184. assert(result.try_get_as<bool>(out value));
  1185. assert(value == true);
  1186. });
  1187. Test.add_func("/invercargill/expressions/string_is_empty_or_null", () => {
  1188. // Test with empty string
  1189. var expr = ExpressionParser.parse("s.is_empty_or_null");
  1190. var props = new PropertyDictionary();
  1191. props.set("s", new NativeElement<string>(""));
  1192. var context = new EvaluationContext(props);
  1193. var result = expr.evaluate(context);
  1194. assert(result != null);
  1195. bool value;
  1196. assert(result.try_get_as<bool>(out value));
  1197. assert(value == true);
  1198. });
  1199. // ==================== String Function Accessor Tests ====================
  1200. Test.add_func("/invercargill/expressions/string_chomp", () => {
  1201. var expr = ExpressionParser.parse("s.chomp()");
  1202. var props = new PropertyDictionary();
  1203. props.set("s", new NativeElement<string>("hello "));
  1204. var context = new EvaluationContext(props);
  1205. var result = expr.evaluate(context);
  1206. assert(result != null);
  1207. string value;
  1208. assert(result.try_get_as<string>(out value));
  1209. assert(value == "hello");
  1210. });
  1211. Test.add_func("/invercargill/expressions/string_chug", () => {
  1212. var expr = ExpressionParser.parse("s.chug()");
  1213. var props = new PropertyDictionary();
  1214. props.set("s", new NativeElement<string>(" hello"));
  1215. var context = new EvaluationContext(props);
  1216. var result = expr.evaluate(context);
  1217. assert(result != null);
  1218. string value;
  1219. assert(result.try_get_as<string>(out value));
  1220. assert(value == "hello");
  1221. });
  1222. Test.add_func("/invercargill/expressions/string_upper", () => {
  1223. var expr = ExpressionParser.parse("s.upper()");
  1224. var props = new PropertyDictionary();
  1225. props.set("s", new NativeElement<string>("hello"));
  1226. var context = new EvaluationContext(props);
  1227. var result = expr.evaluate(context);
  1228. assert(result != null);
  1229. string value;
  1230. assert(result.try_get_as<string>(out value));
  1231. assert(value == "HELLO");
  1232. });
  1233. Test.add_func("/invercargill/expressions/string_lower", () => {
  1234. var expr = ExpressionParser.parse("s.lower()");
  1235. var props = new PropertyDictionary();
  1236. props.set("s", new NativeElement<string>("HELLO"));
  1237. var context = new EvaluationContext(props);
  1238. var result = expr.evaluate(context);
  1239. assert(result != null);
  1240. string value;
  1241. assert(result.try_get_as<string>(out value));
  1242. assert(value == "hello");
  1243. });
  1244. Test.add_func("/invercargill/expressions/string_contains", () => {
  1245. var expr = ExpressionParser.parse("s.contains(\"ell\")");
  1246. var props = new PropertyDictionary();
  1247. props.set("s", new NativeElement<string>("hello"));
  1248. var context = new EvaluationContext(props);
  1249. var result = expr.evaluate(context);
  1250. assert(result != null);
  1251. bool value;
  1252. assert(result.try_get_as<bool>(out value));
  1253. assert(value == true);
  1254. });
  1255. Test.add_func("/invercargill/expressions/string_has_prefix", () => {
  1256. var expr = ExpressionParser.parse("s.has_prefix(\"hel\")");
  1257. var props = new PropertyDictionary();
  1258. props.set("s", new NativeElement<string>("hello"));
  1259. var context = new EvaluationContext(props);
  1260. var result = expr.evaluate(context);
  1261. assert(result != null);
  1262. bool value;
  1263. assert(result.try_get_as<bool>(out value));
  1264. assert(value == true);
  1265. });
  1266. Test.add_func("/invercargill/expressions/string_has_suffix", () => {
  1267. var expr = ExpressionParser.parse("s.has_suffix(\"llo\")");
  1268. var props = new PropertyDictionary();
  1269. props.set("s", new NativeElement<string>("hello"));
  1270. var context = new EvaluationContext(props);
  1271. var result = expr.evaluate(context);
  1272. assert(result != null);
  1273. bool value;
  1274. assert(result.try_get_as<bool>(out value));
  1275. assert(value == true);
  1276. });
  1277. Test.add_func("/invercargill/expressions/string_replace", () => {
  1278. var expr = ExpressionParser.parse("s.replace(\"l\", \"x\")");
  1279. var props = new PropertyDictionary();
  1280. props.set("s", new NativeElement<string>("hello"));
  1281. var context = new EvaluationContext(props);
  1282. var result = expr.evaluate(context);
  1283. assert(result != null);
  1284. string value;
  1285. assert(result.try_get_as<string>(out value));
  1286. assert(value == "hexxo");
  1287. });
  1288. Test.add_func("/invercargill/expressions/string_substring", () => {
  1289. var expr = ExpressionParser.parse("s.substring(1, 3)");
  1290. var props = new PropertyDictionary();
  1291. props.set("s", new NativeElement<string>("hello"));
  1292. var context = new EvaluationContext(props);
  1293. var result = expr.evaluate(context);
  1294. assert(result != null);
  1295. string value;
  1296. assert(result.try_get_as<string>(out value));
  1297. assert(value == "ell");
  1298. });
  1299. Test.add_func("/invercargill/expressions/string_split", () => {
  1300. var expr = ExpressionParser.parse("s.split(\",\")");
  1301. var props = new PropertyDictionary();
  1302. props.set("s", new NativeElement<string>("a,b,c"));
  1303. var context = new EvaluationContext(props);
  1304. var result = expr.evaluate(context);
  1305. assert(result != null);
  1306. Enumerable<string?>? parts;
  1307. assert(result.try_get_as<Enumerable<string?>>(out parts));
  1308. assert(parts != null);
  1309. var count = 0;
  1310. foreach (var part in parts) {
  1311. count++;
  1312. }
  1313. assert(count == 3);
  1314. });
  1315. Test.add_func("/invercargill/expressions/string_char_at", () => {
  1316. var expr = ExpressionParser.parse("s.char_at(1)");
  1317. var props = new PropertyDictionary();
  1318. props.set("s", new NativeElement<string>("hello"));
  1319. var context = new EvaluationContext(props);
  1320. var result = expr.evaluate(context);
  1321. assert(result != null);
  1322. string value;
  1323. assert(result.try_get_as<string>(out value));
  1324. assert(value == "e");
  1325. });
  1326. Test.add_func("/invercargill/expressions/string_char_at_negative_index", () => {
  1327. // Test negative index (from end)
  1328. var expr = ExpressionParser.parse("s.char_at(-1)");
  1329. var props = new PropertyDictionary();
  1330. props.set("s", new NativeElement<string>("hello"));
  1331. var context = new EvaluationContext(props);
  1332. var result = expr.evaluate(context);
  1333. assert(result != null);
  1334. string value;
  1335. assert(result.try_get_as<string>(out value));
  1336. assert(value == "o");
  1337. });
  1338. Test.add_func("/invercargill/expressions/string_char_at_out_of_bounds", () => {
  1339. // Test out of bounds returns null
  1340. var expr = ExpressionParser.parse("s.char_at(100)");
  1341. var props = new PropertyDictionary();
  1342. props.set("s", new NativeElement<string>("hello"));
  1343. var context = new EvaluationContext(props);
  1344. var result = expr.evaluate(context);
  1345. assert(result != null);
  1346. assert(result.is_null());
  1347. });
  1348. Test.add_func("/invercargill/expressions/string_reverse", () => {
  1349. var expr = ExpressionParser.parse("s.reverse()");
  1350. var props = new PropertyDictionary();
  1351. props.set("s", new NativeElement<string>("hello"));
  1352. var context = new EvaluationContext(props);
  1353. var result = expr.evaluate(context);
  1354. assert(result != null);
  1355. string value;
  1356. assert(result.try_get_as<string>(out value));
  1357. assert(value == "olleh");
  1358. });
  1359. Test.add_func("/invercargill/expressions/string_chained_operations", () => {
  1360. // Test chaining: s.chug().chomp().upper()
  1361. var expr = ExpressionParser.parse("s.chug().chomp().upper()");
  1362. var props = new PropertyDictionary();
  1363. props.set("s", new NativeElement<string>(" hello "));
  1364. var context = new EvaluationContext(props);
  1365. var result = expr.evaluate(context);
  1366. assert(result != null);
  1367. string value;
  1368. assert(result.try_get_as<string>(out value));
  1369. assert(value == "HELLO");
  1370. });
  1371. Test.add_func("/invercargill/expressions/string_in_complex_expression", () => {
  1372. // Test string operations in a complex expression
  1373. // name.chug().chomp().length > 3
  1374. var expr = ExpressionParser.parse("name.chug().chomp().length > 3");
  1375. var props = new PropertyDictionary();
  1376. props.set("name", new NativeElement<string>(" hello "));
  1377. var context = new EvaluationContext(props);
  1378. var result = expr.evaluate(context);
  1379. assert(result != null);
  1380. bool value;
  1381. assert(result.try_get_as<bool>(out value));
  1382. assert(value == true);
  1383. });
  1384. // ==================== Element.stringify() Tests ====================
  1385. Test.add_func("/invercargill/expressions/element_stringify_null", () => {
  1386. // Test null element → empty string
  1387. var element = new NullElement();
  1388. assert(element.stringify() == "");
  1389. });
  1390. Test.add_func("/invercargill/expressions/element_stringify_string", () => {
  1391. // Test string element → returns the string
  1392. var element = new NativeElement<string>("hello world");
  1393. assert(element.stringify() == "hello world");
  1394. });
  1395. Test.add_func("/invercargill/expressions/element_stringify_string_null", () => {
  1396. // Test null string element → empty string
  1397. var element = new NullElement();
  1398. assert(element.stringify() == "");
  1399. });
  1400. Test.add_func("/invercargill/expressions/element_stringify_int", () => {
  1401. // Test int element → decimal representation
  1402. var element = new NativeElement<int>(42);
  1403. assert(element.stringify() == "42");
  1404. });
  1405. Test.add_func("/invercargill/expressions/element_stringify_int_negative", () => {
  1406. // Test negative int element → decimal representation
  1407. var element = new NativeElement<int>(-17);
  1408. assert(element.stringify() == "-17");
  1409. });
  1410. Test.add_func("/invercargill/expressions/element_stringify_long", () => {
  1411. // Test long element → decimal representation
  1412. var element = new NativeElement<long>(123456789L);
  1413. assert(element.stringify() == "123456789");
  1414. });
  1415. Test.add_func("/invercargill/expressions/element_stringify_int64", () => {
  1416. // Test int64 element → decimal representation
  1417. var element = new NativeElement<int64?>(9876543210LL);
  1418. assert(element.stringify() == "9876543210");
  1419. });
  1420. Test.add_func("/invercargill/expressions/element_stringify_double", () => {
  1421. // Test double element → decimal representation
  1422. var element = new NativeElement<double?>(3.5);
  1423. assert(element.stringify() == "3.5");
  1424. });
  1425. Test.add_func("/invercargill/expressions/element_stringify_float", () => {
  1426. // Test float element → decimal representation
  1427. var element = new NativeElement<float?>(2.5f);
  1428. assert(element.stringify() == "2.5");
  1429. });
  1430. Test.add_func("/invercargill/expressions/element_stringify_bool_true", () => {
  1431. // Test bool element true → "true"
  1432. var element = new NativeElement<bool>(true);
  1433. assert(element.stringify() == "true");
  1434. });
  1435. Test.add_func("/invercargill/expressions/element_stringify_bool_false", () => {
  1436. // Test bool element false → "false"
  1437. var element = new NativeElement<bool>(false);
  1438. assert(element.stringify() == "false");
  1439. });
  1440. Test.add_func("/invercargill/expressions/element_stringify_object_fallback", () => {
  1441. // Test other types → falls back to to_string()
  1442. var person = new ExprTestPerson("Alice", 30);
  1443. var element = new NativeElement<ExprTestPerson?>(person);
  1444. // The default to_string() returns "Element[type_name()]"
  1445. assert(element.stringify().contains("ExprTestPerson"));
  1446. });
  1447. // ==================== Handlebars format() Function Tests ====================
  1448. Test.add_func("/invercargill/expressions/format_handlebars_simple", () => {
  1449. // Simple variable interpolation: format("Hello, {{name}}!") with name="World"
  1450. var expr = ExpressionParser.parse("format(\"Hello, {{name}}!\")");
  1451. var props = new PropertyDictionary();
  1452. props.set("name", new NativeElement<string>("World"));
  1453. var context = new EvaluationContext(props);
  1454. var result = expr.evaluate(context);
  1455. assert(result != null);
  1456. string value;
  1457. assert(result.try_get_as<string>(out value));
  1458. assert(value == "Hello, World!");
  1459. });
  1460. Test.add_func("/invercargill/expressions/format_handlebars_multiple", () => {
  1461. // Multiple interpolations: format("{{a}} + {{b}} = {{c}}")
  1462. var expr = ExpressionParser.parse("format(\"{{a}} + {{b}} = {{c}}\")");
  1463. var props = new PropertyDictionary();
  1464. props.set("a", new NativeElement<int>(2));
  1465. props.set("b", new NativeElement<int>(3));
  1466. props.set("c", new NativeElement<int>(5));
  1467. var context = new EvaluationContext(props);
  1468. var result = expr.evaluate(context);
  1469. assert(result != null);
  1470. string value;
  1471. assert(result.try_get_as<string>(out value));
  1472. assert(value == "2 + 3 = 5");
  1473. });
  1474. Test.add_func("/invercargill/expressions/format_handlebars_expression", () => {
  1475. // Expression evaluation: format("Sum: {{a + b}}")
  1476. var expr = ExpressionParser.parse("format(\"Sum: {{a + b}}\")");
  1477. var props = new PropertyDictionary();
  1478. props.set("a", new NativeElement<int>(10));
  1479. props.set("b", new NativeElement<int>(32));
  1480. var context = new EvaluationContext(props);
  1481. var result = expr.evaluate(context);
  1482. assert(result != null);
  1483. string value;
  1484. assert(result.try_get_as<string>(out value));
  1485. assert(value == "Sum: 42");
  1486. });
  1487. Test.add_func("/invercargill/expressions/format_handlebars_property_access", () => {
  1488. // Property access: format("User: {{user.name}}")
  1489. var person = new ExprTestPerson("Alice", 30);
  1490. var expr = ExpressionParser.parse("format(\"User: {{user.name}}, Age: {{user.age}}\")");
  1491. var props = new PropertyDictionary();
  1492. props.set("user", new NativeElement<ExprTestPerson?>(person));
  1493. var context = new EvaluationContext(props);
  1494. var result = expr.evaluate(context);
  1495. assert(result != null);
  1496. string value;
  1497. assert(result.try_get_as<string>(out value));
  1498. assert(value == "User: Alice, Age: 30");
  1499. });
  1500. Test.add_func("/invercargill/expressions/format_handlebars_escape_braces", () => {
  1501. // Escape sequences: format("Literal: \\{{name}}") → "Literal: {{name}}"
  1502. // In Vala source: "\\\\{{" -> runtime string: "\\{{" -> parsed string: "\{{" -> format outputs: "{{"
  1503. var expr = ExpressionParser.parse("format(\"Literal: \\\\{{name}}\")");
  1504. var props = new PropertyDictionary();
  1505. props.set("name", new NativeElement<string>("World"));
  1506. var context = new EvaluationContext(props);
  1507. var result = expr.evaluate(context);
  1508. assert(result != null);
  1509. string value;
  1510. assert(result.try_get_as<string>(out value));
  1511. assert(value == "Literal: {{name}}");
  1512. });
  1513. Test.add_func("/invercargill/expressions/format_handlebars_escape_backslash", () => {
  1514. // Escape backslash: format("Path: C:\\\\Users") → "Path: C:\Users"
  1515. // In Vala source: "\\\\\\\\" -> runtime string: "\\\\" -> parsed string: "\\" -> format outputs: "\"
  1516. // To get a literal backslash in the format output, we need "\\" in the parsed string
  1517. var expr = ExpressionParser.parse("format(\"Path: C:\\\\\\\\Users\")");
  1518. var props = new PropertyDictionary();
  1519. var context = new EvaluationContext(props);
  1520. var result = expr.evaluate(context);
  1521. assert(result != null);
  1522. string value;
  1523. assert(result.try_get_as<string>(out value));
  1524. assert(value == "Path: C:\\Users");
  1525. });
  1526. Test.add_func("/invercargill/expressions/format_handlebars_empty_expression", () => {
  1527. // Empty expression: format("Empty: {{}}") → "Empty: "
  1528. var expr = ExpressionParser.parse("format(\"Empty: {{}}\")");
  1529. var props = new PropertyDictionary();
  1530. var context = new EvaluationContext(props);
  1531. var result = expr.evaluate(context);
  1532. assert(result != null);
  1533. string value;
  1534. assert(result.try_get_as<string>(out value));
  1535. assert(value == "Empty: ");
  1536. });
  1537. Test.add_func("/invercargill/expressions/format_handlebars_whitespace_in_expression", () => {
  1538. // Whitespace in expression should be trimmed
  1539. var expr = ExpressionParser.parse("format(\"Value: {{ name }}\")");
  1540. var props = new PropertyDictionary();
  1541. props.set("name", new NativeElement<string>("test"));
  1542. var context = new EvaluationContext(props);
  1543. var result = expr.evaluate(context);
  1544. assert(result != null);
  1545. string value;
  1546. assert(result.try_get_as<string>(out value));
  1547. assert(value == "Value: test");
  1548. });
  1549. Test.add_func("/invercargill/expressions/format_handlebars_bool", () => {
  1550. // Boolean interpolation
  1551. var expr = ExpressionParser.parse("format(\"Is active: {{active}}\")");
  1552. var props = new PropertyDictionary();
  1553. props.set("active", new NativeElement<bool>(true));
  1554. var context = new EvaluationContext(props);
  1555. var result = expr.evaluate(context);
  1556. assert(result != null);
  1557. string value;
  1558. assert(result.try_get_as<string>(out value));
  1559. assert(value == "Is active: true");
  1560. });
  1561. Test.add_func("/invercargill/expressions/format_handlebars_null", () => {
  1562. // Null interpolation → empty string
  1563. var expr = ExpressionParser.parse("format(\"Value: '{{value}}'\")");
  1564. var props = new PropertyDictionary();
  1565. props.set("value", new NullElement());
  1566. var context = new EvaluationContext(props);
  1567. var result = expr.evaluate(context);
  1568. assert(result != null);
  1569. string value;
  1570. assert(result.try_get_as<string>(out value));
  1571. assert(value == "Value: ''");
  1572. });
  1573. Test.add_func("/invercargill/expressions/format_handlebars_no_placeholders", () => {
  1574. // Template with no placeholders
  1575. var expr = ExpressionParser.parse("format(\"Just a plain string\")");
  1576. var props = new PropertyDictionary();
  1577. var context = new EvaluationContext(props);
  1578. var result = expr.evaluate(context);
  1579. assert(result != null);
  1580. string value;
  1581. assert(result.try_get_as<string>(out value));
  1582. assert(value == "Just a plain string");
  1583. });
  1584. Test.add_func("/invercargill/expressions/format_handlebars_ternary", () => {
  1585. // Ternary expression in handlebars
  1586. var expr = ExpressionParser.parse("format(\"Status: {{active ? \\\"active\\\" : \\\"inactive\\\"}}\")");
  1587. var props = new PropertyDictionary();
  1588. props.set("active", new NativeElement<bool>(true));
  1589. var context = new EvaluationContext(props);
  1590. var result = expr.evaluate(context);
  1591. assert(result != null);
  1592. string value;
  1593. assert(result.try_get_as<string>(out value));
  1594. assert(value == "Status: active");
  1595. });
  1596. // ==================== stringify() Global Function Tests ====================
  1597. Test.add_func("/invercargill/expressions/stringify_single_int", () => {
  1598. // Single argument: stringify(42) → "42"
  1599. var expr = ExpressionParser.parse("stringify(42)");
  1600. var props = new PropertyDictionary();
  1601. var context = new EvaluationContext(props);
  1602. var result = expr.evaluate(context);
  1603. assert(result != null);
  1604. string value;
  1605. assert(result.try_get_as<string>(out value));
  1606. assert(value == "42");
  1607. });
  1608. Test.add_func("/invercargill/expressions/stringify_single_string", () => {
  1609. // Single string argument: stringify("hello") → "hello"
  1610. var expr = ExpressionParser.parse("stringify(\"hello\")");
  1611. var props = new PropertyDictionary();
  1612. var context = new EvaluationContext(props);
  1613. var result = expr.evaluate(context);
  1614. assert(result != null);
  1615. string value;
  1616. assert(result.try_get_as<string>(out value));
  1617. assert(value == "hello");
  1618. });
  1619. Test.add_func("/invercargill/expressions/stringify_multiple_strings", () => {
  1620. // Multiple arguments: stringify("Hello", " ", "World") → "Hello World"
  1621. var expr = ExpressionParser.parse("stringify(\"Hello\", \" \", \"World\")");
  1622. var props = new PropertyDictionary();
  1623. var context = new EvaluationContext(props);
  1624. var result = expr.evaluate(context);
  1625. assert(result != null);
  1626. string value;
  1627. assert(result.try_get_as<string>(out value));
  1628. assert(value == "Hello World");
  1629. });
  1630. Test.add_func("/invercargill/expressions/stringify_mixed_types", () => {
  1631. // Mixed types: stringify(1, " + ", 2, " = ", 3) → "1 + 2 = 3"
  1632. var expr = ExpressionParser.parse("stringify(1, \" + \", 2, \" = \", 3)");
  1633. var props = new PropertyDictionary();
  1634. var context = new EvaluationContext(props);
  1635. var result = expr.evaluate(context);
  1636. assert(result != null);
  1637. string value;
  1638. assert(result.try_get_as<string>(out value));
  1639. assert(value == "1 + 2 = 3");
  1640. });
  1641. Test.add_func("/invercargill/expressions/stringify_no_arguments", () => {
  1642. // No arguments: stringify() → ""
  1643. var expr = ExpressionParser.parse("stringify()");
  1644. var props = new PropertyDictionary();
  1645. var context = new EvaluationContext(props);
  1646. var result = expr.evaluate(context);
  1647. assert(result != null);
  1648. string value;
  1649. assert(result.try_get_as<string>(out value));
  1650. assert(value == "");
  1651. });
  1652. Test.add_func("/invercargill/expressions/stringify_null", () => {
  1653. // Null argument: stringify(null) → ""
  1654. var expr = ExpressionParser.parse("stringify(null)");
  1655. var props = new PropertyDictionary();
  1656. var context = new EvaluationContext(props);
  1657. var result = expr.evaluate(context);
  1658. assert(result != null);
  1659. string value;
  1660. assert(result.try_get_as<string>(out value));
  1661. assert(value == "");
  1662. });
  1663. Test.add_func("/invercargill/expressions/stringify_bool", () => {
  1664. // Boolean arguments: stringify(true, " or ", false) → "true or false"
  1665. var expr = ExpressionParser.parse("stringify(true, \" or \", false)");
  1666. var props = new PropertyDictionary();
  1667. var context = new EvaluationContext(props);
  1668. var result = expr.evaluate(context);
  1669. assert(result != null);
  1670. string value;
  1671. assert(result.try_get_as<string>(out value));
  1672. assert(value == "true or false");
  1673. });
  1674. Test.add_func("/invercargill/expressions/stringify_variable", () => {
  1675. // Stringify with variable
  1676. var expr = ExpressionParser.parse("stringify(\"The answer is: \", answer)");
  1677. var props = new PropertyDictionary();
  1678. props.set("answer", new NativeElement<int>(42));
  1679. var context = new EvaluationContext(props);
  1680. var result = expr.evaluate(context);
  1681. assert(result != null);
  1682. string value;
  1683. assert(result.try_get_as<string>(out value));
  1684. assert(value == "The answer is: 42");
  1685. });
  1686. Test.add_func("/invercargill/expressions/stringify_expression", () => {
  1687. // Stringify with expression evaluation
  1688. var expr = ExpressionParser.parse("stringify(\"Sum: \", 2 + 3)");
  1689. var props = new PropertyDictionary();
  1690. var context = new EvaluationContext(props);
  1691. var result = expr.evaluate(context);
  1692. assert(result != null);
  1693. string value;
  1694. assert(result.try_get_as<string>(out value));
  1695. assert(value == "Sum: 5");
  1696. });
  1697. Test.add_func("/invercargill/expressions/stringify_float", () => {
  1698. // Stringify floating point number
  1699. var expr = ExpressionParser.parse("stringify(3.5)");
  1700. var props = new PropertyDictionary();
  1701. var context = new EvaluationContext(props);
  1702. var result = expr.evaluate(context);
  1703. assert(result != null);
  1704. string value;
  1705. assert(result.try_get_as<string>(out value));
  1706. assert(value == "3.5");
  1707. });
  1708. // ==================== Lot Length Formatting Bug Tests ====================
  1709. // BUG: format strings referencing lot.length print large random numbers
  1710. // Root cause: NativeElement.try_get_as<TOut>() generic value type casts
  1711. // may read garbage memory when T=uint
  1712. Test.add_func("/invercargill/expressions/format_lot_length_bug", () => {
  1713. // Create a Vector with 3 elements (known length)
  1714. var vector = new Vector<int>();
  1715. vector.add(1);
  1716. vector.add(2);
  1717. vector.add(3);
  1718. // Use format string with {{lot.length}}
  1719. var expr = ExpressionParser.parse("format(\"Length: {{lot.length}}\")");
  1720. var props = new PropertyDictionary();
  1721. props.set("lot", new NativeElement<Lot<int>?>(vector));
  1722. var context = new EvaluationContext(props);
  1723. var result = expr.evaluate(context);
  1724. assert(result != null);
  1725. string value;
  1726. assert(result.try_get_as<string>(out value));
  1727. // This assertion should fail due to the bug - it will show a large
  1728. // random number instead of "3"
  1729. assert(value == "Length: 3");
  1730. });
  1731. // ==================== Parameterized Expression Tests ====================
  1732. Test.add_func("/invercargill/expressions/parameter_single", () => {
  1733. // Single parameter: $0 > 5
  1734. var params = new Series<Element>();
  1735. params.add(new NativeElement<int>(10));
  1736. var expr = ExpressionParser.parse_with_params("$0 > 5", params);
  1737. var context = new EvaluationContext(new PropertyDictionary());
  1738. var result = expr.evaluate(context);
  1739. bool value;
  1740. assert(result.try_get_as<bool>(out value));
  1741. assert(value == true);
  1742. });
  1743. Test.add_func("/invercargill/expressions/parameter_multiple", () => {
  1744. // Multiple parameters: $0 + $1
  1745. var params = new Series<Element>();
  1746. params.add(new NativeElement<int>(3));
  1747. params.add(new NativeElement<int>(4));
  1748. var expr = ExpressionParser.parse_with_params("$0 + $1", params);
  1749. var context = new EvaluationContext(new PropertyDictionary());
  1750. var result = expr.evaluate(context);
  1751. int64? value;
  1752. assert(result.try_get_as<int64?>(out value));
  1753. assert(value == 7);
  1754. });
  1755. Test.add_func("/invercargill/expressions/parameter_with_variable", () => {
  1756. // Parameter with variable: x > $0
  1757. var params = new Series<Element>();
  1758. params.add(new NativeElement<int>(5));
  1759. var expr = ExpressionParser.parse_with_params("x > $0", params);
  1760. var props = new PropertyDictionary();
  1761. props.set("x", new NativeElement<int>(10));
  1762. var context = new EvaluationContext(props);
  1763. var result = expr.evaluate(context);
  1764. bool value;
  1765. assert(result.try_get_as<bool>(out value));
  1766. assert(value == true);
  1767. });
  1768. Test.add_func("/invercargill/expressions/parameter_string", () => {
  1769. // String parameter: $0 == "hello"
  1770. var params = new Series<Element>();
  1771. params.add(new NativeElement<string>("hello"));
  1772. var expr = ExpressionParser.parse_with_params("$0 == \"hello\"", params);
  1773. var context = new EvaluationContext(new PropertyDictionary());
  1774. var result = expr.evaluate(context);
  1775. bool value;
  1776. assert(result.try_get_as<bool>(out value));
  1777. assert(value == true);
  1778. });
  1779. Test.add_func("/invercargill/expressions/parameter_complex_object", () => {
  1780. // Complex object parameter: $0.name
  1781. var person = new ExprTestPerson("Alice", 30);
  1782. var params = new Series<Element>();
  1783. params.add(new NativeElement<ExprTestPerson>(person));
  1784. var expr = ExpressionParser.parse_with_params("$0.name", params);
  1785. var context = new EvaluationContext(new PropertyDictionary());
  1786. var result = expr.evaluate(context);
  1787. string value;
  1788. assert(result.try_get_as<string>(out value));
  1789. assert(value == "Alice");
  1790. });
  1791. Test.add_func("/invercargill/expressions/parameter_in_ternary", () => {
  1792. // Parameter in ternary: $0 ? "yes" : "no"
  1793. var params = new Series<Element>();
  1794. params.add(new NativeElement<bool>(true));
  1795. var expr = ExpressionParser.parse_with_params("$0 ? \"yes\" : \"no\"", params);
  1796. var context = new EvaluationContext(new PropertyDictionary());
  1797. var result = expr.evaluate(context);
  1798. string value;
  1799. assert(result.try_get_as<string>(out value));
  1800. assert(value == "yes");
  1801. });
  1802. Test.add_func("/invercargill/expressions/parameter_in_lambda", () => {
  1803. // Parameter in lambda: items.where(i => i.rank > $0)
  1804. var persons = new Series<ExprTestPerson>();
  1805. persons.add(new ExprTestPerson("Alice", 30, 5));
  1806. persons.add(new ExprTestPerson("Bob", 25, 1));
  1807. persons.add(new ExprTestPerson("Charlie", 35, 3));
  1808. var params = new Series<Element>();
  1809. params.add(new NativeElement<int>(2));
  1810. var expr = ExpressionParser.parse_with_params("items.where(i => i.rank > $0)", params);
  1811. var props = new PropertyDictionary();
  1812. props.set("items", new NativeElement<Enumerable<ExprTestPerson>?>(persons));
  1813. var context = new EvaluationContext(props);
  1814. var result = expr.evaluate(context);
  1815. Elements elements;
  1816. assert(result.try_get_as<Elements>(out elements));
  1817. var count = 0;
  1818. elements.iterate(e => count++);
  1819. assert(count == 2); // Alice (rank 5) and Charlie (rank 3)
  1820. });
  1821. Test.add_func("/invercargill/expressions/parameter_range_check", () => {
  1822. // Range check with two parameters: $0 <= x && x <= $1
  1823. var params = new Series<Element>();
  1824. params.add(new NativeElement<int>(0));
  1825. params.add(new NativeElement<int>(100));
  1826. var expr = ExpressionParser.parse_with_params("$0 <= x && x <= $1", params);
  1827. var props = new PropertyDictionary();
  1828. props.set("x", new NativeElement<int>(50));
  1829. var context = new EvaluationContext(props);
  1830. var result = expr.evaluate(context);
  1831. bool value;
  1832. assert(result.try_get_as<bool>(out value));
  1833. assert(value == true);
  1834. });
  1835. Test.add_func("/invercargill/expressions/parameter_expression_string", () => {
  1836. // Test to_expression_string returns $N format
  1837. var expr = new ParameterExpression(0, new NativeElement<int>(42));
  1838. assert(expr.to_expression_string() == "$0");
  1839. });
  1840. Test.add_func("/invercargill/expressions/parameter_expression_type", () => {
  1841. // Test expression_type is PARAMETER
  1842. var expr = new ParameterExpression(0, new NativeElement<int>(42));
  1843. assert(expr.expression_type == ExpressionType.PARAMETER);
  1844. });
  1845. }