Skip to content

Commit

Permalink
Add test simple JSON to coord code
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyelewis committed Aug 19, 2015
1 parent 15b4f37 commit 5bc6b0f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions source/structure/geometry/coord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ ostream & cath::geom::operator<<(ostream &arg_os, ///< TODOCUMENT
return arg_os;
}

/// \brief Build a coord from a coord-populated ptree
///
/// \relates coord
coord cath::geom::coord_from_ptree(const ptree &arg_ptree ///< The ptree from which the coord should be read
) {
return {
arg_ptree.get<double>( "x" ),
arg_ptree.get<double>( "y" ),
arg_ptree.get<double>( "z" )
};
}

/// \brief Save the specified coord to the specified Boost Property Tree ptree
///
/// \relates coord
Expand All @@ -200,6 +212,17 @@ ptree cath::geom::make_ptree_of(const coord &arg_coord ///< The coord that the n
return new_ptree;
}

/// \brief Build a coord from a JSON string (via a ptree)
///
/// \relates coord
coord cath::geom::coord_from_json_string(const string &arg_json_string ///< The JSON string from which the coord should be read
) {
ptree tree;
istringstream in_ss( arg_json_string );
read_json( in_ss, tree);
return coord_from_ptree( tree );
}

/// \brief Create a JSON string to represent the specified coord
///
/// \relates coord
Expand Down
2 changes: 2 additions & 0 deletions source/structure/geometry/coord.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,13 @@ namespace cath {
std::ostream & operator<<(std::ostream &,
const coord &);

coord coord_from_ptree(const boost::property_tree::ptree &);
void save_to_ptree(boost::property_tree::ptree &,
const coord &);

boost::property_tree::ptree make_ptree_of(const coord &);

coord coord_from_json_string(const std::string &);
std::string to_json_string(const coord &,
const bool &arg_pretty_print = true);

Expand Down
16 changes: 16 additions & 0 deletions source/structure/geometry/coord_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ BOOST_AUTO_TEST_CASE(to_json_string_works_for_unit_z) {
BOOST_CHECK_EQUAL( to_json_string( coord::UNIT_Z, false ), R"({"x":"0","y":"0","z":"1"})" "\n" );
}

BOOST_AUTO_TEST_CASE(from_json_string_works_for_origin) {
BOOST_CHECK_EQUAL( coord_from_json_string( R"({"x":"0","y":"0","z":"0"})" ), coord::ORIGIN_COORD );
}

BOOST_AUTO_TEST_CASE(from_json_string_works_for_unit_x) {
BOOST_CHECK_EQUAL( coord_from_json_string( R"({"x":"1","y":"0","z":"0"})" ), coord::UNIT_X );
}

BOOST_AUTO_TEST_CASE(from_json_string_works_for_unit_y) {
BOOST_CHECK_EQUAL( coord_from_json_string( R"({"x":"0","y":"1","z":"0"})" ), coord::UNIT_Y );
}

BOOST_AUTO_TEST_CASE(from_json_string_works_for_unit_z) {
BOOST_CHECK_EQUAL( coord_from_json_string( R"({"x":"0","y":"0","z":"1"})" ), coord::UNIT_Z );
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 5bc6b0f

Please sign in to comment.