From 279ccb54323b34a411669a7ac01581fd7c6bec98 Mon Sep 17 00:00:00 2001 From: ala89 Date: Thu, 4 Jan 2024 22:25:05 +0100 Subject: [PATCH] Add closure tests --- test/functions.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/functions.cpp b/test/functions.cpp index 567af3f..4c99acd 100644 --- a/test/functions.cpp +++ b/test/functions.cpp @@ -110,5 +110,47 @@ int main() { true ); + _TEST_ASSERT( + _TEST_NO_EXCEPTION(get(execute(R"( + int x = 1; + int a() { + return x; + } + int b() { + int x = 2; + return a(); + } + b(); + )")) == 1), + "Closure capture", + true + ); + + _TEST_ASSERT( + _TEST_NO_EXCEPTION(get(execute(R"( + int x = 1; + int a() { + return x; + } + x = 2; + a(); + )")) == 2), + "Closure mutation externe", + true + ); + + _TEST_ASSERT( + _TEST_NO_EXCEPTION(get(execute(R"( + int x = 1; + void a() { + x = 2; + } + a(); + x; + )")) == 2), + "Closure mutation interne", + true + ); + return 0; } \ No newline at end of file