Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 26 | #include "nfdc/format-helpers.hpp" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace tools { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 32 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 33 | namespace tests { |
| 34 | |
| 35 | using boost::test_tools::output_test_stream; |
| 36 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Nfdc) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(TestFormatHelpers) |
| 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(Xml) |
| 41 | |
| 42 | BOOST_AUTO_TEST_CASE(TextEscaping) |
| 43 | { |
| 44 | output_test_stream os; |
| 45 | os << xml::Text{"\"less than\" & 'greater than' surround XML <element> tag name"}; |
| 46 | |
| 47 | BOOST_CHECK(os.is_equal(""less than" & 'greater than'" |
| 48 | " surround XML <element> tag name")); |
| 49 | } |
| 50 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 51 | BOOST_AUTO_TEST_CASE(DurationFormatting) |
| 52 | { |
| 53 | time::nanoseconds d1 = 53000_s + 87_ms + 3_us; |
| 54 | BOOST_CHECK_EQUAL(xml::formatDuration(d1), "PT53000.087S"); |
| 55 | |
| 56 | time::nanoseconds d2 = 56_s; |
| 57 | BOOST_CHECK_EQUAL(xml::formatDuration(d2), "PT56S"); |
| 58 | |
| 59 | time::nanoseconds d3 = 3_min; |
| 60 | BOOST_CHECK_EQUAL(xml::formatDuration(d3), "PT180S"); |
| 61 | |
| 62 | time::nanoseconds d4 = 0_ns; |
| 63 | BOOST_CHECK_EQUAL(xml::formatDuration(d4), "PT0S"); |
| 64 | |
| 65 | time::nanoseconds d5 = 1_ms; |
| 66 | BOOST_CHECK_EQUAL(xml::formatDuration(d5), "PT0.001S"); |
| 67 | |
| 68 | time::nanoseconds d6 = -53_s - 250_ms; |
| 69 | BOOST_CHECK_EQUAL(xml::formatDuration(d6), "-PT53.250S"); |
| 70 | |
| 71 | time::nanoseconds d7 = 1_us; |
| 72 | BOOST_CHECK_EQUAL(xml::formatDuration(d7), "PT0S"); |
| 73 | } |
| 74 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 75 | BOOST_AUTO_TEST_SUITE_END() // Xml |
| 76 | |
| 77 | BOOST_AUTO_TEST_SUITE(Text) |
| 78 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 79 | BOOST_AUTO_TEST_CASE(Space) |
| 80 | { |
| 81 | output_test_stream os; |
| 82 | os << 'A' << text::Spaces{-1} << 'B' << text::Spaces{0} << 'C' << text::Spaces{5} << 'D'; |
| 83 | |
| 84 | BOOST_CHECK(os.is_equal("ABC D")); |
| 85 | } |
| 86 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 87 | BOOST_AUTO_TEST_CASE(Sep) |
| 88 | { |
| 89 | output_test_stream os; |
| 90 | text::Separator sep(","); |
| 91 | for (int i = 1; i <= 3; ++i) { |
| 92 | os << sep << i; |
| 93 | } |
| 94 | |
| 95 | BOOST_CHECK(os.is_equal("1,2,3")); |
| 96 | } |
| 97 | |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 98 | static void |
| 99 | printItemAttributes(std::ostream& os, bool wantMultiLine) |
| 100 | { |
| 101 | text::ItemAttributes ia(wantMultiLine, 3); |
| 102 | os << ia("id") << 500 |
| 103 | << ia("uri") << "udp4://192.0.2.1:6363" |
| 104 | << ia.end(); |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_CASE(ItemAttributesSingleLine) |
| 108 | { |
| 109 | output_test_stream os; |
| 110 | printItemAttributes(os, false); |
| 111 | BOOST_CHECK(os.is_equal("id=500 uri=udp4://192.0.2.1:6363")); |
| 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE(ItemAttributesMultiLine) |
| 115 | { |
| 116 | output_test_stream os; |
| 117 | printItemAttributes(os, true); |
| 118 | BOOST_CHECK(os.is_equal(" id=500\nuri=udp4://192.0.2.1:6363\n")); |
| 119 | } |
| 120 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 121 | BOOST_AUTO_TEST_SUITE_END() // Text |
| 122 | |
| 123 | BOOST_AUTO_TEST_SUITE_END() // TestFormatHelpers |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 124 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 125 | |
| 126 | } // namespace tests |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 127 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 128 | } // namespace tools |
| 129 | } // namespace nfd |