Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -503,21 +503,13 @@ export class CPlusPlusRenderer extends ConvenienceRenderer {
"",
]);

if (this._options.typeSourceStyle) {
this.forEachTopLevel("none", (_, topLevelName) => {
this.emitLine(
"// ",
topLevelName,
" data = nlohmann::json::parse(jsonString);",
);
});
} else {
this.forEachTopLevel("none", (_, topLevelName) => {
this.emitLine(
"// ",
basename,
topLevelName,
" data = nlohmann::json::parse(jsonString);",
);
}
});

if (this._options.wstring) {
this.emitLine("//");
Expand Down Expand Up @@ -3187,6 +3179,7 @@ export class CPlusPlusRenderer extends ConvenienceRenderer {
this.emitHelper();

this.startFile("Generators.hpp", true);
this._generatedFiles.add("Generators.hpp");

this._allTypeNames.forEach((t) => {
this.emitInclude(false, [t, ".hpp"]);
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,10 @@ export const allFixtures: Fixture[] = [
new JSONFixture(languages.CJSONMultiHeaderLanguage, "cjson-multi-header"),
new JSONFixture(languages.CJSONMultiSplitLanguage, "cjson-multi-split"),
new JSONFixture(languages.CPlusPlusLanguage),
new JSONFixture(
languages.CPlusPlusMultiSourceLanguage,
"cplusplus-multi-source",
),
new JSONFixture(languages.PHPLanguage),
new JSONFixture(languages.RustLanguage),
new JSONFixture(languages.RubyLanguage),
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/cplusplus/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include <fstream>
#include <streambuf>

#include "TopLevel.hpp"
#include "Generators.hpp"
#include "quicktype.hpp"

using quicktype::TopLevel;
using nlohmann::json;
Expand Down
10 changes: 8 additions & 2 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ export const CPlusPlusLanguage: Language = {
"union",
"no-defaults",
],
output: "TopLevel.hpp",
output: "quicktype.hpp",
topLevel: "TopLevel",
skipJSON: [
// fails on a string containing null
Expand All @@ -737,7 +737,6 @@ export const CPlusPlusLanguage: Language = {
],
rendererOptions: {},
quickTestRendererOptions: [
{ "source-style": "multi-source" },
{ "code-format": "with-struct" },
{ wstring: "use-wstring" },
{ "const-style": "east-const" },
Expand All @@ -753,6 +752,13 @@ export const CPlusPlusLanguage: Language = {
sourceFiles: ["src/language/CPlusPlus/index.ts"],
};

export const CPlusPlusMultiSourceLanguage: Language = {
...CPlusPlusLanguage,
includeJSON: ["pokedex.json"],
rendererOptions: { "source-style": "multi-source" },
quickTestRendererOptions: [],
};

export const ElmLanguage: Language = {
name: "elm",
base: "test/fixtures/elm",
Expand Down
59 changes: 59 additions & 0 deletions test/unit/cplusplus-multi-source.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, test } from "vitest";

import {
InputData,
jsonInputForTargetLanguage,
quicktypeMultiFile,
} from "quicktype-core";

async function cPlusPlusMultiSourceFiles(): Promise<Map<string, string>> {
const jsonInput = jsonInputForTargetLanguage("cplusplus");
await jsonInput.addSource({
name: "ChunkCache",
samples: ['{"chunks":["one"],"size":1}'],
});
await jsonInput.addSource({
name: "BufferPath",
samples: ['{"path":"somewhere","maxSize":2}'],
});

const inputData = new InputData();
inputData.addInput(jsonInput);
const result = await quicktypeMultiFile({
inputData,
lang: "cplusplus",
outputFilename: "quicktype.hpp",
rendererOptions: { "source-style": "multi-source" },
});

return new Map(
Array.from(result, ([filename, serialized]) => [
filename,
serialized.lines.join("\n"),
]),
);
}

describe("C++ multi-source output", () => {
test("the umbrella header includes the JSON generators", async () => {
const files = await cPlusPlusMultiSourceFiles();
expect(files.get("quicktype.hpp")).toContain(
'#include "Generators.hpp"',
);
});

test("usage comments name top-level types, not generated files", async () => {
const files = await cPlusPlusMultiSourceFiles();
for (const [filename, source] of files) {
expect(source).toContain(
"// ChunkCache data = nlohmann::json::parse(jsonString);",
);
expect(source).toContain(
"// BufferPath data = nlohmann::json::parse(jsonString);",
);
expect(source).not.toContain(
`// ${filename} data = nlohmann::json::parse(jsonString);`,
);
}
});
});
Loading