PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/2.02/AStyleTest/srccon/AStyleTestCon_Console2.cpp

#
C++ | 1520 lines | 1100 code | 103 blank | 317 comment | 109 complexity | 94290b6c3d140a1ad336c33ac8122ac5 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. // AStyleTestCon tests the ASConsole class only. This class is used only in
  2. // the console build. It also tests the parseOption function for options used
  3. // by only by the console build (e.g. recursive, preserve-date, verbose). It
  4. // does not explicitely test the ASStreamIterator class or any other part
  5. // of the program.
  6. //----------------------------------------------------------------------------
  7. // headers
  8. //----------------------------------------------------------------------------
  9. #include <algorithm>
  10. #include "AStyleTestCon.h"
  11. #ifdef _WIN32
  12. #include <windows.h>
  13. #endif
  14. //----------------------------------------------------------------------------
  15. // global variables
  16. //----------------------------------------------------------------------------
  17. // defined in astyle_main.cpp
  18. extern int _CRT_glob;
  19. //----------------------------------------------------------------------------
  20. // anonymous namespace
  21. //----------------------------------------------------------------------------
  22. namespace
  23. {
  24. //----------------------------------------------------------------------------
  25. // AStyle test getFilePaths(), wildcmp(), and fileName vector
  26. //----------------------------------------------------------------------------
  27. struct GetFilePathsF : public ::testing::Test
  28. {
  29. ASFormatter formatter;
  30. vector<string> fileNames;
  31. // build fileNames vector and write the output files
  32. GetFilePathsF()
  33. {
  34. char textIn[] =
  35. "\nvoid foo()\n"
  36. "{\n"
  37. "bar();\n"
  38. "}\n";
  39. cleanTestDirectory(getTestDirectory());
  40. createConsoleGlobalObject(formatter);
  41. fileNames.push_back(getTestDirectory() + "/getFilePaths1.cpp");
  42. fileNames.push_back(getTestDirectory() + "/getFilePaths2.cpp");
  43. fileNames.push_back(getTestDirectory() + "/getFilePaths3.cpp");
  44. fileNames.push_back(getTestDirectory() + "/getFilePaths4.cpp");
  45. for (size_t i = 0; i < fileNames.size(); i++)
  46. {
  47. g_console->standardizePath(fileNames[i]);
  48. createTestFile(fileNames[i], textIn);
  49. }
  50. }
  51. ~GetFilePathsF()
  52. {
  53. deleteConsoleGlobalObject();
  54. }
  55. };
  56. TEST_F(GetFilePathsF, FilePaths1)
  57. // test fileName vector and getFilePaths with *.cpp
  58. {
  59. assert(g_console != NULL);
  60. g_console->setIsQuiet(true); // change this to see results
  61. // call astyle processOptions()
  62. vector<string> astyleOptionsVector;
  63. astyleOptionsVector.push_back(getTestDirectory() + "/*.cpp");
  64. g_console->processOptions(astyleOptionsVector);
  65. // call astyle processFiles()
  66. g_console->processFiles();
  67. // check the files found
  68. vector<string> fileName = g_console->getFileName();
  69. ASSERT_EQ(fileNames.size(), fileName.size()) << "Vector sizes not equal.";
  70. for (size_t i = 0; i < fileName.size(); i++)
  71. EXPECT_EQ(fileNames[i], fileName[i]);
  72. }
  73. TEST_F(GetFilePathsF, FilePaths2)
  74. // test fileName vector and getFilePaths with *.c??
  75. {
  76. assert(g_console != NULL);
  77. g_console->setIsQuiet(true); // change this to see results
  78. // call astyle processOptions()
  79. vector<string> astyleOptionsVector;
  80. astyleOptionsVector.push_back(getTestDirectory() + "/*.c??");
  81. g_console->processOptions(astyleOptionsVector);
  82. // call astyle processFiles()
  83. g_console->processFiles();
  84. // check the files found
  85. vector<string> fileName = g_console->getFileName();
  86. ASSERT_EQ(fileNames.size(), fileName.size()) << "Vector sizes not equal.";
  87. for (size_t i = 0; i < fileName.size(); i++)
  88. EXPECT_EQ(fileNames[i], fileName[i]);
  89. }
  90. TEST_F(GetFilePathsF, FilePaths3)
  91. // test fileName vector and getFilePaths with getFilePaths*.cpp
  92. {
  93. assert(g_console != NULL);
  94. g_console->setIsQuiet(true); // change this to see results
  95. // call astyle processOptions()
  96. vector<string> astyleOptionsVector;
  97. astyleOptionsVector.push_back(getTestDirectory() + "/getFilePaths*.cpp");
  98. g_console->processOptions(astyleOptionsVector);
  99. // call astyle processFiles()
  100. g_console->processFiles();
  101. // check the files found
  102. vector<string> fileName = g_console->getFileName();
  103. ASSERT_EQ(fileNames.size(), fileName.size()) << "Vector sizes not equal.";
  104. for (size_t i = 0; i < fileName.size(); i++)
  105. EXPECT_EQ(fileNames[i], fileName[i]);
  106. }
  107. TEST_F(GetFilePathsF, FilePaths4)
  108. // test fileName vector and getFilePaths with *.c*
  109. // * at the end WITH remaining data allows complete coverage of wildcmp function
  110. {
  111. assert(g_console != NULL);
  112. g_console->setIsQuiet(true); // change this to see results
  113. // call astyle processOptions()
  114. vector<string> astyleOptionsVector;
  115. astyleOptionsVector.push_back(getTestDirectory() + "/*.c*");
  116. g_console->processOptions(astyleOptionsVector);
  117. // call astyle processFiles()
  118. g_console->processFiles();
  119. // check the files found
  120. vector<string> fileName = g_console->getFileName();
  121. ASSERT_EQ(fileNames.size(), fileName.size()) << "Vector sizes not equal.";
  122. for (size_t i = 0; i < fileName.size(); i++)
  123. EXPECT_EQ(fileNames[i], fileName[i]);
  124. }
  125. TEST_F(GetFilePathsF, FilePaths5)
  126. // test fileName vector and getFilePaths with *.cpp*
  127. // * at the end WITHOUT remaining data allows complete coverage of wildcmp function
  128. {
  129. assert(g_console != NULL);
  130. g_console->setIsQuiet(true); // change this to see results
  131. // call astyle processOptions()
  132. vector<string> astyleOptionsVector;
  133. astyleOptionsVector.push_back(getTestDirectory() + "/*.cpp*");
  134. g_console->processOptions(astyleOptionsVector);
  135. // call astyle processFiles()
  136. g_console->processFiles();
  137. // check the files found
  138. vector<string> fileName = g_console->getFileName();
  139. ASSERT_EQ(fileNames.size(), fileName.size()) << "Vector sizes not equal.";
  140. for (size_t i = 0; i < fileName.size(); i++)
  141. EXPECT_EQ(fileNames[i], fileName[i]);
  142. }
  143. TEST_F(GetFilePathsF, FilePathsError)
  144. // test fileName vector and getFilePaths with bad file path
  145. {
  146. assert(g_console != NULL);
  147. g_console->setIsQuiet(true); // change this to see results
  148. // call astyle processOptions()
  149. vector<string> astyleOptionsVector;
  150. astyleOptionsVector.push_back(getTestDirectory() + "/AStyleError*");
  151. g_console->processOptions(astyleOptionsVector);
  152. // change special chars in filepath to '.' for windows regex comparison
  153. size_t i = 0;
  154. while (i < astyleOptionsVector.back().length())
  155. {
  156. i = astyleOptionsVector.back().find_first_of("\\/*?", i);
  157. if (i == string::npos)
  158. break;
  159. astyleOptionsVector.back()[i] = '.';
  160. ++i;
  161. }
  162. string regex = "No file to process " + astyleOptionsVector.back() +
  163. "\nDid you intend to use --recursive?";
  164. // cannot use death test with leak finder
  165. #if GTEST_HAS_DEATH_TEST && !LEAK_FINDER
  166. // test processFiles with bad file path
  167. EXPECT_EXIT(g_console->processFiles(),
  168. ::testing::ExitedWithCode(EXIT_FAILURE),
  169. regex);
  170. #endif
  171. }
  172. TEST_F(GetFilePathsF, FilePathsErrorRecursive)
  173. // test fileName vector and getFilePaths with bad file path using recursive option
  174. {
  175. assert(g_console != NULL);
  176. g_console->setIsQuiet(true); // change this to see results
  177. g_console->setIsRecursive(true);
  178. // call astyle processOptions()
  179. vector<string> astyleOptionsVector;
  180. astyleOptionsVector.push_back(getTestDirectory() + "/AStyleError*");
  181. g_console->processOptions(astyleOptionsVector);
  182. // change special chars in filepath to '.' for windows regex comparison
  183. size_t i = 0;
  184. while (i < astyleOptionsVector.back().length())
  185. {
  186. i = astyleOptionsVector.back().find_first_of("\\/*?", i);
  187. if (i == string::npos)
  188. break;
  189. astyleOptionsVector.back()[i] = '.';
  190. ++i;
  191. }
  192. string regex = "No file to process " + astyleOptionsVector.back();
  193. // cannot use death test with leak finder
  194. #if GTEST_HAS_DEATH_TEST && !LEAK_FINDER
  195. // test processFiles with bad file path
  196. EXPECT_EXIT(g_console->processFiles(),
  197. ::testing::ExitedWithCode(EXIT_FAILURE),
  198. regex);
  199. #endif
  200. }
  201. TEST_F(GetFilePathsF, FilePathsErrorSansFilename)
  202. // test fileName vector and getFilePaths with no filename and no recursive option
  203. {
  204. assert(g_console != NULL);
  205. g_console->setIsQuiet(true); // change this to see results
  206. vector<string> astyleOptionsVector;
  207. astyleOptionsVector.push_back(getTestDirectory() + "/"); // no file
  208. g_console->processOptions(astyleOptionsVector);
  209. // change special chars in filepath to '.' for windows regex comparison
  210. size_t i = 0;
  211. while (i < astyleOptionsVector.back().length())
  212. {
  213. i = astyleOptionsVector.back().find_first_of("\\/*?", i);
  214. if (i == string::npos)
  215. break;
  216. astyleOptionsVector.back()[i] = '.';
  217. ++i;
  218. }
  219. string regex = "Missing filename in " + astyleOptionsVector.back();
  220. // cannot use death test with leak finder
  221. #if GTEST_HAS_DEATH_TEST && !LEAK_FINDER
  222. // test processFiles with bad file path
  223. EXPECT_EXIT(g_console->processFiles(),
  224. ::testing::ExitedWithCode(EXIT_FAILURE),
  225. regex);
  226. #endif
  227. }
  228. TEST_F(GetFilePathsF, FilePathsErrorRecursiveSansWildcard)
  229. // test fileName vector and getFilePaths with recursive option and no wildcard
  230. {
  231. assert(g_console != NULL);
  232. g_console->setIsQuiet(true); // change this to see results
  233. g_console->setIsRecursive(true);
  234. vector<string> astyleOptionsVector;
  235. astyleOptionsVector.push_back(getTestDirectory() + "/noWildcard.cpp");
  236. g_console->processOptions(astyleOptionsVector);
  237. // change special chars in filepath to '.' for windows regex comparison
  238. size_t i = 0;
  239. while (i < astyleOptionsVector.back().length())
  240. {
  241. i = astyleOptionsVector.back().find_first_of("\\/*?", i);
  242. if (i == string::npos)
  243. break;
  244. astyleOptionsVector.back()[i] = '.';
  245. ++i;
  246. }
  247. string regex = "Recursive option with no wildcard\n";
  248. #ifndef _WIN32
  249. regex.append("Did you intend quote the filename?\n");
  250. #endif
  251. // cannot use death test with leak finder
  252. #if GTEST_HAS_DEATH_TEST && !LEAK_FINDER
  253. // test processFiles with bad file path
  254. EXPECT_EXIT(g_console->processFiles(),
  255. ::testing::ExitedWithCode(EXIT_FAILURE),
  256. regex);
  257. #endif
  258. }
  259. TEST_F(GetFilePathsF, FilePathsErrorInDirectoryName)
  260. // test fileName vector and getFilePaths with a non-existant directory
  261. {
  262. assert(g_console != NULL);
  263. g_console->setIsQuiet(true); // change this to see results
  264. g_console->setIsRecursive(true);
  265. vector<string> astyleOptionsVector;
  266. astyleOptionsVector.push_back(getTestDirectory() + "/errorInDirectoryName/*.cpp");
  267. g_console->processOptions(astyleOptionsVector);
  268. // change special chars in filepath to '.' for windows regex comparison
  269. size_t i = 0;
  270. while (i < astyleOptionsVector.back().length())
  271. {
  272. i = astyleOptionsVector.back().find_first_of("\\/*?", i);
  273. if (i == string::npos)
  274. break;
  275. astyleOptionsVector.back()[i] = '.';
  276. ++i;
  277. }
  278. string regex = "Cannot open directory " + astyleOptionsVector.back();
  279. regex = regex.substr(0, regex.length() - 6); // remove the wilscard
  280. // cannot use death test with leak finder
  281. #if GTEST_HAS_DEATH_TEST && !LEAK_FINDER
  282. // test processFiles with bad file path
  283. EXPECT_EXIT(g_console->processFiles(),
  284. ::testing::ExitedWithCode(EXIT_FAILURE),
  285. regex);
  286. #endif
  287. }
  288. //----------------------------------------------------------------------------
  289. // AStyle test getFileType() - C_TYPE, JAVA_TYPE, SHARP_TYPE
  290. //----------------------------------------------------------------------------
  291. struct GetFileTypeF : public ::testing::Test
  292. {
  293. ASFormatter formatter;
  294. vector<string> fileNames;
  295. // build fileNames vector and write the output files
  296. GetFileTypeF()
  297. {
  298. char textIn[] =
  299. "\nvoid foo()\n"
  300. "{\n"
  301. " bar();\n"
  302. "}\n";
  303. cleanTestDirectory(getTestDirectory());
  304. createConsoleGlobalObject(formatter);
  305. fileNames.push_back(getTestDirectory() + "/getFileType1.cpp");
  306. fileNames.push_back(getTestDirectory() + "/getFileType2.java");
  307. fileNames.push_back(getTestDirectory() + "/getFileType3.cs");
  308. fileNames.push_back(getTestDirectory() + "/getFileType4.error");
  309. for (size_t i = 0; i < fileNames.size(); i++)
  310. {
  311. g_console->standardizePath(fileNames[i]);
  312. createTestFile(fileNames[i], textIn);
  313. }
  314. }
  315. ~GetFileTypeF()
  316. {
  317. deleteConsoleGlobalObject();
  318. }
  319. };
  320. TEST_F(GetFileTypeF, FileTypeC)
  321. // test getFileType with *.cpp
  322. {
  323. assert(g_console != NULL);
  324. g_console->setIsQuiet(true); // change this to see results
  325. // call astyle processOptions()
  326. vector<string> astyleOptionsVector;
  327. astyleOptionsVector.push_back(getTestDirectory() + "/*.cpp");
  328. g_console->processOptions(astyleOptionsVector);
  329. // call astyle processFiles()
  330. g_console->processFiles();
  331. // check the file type
  332. EXPECT_EQ(C_TYPE, formatter.getFileType());
  333. }
  334. TEST_F(GetFileTypeF, FileTypeJava)
  335. // test getFileType with *.java
  336. {
  337. assert(g_console != NULL);
  338. g_console->setIsQuiet(true); // change this to see results
  339. // call astyle processOptions()
  340. vector<string> astyleOptionsVector;
  341. astyleOptionsVector.push_back(getTestDirectory() + "/*.java");
  342. g_console->processOptions(astyleOptionsVector);
  343. // call astyle processFiles()
  344. g_console->processFiles();
  345. // check the file type
  346. EXPECT_EQ(JAVA_TYPE, formatter.getFileType());
  347. }
  348. TEST_F(GetFileTypeF, FileTypeSharp)
  349. // test getFileType with *.cs
  350. {
  351. assert(g_console != NULL);
  352. g_console->setIsQuiet(true); // change this to see results
  353. // call astyle processOptions()
  354. vector<string> astyleOptionsVector;
  355. astyleOptionsVector.push_back(getTestDirectory() + "/*.cs");
  356. g_console->processOptions(astyleOptionsVector);
  357. // call astyle processFiles()
  358. g_console->processFiles();
  359. // check the file type
  360. EXPECT_EQ(SHARP_TYPE, formatter.getFileType());
  361. }
  362. TEST_F(GetFileTypeF, FileTypeError)
  363. // test getFileType with an invalid type, should return C_TYPE
  364. {
  365. assert(g_console != NULL);
  366. g_console->setIsQuiet(true); // change this to see results
  367. // call astyle processOptions()
  368. vector<string> astyleOptionsVector;
  369. astyleOptionsVector.push_back(getTestDirectory() + "/*.error");
  370. g_console->processOptions(astyleOptionsVector);
  371. // call astyle processFiles()
  372. g_console->processFiles();
  373. // check the file type
  374. EXPECT_EQ(C_TYPE, formatter.getFileType());
  375. }
  376. //----------------------------------------------------------------------------
  377. // AStyle Language Vectors
  378. //----------------------------------------------------------------------------
  379. struct LanguageVectorsF : public ::testing::Test
  380. {
  381. ASFormatter formatter;
  382. vector<string> fileNames;
  383. // build fileNames vector and write the output files
  384. LanguageVectorsF()
  385. {
  386. char textIn[] =
  387. "\nvoid foo()\n"
  388. "{\n"
  389. " bar();\n"
  390. "}\n";
  391. cleanTestDirectory(getTestDirectory());
  392. createConsoleGlobalObject(formatter);
  393. fileNames.push_back(getTestDirectory() + "/getFileType1.cpp");
  394. fileNames.push_back(getTestDirectory() + "/getFileType2.java");
  395. fileNames.push_back(getTestDirectory() + "/getFileType3.cs");
  396. fileNames.push_back(getTestDirectory() + "/getFileType4.error");
  397. for (size_t i = 0; i < fileNames.size(); i++)
  398. {
  399. g_console->standardizePath(fileNames[i]);
  400. createTestFile(fileNames[i], textIn);
  401. }
  402. }
  403. ~LanguageVectorsF()
  404. {
  405. deleteConsoleGlobalObject();
  406. }
  407. };
  408. TEST_F(LanguageVectorsF, FileTypeC)
  409. // Test the language vector setting with *.cpp.
  410. {
  411. assert(g_console != NULL);
  412. g_console->setIsQuiet(true); // change this to see results
  413. // verify initial setting
  414. EXPECT_EQ(9, formatter.getFormatterFileType());
  415. EXPECT_EQ(9, formatter.getBeautifierFileType());
  416. // test language vectors for C++ files
  417. vector<string> astyleOptionsVector;
  418. astyleOptionsVector.push_back(getTestDirectory() + "/*.cpp");
  419. g_console->processOptions(astyleOptionsVector);
  420. g_console->processFiles();
  421. EXPECT_EQ(C_TYPE, formatter.getFormatterFileType());
  422. EXPECT_EQ(C_TYPE, formatter.getBeautifierFileType());
  423. }
  424. TEST_F(LanguageVectorsF, FileTypeJava)
  425. // Test the language vector setting with *.java.
  426. {
  427. assert(g_console != NULL);
  428. g_console->setIsQuiet(true); // change this to see results
  429. // verify initial setting
  430. EXPECT_EQ(9, formatter.getFormatterFileType());
  431. EXPECT_EQ(9, formatter.getBeautifierFileType());
  432. // test language vectors for Java files
  433. vector<string> astyleOptionsVector;
  434. astyleOptionsVector.push_back(getTestDirectory() + "/*.java");
  435. g_console->processOptions(astyleOptionsVector);
  436. g_console->processFiles();
  437. EXPECT_EQ(JAVA_TYPE, formatter.getFormatterFileType());
  438. EXPECT_EQ(JAVA_TYPE, formatter.getBeautifierFileType());
  439. }
  440. TEST_F(LanguageVectorsF, FileTypeSharp)
  441. // Test the language vector setting with *.cs.
  442. {
  443. assert(g_console != NULL);
  444. g_console->setIsQuiet(true); // change this to see results
  445. // verify initial setting
  446. EXPECT_EQ(9, formatter.getFormatterFileType());
  447. EXPECT_EQ(9, formatter.getBeautifierFileType());
  448. // test language vectors for C# files
  449. vector<string> astyleOptionsVector;
  450. astyleOptionsVector.push_back(getTestDirectory() + "/*.cs");
  451. g_console->processOptions(astyleOptionsVector);
  452. g_console->processFiles();
  453. EXPECT_EQ(SHARP_TYPE, formatter.getFormatterFileType());
  454. EXPECT_EQ(SHARP_TYPE, formatter.getBeautifierFileType());
  455. }
  456. TEST(LanguageVectors, MultipleObjects)
  457. // Static language vectors were removed in release 2.02
  458. // to allow multiple ASFormatter objects.
  459. // This was requested by KDevelop to allow multiple objects
  460. // via the factory method.
  461. // This checks initialization of the previously static vectors.
  462. // Additional tests are done by the "GetFileTypeF" and
  463. // "LanguageVectorsF" tests.
  464. {
  465. ASFormatter formatter1;
  466. // Aborted here in Debug for static objects:
  467. // "Assertion `container == __null' failed."
  468. ASFormatter formatter2;
  469. ASFormatter formatter3;
  470. // verify initial value
  471. EXPECT_EQ(9, formatter1.getFormatterFileType());
  472. EXPECT_EQ(9, formatter2.getFormatterFileType());
  473. EXPECT_EQ(9, formatter3.getFormatterFileType());
  474. EXPECT_EQ(9, formatter1.getBeautifierFileType());
  475. EXPECT_EQ(9, formatter2.getBeautifierFileType());
  476. EXPECT_EQ(9, formatter3.getBeautifierFileType());
  477. // initialize formatter1 with C++
  478. stringstream in;
  479. formatter1.setCStyle();
  480. // NOTE: For some reason this will not compile
  481. // with gcc if speed optimizations are used???
  482. // With intel use disable optimizations.
  483. ASStreamIterator<stringstream> streamIterator1(&in);
  484. formatter1.init(&streamIterator1);
  485. // initialize formatter2 with Java
  486. formatter2.setJavaStyle();
  487. ASStreamIterator<stringstream> streamIterator2(&in);
  488. formatter2.init(&streamIterator2);
  489. // initialize formatter3 with C#
  490. formatter3.setSharpStyle();
  491. ASStreamIterator<stringstream> streamIterator3(&in);
  492. formatter3.init(&streamIterator3);
  493. // check the file types
  494. EXPECT_EQ(C_TYPE, formatter1.getFormatterFileType());
  495. EXPECT_EQ(C_TYPE, formatter1.getBeautifierFileType());
  496. EXPECT_EQ(JAVA_TYPE, formatter2.getFormatterFileType());
  497. EXPECT_EQ(JAVA_TYPE, formatter2.getBeautifierFileType());
  498. EXPECT_EQ(SHARP_TYPE, formatter3.getFormatterFileType());
  499. EXPECT_EQ(SHARP_TYPE, formatter3.getBeautifierFileType());
  500. }
  501. //----------------------------------------------------------------------------
  502. // AStyle test recursive option - getFilePaths(), wildcmp(), and fileName vector
  503. //----------------------------------------------------------------------------
  504. struct RecursiveF : public ::testing::Test
  505. {
  506. ASFormatter formatter;
  507. vector<string> fileNames;
  508. // build fileNames vector and write the output files
  509. RecursiveF()
  510. {
  511. char textIn[] =
  512. "\nvoid foo()\n"
  513. "{\n"
  514. "bar();\n"
  515. "}\n";
  516. cleanTestDirectory(getTestDirectory());
  517. createConsoleGlobalObject(formatter);
  518. // create directories
  519. string sub1 = getTestDirectory() + "/subdir1";
  520. string sub1a = getTestDirectory() + "/subdir1/subdir1a";
  521. string sub1b = getTestDirectory() + "/subdir1/subdir1b";
  522. string sub2 = getTestDirectory() + "/subdir2";
  523. g_console->standardizePath(sub1);
  524. g_console->standardizePath(sub1a);
  525. g_console->standardizePath(sub1b);
  526. g_console->standardizePath(sub2);
  527. createTestDirectory(sub1);
  528. createTestDirectory(sub1a);
  529. createTestDirectory(sub1b);
  530. createTestDirectory(sub2);
  531. // create test files
  532. fileNames.push_back(getTestDirectory() + "/recursive1.cpp");
  533. fileNames.push_back(getTestDirectory() + "/recursive2.cpp");
  534. fileNames.push_back(sub1 + "/recursive3.cpp");
  535. fileNames.push_back(sub1 + "/recursive4.cpp");
  536. fileNames.push_back(sub1a + "/recursive5.cpp");
  537. fileNames.push_back(sub1a + "/recursive6.cpp");
  538. fileNames.push_back(sub1b + "/recursive7.cpp");
  539. fileNames.push_back(sub1b + "/recursive8.cpp");
  540. fileNames.push_back(sub2 + "/recursive9.cpp");
  541. fileNames.push_back(sub2 + "/recursive10.cpp");
  542. for (size_t i = 0; i < fileNames.size(); i++)
  543. {
  544. g_console->standardizePath(fileNames[i]);
  545. createTestFile(fileNames[i], textIn);
  546. }
  547. // sort file names for comparison
  548. sort(fileNames.begin(), fileNames.end());
  549. }
  550. ~RecursiveF()
  551. {
  552. deleteConsoleGlobalObject();
  553. }
  554. };
  555. TEST_F(RecursiveF, Default)
  556. // test recursive option
  557. {
  558. assert(g_console != NULL);
  559. g_console->setIsQuiet(true); // change this to see results
  560. g_console->setIsRecursive(true);
  561. // call astyle processOptions()
  562. vector<string> astyleOptionsVector;
  563. astyleOptionsVector.push_back(getTestDirectory() + "/*.cpp");
  564. g_console->processOptions(astyleOptionsVector);
  565. // call astyle processFiles()
  566. g_console->processFiles();
  567. // check the fileName vector
  568. vector<string> fileName = g_console->getFileName();
  569. ASSERT_EQ(fileNames.size(), fileName.size());
  570. for (size_t i = 0; i < fileNames.size(); i++)
  571. EXPECT_EQ(fileNames[i], fileName[i]);
  572. }
  573. TEST_F(RecursiveF, Sans)
  574. // test *.cpp WITHOUT recursive
  575. {
  576. assert(g_console != NULL);
  577. g_console->setIsQuiet(true); // change this to see results
  578. g_console->setIsRecursive(false);
  579. // call astyle processOptions()
  580. vector<string> astyleOptionsVector;
  581. astyleOptionsVector.push_back(getTestDirectory() + "/*.cpp");
  582. g_console->processOptions(astyleOptionsVector);
  583. // call astyle processFiles()
  584. g_console->processFiles();
  585. // delete sub directory files from the fileName vector
  586. size_t searchStart = getTestDirectory().length() + 1;
  587. for (int i = 0; i < (int) fileNames.size(); i++)
  588. {
  589. if (fileNames[i].find_first_of("\\/", searchStart) != string::npos)
  590. {
  591. // cout << fileNames[i] << endl;
  592. vector<string>::iterator iter = fileNames.begin() + i;
  593. fileNames.erase(iter);
  594. i--;
  595. continue;
  596. }
  597. }
  598. // check the files
  599. EXPECT_TRUE(fileNames.size() > 0);
  600. vector<string> fileName = g_console->getFileName();
  601. ASSERT_EQ(fileNames.size(), fileName.size());
  602. for (size_t i = 0; i < fileNames.size(); i++)
  603. EXPECT_EQ(fileNames[i], fileName[i]);
  604. }
  605. TEST_F(RecursiveF, Exclude)
  606. // test recursive option with exclude
  607. {
  608. assert(g_console != NULL);
  609. g_console->setIsQuiet(true); // change this to see results
  610. g_console->setIsRecursive(true);
  611. // call astyle processOptions()
  612. size_t filesExcluded = 0;
  613. vector<string> astyleOptionsVector;
  614. // file
  615. astyleOptionsVector.push_back("--exclude=recursive1.cpp");
  616. filesExcluded += 1;
  617. // directory
  618. astyleOptionsVector.push_back("--exclude=subdir1a");
  619. filesExcluded += 2;
  620. // full path file
  621. astyleOptionsVector.push_back("--exclude=/subdir2/recursive9.cpp");
  622. filesExcluded += 1;
  623. // full path directory
  624. astyleOptionsVector.push_back("--exclude=/subdir1/subdir1b");
  625. filesExcluded += 2;
  626. astyleOptionsVector.push_back(getTestDirectory() + "/*.cpp");
  627. g_console->processOptions(astyleOptionsVector);
  628. // call astyle processFiles()
  629. g_console->processFiles();
  630. // verify excludes
  631. vector<string> fileName = g_console->getFileName();
  632. EXPECT_EQ(fileNames.size() - filesExcluded, fileName.size());
  633. }
  634. TEST_F(RecursiveF, ExcludeErrors)
  635. // test recursive option with unmatched excludes
  636. {
  637. assert(g_console != NULL);
  638. g_console->setIsQuiet(true); // change this to see results
  639. // call astyle processOptions()
  640. vector<string> astyleOptionsVector;
  641. // partial match on file
  642. astyleOptionsVector.push_back("--exclude=ecursive1.cpp");
  643. // partial match on directory
  644. astyleOptionsVector.push_back("--exclude=ubdir1a");
  645. astyleOptionsVector.push_back(getTestDirectory() + "/*.cpp");
  646. g_console->processOptions(astyleOptionsVector);
  647. // error message regular expression
  648. string regex = "Exclude .unmatched. ecursive1.cpp\n"
  649. "Exclude .unmatched. ubdir1a\n"
  650. "Did you intend to use --recursive";
  651. // cannot use death test with leak finder
  652. #if GTEST_HAS_DEATH_TEST && !LEAK_FINDER
  653. // test processFiles with unmatched excludes
  654. EXPECT_EXIT(g_console->processFiles(),
  655. ::testing::ExitedWithCode(EXIT_FAILURE),
  656. regex);
  657. #endif
  658. }
  659. TEST_F(RecursiveF, ExcludeErrorsRecursive)
  660. // test recursive option with unmatched excludes and recursive option
  661. {
  662. assert(g_console != NULL);
  663. g_console->setIsQuiet(true); // change this to see results
  664. g_console->setIsRecursive(true);
  665. // call astyle processOptions()
  666. vector<string> astyleOptionsVector;
  667. // partial match on file
  668. astyleOptionsVector.push_back("--exclude=ecursive1.cpp");
  669. // partial match on directory
  670. astyleOptionsVector.push_back("--exclude=ubdir1a");
  671. astyleOptionsVector.push_back(getTestDirectory() + "/*.cpp");
  672. g_console->processOptions(astyleOptionsVector);
  673. // error message regular expression
  674. string regex = "Exclude .unmatched. ecursive1.cpp\n"
  675. "Exclude .unmatched. ubdir1a";
  676. // cannot use death test with leak finder
  677. #if GTEST_HAS_DEATH_TEST && !LEAK_FINDER
  678. // test processFiles with unmatched excludes
  679. EXPECT_EXIT(g_console->processFiles(),
  680. ::testing::ExitedWithCode(EXIT_FAILURE),
  681. regex);
  682. #endif
  683. }
  684. TEST_F(RecursiveF, HiddenFiles)
  685. // test recursive option with hidden files
  686. {
  687. assert(g_console != NULL);
  688. g_console->setIsQuiet(true); // change this to see results
  689. g_console->setIsRecursive(true);
  690. // write the hidden files
  691. char textIn[] = "void foo(){}\n";
  692. // hidden on Linux and Windows
  693. string dirOut1 = getTestDirectory() + "/.hidden";
  694. g_console->standardizePath(dirOut1);
  695. createTestDirectory(dirOut1);
  696. string fileOut1 = getTestDirectory() + "/.hidden/hidden1.cpp";
  697. g_console->standardizePath(fileOut1);
  698. createTestFile(fileOut1, textIn);
  699. string fileOut2 = getTestDirectory() + "/.hidden2.cpp";
  700. g_console->standardizePath(fileOut2);
  701. createTestFile(fileOut2, textIn);
  702. // hidden on Windows only
  703. #ifdef _WIN32
  704. string dirOut1w = getTestDirectory() + "/readonlywin";
  705. g_console->standardizePath(dirOut1w);
  706. createTestDirectory(dirOut1w);
  707. SetFileAttributes(dirOut1w.c_str(), FILE_ATTRIBUTE_READONLY );
  708. string fileOut1w = getTestDirectory() + "/readonlywin/readonly1win.cpp";
  709. g_console->standardizePath(fileOut1w);
  710. createTestFile(fileOut1w, textIn);
  711. string fileOut2w = getTestDirectory() + "/hidden2win.cpp";
  712. g_console->standardizePath(fileOut2w);
  713. createTestFile(fileOut2w, textIn);
  714. SetFileAttributes(fileOut2w.c_str(), FILE_ATTRIBUTE_HIDDEN);
  715. string fileOut3w = getTestDirectory() + "/readonly2win.cpp";
  716. g_console->standardizePath(fileOut3w);
  717. createTestFile(fileOut3w, textIn);
  718. SetFileAttributes(fileOut3w.c_str(), FILE_ATTRIBUTE_READONLY);
  719. #endif
  720. // call astyle processOptions()
  721. vector<string> astyleOptionsVector;
  722. astyleOptionsVector.push_back(getTestDirectory() + "/*.cpp");
  723. g_console->processOptions(astyleOptionsVector);
  724. // call astyle processFiles()
  725. g_console->processFiles();
  726. // hidden files should not be in g_console vector
  727. vector<string> fileName = g_console->getFileName();
  728. EXPECT_EQ(fileNames.size(), fileName.size());
  729. #ifdef _WIN32
  730. // reset file attributes
  731. SetFileAttributes(dirOut1w.c_str(), FILE_ATTRIBUTE_NORMAL);
  732. SetFileAttributes(fileOut2w.c_str(), FILE_ATTRIBUTE_NORMAL);
  733. SetFileAttributes(fileOut3w.c_str(), FILE_ATTRIBUTE_NORMAL);
  734. #endif
  735. }
  736. //----------------------------------------------------------------------------
  737. // AStyle line ends formatted
  738. // tests if a line end change formats the file
  739. //----------------------------------------------------------------------------
  740. struct LineEndsFormattedF : public ::testing::Test
  741. {
  742. ASFormatter formatter;
  743. vector<string> fileNames;
  744. string textLinuxStr;
  745. string textWindowsStr;
  746. string textMacOldStr;
  747. const char* textLinux;
  748. const char* textWindows;
  749. const char* textMacOld;
  750. string pathLinux;
  751. string pathWindows;
  752. string pathMacOld;
  753. // build fileNames vector and write the output files
  754. LineEndsFormattedF()
  755. {
  756. textLinuxStr = // has macold line ends
  757. "\rvoid foo()\r"
  758. "{\r"
  759. " bar()\r"
  760. "}\r";
  761. textWindowsStr = // has linux line ends
  762. "\nvoid foo()\n"
  763. "{\n"
  764. " bar()\n"
  765. "}\n";
  766. textMacOldStr = // has windows line ends
  767. "\r\nvoid foo()\r\n"
  768. "{\r\n"
  769. " bar()\r\n"
  770. "}\r\n";
  771. cleanTestDirectory(getTestDirectory());
  772. createConsoleGlobalObject(formatter);
  773. // build text strings
  774. textLinux = textLinuxStr.c_str();
  775. textWindows = textWindowsStr.c_str();
  776. textMacOld = textMacOldStr.c_str();
  777. // create test files
  778. pathLinux = getTestDirectory() + "/textLinux.cpp";
  779. g_console->standardizePath(pathLinux);
  780. createTestFile(pathLinux, textLinux);
  781. pathWindows = getTestDirectory() + "/textWindows.cpp";
  782. g_console->standardizePath(pathWindows);
  783. createTestFile(pathWindows, textWindows);
  784. pathMacOld = getTestDirectory() + "/textMac.cpp";
  785. g_console->standardizePath(pathMacOld);
  786. createTestFile(pathMacOld, textMacOld);
  787. }
  788. ~LineEndsFormattedF()
  789. {
  790. deleteConsoleGlobalObject();
  791. }
  792. };
  793. TEST_F(LineEndsFormattedF, LineEndWindows)
  794. // test if lineend=windows formats the file
  795. {
  796. assert(g_console != NULL);
  797. g_console->setIsQuiet(true); // change this to see results
  798. // call astyle processOptions()
  799. vector<string> astyleOptionsVector;
  800. astyleOptionsVector.push_back("--lineend=windows");
  801. astyleOptionsVector.push_back(pathWindows);
  802. g_console->processOptions(astyleOptionsVector);
  803. // call astyle processFiles()
  804. g_console->processFiles();
  805. // check for .orig file
  806. string origFileName = pathWindows + ".orig";
  807. struct stat stBuf;
  808. // display error if file is not present
  809. if (stat(origFileName.c_str(), &stBuf) == -1)
  810. EXPECT_EQ("\"no file\"", origFileName.c_str());
  811. }
  812. TEST_F(LineEndsFormattedF, LineEndLinux)
  813. // test if lineend=linux formats the file
  814. {
  815. assert(g_console != NULL);
  816. g_console->setIsQuiet(true); // change this to see results
  817. // call astyle processOptions()
  818. vector<string> astyleOptionsVector;
  819. astyleOptionsVector.push_back("--lineend=linux");
  820. astyleOptionsVector.push_back(pathLinux);
  821. g_console->processOptions(astyleOptionsVector);
  822. // call astyle processFiles()
  823. g_console->processFiles();
  824. // check for .orig file
  825. string origFileName = pathLinux + ".orig";
  826. struct stat stBuf;
  827. // display error if file is not present
  828. if (stat(origFileName.c_str(), &stBuf) == -1)
  829. EXPECT_EQ("\"no file\"", origFileName.c_str());
  830. }
  831. TEST_F(LineEndsFormattedF, LineEndMacOld)
  832. // test if lineend=macold formats the file
  833. {
  834. assert(g_console != NULL);
  835. g_console->setIsQuiet(true); // change this to see results
  836. // call astyle processOptions()
  837. vector<string> astyleOptionsVector;
  838. astyleOptionsVector.push_back("--lineend=macold");
  839. astyleOptionsVector.push_back(pathMacOld);
  840. g_console->processOptions(astyleOptionsVector);
  841. // call astyle processFiles()
  842. g_console->processFiles();
  843. // check for .orig file
  844. string origFileName = pathMacOld + ".orig";
  845. struct stat stBuf;
  846. // display error if file is not present
  847. if (stat(origFileName.c_str(), &stBuf) == -1)
  848. EXPECT_EQ("\"no file\"", origFileName.c_str());
  849. }
  850. //----------------------------------------------------------------------------
  851. // AStyle line ends unchanged
  852. // tests if an unchanged line end leaves the file unchanged
  853. //----------------------------------------------------------------------------
  854. struct LineEndsUnchangedF : public ::testing::Test
  855. {
  856. ASFormatter formatter;
  857. vector<string> fileNames;
  858. string textLinuxStr;
  859. string textWindowsStr;
  860. string textMacOldStr;
  861. const char* textLinux;
  862. const char* textWindows;
  863. const char* textMacOld;
  864. string pathLinux;
  865. string pathWindows;
  866. string pathMacOld;
  867. // build fileNames vector and write the output files
  868. LineEndsUnchangedF()
  869. {
  870. textLinuxStr = // has linux line ends
  871. "\nvoid foo()\n"
  872. "{\n"
  873. " bar()\n"
  874. "}\n";
  875. textWindowsStr = // has windows line ends
  876. "\r\nvoid foo()\r\n"
  877. "{\r\n"
  878. " bar()\r\n"
  879. "}\r\n";
  880. textMacOldStr = // has macold line ends
  881. "\rvoid foo()\r"
  882. "{\r"
  883. " bar()\r"
  884. "}\r";
  885. cleanTestDirectory(getTestDirectory());
  886. createConsoleGlobalObject(formatter);
  887. // build text strings
  888. textLinux = textLinuxStr.c_str();
  889. textWindows = textWindowsStr.c_str();
  890. textMacOld = textMacOldStr.c_str();
  891. // create test files
  892. pathLinux = getTestDirectory() + "/textLinux.cpp";
  893. g_console->standardizePath(pathLinux);
  894. createTestFile(pathLinux, textLinux);
  895. pathWindows = getTestDirectory() + "/textWindows.cpp";
  896. g_console->standardizePath(pathWindows);
  897. createTestFile(pathWindows, textWindows);
  898. pathMacOld = getTestDirectory() + "/textMac.cpp";
  899. g_console->standardizePath(pathMacOld);
  900. createTestFile(pathMacOld, textMacOld);
  901. }
  902. ~LineEndsUnchangedF()
  903. {
  904. deleteConsoleGlobalObject();
  905. }
  906. };
  907. TEST_F(LineEndsUnchangedF, LineEndWindows)
  908. // test if lineend=windows leaves the file unchanged
  909. {
  910. assert(g_console != NULL);
  911. g_console->setIsQuiet(true); // change this to see results
  912. // call astyle processOptions()
  913. vector<string> astyleOptionsVector;
  914. astyleOptionsVector.push_back("--lineend=windows");
  915. astyleOptionsVector.push_back(pathMacOld);
  916. g_console->processOptions(astyleOptionsVector);
  917. // call astyle processFiles()
  918. g_console->processFiles();
  919. // check for .orig file
  920. string origFileName = pathWindows + ".orig";
  921. struct stat stBuf;
  922. // display error if file is present
  923. if (stat(origFileName.c_str(), &stBuf) != -1)
  924. EXPECT_EQ("\"no file\"", origFileName.c_str());
  925. }
  926. TEST_F(LineEndsUnchangedF, LineEndLinux)
  927. // test if lineend=linux leaves the file unchanged
  928. {
  929. assert(g_console != NULL);
  930. g_console->setIsQuiet(true); // change this to see results
  931. // call astyle processOptions()
  932. vector<string> astyleOptionsVector;
  933. astyleOptionsVector.push_back("--lineend=linux");
  934. astyleOptionsVector.push_back(pathWindows);
  935. g_console->processOptions(astyleOptionsVector);
  936. // call astyle processFiles()
  937. g_console->processFiles();
  938. // check for .orig file
  939. string origFileName = pathLinux + ".orig";
  940. struct stat stBuf;
  941. // display error if file is present
  942. if (stat(origFileName.c_str(), &stBuf) != -1)
  943. EXPECT_EQ("\"no file\"", origFileName.c_str());
  944. }
  945. TEST_F(LineEndsUnchangedF, LineEndMacOld)
  946. // test if lineend=macold leaves the file unchanged
  947. {
  948. assert(g_console != NULL);
  949. g_console->setIsQuiet(true); // change this to see results
  950. // call astyle processOptions()
  951. vector<string> astyleOptionsVector;
  952. astyleOptionsVector.push_back("--lineend=macold");
  953. astyleOptionsVector.push_back(pathLinux);
  954. g_console->processOptions(astyleOptionsVector);
  955. // call astyle processFiles()
  956. g_console->processFiles();
  957. // check for .orig file
  958. string origFileName = pathMacOld + ".orig";
  959. struct stat stBuf;
  960. // display error if file is present
  961. if (stat(origFileName.c_str(), &stBuf) != -1)
  962. EXPECT_EQ("\"no file\"", origFileName.c_str());
  963. }
  964. //----------------------------------------------------------------------------
  965. // AStyle default line ends
  966. // tests if default line end leaves the file unchanged
  967. //----------------------------------------------------------------------------
  968. struct LineEndsDefaultF : public ::testing::Test
  969. {
  970. ASFormatter formatter;
  971. vector<string> fileNames;
  972. string textLinuxStr;
  973. string textWindowsStr;
  974. string textMacOldStr;
  975. const char* textLinux;
  976. const char* textWindows;
  977. const char* textMacOld;
  978. string pathLinux;
  979. string pathWindows;
  980. string pathMacOld;
  981. // build fileNames vector and write the output files
  982. LineEndsDefaultF()
  983. {
  984. textLinuxStr = // has linux line ends
  985. "\nvoid foo()\n"
  986. "{\n"
  987. " bar()\n"
  988. "}\n";
  989. textWindowsStr = // has windows line ends
  990. "\r\nvoid foo()\r\n"
  991. "{\r\n"
  992. " bar()\r\n"
  993. "}\r\n";
  994. textMacOldStr = // has macold line ends
  995. "\rvoid foo()\r"
  996. "{\r"
  997. " bar()\r"
  998. "}\r";
  999. cleanTestDirectory(getTestDirectory());
  1000. createConsoleGlobalObject(formatter);
  1001. // build text strings
  1002. textLinux = textLinuxStr.c_str();
  1003. textWindows = textWindowsStr.c_str();
  1004. textMacOld = textMacOldStr.c_str();
  1005. // create test files
  1006. pathLinux = getTestDirectory() + "/textLinux.cpp";
  1007. g_console->standardizePath(pathLinux);
  1008. createTestFile(pathLinux, textLinux);
  1009. pathWindows = getTestDirectory() + "/textWindows.cpp";
  1010. g_console->standardizePath(pathWindows);
  1011. createTestFile(pathWindows, textWindows);
  1012. pathMacOld = getTestDirectory() + "/textMac.cpp";
  1013. g_console->standardizePath(pathMacOld);
  1014. createTestFile(pathMacOld, textMacOld);
  1015. }
  1016. ~LineEndsDefaultF()
  1017. {
  1018. deleteConsoleGlobalObject();
  1019. }
  1020. };
  1021. TEST_F(LineEndsDefaultF, LineEndWindows)
  1022. // test if default line end leaves a windows file unchanged
  1023. {
  1024. assert(g_console != NULL);
  1025. g_console->setIsQuiet(true); // change this to see results
  1026. // call astyle processOptions()
  1027. vector<string> astyleOptionsVector;
  1028. astyleOptionsVector.push_back(pathWindows);
  1029. g_console->processOptions(astyleOptionsVector);
  1030. // call astyle processFiles()
  1031. g_console->processFiles();
  1032. // check for .orig file
  1033. string origFileName = pathWindows + ".orig";
  1034. struct stat stBuf;
  1035. // display error if file is present
  1036. if (stat(origFileName.c_str(), &stBuf) != -1)
  1037. EXPECT_EQ("\"no file\"", origFileName.c_str());
  1038. }
  1039. TEST_F(LineEndsDefaultF, LineEndLinux)
  1040. // test if default line end leaves a linux file unchanged
  1041. {
  1042. assert(g_console != NULL);
  1043. g_console->setIsQuiet(true); // change this to see results
  1044. // call astyle processOptions()
  1045. vector<string> astyleOptionsVector;
  1046. astyleOptionsVector.push_back(pathLinux);
  1047. g_console->processOptions(astyleOptionsVector);
  1048. // call astyle processFiles()
  1049. g_console->processFiles();
  1050. // check for .orig file
  1051. string origFileName = pathLinux + ".orig";
  1052. struct stat stBuf;
  1053. // display error if file is present
  1054. if (stat(origFileName.c_str(), &stBuf) != -1)
  1055. EXPECT_EQ("\"no file\"", origFileName.c_str());
  1056. }
  1057. TEST_F(LineEndsDefaultF, LineEndMacOld)
  1058. // test if default line end leaves a macold file unchanged
  1059. {
  1060. assert(g_console != NULL);
  1061. g_console->setIsQuiet(true); // change this to see results
  1062. // call astyle processOptions()
  1063. vector<string> astyleOptionsVector;
  1064. astyleOptionsVector.push_back(pathMacOld);
  1065. g_console->processOptions(astyleOptionsVector);
  1066. // call astyle processFiles()
  1067. g_console->processFiles();
  1068. // check for .orig file
  1069. string origFileName = pathMacOld + ".orig";
  1070. struct stat stBuf;
  1071. // display error if file is present
  1072. if (stat(origFileName.c_str(), &stBuf) != -1)
  1073. EXPECT_EQ("\"no file\"", origFileName.c_str());
  1074. }
  1075. //----------------------------------------------------------------------------
  1076. // AStyle line ends formatted
  1077. // tests if default line ends calls the convertLineEnds() function when needed
  1078. //----------------------------------------------------------------------------
  1079. struct LineEndsDefaultMixedF : public ::testing::Test
  1080. {
  1081. ASFormatter formatter;
  1082. vector<string> fileNames;
  1083. string textLinuxStr;
  1084. string textWindowsStr;
  1085. string textMacOldStr;
  1086. const char* textLinux;
  1087. const char* textWindows;
  1088. const char* textMacOld;
  1089. string pathLinux;
  1090. string pathWindows;
  1091. string pathMacOld;
  1092. // build fileNames vector and write the output files
  1093. LineEndsDefaultMixedF()
  1094. {
  1095. textLinuxStr =
  1096. "\r\nvoid foo()\r\n" // not a linux line end
  1097. "{\n"
  1098. " bar1()\n"
  1099. " bar2()\n"
  1100. "}\n";
  1101. textWindowsStr =
  1102. "\rvoid foo()\r" // not a windows line end
  1103. "{\r\n"
  1104. " bar1()\r\n"
  1105. " bar2()\r\n"
  1106. "}\r\n";
  1107. textMacOldStr =
  1108. "\r\nvoid foo()\r\n" // not a macold line end
  1109. "{\r"
  1110. " bar1()\r"
  1111. " bar2()\r"
  1112. "}\r";
  1113. cleanTestDirectory(getTestDirectory());
  1114. createConsoleGlobalObject(formatter);
  1115. // build text strings
  1116. textLinux = textLinuxStr.c_str();
  1117. textWindows = textWindowsStr.c_str();
  1118. textMacOld = textMacOldStr.c_str();
  1119. // create test files
  1120. pathLinux = getTestDirectory() + "/textLinux.cpp";
  1121. g_console->standardizePath(pathLinux);
  1122. createTestFile(pathLinux, textLinux);
  1123. pathWindows = getTestDirectory() + "/textWindows.cpp";
  1124. g_console->standardizePath(pathWindows);
  1125. createTestFile(pathWindows, textWindows);
  1126. pathMacOld = getTestDirectory() + "/textMac.cpp";
  1127. g_console->standardizePath(pathMacOld);
  1128. createTestFile(pathMacOld, textMacOld);
  1129. }
  1130. ~LineEndsDefaultMixedF()
  1131. {
  1132. deleteConsoleGlobalObject();
  1133. }
  1134. };
  1135. TEST_F(LineEndsDefaultMixedF, LineEndWindows)
  1136. // test if default line ends outputs windows line ends by calling convertLineEnds()
  1137. {
  1138. assert(g_console != NULL);
  1139. g_console->setIsQuiet(true); // change this to see results
  1140. // call astyle processOptions()
  1141. vector<string> astyleOptionsVector;
  1142. astyleOptionsVector.push_back(pathWindows);
  1143. g_console->processOptions(astyleOptionsVector);
  1144. // call astyle processFiles()
  1145. g_console->processFiles();
  1146. // check for .orig file
  1147. string origFileName = pathWindows + ".orig";
  1148. struct stat stBuf;
  1149. // display error if file is not present
  1150. if (stat(origFileName.c_str(), &stBuf) == -1)
  1151. EXPECT_EQ("\"no file\"", origFileName.c_str());
  1152. // check that convertLineEnds is called
  1153. EXPECT_TRUE(g_console->getLineEndsMixed());
  1154. // the line ends must be checked manually
  1155. // systemPause("Check Windows Line Ends");
  1156. }
  1157. TEST_F(LineEndsDefaultMixedF, LineEndLinux)
  1158. // test if default line ends outputs linux line ends by calling convertLineEnds()
  1159. {
  1160. assert(g_console != NULL);
  1161. g_console->setIsQuiet(true); // change this to see results
  1162. // call astyle processOptions()
  1163. vector<string> astyleOptionsVector;
  1164. astyleOptionsVector.push_back(pathLinux);
  1165. g_console->processOptions(astyleOptionsVector);
  1166. // call astyle processFiles()
  1167. g_console->processFiles();
  1168. // check for .orig file
  1169. string origFileName = pathLinux + ".orig";
  1170. struct stat stBuf;
  1171. // display error if file is not present
  1172. if (stat(origFileName.c_str(), &stBuf) == -1)
  1173. EXPECT_EQ("\"no file\"", origFileName.c_str());
  1174. // check that convertLineEnds is called
  1175. EXPECT_TRUE(g_console->getLineEndsMixed());
  1176. // the line ends must be checked manually
  1177. // systemPause("Check Linux Line Ends");
  1178. }
  1179. TEST_F(LineEndsDefaultMixedF, LineEndMacOld)
  1180. // test if default line ends outputs linux line ends by calling convertLineEnds()
  1181. {
  1182. assert(g_console != NULL);
  1183. g_console->setIsQuiet(true); // change this to see results
  1184. // call astyle processOptions()
  1185. vector<string> astyleOptionsVector;
  1186. astyleOptionsVector.push_back(pathMacOld);
  1187. g_console->processOptions(astyleOptionsVector);
  1188. // call astyle processFiles()
  1189. g_console->processFiles();
  1190. // check for .orig file
  1191. string origFileName = pathMacOld + ".orig";
  1192. struct stat stBuf;
  1193. // display error if file is not present
  1194. if (stat(origFileName.c_str(), &stBuf) == -1)
  1195. EXPECT_EQ("\"no file\"", origFileName.c_str());
  1196. // check that convertLineEnds is called
  1197. EXPECT_TRUE(g_console->getLineEndsMixed());
  1198. // the line ends must be checked manually
  1199. // systemPause("Check MacOld Line Ends");
  1200. }
  1201. //----------------------------------------------------------------------------
  1202. // AStyle line ends formatted
  1203. // tests that default line ends does NOT call the convertLineEnds() function when not needed
  1204. //----------------------------------------------------------------------------
  1205. struct LineEndsDefaultMixedSansF : public ::testing::Test
  1206. {
  1207. ASFormatter formatter;
  1208. vector<string> fileNames;
  1209. string textLinuxStr;
  1210. string textWindowsStr;
  1211. string textMacOldStr;
  1212. const char* textLinux;
  1213. const char* textWindows;
  1214. const char* textMacOld;
  1215. string pathLinux;
  1216. string pathWindows;
  1217. string pathMacOld;
  1218. // build fileNames vector and write the output files
  1219. LineEndsDefaultMixedSansF()
  1220. {
  1221. textLinuxStr =
  1222. "\nvoid foo()\n"
  1223. "{\n"
  1224. " bar()\r\n" // not a linux line end
  1225. "}\n";
  1226. textWindowsStr =
  1227. "\r\nvoid foo()\r\n"
  1228. "{\r\n"
  1229. " bar()\r" // not a windows line end
  1230. "}\r\n";
  1231. textMacOldStr =
  1232. "\rvoid foo()\r"
  1233. "{\r"
  1234. " bar()\r\n" // not a macold line end
  1235. "}\r";
  1236. cleanTestDirectory(getTestDirectory());
  1237. createConsoleGlobalObject(formatter);
  1238. // build text strings
  1239. textLinux = textLinuxStr.c_str();
  1240. textWindows = textWindowsStr.c_str();
  1241. textMacOld = textMacOldStr.c_str();
  1242. // create test files
  1243. pathLinux = getTestDirectory() + "/textLinux.cpp";
  1244. g_console->standardizePath(pathLinux);
  1245. createTestFile(pathLinux, textLinux);
  1246. pathWindows = getTestDirectory() + "/textWindows.cpp";
  1247. g_console->standardizePath(pathWindows);
  1248. createTestFile(pathWindows, textWindows);
  1249. pathMacOld = getTestDirectory() + "/textMac.cpp";
  1250. g_console->standardizePath(pathMacOld);
  1251. createTestFile(pathMacOld, textMacOld);
  1252. }
  1253. ~LineEndsDefaultMixedSansF()
  1254. {
  1255. deleteConsoleGlobalObject();
  1256. }
  1257. };
  1258. TEST_F(LineEndsDefaultMixedSansF, LineEndWindows)
  1259. // test if default line ends outputs windows line ends without calling convertLineEnds()
  1260. {
  1261. assert(g_console != NULL);
  1262. g_console->setIsQuiet(true); // change this to see results
  1263. // call astyle processOptions()
  1264. vector<string> astyleOptionsVector;
  1265. astyleOptionsVector.push_back(pathWindows);
  1266. g_console->processOptions(astyleOptionsVector);
  1267. // call astyle processFiles()
  1268. g_console->processFiles();
  1269. // check for .orig file
  1270. string origFileName = pathWindows + ".orig";
  1271. struct stat stBuf;
  1272. // display error if file is not present
  1273. if (stat(origFileName.c_str(), &stBuf) == -1)
  1274. EXPECT_EQ("\"no file\"", origFileName.c_str());
  1275. // check that convertLineEnds is NOT called
  1276. EXPECT_FALSE(g_console->getLineEndsMixed());
  1277. // the line ends must be checked manually
  1278. // systemPause("Check Windows Line Ends");
  1279. }
  1280. TEST_F(LineEndsDefaultMixedSansF, LineEndLinux)
  1281. // test if default line ends outputs linux line ends without calling convertLineEnds()
  1282. {
  1283. assert(g_console != NULL);
  1284. g_console->setIsQuiet(true); // change this to see results
  1285. // call astyle processOptions()
  1286. vector<string> astyleOptionsVector;
  1287. astyleOptionsVector.push_back(pathLinux);
  1288. g_console->processOptions(astyleOptionsVector);
  1289. // call astyle processFiles()
  1290. g_console->processFiles();
  1291. // check for .orig file
  1292. string origFileName = pathLinux + ".orig";
  1293. struct stat stBuf;
  1294. // display error if file is not present
  1295. if (stat(origFileName.c_str(), &stBuf) == -1)
  1296. EXPECT_EQ("\"no file\"", origFileName.c_str());
  1297. // check that convertLineEnds is NOT called
  1298. EXPECT_FALSE(g_console->getLineEndsMixed());
  1299. // the line ends must be checked manually
  1300. // systemPause("Check Linux Line Ends");
  1301. }
  1302. TEST_F(LineEndsDefaultMixedSansF, LineEndMacOld)
  1303. // test if default line ends outputs linux line ends without calling convertLineEnds()
  1304. {
  1305. assert(g_console != NULL);
  1306. g_console->setIsQuiet(true); // change this to see results
  1307. // call astyle processOptions()
  1308. vector<string> astyleOptionsVector;
  1309. astyleOptionsVector.push_back(pathMacOld);
  1310. g_console->processOptions(astyleOptionsVector);
  1311. // call astyle processFiles()
  1312. g_console->processFiles();
  1313. // check for .orig file
  1314. string origFileName = pathMacOld + ".orig";
  1315. struct stat stBuf;
  1316. // display error if file is not present
  1317. if (stat(origFileName.c_str(), &stBuf) == -1)
  1318. EXPECT_EQ("\"no file\"", origFileName.c_str());
  1319. // check that convertLineEnds is NOT called
  1320. EXPECT_FALSE(g_console->getLineEndsMixed());
  1321. // the line ends must be checked manually
  1322. // systemPause("Check MacOld Line Ends");
  1323. }
  1324. //----------------------------------------------------------------------------
  1325. // AStyle other tests
  1326. //----------------------------------------------------------------------------
  1327. TEST(Other, MingwFileGlobbing)
  1328. // test that MinGW file globbing is turned OFF
  1329. {
  1330. // _CRT_glob is a global variable defined in astyle_main.cpp
  1331. // will get a link error if it is not defined in the GLOBAL namespace
  1332. EXPECT_TRUE(_CRT_glob == 0);
  1333. }
  1334. TEST(Other, ErrorExit)
  1335. // test the error exit without message
  1336. {
  1337. ASFormatter formatter;
  1338. createConsoleGlobalObject(formatter);
  1339. // cannot use death test with leak finder
  1340. #if GTEST_HAS_DEATH_TEST && !LEAK_FINDER
  1341. // death test without error message
  1342. EXPECT_EXIT(g_console->error(),
  1343. ::testing::ExitedWithCode(EXIT_FAILURE),
  1344. "\nArtistic Style "); // "Artistic Style has terminated!"
  1345. #endif
  1346. deleteConsoleGlobalObject();
  1347. }
  1348. TEST(Other, ErrorExitWihMessage)
  1349. // test the error exit with message
  1350. {
  1351. ASFormatter formatter;
  1352. createConsoleGlobalObject(formatter);
  1353. // cannot use death test with leak finder
  1354. #if GTEST_HAS_DEATH_TEST && !LEAK_FINDER
  1355. // death test with error message
  1356. EXPECT_EXIT(g_console->error("why", "what"),
  1357. ::testing::ExitedWithCode(EXIT_FAILURE),
  1358. "why what\n\nArtistic Style "); // "Artistic Style has terminated!"
  1359. #endif
  1360. deleteConsoleGlobalObject();
  1361. }
  1362. TEST(Other, VerifyCinPeek)
  1363. // test the verifyCinPeek() method for failure.
  1364. {
  1365. ASFormatter formatter;
  1366. createConsoleGlobalObject(formatter);
  1367. // cannot use death test with leak finder
  1368. #if GTEST_HAS_DEATH_TEST && !LEAK_FINDER
  1369. // set the position of the get pointer past the end of file.
  1370. cin.seekg (1, ios_base::end);
  1371. EXPECT_EXIT(g_console->verifyCinPeek(),
  1372. ::testing::ExitedWithCode(EXIT_FAILURE),
  1373. "\nArtistic Style "); // "Artistic Style has terminated!"
  1374. #endif
  1375. deleteConsoleGlobalObject();
  1376. }
  1377. //----------------------------------------------------------------------------
  1378. // AStyle BugFix tests
  1379. //--------------------------------------------…

Large files files are truncated, but you can click here to view the full file