summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeLib/testVisualStudioSlnParser.cxx
blob: 7fdba9a2a3a90ff82e97faccd7f3eff477f9ace2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "testVisualStudioSlnParser.h"

#include <iostream>

#include "cmVisualStudioSlnData.h"
#include "cmVisualStudioSlnParser.h"

static bool parsedRight(cmVisualStudioSlnParser& parser,
                        const std::string& file, cmSlnData& data,
                        cmVisualStudioSlnParser::ParseResult expected =
                          cmVisualStudioSlnParser::ResultOK)
{
  if (parser.ParseFile(SOURCE_DIR "/testVisualStudioSlnParser_data/" + file +
                         "." SLN_EXTENSION,
                       data, cmVisualStudioSlnParser::DataGroupProjects)) {
    if (expected == cmVisualStudioSlnParser::ResultOK) {
      return true;
    }
  } else {
    if (parser.GetParseResult() == expected) {
      return true;
    }
  }
  std::cerr << "cmVisualStudioSlnParser mis-parsed " << file
            << "." SLN_EXTENSION << "; expected result " << expected
            << ", got " << parser.GetParseResult() << std::endl;
  return false;
}

int testVisualStudioSlnParser(int, char* [])
{
  cmVisualStudioSlnParser parser;

  // Test clean parser
  if (parser.GetParseResult() != cmVisualStudioSlnParser::ResultOK) {
    std::cerr << "cmVisualStudioSlnParser initialization failed" << std::endl;
    return 1;
  }

  // Test parsing valid sln
  {
    cmSlnData data;
    if (!parsedRight(parser, "valid", data)) {
      return 1;
    }
    const std::vector<cmSlnProjectEntry>& projects = data.GetProjects();
    const char* const names[] = {
      "3rdParty",
      "ALL_BUILD",
      "CMakeLib",
      "CMakeLibTests",
      "CMakePredefinedTargets",
      "CPackLib",
      "CTestDashboardTargets",
      "CTestLib",
      "Continuous",
      "Documentation",
      "Experimental",
      "INSTALL",
      "KWSys",
      "LIBCURL",
      "Nightly",
      "NightlyMemoryCheck",
      "PACKAGE",
      "RUN_TESTS",
      "Tests",
      "Utilities",
      "Win9xCompat",
      "ZERO_CHECK",
      "cmIML_test",
      "cmake",
      "cmbzip2",
      "cmcldeps",
      "cmcompress",
      "cmcurl",
      "cmexpat",
      "cmlibarchive",
      "cmsys",
      "cmsysEncodeExecutable",
      "cmsysProcessFwd9x",
      "cmsysTestDynload",
      "cmsysTestProcess",
      "cmsysTestSharedForward",
      "cmsysTestsC",
      "cmsysTestsCxx",
      "cmsys_c",
      "cmw9xcom",
      "cmzlib",
      "cpack",
      "ctest",
      "documentation",
      "memcheck_fail",
      "pseudo_BC",
      "pseudo_purify",
      "pseudo_valgrind",
      "test_clean",
      "uninstall"
      /* clang-format needs this comment to break after the opening brace */
    };
    const size_t expectedProjectCount = sizeof(names) / sizeof(*names);
    if (projects.size() != expectedProjectCount) {
      std::cerr << "cmVisualStudioSlnParser returned bad number of "
                << "projects (" << projects.size() << " instead of "
                << expectedProjectCount << ')' << std::endl;
      return 1;
    }
    for (size_t idx = 0; idx < expectedProjectCount; ++idx) {
      if (projects[idx].GetName() != names[idx]) {
        std::cerr << "cmVisualStudioSlnParser returned bad project #" << idx
                  << "; expected \"" << names[idx] << "\", got \""
                  << projects[idx].GetName() << '"' << std::endl;
        return 1;
      }
    }
    if (projects[0].GetRelativePath() != "Utilities\\3rdParty") {
      std::cerr << "cmVisualStudioSlnParser returned bad relative path of "
                << "project 3rdParty; expected \"Utilities\\3rdParty\", "
                << "got \"" << projects[0].GetRelativePath() << '"'
                << std::endl;
      return 1;
    }
    if (projects[2].GetGUID() != "{59BCCCCD-3AD1-4491-B8F4-C5793AC007E2}") {
      std::cerr << "cmVisualStudioSlnParser returned bad relative path of "
                << "project CMakeLib; expected "
                << "\"{59BCCCCD-3AD1-4491-B8F4-C5793AC007E2}\", "
                << "got \"" << projects[2].GetGUID() << '"' << std::endl;
      return 1;
    }
  }

  // Test BOM parsing
  {
    cmSlnData data;

    if (!parsedRight(parser, "bom", data)) {
      return 1;
    }
    if (!parser.GetParseHadBOM()) {
      std::cerr << "cmVisualStudioSlnParser didn't find BOM in bom."
                << SLN_EXTENSION << std::endl;
      return 1;
    }

    if (!parsedRight(parser, "nobom", data)) {
      return 1;
    }
    if (parser.GetParseHadBOM()) {
      std::cerr << "cmVisualStudioSlnParser found BOM in nobom."
                << SLN_EXTENSION << std::endl;
      return 1;
    }
  }

  // Test invalid sln
  {
    {
      cmSlnData data;
      if (!parsedRight(parser, "err-nonexistent", data,
                       cmVisualStudioSlnParser::ResultErrorOpeningInput)) {
        return 1;
      }
    }
    {
      cmSlnData data;
      if (!parsedRight(parser, "err-empty", data,
                       cmVisualStudioSlnParser::ResultErrorReadingInput)) {
        return 1;
      }
    }
    const char* const files[] = {
      "header",         "projectArgs", "topLevel", "projectContents",
      "projectSection", "global",      "unclosed", "strayQuote",
      "strayParen",     "strayQuote2"
      /* clang-format needs this comment to break after the opening brace */
    };
    for (size_t idx = 0; idx < sizeof(files) / sizeof(files[0]); ++idx) {
      cmSlnData data;
      if (!parsedRight(parser, std::string("err-structure-") + files[idx],
                       data,
                       cmVisualStudioSlnParser::ResultErrorInputStructure)) {
        return 1;
      }
    }
    {
      cmSlnData data;
      if (!parsedRight(parser, "err-data", data,
                       cmVisualStudioSlnParser::ResultErrorInputData)) {
        return 1;
      }
    }
  }

  // All is well
  return 0;
}