github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/common/gtest_util_json.cpp (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  #include "common/configuration.h"
    21  #include "common/ir_conf_reader.h"
    22  #include "fs/util.h"
    23  #include <boost/property_tree/json_parser.hpp>
    24  #include <boost/property_tree/ptree.hpp>
    25  #include <gtest/gtest.h>
    26  
    27  TEST(test_boost_read_json, simple) {
    28    std::string fp =
    29        neb::fs::join_path(neb::configuration::instance().nbre_root_dir(),
    30                           "test/data/common/nasir.json");
    31  
    32    boost::property_tree::ptree json_root;
    33    boost::property_tree::read_json(fp, json_root);
    34  }
    35  
    36  TEST(test_common_json_util, read_json) {
    37    std::string fp =
    38        neb::fs::join_path(neb::configuration::instance().nbre_root_dir(),
    39                           "test/data/common/json_util.json");
    40    neb::ir_conf_reader json_reader(fp);
    41    EXPECT_EQ(json_reader.self_ref().name(), "nr");
    42    EXPECT_EQ(json_reader.self_ref().version().major_version(), 0);
    43    EXPECT_EQ(json_reader.self_ref().version().minor_version(), 0);
    44    EXPECT_EQ(json_reader.self_ref().version().patch_version(), 1);
    45    EXPECT_EQ(json_reader.available_height(), 100);
    46    EXPECT_EQ(json_reader.depends()[0].name(), "yyy");
    47    EXPECT_EQ(json_reader.depends()[0].version().major_version(), 11);
    48    EXPECT_EQ(json_reader.depends()[0].version().minor_version(), 12);
    49    EXPECT_EQ(json_reader.depends()[0].version().patch_version(), 13);
    50    EXPECT_EQ(json_reader.depends()[1].name(), "zzz");
    51    EXPECT_EQ(json_reader.depends()[1].version().major_version(), 21);
    52    EXPECT_EQ(json_reader.depends()[1].version().minor_version(), 22);
    53    EXPECT_EQ(json_reader.depends()[1].version().patch_version(), 23);
    54  }
    55  
    56  TEST(test_common_json_util, throw_json) {
    57    EXPECT_THROW(neb::ir_conf_reader json_reader("xxx"),
    58                 neb::json_general_failure);
    59  
    60    std::string fp =
    61        neb::fs::join_path(neb::configuration::instance().nbre_root_dir(),
    62                           "test/data/common/test_throw_exceptions.json");
    63    EXPECT_THROW(neb::ir_conf_reader json_reader(fp), neb::json_general_failure);
    64  }