2021-10-30 04:25:12 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Luau/Config.h"
|
2023-07-28 23:13:53 +08:00
|
|
|
#include "Luau/Differ.h"
|
|
|
|
#include "Luau/Error.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
#include "Luau/FileResolver.h"
|
|
|
|
#include "Luau/Frontend.h"
|
|
|
|
#include "Luau/IostreamHelpers.h"
|
|
|
|
#include "Luau/Linter.h"
|
|
|
|
#include "Luau/Location.h"
|
|
|
|
#include "Luau/ModuleResolver.h"
|
2022-06-24 09:56:00 +08:00
|
|
|
#include "Luau/Scope.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
#include "Luau/ToString.h"
|
2023-01-05 04:53:17 +08:00
|
|
|
#include "Luau/Type.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
#include "IostreamOptional.h"
|
|
|
|
#include "ScopedFlags.h"
|
|
|
|
|
2023-07-28 23:13:53 +08:00
|
|
|
#include "doctest.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
2022-12-10 03:57:01 +08:00
|
|
|
struct TypeChecker;
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
struct TestFileResolver
|
|
|
|
: FileResolver
|
|
|
|
, ModuleResolver
|
|
|
|
{
|
2022-12-10 03:57:01 +08:00
|
|
|
std::optional<ModuleInfo> resolveModuleInfo(const ModuleName& currentModuleName, const AstExpr& pathExpr) override;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-12-10 03:57:01 +08:00
|
|
|
const ModulePtr getModule(const ModuleName& moduleName) const override;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-12-10 03:57:01 +08:00
|
|
|
bool moduleExists(const ModuleName& moduleName) const override;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-12-10 03:57:01 +08:00
|
|
|
std::optional<SourceCode> readSource(const ModuleName& name) override;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2021-11-05 10:34:35 +08:00
|
|
|
std::optional<ModuleInfo> resolveModule(const ModuleInfo* context, AstExpr* expr) override;
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
std::string getHumanReadableModuleName(const ModuleName& name) const override;
|
|
|
|
|
|
|
|
std::optional<std::string> getEnvironmentForModule(const ModuleName& name) const override;
|
|
|
|
|
|
|
|
std::unordered_map<ModuleName, std::string> source;
|
|
|
|
std::unordered_map<ModuleName, SourceCode::Type> sourceTypes;
|
|
|
|
std::unordered_map<ModuleName, std::string> environments;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestConfigResolver : ConfigResolver
|
|
|
|
{
|
|
|
|
Config defaultConfig;
|
|
|
|
std::unordered_map<ModuleName, Config> configFiles;
|
|
|
|
|
2022-12-10 03:57:01 +08:00
|
|
|
const Config& getConfig(const ModuleName& name) const override;
|
2021-10-30 04:25:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Fixture
|
|
|
|
{
|
2022-04-08 05:29:01 +08:00
|
|
|
explicit Fixture(bool freeze = true, bool prepareAutocomplete = false);
|
2021-10-30 04:25:12 +08:00
|
|
|
~Fixture();
|
|
|
|
|
|
|
|
// Throws Luau::ParseErrors if the parse fails.
|
|
|
|
AstStatBlock* parse(const std::string& source, const ParseOptions& parseOptions = {});
|
2023-10-14 04:20:12 +08:00
|
|
|
CheckResult check(Mode mode, const std::string& source);
|
2021-10-30 04:25:12 +08:00
|
|
|
CheckResult check(const std::string& source);
|
|
|
|
|
|
|
|
LintResult lint(const std::string& source, const std::optional<LintOptions>& lintOptions = {});
|
2023-03-18 03:20:37 +08:00
|
|
|
LintResult lintModule(const ModuleName& moduleName, const std::optional<LintOptions>& lintOptions = {});
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
/// Parse with all language extensions enabled
|
|
|
|
ParseResult parseEx(const std::string& source, const ParseOptions& parseOptions = {});
|
|
|
|
ParseResult tryParse(const std::string& source, const ParseOptions& parseOptions = {});
|
2022-01-15 00:20:09 +08:00
|
|
|
ParseResult matchParseError(const std::string& source, const std::string& message, std::optional<Location> location = std::nullopt);
|
2021-10-30 04:25:12 +08:00
|
|
|
// Verify a parse error occurs and the parse error message has the specified prefix
|
|
|
|
ParseResult matchParseErrorPrefix(const std::string& source, const std::string& prefix);
|
|
|
|
|
|
|
|
ModulePtr getMainModule();
|
|
|
|
SourceModule* getMainSourceModule();
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
std::optional<PrimitiveType::Type> getPrimitiveType(TypeId ty);
|
2021-10-30 04:25:12 +08:00
|
|
|
std::optional<TypeId> getType(const std::string& name);
|
|
|
|
TypeId requireType(const std::string& name);
|
|
|
|
TypeId requireType(const ModuleName& moduleName, const std::string& name);
|
|
|
|
TypeId requireType(const ModulePtr& module, const std::string& name);
|
|
|
|
TypeId requireType(const ScopePtr& scope, const std::string& name);
|
|
|
|
|
|
|
|
std::optional<TypeId> findTypeAtPosition(Position position);
|
|
|
|
TypeId requireTypeAtPosition(Position position);
|
2021-12-03 14:41:04 +08:00
|
|
|
std::optional<TypeId> findExpectedTypeAtPosition(Position position);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::optional<TypeId> lookupType(const std::string& name);
|
|
|
|
std::optional<TypeId> lookupImportedType(const std::string& moduleAlias, const std::string& name);
|
2023-01-07 05:14:35 +08:00
|
|
|
TypeId requireTypeAlias(const std::string& name);
|
2023-07-28 23:13:53 +08:00
|
|
|
TypeId requireExportedType(const ModuleName& moduleName, const std::string& name);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
ScopedFastFlag sff_DebugLuauFreezeArena;
|
|
|
|
|
|
|
|
TestFileResolver fileResolver;
|
|
|
|
TestConfigResolver configResolver;
|
2022-09-02 07:14:03 +08:00
|
|
|
NullModuleResolver moduleResolver;
|
2021-10-30 04:25:12 +08:00
|
|
|
std::unique_ptr<SourceModule> sourceModule;
|
|
|
|
Frontend frontend;
|
2022-07-01 07:52:43 +08:00
|
|
|
InternalErrorReporter ice;
|
2023-01-05 04:53:17 +08:00
|
|
|
NotNull<BuiltinTypes> builtinTypes;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::string decorateWithTypes(const std::string& code);
|
|
|
|
|
|
|
|
void dumpErrors(std::ostream& os, const std::vector<TypeError>& errors);
|
|
|
|
|
|
|
|
void dumpErrors(const CheckResult& cr);
|
|
|
|
void dumpErrors(const ModulePtr& module);
|
|
|
|
void dumpErrors(const Module& module);
|
|
|
|
|
|
|
|
void validateErrors(const std::vector<TypeError>& errors);
|
|
|
|
|
|
|
|
std::string getErrors(const CheckResult& cr);
|
|
|
|
|
|
|
|
void registerTestTypes();
|
|
|
|
|
|
|
|
LoadDefinitionFileResult loadDefinition(const std::string& source);
|
|
|
|
};
|
|
|
|
|
2022-05-14 03:36:37 +08:00
|
|
|
struct BuiltinsFixture : Fixture
|
|
|
|
{
|
|
|
|
BuiltinsFixture(bool freeze = true, bool prepareAutocomplete = false);
|
|
|
|
};
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
ModuleName fromString(std::string_view name);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::optional<T> get(const std::map<Name, T>& map, const Name& name)
|
|
|
|
{
|
|
|
|
auto it = map.find(name);
|
|
|
|
if (it != map.end())
|
|
|
|
return std::optional<T>(it->second);
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rep(const std::string& s, size_t n);
|
|
|
|
|
|
|
|
bool isInArena(TypeId t, const TypeArena& arena);
|
|
|
|
|
|
|
|
void dumpErrors(const ModulePtr& module);
|
|
|
|
void dumpErrors(const Module& module);
|
|
|
|
void dump(const std::string& name, TypeId ty);
|
2022-06-04 06:15:45 +08:00
|
|
|
void dump(const std::vector<Constraint>& constraints);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::optional<TypeId> lookupName(ScopePtr scope, const std::string& name); // Warning: This function runs in O(n**2)
|
|
|
|
|
2022-07-29 12:24:07 +08:00
|
|
|
std::optional<TypeId> linearSearchForBinding(Scope* scope, const char* name);
|
2022-06-04 06:15:45 +08:00
|
|
|
|
2023-01-07 05:14:35 +08:00
|
|
|
void registerHiddenTypes(Frontend* frontend);
|
|
|
|
void createSomeClasses(Frontend* frontend);
|
2022-10-28 18:37:29 +08:00
|
|
|
|
2023-07-28 23:13:53 +08:00
|
|
|
template<typename BaseFixture>
|
|
|
|
struct DifferFixtureGeneric : BaseFixture
|
|
|
|
{
|
2023-08-11 22:42:37 +08:00
|
|
|
std::string normalizeWhitespace(std::string msg)
|
|
|
|
{
|
|
|
|
std::string normalizedMsg = "";
|
|
|
|
bool wasWhitespace = true;
|
|
|
|
for (char c : msg)
|
|
|
|
{
|
|
|
|
bool isWhitespace = c == ' ' || c == '\n';
|
|
|
|
if (wasWhitespace && isWhitespace)
|
|
|
|
continue;
|
|
|
|
normalizedMsg += isWhitespace ? ' ' : c;
|
|
|
|
wasWhitespace = isWhitespace;
|
|
|
|
}
|
|
|
|
if (wasWhitespace)
|
|
|
|
normalizedMsg.pop_back();
|
|
|
|
return normalizedMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void compareNe(TypeId left, TypeId right, const std::string& expectedMessage, bool multiLine)
|
|
|
|
{
|
|
|
|
compareNe(left, std::nullopt, right, std::nullopt, expectedMessage, multiLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
void compareNe(TypeId left, std::optional<std::string> symbolLeft, TypeId right, std::optional<std::string> symbolRight,
|
|
|
|
const std::string& expectedMessage, bool multiLine)
|
2023-07-28 23:13:53 +08:00
|
|
|
{
|
2023-11-11 05:10:07 +08:00
|
|
|
DifferResult diffRes = diffWithSymbols(left, right, symbolLeft, symbolRight);
|
|
|
|
REQUIRE_MESSAGE(diffRes.diffError.has_value(), "Differ did not report type error, even though types are unequal");
|
|
|
|
std::string diffMessage = diffRes.diffError->toString(multiLine);
|
2023-07-28 23:13:53 +08:00
|
|
|
CHECK_EQ(expectedMessage, diffMessage);
|
|
|
|
}
|
|
|
|
|
2023-08-11 22:42:37 +08:00
|
|
|
void compareTypesNe(const std::string& leftSymbol, const std::string& rightSymbol, const std::string& expectedMessage, bool forwardSymbol = false,
|
|
|
|
bool multiLine = false)
|
2023-07-28 23:13:53 +08:00
|
|
|
{
|
2023-08-11 22:42:37 +08:00
|
|
|
if (forwardSymbol)
|
|
|
|
{
|
|
|
|
compareNe(
|
|
|
|
BaseFixture::requireType(leftSymbol), leftSymbol, BaseFixture::requireType(rightSymbol), rightSymbol, expectedMessage, multiLine);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
compareNe(
|
|
|
|
BaseFixture::requireType(leftSymbol), std::nullopt, BaseFixture::requireType(rightSymbol), std::nullopt, expectedMessage, multiLine);
|
|
|
|
}
|
2023-07-28 23:13:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void compareEq(TypeId left, TypeId right)
|
|
|
|
{
|
2023-11-11 05:10:07 +08:00
|
|
|
DifferResult diffRes = diff(left, right);
|
|
|
|
CHECK(!diffRes.diffError);
|
|
|
|
if (diffRes.diffError)
|
|
|
|
INFO(diffRes.diffError->toString());
|
2023-07-28 23:13:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void compareTypesEq(const std::string& leftSymbol, const std::string& rightSymbol)
|
|
|
|
{
|
|
|
|
compareEq(BaseFixture::requireType(leftSymbol), BaseFixture::requireType(rightSymbol));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
using DifferFixture = DifferFixtureGeneric<Fixture>;
|
|
|
|
using DifferFixtureWithBuiltins = DifferFixtureGeneric<BuiltinsFixture>;
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
} // namespace Luau
|
|
|
|
|
|
|
|
#define LUAU_REQUIRE_ERRORS(result) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
auto&& r = (result); \
|
|
|
|
validateErrors(r.errors); \
|
|
|
|
REQUIRE(!r.errors.empty()); \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define LUAU_REQUIRE_ERROR_COUNT(count, result) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
auto&& r = (result); \
|
|
|
|
validateErrors(r.errors); \
|
|
|
|
REQUIRE_MESSAGE(count == r.errors.size(), getErrors(r)); \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define LUAU_REQUIRE_NO_ERRORS(result) LUAU_REQUIRE_ERROR_COUNT(0, result)
|
2024-06-21 07:37:55 +08:00
|
|
|
|
|
|
|
#define LUAU_CHECK_ERRORS(result) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
auto&& r = (result); \
|
|
|
|
validateErrors(r.errors); \
|
|
|
|
CHECK(!r.errors.empty()); \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define LUAU_CHECK_ERROR_COUNT(count, result) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
auto&& r = (result); \
|
|
|
|
validateErrors(r.errors); \
|
|
|
|
CHECK_MESSAGE(count == r.errors.size(), getErrors(r)); \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define LUAU_CHECK_NO_ERRORS(result) LUAU_CHECK_ERROR_COUNT(0, result)
|