aboutsummaryrefslogtreecommitdiff
path: root/llvm/utils/llvm-test-mustache-spec/llvm-test-mustache-spec.cpp
blob: 1f566e13f070a6f17e5c6d6a70558c1c54ef6593 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Simple drivers to test the mustache spec found at:
// https://github.com/mustache/spec
//
// It is used to verify that the current implementation conforms to the spec.
// Simply download the spec and pass the test JSON files to the driver. Each
// spec file should have a list of tests for compliance with the spec. These
// are loaded as test cases, and rendered with our Mustache implementation,
// which is then compared against the expected output from the spec.
//
// The current implementation only supports non-optional parts of the spec, so
// we do not expect any of the dynamic-names, inheritance, or lambda tests to
// pass. Additionally, Triple Mustache is not supported. Unsupported tests are
// marked as XFail and are removed from the XFail list as they are fixed.
//
// Usage:
//  llvm-test-mustache-spec path/to/test/file.json path/to/test/file2.json ...
//===----------------------------------------------------------------------===//

#include "llvm/ADT/StringSet.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Mustache.h"
#include "llvm/Support/Path.h"
#include <string>

using namespace llvm;
using namespace llvm::json;
using namespace llvm::mustache;

#define DEBUG_TYPE "llvm-test-mustache-spec"

static cl::OptionCategory Cat("llvm-test-mustache-spec Options");

static cl::list<std::string>
    InputFiles(cl::Positional, cl::desc("<input files>"), cl::OneOrMore);

static cl::opt<bool> ReportErrors("report-errors",
                                  cl::desc("Report errors in spec tests"),
                                  cl::cat(Cat));

static ExitOnError ExitOnErr;

static int NumXFail = 0;
static int NumSuccess = 0;

static const StringMap<StringSet<>> XFailTestNames = {{
    {"delimiters.json",
     {
         "Pair Behavior",
         "Special Characters",
         "Sections",
         "Inverted Sections",
         "Partial Inheritence",
         "Post-Partial Behavior",
         "Standalone Tag",
         "Indented Standalone Tag",
         "Standalone Line Endings",
         "Standalone Without Previous Line",
         "Standalone Without Newline",
     }},
    {"~dynamic-names.json",
     {
         "Basic Behavior - Partial",
         "Basic Behavior - Name Resolution",
         "Context",
         "Dotted Names",
         "Dotted Names - Failed Lookup",
         "Dotted names - Context Stacking",
         "Dotted names - Context Stacking Under Repetition",
         "Dotted names - Context Stacking Failed Lookup",
         "Recursion",
         "Surrounding Whitespace",
         "Inline Indentation",
         "Standalone Line Endings",
         "Standalone Without Previous Line",
         "Standalone Without Newline",
         "Standalone Indentation",
         "Padding Whitespace",
     }},
    {"~inheritance.json",
     {
         "Default",
         "Variable",
         "Triple Mustache",
         "Sections",
         "Negative Sections",
         "Mustache Injection",
         "Inherit",
         "Overridden content",
         "Data does not override block default",
         "Two overridden parents",
         "Override parent with newlines",
         "Inherit indentation",
         "Only one override",
         "Parent template",
         "Recursion",
         "Multi-level inheritance, no sub child",
         "Text inside parent",
         "Text inside parent",
         "Block scope",
         "Standalone parent",
         "Standalone block",
         "Block reindentation",
         "Intrinsic indentation",
         "Nested block reindentation",

     }},
    {"~lambdas.json",
     {
         "Interpolation",
         "Interpolation - Expansion",
         "Interpolation - Alternate Delimiters",
         "Interpolation - Multiple Calls",
         "Escaping",
         "Section",
         "Section - Expansion",
         "Section - Alternate Delimiters",
         "Section - Multiple Calls",

     }},
    {"interpolation.json",
     {
         "Triple Mustache",
         "Triple Mustache Integer Interpolation",
         "Triple Mustache Decimal Interpolation",
         "Triple Mustache Null Interpolation",
         "Triple Mustache Context Miss Interpolation",
         "Dotted Names - Triple Mustache Interpolation",
         "Implicit Iterators - Triple Mustache",
         "Triple Mustache - Surrounding Whitespace",
         "Triple Mustache - Standalone",
         "Triple Mustache With Padding",
     }},
    {"partials.json", {"Standalone Indentation"}},
    {"sections.json", {"Implicit Iterator - Triple mustache"}},
}};

struct TestData {
  TestData() = default;
  explicit TestData(const json::Object &TestCase)
      : TemplateStr(*TestCase.getString("template")),
        ExpectedStr(*TestCase.getString("expected")),
        Name(*TestCase.getString("name")), Data(TestCase.get("data")),
        Partials(TestCase.get("partials")) {}

  static Expected<TestData> createTestData(json::Object *TestCase,
                                           StringRef InputFile) {
    // If any of the needed elements are missing, we cannot continue.
    // NOTE: partials are optional in the test schema.
    if (!TestCase || !TestCase->getString("template") ||
        !TestCase->getString("expected") || !TestCase->getString("name") ||
        !TestCase->get("data"))
      return createStringError(
          llvm::inconvertibleErrorCode(),
          "invalid JSON schema in test file: " + InputFile + "\n");

    return TestData(*TestCase);
  }

  StringRef TemplateStr;
  StringRef ExpectedStr;
  StringRef Name;
  const Value *Data;
  const Value *Partials;
};

static void reportTestFailure(const TestData &TD, StringRef ActualStr,
                              bool IsXFail) {
  LLVM_DEBUG(dbgs() << "Template: " << TD.TemplateStr << "\n");
  if (TD.Partials) {
    LLVM_DEBUG(dbgs() << "Partial: ");
    LLVM_DEBUG(TD.Partials->print(dbgs()));
    LLVM_DEBUG(dbgs() << "\n");
  }
  LLVM_DEBUG(dbgs() << "JSON Data: ");
  LLVM_DEBUG(TD.Data->print(dbgs()));
  LLVM_DEBUG(dbgs() << "\n");
  outs() << formatv("Test {}: {}\n", (IsXFail ? "XFailed" : "Failed"), TD.Name);
  if (ReportErrors) {
    outs() << "  Expected: \'" << TD.ExpectedStr << "\'\n"
           << "  Actual: \'" << ActualStr << "\'\n"
           << " ====================\n";
  }
}

static void registerPartials(const Value *Partials, Template &T) {
  if (!Partials)
    return;
  for (const auto &[Partial, Str] : *Partials->getAsObject())
    T.registerPartial(Partial.str(), Str.getAsString()->str());
}

static json::Value readJsonFromFile(StringRef &InputFile) {
  std::unique_ptr<MemoryBuffer> Buffer =
      ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(InputFile)));
  return ExitOnErr(parse(Buffer->getBuffer()));
}

static bool isTestXFail(StringRef FileName, StringRef TestName) {
  auto P = llvm::sys::path::filename(FileName);
  auto It = XFailTestNames.find(P);
  return It != XFailTestNames.end() && It->second.contains(TestName);
}

static bool evaluateTest(StringRef &InputFile, TestData &TestData,
                         std::string &ActualStr) {
  bool IsXFail = isTestXFail(InputFile, TestData.Name);
  bool Matches = TestData.ExpectedStr == ActualStr;
  if ((Matches && IsXFail) || (!Matches && !IsXFail)) {
    reportTestFailure(TestData, ActualStr, IsXFail);
    return false;
  }
  IsXFail ? NumXFail++ : NumSuccess++;
  return true;
}

static void runTest(StringRef InputFile) {
  NumXFail = 0;
  NumSuccess = 0;
  outs() << "Running Tests: " << InputFile << "\n";
  json::Value Json = readJsonFromFile(InputFile);

  json::Object *Obj = Json.getAsObject();
  Array *TestArray = Obj->getArray("tests");
  // Even though we parsed the JSON, it can have a bad format, so check it.
  if (!TestArray)
    ExitOnErr(createStringError(
        llvm::inconvertibleErrorCode(),
        "invalid JSON schema in test file: " + InputFile + "\n"));

  const size_t Total = TestArray->size();

  for (Value V : *TestArray) {
    auto TestData =
        ExitOnErr(TestData::createTestData(V.getAsObject(), InputFile));
    Template T(TestData.TemplateStr);
    registerPartials(TestData.Partials, T);

    std::string ActualStr;
    raw_string_ostream OS(ActualStr);
    T.render(*TestData.Data, OS);
    evaluateTest(InputFile, TestData, ActualStr);
  }

  const int NumFailed = Total - NumSuccess - NumXFail;
  outs() << formatv("===Results===\n"
                    " Suceeded: {}\n"
                    " Expectedly Failed: {}\n"
                    " Failed: {}\n"
                    " Total: {}\n",
                    NumSuccess, NumXFail, NumFailed, Total);
}

int main(int argc, char **argv) {
  ExitOnErr.setBanner(std::string(argv[0]) + " error: ");
  cl::ParseCommandLineOptions(argc, argv);
  for (const auto &FileName : InputFiles)
    runTest(FileName);
  return 0;
}