Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/tests/ConstraintSolver.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
3
#include "ConstraintGeneratorFixture.h"
4
#include "Fixture.h"
5
#include "doctest.h"
6
7
using namespace Luau;
8
9
static TypeId requireBinding(Scope* scope, const char* name)
10
{
11
auto b = linearSearchForBinding(scope, name);
12
LUAU_ASSERT(b.has_value());
13
return *b;
14
}
15
16
TEST_SUITE_BEGIN("ConstraintSolver");
17
18
TEST_CASE_FIXTURE(ConstraintGeneratorFixture, "constraint_basics")
19
{
20
solve(R"(
21
local a = 55
22
local b = a
23
)");
24
25
TypeId bType = requireBinding(rootScope, "b");
26
27
CHECK("number" == toString(bType));
28
}
29
30
TEST_CASE_FIXTURE(ConstraintGeneratorFixture, "generic_function")
31
{
32
solve(R"(
33
local function id(a)
34
return a
35
end
36
)");
37
38
TypeId idType = requireBinding(rootScope, "id");
39
40
CHECK("<a>(a) -> a" == toString(idType));
41
}
42
43
TEST_CASE_FIXTURE(ConstraintGeneratorFixture, "proper_let_generalization")
44
{
45
solve(R"(
46
local function a(c)
47
local function d(e)
48
return c
49
end
50
51
return d
52
end
53
54
local b = a(5)
55
)");
56
57
TypeId idType = requireBinding(rootScope, "b");
58
59
CHECK("(unknown) -> number" == toString(idType));
60
}
61
62
TEST_CASE_FIXTURE(ConstraintGeneratorFixture, "table_prop_access_diamond")
63
{
64
CheckResult result = check(R"(
65
export type ItemDetails = { Id: number }
66
67
export type AssetDetails = ItemDetails & {}
68
export type BundleDetails = ItemDetails & {}
69
70
export type CatalogPage = { AssetDetails | BundleDetails }
71
72
local function isRestricted(item: number) end
73
74
-- Clear all item tiles and create new ones for the items in the specified page
75
local function displayPage(catalogPage: CatalogPage)
76
for _, itemDetails in catalogPage do
77
if isRestricted(itemDetails.Id) then
78
continue
79
end
80
end
81
end
82
)");
83
84
LUAU_REQUIRE_NO_ERRORS(result);
85
}
86
87
TEST_SUITE_END();
88
89