v8/test/unittests/torque/ls-json-unittest.cc
Simon Zünd 2d8f2e86ee Reland^3 "[torque] Throw exception instead of aborting if something goes wrong"
This is a reland of ffe6940fbc

The UBSan issue is fixed with https://crrev.com/c/1566511

TBR=tebbi@chromium.org

Original change's description:
> Reland^2 "[torque] Throw exception instead of aborting if something goes wrong"
>
> This is a reland of 251d1623f3
>
> The reland fixes ASAN component builds by adding RTTI build config to both
> torque executables. Big thanks to sigurds for finding the fix.
>
> Original change's description:
> > Reland "[torque] Throw exception instead of aborting if something goes wrong"
> >
> > This is a reland of 3bd49f9b90
> >
> > The issue on the windows bot is apparently a compiler bug in MSVC related to
> > move construction. The fix seems to be to change the order of the fields in
> > "JsonParseResult" (go figure).
> >
> > Drive-by-change: Fix LS on windows by emitting correct line endings and
> > enabling exceptions for the LS executable as well.
> >
> > Original change's description:
> > > [torque] Throw exception instead of aborting if something goes wrong
> > >
> > > This CL enables exceptions for the Torque compiler and Torque language
> > > server. Instead of aborting when something goes wrong during
> > > compilation, a TorqueError is thrown, containing the error message
> > > and a source position. The compiler executable still prints the error
> > > and aborts, while the language server will pass this information
> > > along to the client (not included in this CL).
> > >
> > > R=danno@chromium.org
> > >
> > > Bug: v8:8880
> > > Change-Id: Iad83c46fb6a91c1babbc0ae7dbd94fbe4e7f1663
> > > Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1526003
> > > Reviewed-by: Daniel Clifford <danno@chromium.org>
> > > Commit-Queue: Simon Zünd <szuend@chromium.org>
> > > Cr-Commit-Position: refs/heads/master@{#60512}
> >
> > Bug: v8:8880
> > Change-Id: I00e6591bbb4c516dd7540a7e27196853bc637f11
> > Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1545995
> > Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
> > Commit-Queue: Simon Zünd <szuend@chromium.org>
> > Cr-Commit-Position: refs/heads/master@{#60736}
>
> Bug: v8:8880
> Change-Id: Iba198d771169283e83e74324f27aa9e90b8d8975
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1563770
> Reviewed-by: Sigurd Schneider <sigurds@chromium.org>
> Commit-Queue: Simon Zünd <szuend@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#60804}

Bug: v8:8880
Change-Id: I5b7e40ad27bff8f7bfa22240954c2cb75083ad82
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1564065
Reviewed-by: Simon Zünd <szuend@chromium.org>
Commit-Queue: Simon Zünd <szuend@chromium.org>
Auto-Submit: Simon Zünd <szuend@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60860}
2019-04-16 10:41:20 +00:00

122 lines
4.2 KiB
C++

// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/torque/ls/json-parser.h"
#include "src/torque/ls/json.h"
#include "src/torque/source-positions.h"
#include "src/torque/utils.h"
#include "test/unittests/test-utils.h"
#include "testing/gmock-support.h"
namespace v8 {
namespace internal {
namespace torque {
namespace ls {
TEST(LanguageServerJson, TestJsonPrimitives) {
const JsonValue true_result = ParseJson("true").value;
ASSERT_EQ(true_result.tag, JsonValue::BOOL);
EXPECT_EQ(true_result.ToBool(), true);
const JsonValue false_result = ParseJson("false").value;
ASSERT_EQ(false_result.tag, JsonValue::BOOL);
EXPECT_EQ(false_result.ToBool(), false);
const JsonValue null_result = ParseJson("null").value;
ASSERT_EQ(null_result.tag, JsonValue::IS_NULL);
const JsonValue number = ParseJson("42").value;
ASSERT_EQ(number.tag, JsonValue::NUMBER);
EXPECT_EQ(number.ToNumber(), 42);
}
TEST(LanguageServerJson, TestJsonStrings) {
const JsonValue basic = ParseJson("\"basic\"").value;
ASSERT_EQ(basic.tag, JsonValue::STRING);
EXPECT_EQ(basic.ToString(), "basic");
const JsonValue singleQuote = ParseJson("\"'\"").value;
ASSERT_EQ(singleQuote.tag, JsonValue::STRING);
EXPECT_EQ(singleQuote.ToString(), "'");
}
TEST(LanguageServerJson, TestJsonArrays) {
const JsonValue empty_array = ParseJson("[]").value;
ASSERT_EQ(empty_array.tag, JsonValue::ARRAY);
EXPECT_EQ(empty_array.ToArray().size(), (size_t)0);
const JsonValue number_array = ParseJson("[1, 2, 3, 4]").value;
ASSERT_EQ(number_array.tag, JsonValue::ARRAY);
const JsonArray& array = number_array.ToArray();
ASSERT_EQ(array.size(), (size_t)4);
ASSERT_EQ(array[1].tag, JsonValue::NUMBER);
EXPECT_EQ(array[1].ToNumber(), 2);
const JsonValue string_array_object = ParseJson("[\"a\", \"b\"]").value;
ASSERT_EQ(string_array_object.tag, JsonValue::ARRAY);
const JsonArray& string_array = string_array_object.ToArray();
ASSERT_EQ(string_array.size(), (size_t)2);
ASSERT_EQ(string_array[1].tag, JsonValue::STRING);
EXPECT_EQ(string_array[1].ToString(), "b");
}
TEST(LanguageServerJson, TestJsonObjects) {
const JsonValue empty_object = ParseJson("{}").value;
ASSERT_EQ(empty_object.tag, JsonValue::OBJECT);
EXPECT_EQ(empty_object.ToObject().size(), (size_t)0);
const JsonValue primitive_fields =
ParseJson("{ \"flag\": true, \"id\": 5}").value;
EXPECT_EQ(primitive_fields.tag, JsonValue::OBJECT);
const JsonValue& flag = primitive_fields.ToObject().at("flag");
ASSERT_EQ(flag.tag, JsonValue::BOOL);
EXPECT_TRUE(flag.ToBool());
const JsonValue& id = primitive_fields.ToObject().at("id");
ASSERT_EQ(id.tag, JsonValue::NUMBER);
EXPECT_EQ(id.ToNumber(), 5);
const JsonValue& complex_fields =
ParseJson("{ \"array\": [], \"object\": { \"name\": \"torque\" } }")
.value;
ASSERT_EQ(complex_fields.tag, JsonValue::OBJECT);
const JsonValue& array = complex_fields.ToObject().at("array");
ASSERT_EQ(array.tag, JsonValue::ARRAY);
EXPECT_EQ(array.ToArray().size(), (size_t)0);
const JsonValue& object = complex_fields.ToObject().at("object");
ASSERT_EQ(object.tag, JsonValue::OBJECT);
ASSERT_EQ(object.ToObject().at("name").tag, JsonValue::STRING);
EXPECT_EQ(object.ToObject().at("name").ToString(), "torque");
}
// These tests currently fail on Windows as there seems to be a linking
// issue with exceptions enabled for Torque.
// TODO(szuend): Remove the OS check when errors are reported differently,
// or the issue is resolved.
#if !defined(V8_OS_WIN)
using ::testing::HasSubstr;
TEST(LanguageServerJson, ParserError) {
JsonParserResult result = ParseJson("{]");
ASSERT_TRUE(result.error.has_value());
EXPECT_THAT(result.error->message,
HasSubstr("Parser Error: unexpected token"));
}
TEST(LanguageServerJson, LexerError) {
JsonParserResult result = ParseJson("{ noquoteskey: null }");
ASSERT_TRUE(result.error.has_value());
EXPECT_THAT(result.error->message, HasSubstr("Lexer Error: unknown token"));
}
#endif
} // namespace ls
} // namespace torque
} // namespace internal
} // namespace v8