blob: d5719ae5b961efcb3daae0a0a2b8f7122cb5e1db [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/*
3 * Copyright (c) 2014-2018, 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
30namespace nfd {
31namespace tools {
Junxiao Shi331ade72016-08-19 14:07:19 +000032namespace nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000033namespace tests {
34
35using boost::test_tools::output_test_stream;
36
Junxiao Shi331ade72016-08-19 14:07:19 +000037BOOST_AUTO_TEST_SUITE(Nfdc)
Junxiao Shi38f4ce92016-08-04 10:01:52 +000038BOOST_AUTO_TEST_SUITE(TestFormatHelpers)
39
40BOOST_AUTO_TEST_SUITE(Xml)
41
42BOOST_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("&quot;less than&quot; &amp; &apos;greater than&apos;"
48 " surround XML &lt;element&gt; tag name"));
49}
50
Junxiao Shi7a36ac72018-03-21 15:23:22 +000051BOOST_AUTO_TEST_CASE(Flag)
52{
53 output_test_stream os;
54 os << "<A>" << xml::Flag{"B", true} << "</A><C>" << xml::Flag{"D", false} << "</C>";
55
56 BOOST_CHECK(os.is_equal("<A><B/></A><C></C>"));
57}
58
Eric Newberryde332452018-01-30 11:45:32 -070059BOOST_AUTO_TEST_CASE(DurationFormatting)
60{
61 time::nanoseconds d1 = 53000_s + 87_ms + 3_us;
62 BOOST_CHECK_EQUAL(xml::formatDuration(d1), "PT53000.087S");
63
64 time::nanoseconds d2 = 56_s;
65 BOOST_CHECK_EQUAL(xml::formatDuration(d2), "PT56S");
66
67 time::nanoseconds d3 = 3_min;
68 BOOST_CHECK_EQUAL(xml::formatDuration(d3), "PT180S");
69
70 time::nanoseconds d4 = 0_ns;
71 BOOST_CHECK_EQUAL(xml::formatDuration(d4), "PT0S");
72
73 time::nanoseconds d5 = 1_ms;
74 BOOST_CHECK_EQUAL(xml::formatDuration(d5), "PT0.001S");
75
76 time::nanoseconds d6 = -53_s - 250_ms;
77 BOOST_CHECK_EQUAL(xml::formatDuration(d6), "-PT53.250S");
78
79 time::nanoseconds d7 = 1_us;
80 BOOST_CHECK_EQUAL(xml::formatDuration(d7), "PT0S");
81}
82
Junxiao Shi38f4ce92016-08-04 10:01:52 +000083BOOST_AUTO_TEST_SUITE_END() // Xml
84
85BOOST_AUTO_TEST_SUITE(Text)
86
Junxiao Shi6c135622016-11-21 14:30:33 +000087BOOST_AUTO_TEST_CASE(Space)
88{
89 output_test_stream os;
90 os << 'A' << text::Spaces{-1} << 'B' << text::Spaces{0} << 'C' << text::Spaces{5} << 'D';
91
92 BOOST_CHECK(os.is_equal("ABC D"));
93}
94
Junxiao Shi38f4ce92016-08-04 10:01:52 +000095BOOST_AUTO_TEST_CASE(Sep)
96{
97 output_test_stream os;
98 text::Separator sep(",");
99 for (int i = 1; i <= 3; ++i) {
100 os << sep << i;
101 }
102
103 BOOST_CHECK(os.is_equal("1,2,3"));
104}
105
Junxiao Shi056815e2017-01-29 16:39:19 +0000106static void
107printItemAttributes(std::ostream& os, bool wantMultiLine)
108{
109 text::ItemAttributes ia(wantMultiLine, 3);
110 os << ia("id") << 500
111 << ia("uri") << "udp4://192.0.2.1:6363"
112 << ia.end();
113}
114
115BOOST_AUTO_TEST_CASE(ItemAttributesSingleLine)
116{
117 output_test_stream os;
118 printItemAttributes(os, false);
119 BOOST_CHECK(os.is_equal("id=500 uri=udp4://192.0.2.1:6363"));
120}
121
122BOOST_AUTO_TEST_CASE(ItemAttributesMultiLine)
123{
124 output_test_stream os;
125 printItemAttributes(os, true);
126 BOOST_CHECK(os.is_equal(" id=500\nuri=udp4://192.0.2.1:6363\n"));
127}
128
Junxiao Shi8dc473a2018-03-02 15:10:18 +0000129BOOST_AUTO_TEST_CASE(OnOff)
130{
131 output_test_stream os;
132 os << 'A' << text::OnOff{true} << 'B' << text::OnOff{false} << 'C';
133
134 BOOST_CHECK(os.is_equal("AonBoffC"));
135}
136
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000137BOOST_AUTO_TEST_SUITE_END() // Text
138
139BOOST_AUTO_TEST_SUITE_END() // TestFormatHelpers
Junxiao Shi331ade72016-08-19 14:07:19 +0000140BOOST_AUTO_TEST_SUITE_END() // Nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000141
142} // namespace tests
Junxiao Shi331ade72016-08-19 14:07:19 +0000143} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000144} // namespace tools
145} // namespace nfd