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
610std::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