c-repl/test/variables.cpp

57 lines
1.4 KiB
C++
Raw Permalink Normal View History

2024-01-12 14:10:23 +01:00
#include "include/test.hpp"
2023-11-10 22:24:14 +01:00
2024-01-12 14:10:23 +01:00
#include "../src/include/execute.hpp"
#include "../src/include/utils.hpp"
2023-11-10 22:24:14 +01:00
2023-11-22 15:31:30 +01:00
EvalResult execute(string s) {
2024-01-03 16:20:42 +01:00
Memory memory;
return execute(split_string(s, '\n'), memory);
2023-11-10 22:24:14 +01:00
}
int main() {
_TEST_PRESENTATION("Variables");
2023-11-14 17:03:42 +01:00
_TEST_ASSERT(
2024-01-10 15:42:30 +01:00
_TEST_NO_EXCEPTION(get<int>(execute("int __une_variable_farfelue__ = 12;")) == 12),
2023-11-15 11:59:38 +01:00
"Déclaration avec assignement",
true
2023-11-14 17:03:42 +01:00
);
2023-11-10 22:24:14 +01:00
2023-11-14 17:03:42 +01:00
_TEST_ASSERT(
2023-11-22 15:31:30 +01:00
_TEST_NO_EXCEPTION(get<int>(execute("int x = 5; x = x + 3;")) == 8),
2023-11-15 11:59:38 +01:00
"Déclaration puis assignement",
true
2023-11-14 17:03:42 +01:00
);
2023-11-10 22:24:14 +01:00
2023-11-14 20:00:14 +01:00
_TEST_ASSERT(
2023-12-15 15:10:05 +01:00
_TEST_IS_EXCEPTION(execute("1 + x;"), ErrorType::UnknownIdentifier),
2023-11-22 15:31:30 +01:00
"Identifieur indéfini",
2023-11-15 11:59:38 +01:00
true
2023-11-14 20:00:14 +01:00
);
_TEST_ASSERT(
2023-12-26 12:34:05 +01:00
_TEST_IS_EXCEPTION(execute("int x; int x;"), ErrorType::AlreadyDeclaredIdentifier),
2023-11-22 15:31:30 +01:00
"Identifieur déjà défini",
2023-11-15 11:59:38 +01:00
true
2023-11-14 20:00:14 +01:00
);
2024-01-04 21:22:35 +01:00
_TEST_ASSERT(
_TEST_IS_EXCEPTION(execute("void x;"), ErrorType::IncompleteType),
"Type void",
true
);
2023-11-14 20:00:14 +01:00
_TEST_ASSERT(
2023-11-22 15:31:30 +01:00
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute("int x; { int x; }"))),
"Identifieur déjà défini dans une autre scope",
2023-11-15 11:59:38 +01:00
true
2023-11-14 20:00:14 +01:00
);
2023-11-15 16:10:36 +01:00
_TEST_ASSERT(
2023-11-22 15:31:30 +01:00
_TEST_NO_EXCEPTION(get<int>(execute("int x = 1; int y; { int x = 2; y = x + 2; }; y;")) == 4),
"Shadowing",
2023-11-15 16:10:36 +01:00
true
);
2023-11-10 22:24:14 +01:00
return 0;
}