cheatah
Source

tests/purrc/kwargs_cr_test.cpp

1// Copyright (c) 2026 BigBrain LLC. MIT-licensed (see LICENSE).
2// Original work; see ACKNOWLEDGMENTS.md for the open-source ideas we build upon.
3// Compile-run system tests for DEFAULT and KEYWORD arguments:
4// fn f(a, b = expr) { ... } — trailing defaults (lower to forwarding overloads, since a
5// C++ default on an `auto` parameter cannot drive deduction);
6// f(x, b = v) / f(b = v, a = u) — keyword arguments, reordered at the call site against the
7// known signature, missing slots filled from the defaults.
8// Each test compiles a .purr with purrc, runs it, and asserts exact stdout; the negative tests
9// assert that purrc REJECTS the program (bad kwarg name, non-trailing default, dup parameter).
10#include "e2e_harness.hpp"
12TEST(KwargsCompileRun, TrailingDefaults) {
13 e2e::expect_e2e("kwargs_defaults", R"PURR(import io
14fn greet(name, greeting = "hello", punct = "!") {
15 io.print(greeting + ", " + name + punct)
17greet("world")
18greet("cheatah", "purr")
19)PURR", "hello, world!\npurr, cheatah!\n");
22TEST(KwargsCompileRun, KeywordReorder) {
23 e2e::expect_e2e("kwargs_reorder", R"PURR(import io
24fn greet(name, greeting = "hello", punct = "!") {
25 io.print(greeting + ", " + name + punct)
27greet("kwargs", punct = "?")
28greet(punct = ".", name = "reordered")
29greet("mixed", greeting = "hi", punct = "~")
30)PURR", "hello, kwargs?\nhello, reordered.\nhi, mixed~\n");
33TEST(KwargsCompileRun, DefaultsInExpressions) {
34 e2e::expect_e2e("kwargs_exprs", R"PURR(import io
35fn area(w, h = 2) { return w * h }
36io.print(area(3))
37io.print(area(3, h = 5))
38io.print(area(h = 4, w = 2))
39)PURR", "6\n15\n8\n");
42// purrc must REJECT: a keyword that names no parameter.
43TEST(KwargsCompileRun, RejectsUnknownKeyword) {
44 e2e::expect_compile_fail("kwargs_unknown", R"PURR(import io
45fn f(a) { io.print(a) }
46f(b = 1)
47)PURR");
50// purrc must REJECT: a non-defaulted parameter after a defaulted one.
51TEST(KwargsCompileRun, RejectsNonTrailingDefault) {
52 e2e::expect_compile_fail("kwargs_nontrailing", R"PURR(import io
53fn f(a = 1, b) { io.print(a + b) }
54f(1, 2)
55)PURR");
58// purrc must REJECT: the same parameter given positionally AND by keyword.
59TEST(KwargsCompileRun, RejectsDuplicateParameter) {
60 e2e::expect_compile_fail("kwargs_dup", R"PURR(import io
61fn f(a, b = 2) { io.print(a + b) }
62f(1, a = 3)
63)PURR");
66// ---- `%` (Python floor-mod) and membership `in` (added alongside the requests port) ----
68TEST(LangFeatures, Modulo) {
69 e2e::expect_e2e("lang_modulo", R"PURR(import io
70io.print(7 % 3)
71io.print(-7 % 3)
72io.print(7.5 % 2)
73io.print(255 % 16)
74)PURR", "1\n2\n1.5\n15\n");
77TEST(LangFeatures, InOperator) {
78 e2e::expect_e2e("lang_in", R"PURR(import io
79let d: dict<str, int> = {}
80d["alpha"] = 1
81io.print("alpha" in d)
82io.print("beta" in d)
83let xs = [10, 20, 30]
84io.print(20 in xs)
85io.print(99 in xs)
86io.print("ell" in "hello")
87)PURR", "True\nFalse\nTrue\nFalse\nTrue\n");