testVisualStudioSlnParser.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "testVisualStudioSlnParser.h"
  2. #include "cmVisualStudioSlnData.h"
  3. #include "cmVisualStudioSlnParser.h"
  4. #include <cmsys/ios/iostream>
  5. //----------------------------------------------------------------------------
  6. static bool parsedRight(cmVisualStudioSlnParser& parser,
  7. const std::string& file, cmSlnData& data,
  8. cmVisualStudioSlnParser::ParseResult expected =
  9. cmVisualStudioSlnParser::ResultOK)
  10. {
  11. if (parser.ParseFile(SOURCE_DIR "/testVisualStudioSlnParser_data/" + file
  12. + "." SLN_EXTENSION,
  13. data, cmVisualStudioSlnParser::DataGroupProjects))
  14. {
  15. if (expected == cmVisualStudioSlnParser::ResultOK)
  16. {
  17. return true;
  18. }
  19. }
  20. else
  21. {
  22. if (parser.GetParseResult() == expected)
  23. {
  24. return true;
  25. }
  26. }
  27. cmsys_ios::cerr << "cmVisualStudioSlnParser mis-parsed " << file
  28. << "." SLN_EXTENSION << "; expected result " << expected
  29. << ", got " << parser.GetParseResult()
  30. << cmsys_ios::endl;
  31. return false;
  32. }
  33. //----------------------------------------------------------------------------
  34. int testVisualStudioSlnParser(int, char*[])
  35. {
  36. cmVisualStudioSlnParser parser;
  37. // Test clean parser
  38. if (parser.GetParseResult() != cmVisualStudioSlnParser::ResultOK)
  39. {
  40. cmsys_ios::cerr << "cmVisualStudioSlnParser initialisation failed"
  41. << cmsys_ios::endl;
  42. return 1;
  43. }
  44. // Test parsing valid sln
  45. {
  46. cmSlnData data;
  47. if (!parsedRight(parser, "valid", data))
  48. {
  49. return 1;
  50. }
  51. const std::vector<cmSlnProjectEntry>& projects = data.GetProjects();
  52. const char * const names[] =
  53. {
  54. "3rdParty", "ALL_BUILD", "CMakeLib", "CMakeLibTests",
  55. "CMakePredefinedTargets", "CPackLib", "CTestDashboardTargets", "CTestLib",
  56. "Continuous", "Documentation",
  57. "Experimental", "INSTALL", "KWSys", "LIBCURL", "Nightly",
  58. "NightlyMemoryCheck", "PACKAGE", "RUN_TESTS", "Tests", "Utilities",
  59. "Win9xCompat", "ZERO_CHECK", "cmIML_test", "cmake", "cmbzip2", "cmcldeps",
  60. "cmcompress", "cmcurl", "cmexpat", "cmlibarchive", "cmsys",
  61. "cmsysEncodeExecutable", "cmsysProcessFwd9x", "cmsysTestDynload",
  62. "cmsysTestProcess", "cmsysTestSharedForward", "cmsysTestsC",
  63. "cmsysTestsCxx", "cmsys_c", "cmw9xcom", "cmzlib", "cpack", "ctest",
  64. "documentation", "memcheck_fail", "pseudo_BC", "pseudo_purify",
  65. "pseudo_valgrind", "test_clean", "uninstall"
  66. };
  67. const size_t expectedProjectCount = sizeof(names) / sizeof(*names);
  68. if (projects.size() != expectedProjectCount)
  69. {
  70. cmsys_ios::cerr << "cmVisualStudioSlnParser returned bad number of "
  71. << "projects (" << projects.size() << " instead of "
  72. << expectedProjectCount << ')'
  73. << cmsys_ios::endl;
  74. return 1;
  75. }
  76. for (size_t idx = 0; idx < expectedProjectCount; ++idx)
  77. {
  78. if (projects[idx].GetName() != names[idx])
  79. {
  80. cmsys_ios::cerr << "cmVisualStudioSlnParser returned bad project #"
  81. << idx << "; expected \"" << names[idx] << "\", got \""
  82. << projects[idx].GetName() << '"'
  83. << cmsys_ios::endl;
  84. return 1;
  85. }
  86. }
  87. if (projects[0].GetRelativePath() != "Utilities\\3rdParty")
  88. {
  89. cmsys_ios::cerr << "cmVisualStudioSlnParser returned bad relative path of "
  90. << "project 3rdParty; expected \"Utilities\\3rdParty\", "
  91. << "got \"" << projects[0].GetRelativePath() << '"'
  92. << cmsys_ios::endl;
  93. return 1;
  94. }
  95. if (projects[2].GetGUID() != "{59BCCCCD-3AD1-4491-B8F4-C5793AC007E2}")
  96. {
  97. cmsys_ios::cerr << "cmVisualStudioSlnParser returned bad relative path of "
  98. << "project CMakeLib; expected "
  99. << "\"{59BCCCCD-3AD1-4491-B8F4-C5793AC007E2}\", "
  100. << "got \"" << projects[2].GetGUID() << '"'
  101. << cmsys_ios::endl;
  102. return 1;
  103. }
  104. }
  105. // Test BOM parsing
  106. {
  107. cmSlnData data;
  108. if (!parsedRight(parser, "bom", data))
  109. {
  110. return 1;
  111. }
  112. if (!parser.GetParseHadBOM())
  113. {
  114. cmsys_ios::cerr << "cmVisualStudioSlnParser didn't find BOM in bom."
  115. << SLN_EXTENSION
  116. << cmsys_ios::endl;
  117. return 1;
  118. }
  119. if (!parsedRight(parser, "nobom", data))
  120. {
  121. return 1;
  122. }
  123. if (parser.GetParseHadBOM())
  124. {
  125. cmsys_ios::cerr << "cmVisualStudioSlnParser found BOM in nobom."
  126. << SLN_EXTENSION
  127. << cmsys_ios::endl;
  128. return 1;
  129. }
  130. }
  131. // Test invalid sln
  132. {
  133. {
  134. cmSlnData data;
  135. if (!parsedRight(parser, "err-nonexistent", data,
  136. cmVisualStudioSlnParser::ResultErrorOpeningInput))
  137. {
  138. return 1;
  139. }
  140. }
  141. {
  142. cmSlnData data;
  143. if (!parsedRight(parser, "err-empty", data,
  144. cmVisualStudioSlnParser::ResultErrorReadingInput))
  145. {
  146. return 1;
  147. }
  148. }
  149. const char * const files[] =
  150. {
  151. "header", "projectArgs", "topLevel", "projectContents", "projectSection",
  152. "global", "unclosed", "strayQuote", "strayParen", "strayQuote2"
  153. };
  154. for (size_t idx = 0; idx < sizeof(files) / sizeof(files[0]); ++idx)
  155. {
  156. cmSlnData data;
  157. if (!parsedRight(parser, std::string("err-structure-") + files[idx], data,
  158. cmVisualStudioSlnParser::ResultErrorInputStructure))
  159. {
  160. return 1;
  161. }
  162. }
  163. {
  164. cmSlnData data;
  165. if (!parsedRight(parser, "err-data", data,
  166. cmVisualStudioSlnParser::ResultErrorInputData))
  167. {
  168. return 1;
  169. }
  170. }
  171. }
  172. // All is well
  173. return 0;
  174. }