Skip to content

Commit cb57345

Browse files
committed
提交语法验证测试
1 parent 8e66430 commit cb57345

37 files changed

+16269
-71
lines changed

CodeService/src/LuaFormatter.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
879879
if (child->GetText() == "then" || child->GetText() == "else")
880880
{
881881
env->AddChild(FormatNodeAndBlockOrEnd(i, children));
882-
env->Add<KeepLineElement>();
882+
env->Add<KeepElement>(1);
883883
}
884884
else if (child->GetText() == "if" || child->GetText() == "elseif")
885885
{
@@ -899,12 +899,6 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
899899
env->Add<KeepBlankElement>(1);
900900
break;
901901
}
902-
// case LuaAstNodeType::Block:
903-
// {
904-
// env->AddChild(FormatNode(child));
905-
// env->Add<KeepLineElement>();
906-
// break;
907-
// }
908902
default:
909903
{
910904
DefaultHandle(child, env);
@@ -1546,7 +1540,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(int& curren
15461540
if (nextMatch(currentIndex, LuaAstNodeType::KeyWord, vec))
15471541
{
15481542
auto next = vec[currentIndex + 1];
1549-
if (next->GetText() == "end" || next->GetText() == "else" || next->GetText() == "elseif")
1543+
if (next->GetText() == "end")
15501544
{
15511545
env->Add<TextElement>(next);
15521546
currentIndex++;
@@ -1555,6 +1549,10 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(int& curren
15551549
{
15561550
env->Add<LineElement>();
15571551
}
1552+
else
1553+
{
1554+
env->Add<KeepElement>(1);
1555+
}
15581556
}
15591557
else // 下一个不是关键词那能是什么那就换个行吧
15601558
{

LuaParser/src/LuaTokenParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ LuaTokenType LuaTokenParser::llex()
423423
{
424424
saveAndNext();
425425
// 只认为第一行的才是shebang
426-
if (_linenumber == 0 && getCurrentChar() == '!')
426+
if (_linenumber == 0 && _tokens.empty())
427427
{
428428
// shebang
429429
while (!currentIsNewLine() && getCurrentChar() != EOZ)

Test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ target_sources(CodeFormatTest
1818

1919
target_link_libraries(CodeFormatTest CodeService Util)
2020

21-
add_test(NAME CodeFormatTest COMMAND CodeFormatTest)
21+
add_test(NAME CodeFormatTest COMMAND CodeFormatTest CheckGrammar -w ${CodeFormatTest_SOURCE_DIR}/test_script/grammar)

Test/src/CodeFormatTest.cpp

Lines changed: 100 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#include <cassert>
22
#include <iostream>
3+
#include <filesystem>
4+
#include <vector>
5+
#include <fstream>
36
#include "CodeService/LuaFormatter.h"
4-
7+
#include "Util/format.h"
8+
#include "Util/CommandLine.h"
59

610
std::string_view TrimSpace(std::string_view source)
711
{
@@ -37,7 +41,7 @@ std::string_view TrimSpace(std::string_view source)
3741
return source.substr(start, end - start + 1);
3842
}
3943

40-
void Test(std::string input, const std::string& shouldBe, std::shared_ptr<LuaCodeStyleOptions> options)
44+
bool TestFormatted(std::string input, const std::string& shouldBe, std::shared_ptr<LuaCodeStyleOptions> options)
4145
{
4246
auto parser = LuaParser::LoadFromBuffer(std::move(input));
4347
parser->BuildAstWithComment();
@@ -48,74 +52,109 @@ void Test(std::string input, const std::string& shouldBe, std::shared_ptr<LuaCod
4852

4953
auto formattedText = formatter.GetFormattedText();
5054

51-
if (TrimSpace(formattedText) != TrimSpace(shouldBe))
55+
return TrimSpace(formattedText) == TrimSpace(shouldBe);
56+
}
57+
58+
bool TestGrammar(std::string input)
59+
{
60+
auto parser = LuaParser::LoadFromBuffer(std::move(input));
61+
parser->BuildAstWithComment();
62+
return parser->GetErrors().empty();
63+
}
64+
65+
void CollectLuaFile(std::filesystem::path directoryPath, std::vector<std::string>& paths, std::filesystem::path& root)
66+
{
67+
if (!std::filesystem::exists(directoryPath))
68+
{
69+
return;
70+
}
71+
72+
for (auto& it : std::filesystem::directory_iterator(directoryPath))
5273
{
53-
throw std::exception();
74+
if (std::filesystem::is_directory(it.status()))
75+
{
76+
CollectLuaFile(it.path().string(), paths, root);
77+
}
78+
else if (it.path().extension() == ".lua")
79+
{
80+
paths.push_back(std::filesystem::relative(it.path(), root).string());
81+
}
5482
}
5583
}
5684

57-
int main()
85+
std::string ReadFile(const std::string& path)
5886
{
59-
auto options = std::make_shared<LuaCodeStyleOptions>();
60-
options->line_separator = '\n';
61-
try
87+
std::fstream fin(path, std::ios::in | std::ios::binary);
88+
89+
if (fin.is_open())
6290
{
63-
Test(
64-
"local t=123 --fff",
65-
"local t = 123 --fff",
66-
options
67-
);
68-
69-
Test(
70-
R"(
71-
local t = 123
72-
local cc = 123
73-
)",
74-
R"(
75-
local t = 123
76-
local cc = 123
77-
)",
78-
options
79-
);
80-
81-
Test(
82-
R"(
83-
function fff()
84-
local t =123
85-
end
86-
)",
87-
R"(
88-
function fff()
89-
local t = 123
90-
end
91-
)",
92-
options
93-
);
94-
95-
Test(
96-
R"(
97-
do return end
98-
)",
99-
R"(
100-
do return end
101-
)",
102-
options
103-
);
104-
105-
Test(
106-
R"(
107-
local f = function (x,y) return x,y end
108-
)",
109-
R"(
110-
local f = function(x, y) return x, y end
111-
)",
112-
options
113-
);
91+
std::stringstream s;
92+
s << fin.rdbuf();
93+
return s.str();
11494
}
115-
catch (std::exception& e)
95+
96+
return "";
97+
}
98+
99+
// 第一个参数是待格式化文本目录,第二个参数是格式化预期结果
100+
int main(int argc, char* argv[])
101+
{
102+
CommandLine commandLine;
103+
104+
commandLine.AddTarget("CheckGrammar");
105+
commandLine.AddTarget("CheckFormatResult");
106+
107+
commandLine.Add<std::string>("work-directory", "w", "special base work directory");
108+
commandLine.Add<std::string>("formatted-work-directory", "f", "special formatted work directory");
109+
110+
if (!commandLine.Parse(argc, argv))
116111
{
112+
commandLine.PrintUsage();
117113
return -1;
118114
}
119115

120-
return 0;
116+
117+
std::filesystem::path workRoot(commandLine.Get<std::string>("work-directory"));
118+
std::vector<std::string> luaFiles;
119+
120+
CollectLuaFile(workRoot, luaFiles, workRoot);
121+
122+
auto target = commandLine.GetTarget();
123+
bool success = true;
124+
125+
if (target == "CheckFormatResult")
126+
{
127+
auto options = std::make_shared<LuaCodeStyleOptions>();
128+
std::filesystem::path formattedRoot(commandLine.Get<std::string>("formatted-work-directory"));
129+
for (auto& path : luaFiles)
130+
{
131+
auto waitFormattingFilePath = workRoot / path;
132+
auto waitFormattingText = ReadFile(waitFormattingFilePath.string());
133+
134+
auto formattedFilePath = formattedRoot / path;
135+
136+
auto formattedText = ReadFile(formattedFilePath.string());
137+
138+
bool passed = TestFormatted(waitFormattingText, formattedText, options);
139+
140+
success &= passed;
141+
std::cout << format("test format {} ... {}", path, passed) << std::endl;
142+
}
143+
}
144+
else if(target == "CheckGrammar")
145+
{
146+
for (auto& path : luaFiles)
147+
{
148+
auto filePath = workRoot / path;
149+
auto text = ReadFile(filePath.string());
150+
151+
bool passed = TestGrammar(text);
152+
153+
success &= passed;
154+
std::cout << format("test check grammar {} ... {}", path, passed) << std::endl;
155+
}
156+
}
157+
158+
159+
return success ? 0 : -1;
121160
}

0 commit comments

Comments
 (0)