| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /* |
| * Copyright (c) 2014-2019, Regents of the University of California, |
| * Arizona Board of Regents, |
| * Colorado State University, |
| * University Pierre & Marie Curie, Sorbonne University, |
| * Washington University in St. Louis, |
| * Beijing Institute of Technology, |
| * The University of Memphis. |
| * |
| * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| * See AUTHORS.md for complete list of NFD authors and contributors. |
| * |
| * NFD is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, |
| * either version 3 of the License, or (at your option) any later version. |
| * |
| * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| * PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License along with |
| * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| |
| #include "nfdc/format-helpers.hpp" |
| |
| #include "tests/test-common.hpp" |
| |
| #if BOOST_VERSION >= 105900 |
| #include <boost/test/tools/output_test_stream.hpp> |
| #else |
| #include <boost/test/output_test_stream.hpp> |
| #endif |
| |
| namespace nfd { |
| namespace tools { |
| namespace nfdc { |
| namespace tests { |
| |
| using boost::test_tools::output_test_stream; |
| |
| BOOST_AUTO_TEST_SUITE(Nfdc) |
| BOOST_AUTO_TEST_SUITE(TestFormatHelpers) |
| |
| BOOST_AUTO_TEST_SUITE(Xml) |
| |
| BOOST_AUTO_TEST_CASE(TextEscaping) |
| { |
| output_test_stream os; |
| os << xml::Text{"\"less than\" & 'greater than' surround XML <element> tag name"}; |
| |
| BOOST_CHECK(os.is_equal(""less than" & 'greater than'" |
| " surround XML <element> tag name")); |
| } |
| |
| BOOST_AUTO_TEST_CASE(Flag) |
| { |
| output_test_stream os; |
| os << "<A>" << xml::Flag{"B", true} << "</A><C>" << xml::Flag{"D", false} << "</C>"; |
| |
| BOOST_CHECK(os.is_equal("<A><B/></A><C></C>")); |
| } |
| |
| BOOST_AUTO_TEST_CASE(DurationFormatting) |
| { |
| time::nanoseconds d1 = 53000_s + 87_ms + 3_us; |
| BOOST_CHECK_EQUAL(xml::formatDuration(d1), "PT53000.087S"); |
| |
| time::nanoseconds d2 = 56_s; |
| BOOST_CHECK_EQUAL(xml::formatDuration(d2), "PT56S"); |
| |
| time::nanoseconds d3 = 3_min; |
| BOOST_CHECK_EQUAL(xml::formatDuration(d3), "PT180S"); |
| |
| time::nanoseconds d4 = 0_ns; |
| BOOST_CHECK_EQUAL(xml::formatDuration(d4), "PT0S"); |
| |
| time::nanoseconds d5 = 1_ms; |
| BOOST_CHECK_EQUAL(xml::formatDuration(d5), "PT0.001S"); |
| |
| time::nanoseconds d6 = -53_s - 250_ms; |
| BOOST_CHECK_EQUAL(xml::formatDuration(d6), "-PT53.250S"); |
| |
| time::nanoseconds d7 = 1_us; |
| BOOST_CHECK_EQUAL(xml::formatDuration(d7), "PT0S"); |
| } |
| |
| BOOST_AUTO_TEST_SUITE_END() // Xml |
| |
| BOOST_AUTO_TEST_SUITE(Text) |
| |
| BOOST_AUTO_TEST_CASE(Space) |
| { |
| output_test_stream os; |
| os << 'A' << text::Spaces{-1} << 'B' << text::Spaces{0} << 'C' << text::Spaces{5} << 'D'; |
| |
| BOOST_CHECK(os.is_equal("ABC D")); |
| } |
| |
| BOOST_AUTO_TEST_CASE(Sep) |
| { |
| output_test_stream os; |
| text::Separator sep(","); |
| for (int i = 1; i <= 3; ++i) { |
| os << sep << i; |
| } |
| |
| BOOST_CHECK(os.is_equal("1,2,3")); |
| } |
| |
| static void |
| printItemAttributes(std::ostream& os, bool wantMultiLine) |
| { |
| text::ItemAttributes ia(wantMultiLine, 3); |
| os << ia("id") << 500 |
| << ia("uri") << "udp4://192.0.2.1:6363" |
| << ia.end(); |
| } |
| |
| BOOST_AUTO_TEST_CASE(ItemAttributesSingleLine) |
| { |
| output_test_stream os; |
| printItemAttributes(os, false); |
| BOOST_CHECK(os.is_equal("id=500 uri=udp4://192.0.2.1:6363")); |
| } |
| |
| BOOST_AUTO_TEST_CASE(ItemAttributesMultiLine) |
| { |
| output_test_stream os; |
| printItemAttributes(os, true); |
| BOOST_CHECK(os.is_equal(" id=500\nuri=udp4://192.0.2.1:6363\n")); |
| } |
| |
| BOOST_AUTO_TEST_CASE(OnOff) |
| { |
| output_test_stream os; |
| os << 'A' << text::OnOff{true} << 'B' << text::OnOff{false} << 'C'; |
| |
| BOOST_CHECK(os.is_equal("AonBoffC")); |
| } |
| |
| BOOST_AUTO_TEST_SUITE_END() // Text |
| |
| BOOST_AUTO_TEST_SUITE_END() // TestFormatHelpers |
| BOOST_AUTO_TEST_SUITE_END() // Nfdc |
| |
| } // namespace tests |
| } // namespace nfdc |
| } // namespace tools |
| } // namespace nfd |