|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "arrow/json/object_parser.h" |
| 19 | + |
| 20 | +#include <gtest/gtest.h> |
| 21 | +#include <gmock/gmock.h> |
| 22 | + |
| 23 | +#include <string> |
| 24 | +#include <unordered_map> |
| 25 | + |
| 26 | +#include "arrow/testing/gtest_util.h" |
| 27 | + |
| 28 | +namespace arrow { |
| 29 | +namespace json { |
| 30 | +namespace internal { |
| 31 | + |
| 32 | +TEST(ObjectParser, GetString) { |
| 33 | + ObjectParser parser; |
| 34 | + |
| 35 | + ASSERT_OK(parser.Parse(R"({"name":"arrow"})")); |
| 36 | + |
| 37 | + ASSERT_OK_AND_ASSIGN(auto value, parser.GetString("name")); |
| 38 | + EXPECT_EQ(value, "arrow"); |
| 39 | +} |
| 40 | + |
| 41 | +TEST(ObjectParser, GetBool) { |
| 42 | + ObjectParser parser; |
| 43 | + |
| 44 | + ASSERT_OK(parser.Parse(R"({"enabled":true})")); |
| 45 | + |
| 46 | + ASSERT_OK_AND_ASSIGN(auto value, parser.GetBool("enabled")); |
| 47 | + EXPECT_TRUE(value); |
| 48 | +} |
| 49 | + |
| 50 | +TEST(ObjectParser, InvalidJson) { |
| 51 | + ObjectParser parser; |
| 52 | + |
| 53 | + EXPECT_RAISES_WITH_MESSAGE_THAT( |
| 54 | + Invalid, |
| 55 | + ::testing::HasSubstr("Json parse error"), |
| 56 | + parser.Parse(R"({"name":)")); |
| 57 | +} |
| 58 | + |
| 59 | +TEST(ObjectParser, GetStringMap) { |
| 60 | + ObjectParser parser; |
| 61 | + |
| 62 | + ASSERT_OK(parser.Parse(R"({ |
| 63 | + "k1": "v1", |
| 64 | + "k2": "v2" |
| 65 | + })")); |
| 66 | + |
| 67 | + ASSERT_OK_AND_ASSIGN(auto map, parser.GetStringMap()); |
| 68 | + |
| 69 | + ASSERT_EQ(map.size(), 2U); |
| 70 | + EXPECT_EQ(map["k1"], "v1"); |
| 71 | + EXPECT_EQ(map["k2"], "v2"); |
| 72 | +} |
| 73 | + |
| 74 | +TEST(ObjectParser, MissingKey) { |
| 75 | + ObjectParser parser; |
| 76 | + |
| 77 | + ASSERT_OK(parser.Parse(R"({ |
| 78 | + "name": "arrow" |
| 79 | + })")); |
| 80 | + |
| 81 | + ASSERT_RAISES(KeyError, parser.GetString("missing")); |
| 82 | + ASSERT_RAISES(KeyError, parser.GetBool("missing")); |
| 83 | +} |
| 84 | + |
| 85 | +TEST(ObjectParser, WrongType) { |
| 86 | + ObjectParser parser; |
| 87 | + |
| 88 | + ASSERT_OK(parser.Parse(R"({ |
| 89 | + "flag": true, |
| 90 | + "name": "arrow" |
| 91 | + })")); |
| 92 | + |
| 93 | + ASSERT_RAISES(TypeError, parser.GetString("flag")); |
| 94 | + ASSERT_RAISES(TypeError, parser.GetBool("name")); |
| 95 | +} |
| 96 | + |
| 97 | +TEST(ObjectParser, NonObjectRoot) { |
| 98 | + ObjectParser parser; |
| 99 | + |
| 100 | + ASSERT_RAISES(TypeError, parser.Parse(R"(["a", "b"])")); |
| 101 | +} |
| 102 | + |
| 103 | +TEST(ObjectParser, EmptyObject) { |
| 104 | + ObjectParser parser; |
| 105 | + |
| 106 | + ASSERT_OK(parser.Parse(R"({})")); |
| 107 | + |
| 108 | + ASSERT_OK_AND_ASSIGN(auto map, parser.GetStringMap()); |
| 109 | + |
| 110 | + EXPECT_TRUE(map.empty()); |
| 111 | +} |
| 112 | + |
| 113 | +} // namespace internal |
| 114 | +} // namespace json |
| 115 | +} // namespace arrow |
0 commit comments