Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/tests/Error.test.cpp
2723 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#include "Luau/Error.h"
3
4
#include "Fixture.h"
5
#include "doctest.h"
6
7
using namespace Luau;
8
9
LUAU_FASTFLAG(DebugLuauForceOldSolver)
10
11
TEST_SUITE_BEGIN("ErrorTests");
12
13
TEST_CASE("TypeError_code_should_return_nonzero_code")
14
{
15
auto e = TypeError{{{0, 0}, {0, 1}}, UnknownSymbol{"Foo"}};
16
CHECK_GE(e.code(), 1000);
17
}
18
19
TEST_CASE_FIXTURE(BuiltinsFixture, "metatable_names_show_instead_of_tables")
20
{
21
getFrontend().options.retainFullTypeGraphs = false;
22
23
CheckResult result = check(R"(
24
--!strict
25
local Account = {}
26
Account.__index = Account
27
function Account.deposit(self: Account, x: number)
28
self.balance += x
29
end
30
type Account = typeof(setmetatable({} :: { balance: number }, Account))
31
local x: Account = 5
32
)");
33
34
LUAU_REQUIRE_ERROR_COUNT(1, result);
35
36
CHECK_EQ("Expected this to be 'Account', but got 'number'", toString(result.errors[0]));
37
}
38
39
TEST_CASE_FIXTURE(BuiltinsFixture, "binary_op_type_function_errors")
40
{
41
getFrontend().options.retainFullTypeGraphs = false;
42
43
CheckResult result = check(R"(
44
--!strict
45
local x = 1 + "foo"
46
)");
47
48
LUAU_REQUIRE_ERROR_COUNT(1, result);
49
50
if (!FFlag::DebugLuauForceOldSolver)
51
CHECK_EQ(
52
"Operator '+' could not be applied to operands of types number and string; there is no corresponding overload for __add",
53
toString(result.errors[0])
54
);
55
else
56
CHECK_EQ("Expected this to be 'number', but got 'string'", toString(result.errors[0]));
57
}
58
59
TEST_CASE_FIXTURE(BuiltinsFixture, "unary_op_type_function_errors")
60
{
61
getFrontend().options.retainFullTypeGraphs = false;
62
63
CheckResult result = check(R"(
64
--!strict
65
local x = -"foo"
66
)");
67
68
69
if (!FFlag::DebugLuauForceOldSolver)
70
{
71
LUAU_REQUIRE_ERROR_COUNT(2, result);
72
CHECK_EQ(
73
"Operator '-' could not be applied to operand of type string; there is no corresponding overload for __unm", toString(result.errors[0])
74
);
75
76
CHECK_EQ("Expected this to be 'number', but got 'string'", toString(result.errors[1]));
77
}
78
else
79
{
80
LUAU_REQUIRE_ERROR_COUNT(1, result);
81
CHECK_EQ("Expected this to be 'number', but got 'string'", toString(result.errors[0]));
82
}
83
}
84
85
TEST_SUITE_END();
86
87