blob: 26c9b1bbf2272c12a6c20fe7f9d10965a2223bfa [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberryde332452018-01-30 11:45:32 -07002/*
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi38f4ce92016-08-04 10:01:52 +00004 * 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 Shi331ade72016-08-19 14:07:19 +000026#include "nfdc/format-helpers.hpp"
Junxiao Shi38f4ce92016-08-04 10:01:52 +000027
28#include "tests/test-common.hpp"
29
Davide Pesaventocf9e1c72019-12-22 17:56:07 -050030#include <boost/test/tools/output_test_stream.hpp>
Davide Pesaventocf9e1c72019-12-22 17:56:07 -050031
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::tools::nfdc::tests {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000033
34using boost::test_tools::output_test_stream;
35
Junxiao Shi331ade72016-08-19 14:07:19 +000036BOOST_AUTO_TEST_SUITE(Nfdc)
Junxiao Shi38f4ce92016-08-04 10:01:52 +000037BOOST_AUTO_TEST_SUITE(TestFormatHelpers)
38
39BOOST_AUTO_TEST_SUITE(Xml)
40
41BOOST_AUTO_TEST_CASE(TextEscaping)
42{
43 output_test_stream os;
44 os << xml::Text{"\"less than\" & 'greater than' surround XML <element> tag name"};
45
46 BOOST_CHECK(os.is_equal("&quot;less than&quot; &amp; &apos;greater than&apos;"
47 " surround XML &lt;element&gt; tag name"));
48}
49
Junxiao Shi7a36ac72018-03-21 15:23:22 +000050BOOST_AUTO_TEST_CASE(Flag)
51{
52 output_test_stream os;
53 os << "<A>" << xml::Flag{"B", true} << "</A><C>" << xml::Flag{"D", false} << "</C>";
54
55 BOOST_CHECK(os.is_equal("<A><B/></A><C></C>"));
56}
57
Eric Newberryde332452018-01-30 11:45:32 -070058BOOST_AUTO_TEST_CASE(DurationFormatting)
59{
60 time::nanoseconds d1 = 53000_s + 87_ms + 3_us;
61 BOOST_CHECK_EQUAL(xml::formatDuration(d1), "PT53000.087S");
62
63 time::nanoseconds d2 = 56_s;
64 BOOST_CHECK_EQUAL(xml::formatDuration(d2), "PT56S");
65
66 time::nanoseconds d3 = 3_min;
67 BOOST_CHECK_EQUAL(xml::formatDuration(d3), "PT180S");
68
69 time::nanoseconds d4 = 0_ns;
70 BOOST_CHECK_EQUAL(xml::formatDuration(d4), "PT0S");
71
72 time::nanoseconds d5 = 1_ms;
73 BOOST_CHECK_EQUAL(xml::formatDuration(d5), "PT0.001S");
74
75 time::nanoseconds d6 = -53_s - 250_ms;
76 BOOST_CHECK_EQUAL(xml::formatDuration(d6), "-PT53.250S");
77
78 time::nanoseconds d7 = 1_us;
79 BOOST_CHECK_EQUAL(xml::formatDuration(d7), "PT0S");
80}
81
Junxiao Shi38f4ce92016-08-04 10:01:52 +000082BOOST_AUTO_TEST_SUITE_END() // Xml
83
84BOOST_AUTO_TEST_SUITE(Text)
85
Junxiao Shi6c135622016-11-21 14:30:33 +000086BOOST_AUTO_TEST_CASE(Space)
87{
88 output_test_stream os;
89 os << 'A' << text::Spaces{-1} << 'B' << text::Spaces{0} << 'C' << text::Spaces{5} << 'D';
90
91 BOOST_CHECK(os.is_equal("ABC D"));
92}
93
Junxiao Shi38f4ce92016-08-04 10:01:52 +000094BOOST_AUTO_TEST_CASE(Sep)
95{
96 output_test_stream os;
97 text::Separator sep(",");
98 for (int i = 1; i <= 3; ++i) {
99 os << sep << i;
100 }
101
102 BOOST_CHECK(os.is_equal("1,2,3"));
103}
104
Junxiao Shi056815e2017-01-29 16:39:19 +0000105static void
106printItemAttributes(std::ostream& os, bool wantMultiLine)
107{
108 text::ItemAttributes ia(wantMultiLine, 3);
109 os << ia("id") << 500
110 << ia("uri") << "udp4://192.0.2.1:6363"
111 << ia.end();
112}
113
114BOOST_AUTO_TEST_CASE(ItemAttributesSingleLine)
115{
116 output_test_stream os;
117 printItemAttributes(os, false);
118 BOOST_CHECK(os.is_equal("id=500 uri=udp4://192.0.2.1:6363"));
119}
120
121BOOST_AUTO_TEST_CASE(ItemAttributesMultiLine)
122{
123 output_test_stream os;
124 printItemAttributes(os, true);
125 BOOST_CHECK(os.is_equal(" id=500\nuri=udp4://192.0.2.1:6363\n"));
126}
127
Junxiao Shi8dc473a2018-03-02 15:10:18 +0000128BOOST_AUTO_TEST_CASE(OnOff)
129{
130 output_test_stream os;
131 os << 'A' << text::OnOff{true} << 'B' << text::OnOff{false} << 'C';
132
133 BOOST_CHECK(os.is_equal("AonBoffC"));
134}
135
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000136BOOST_AUTO_TEST_SUITE_END() // Text
137
138BOOST_AUTO_TEST_SUITE_END() // TestFormatHelpers
Junxiao Shi331ade72016-08-19 14:07:19 +0000139BOOST_AUTO_TEST_SUITE_END() // Nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000140
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400141} // namespace nfd::tools::nfdc::tests