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 a5
// 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 the7
// known signature, missing slots filled from the defaults.8
// Each test compiles a .purr with purrc, runs it, and asserts exact stdout; the negative tests9
// assert that purrc REJECTS the program (bad kwarg name, non-trailing default, dup parameter).10
#include "e2e_harness.hpp"12
TEST(KwargsCompileRun, TrailingDefaults) {13
e2e::expect_e2e("kwargs_defaults", R"PURR(import io14
fn greet(name, greeting = "hello", punct = "!") {15
io.print(greeting + ", " + name + punct)16
}17
greet("world")18
greet("cheatah", "purr")19
)PURR", "hello, world!\npurr, cheatah!\n");20
}22
TEST(KwargsCompileRun, KeywordReorder) {23
e2e::expect_e2e("kwargs_reorder", R"PURR(import io24
fn greet(name, greeting = "hello", punct = "!") {25
io.print(greeting + ", " + name + punct)26
}27
greet("kwargs", punct = "?")28
greet(punct = ".", name = "reordered")29
greet("mixed", greeting = "hi", punct = "~")30
)PURR", "hello, kwargs?\nhello, reordered.\nhi, mixed~\n");31
}33
TEST(KwargsCompileRun, DefaultsInExpressions) {34
e2e::expect_e2e("kwargs_exprs", R"PURR(import io35
fn area(w, h = 2) { return w * h }36
io.print(area(3))37
io.print(area(3, h = 5))38
io.print(area(h = 4, w = 2))39
)PURR", "6\n15\n8\n");40
}42
// purrc must REJECT: a keyword that names no parameter.43
TEST(KwargsCompileRun, RejectsUnknownKeyword) {44
e2e::expect_compile_fail("kwargs_unknown", R"PURR(import io45
fn f(a) { io.print(a) }46
f(b = 1)47
)PURR");48
}50
// purrc must REJECT: a non-defaulted parameter after a defaulted one.51
TEST(KwargsCompileRun, RejectsNonTrailingDefault) {52
e2e::expect_compile_fail("kwargs_nontrailing", R"PURR(import io53
fn f(a = 1, b) { io.print(a + b) }54
f(1, 2)55
)PURR");56
}58
// purrc must REJECT: the same parameter given positionally AND by keyword.59
TEST(KwargsCompileRun, RejectsDuplicateParameter) {60
e2e::expect_compile_fail("kwargs_dup", R"PURR(import io61
fn f(a, b = 2) { io.print(a + b) }62
f(1, a = 3)63
)PURR");64
}66
// ---- `%` (Python floor-mod) and membership `in` (added alongside the requests port) ----68
TEST(LangFeatures, Modulo) {69
e2e::expect_e2e("lang_modulo", R"PURR(import io70
io.print(7 % 3)71
io.print(-7 % 3)72
io.print(7.5 % 2)73
io.print(255 % 16)74
)PURR", "1\n2\n1.5\n15\n");75
}77
TEST(LangFeatures, InOperator) {78
e2e::expect_e2e("lang_in", R"PURR(import io79
let d: dict<str, int> = {}80
d["alpha"] = 181
io.print("alpha" in d)82
io.print("beta" in d)83
let xs = [10, 20, 30]84
io.print(20 in xs)85
io.print(99 in xs)86
io.print("ell" in "hello")87
)PURR", "True\nFalse\nTrue\nFalse\nTrue\n");88
}