Skip to content

Commit

Permalink
Fix #44: trailing comma when printing null value
Browse files Browse the repository at this point in the history
  • Loading branch information
swolchok committed Dec 22, 2016
1 parent 32680f6 commit f4c16e0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
9 changes: 6 additions & 3 deletions JsonVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ void JsonVisitor::printLocation(const yy::location &location)
<< ",\"end\":" << location.end.column << '}';
}

void JsonVisitor::startPrintingNode(const char *kind, const yy::location &location) {
void JsonVisitor::startPrintingNodeWithoutTrailingComma(const char *kind, const yy::location &location) {
out_.str("");
out_ << "{\"kind\":\"" << kind << "\",\"loc\":";
printLocation(location);
}

void JsonVisitor::startPrintingNode(const char *kind, const yy::location &location) {
startPrintingNodeWithoutTrailingComma(kind, location);
out_ << ',';
return;
}

void JsonVisitor::printChildList(
Expand Down Expand Up @@ -349,7 +352,7 @@ void JsonVisitor::endVisitEnumValue(const EnumValue &enumValue) {
}

void JsonVisitor::endVisitNullValue(const NullValue &nullValue) {
startPrintingNode("NullValue", nullValue.getLocation());
startPrintingNodeWithoutTrailingComma("NullValue", nullValue.getLocation());
out_ << '}';
printed_.back().emplace_back(out_.str());
}
Expand Down
4 changes: 4 additions & 0 deletions JsonVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class JsonVisitor : public AstVisitor {

// Print the opening of a new JSON dictionary, the node kind, and
// the node location.
void startPrintingNodeWithoutTrailingComma(const char *kind, const yy::location &location);

// Like startPrintingNodeWithoutTrailingComma, but adds a trailing
// comma for convenience.
void startPrintingNode(const char *kind, const yy::location &location);

void printLocation(const yy::location &location);
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ INCLUDE_DIRECTORIES(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

FILE(COPY valgrind.supp DESTINATION .)

ADD_EXECUTABLE(runTests ParserTests.cpp BuildCAPI.c)
ADD_EXECUTABLE(runTests ParserTests.cpp JsonVisitorTests.cpp BuildCAPI.c)

TARGET_LINK_LIBRARIES(runTests gtest gtest_main)

Expand Down
28 changes: 28 additions & 0 deletions test/JsonVisitorTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#include <gtest/gtest.h>

#include "Ast.h"
#include "GraphQLParser.h"
#include "c/GraphQLAstToJSON.h"

using namespace facebook::graphql;
using namespace facebook::graphql::ast;

TEST(JsonVisitorTests, NullValueEmitsValidJSONWithoutTrailingComma) {
const char *error = nullptr;
auto AST = parseString("{field(arg: null)}", &error);
ASSERT_STREQ(nullptr, error) << "GraphQL parser error: " << error;
const char *json = graphql_ast_to_json((const struct GraphQLAstNode *)AST.get());

EXPECT_STREQ(
json,
"{\"kind\":\"Document\",\"loc\":{\"start\":1,\"end\":19},\"definitions\":[{\"kind\":\"OperationDefinition\",\"loc\":{\"start\":1,\"end\":19},\"operation\":\"query\",\"name\": null,\"variableDefinitions\":null,\"directives\":null,\"selectionSet\":{\"kind\":\"SelectionSet\",\"loc\":{\"start\":1,\"end\":19},\"selections\":[{\"kind\":\"Field\",\"loc\":{\"start\":2,\"end\":18},\"alias\":null,\"name\":{\"kind\":\"Name\",\"loc\":{\"start\":2,\"end\":7},\"value\":\"field\"},\"arguments\":[{\"kind\":\"Argument\",\"loc\":{\"start\":8,\"end\":17},\"name\":{\"kind\":\"Name\",\"loc\":{\"start\":8,\"end\":11},\"value\":\"arg\"},\"value\":{\"kind\":\"NullValue\",\"loc\":{\"start\":13,\"end\":17}}}],\"directives\":null,\"selectionSet\":null}]}}]}");
}

0 comments on commit f4c16e0

Please sign in to comment.