Line data Source code
1 : /**
2 : * @file PrettyPrinterTests.cpp
3 : * @copyright (c) 2014 by Petr Zemek (s3rvac@gmail.com) and contributors
4 : * @license BSD, see the @c LICENSE file for more details
5 : * @brief Tests for the PrettyPrinter class.
6 : */
7 :
8 : #include <gtest/gtest.h>
9 :
10 : #include "BDictionary.h"
11 : #include "BInteger.h"
12 : #include "BList.h"
13 : #include "BString.h"
14 : #include "PrettyPrinter.h"
15 : #include "TestUtils.h"
16 :
17 : namespace bencoding {
18 : namespace tests {
19 :
20 : using namespace testing;
21 :
22 12 : class PrettyPrinterTests: public Test {
23 : protected:
24 12 : PrettyPrinterTests(): printer(PrettyPrinter::create()) {}
25 :
26 : protected:
27 : std::unique_ptr<PrettyPrinter> printer;
28 : };
29 :
30 : //
31 : // Dictionary representation.
32 : //
33 :
34 5 : TEST_F(PrettyPrinterTests,
35 : PrettyReprOfEmptyDictionaryIsCorrect) {
36 2 : std::shared_ptr<BDictionary> bDictionary(BDictionary::create());
37 :
38 1 : EXPECT_EQ("{\n}", printer->getPrettyRepr(bDictionary));
39 1 : }
40 :
41 5 : TEST_F(PrettyPrinterTests,
42 : PrettyReprOfDictionarysWithOneItemIsCorrect) {
43 2 : std::shared_ptr<BDictionary> bDictionary(BDictionary::create());
44 1 : (*bDictionary)[BString::create("test")] = BInteger::create(1);
45 :
46 1 : EXPECT_EQ("{\n \"test\": 1\n}", printer->getPrettyRepr(bDictionary));
47 1 : }
48 :
49 5 : TEST_F(PrettyPrinterTests,
50 : PrettyReprOfDictionarysWithTwoItemsIsCorrect) {
51 2 : std::shared_ptr<BDictionary> bDictionary(BDictionary::create());
52 1 : (*bDictionary)[BString::create("test1")] = BInteger::create(1);
53 1 : (*bDictionary)[BString::create("test2")] = BInteger::create(2);
54 :
55 1 : EXPECT_EQ("{\n \"test1\": 1,\n \"test2\": 2\n}",
56 1 : printer->getPrettyRepr(bDictionary));
57 1 : }
58 :
59 : //
60 : // Integer representation.
61 : //
62 :
63 5 : TEST_F(PrettyPrinterTests,
64 : PrettyReprOfIntegerZeroIsCorrect) {
65 2 : std::shared_ptr<BItem> data(BInteger::create(0));
66 :
67 1 : EXPECT_EQ("0", printer->getPrettyRepr(data));
68 1 : }
69 :
70 5 : TEST_F(PrettyPrinterTests,
71 : PrettyReprOfIntegerWithPositiveValueIsCorrect) {
72 2 : std::shared_ptr<BItem> data(BInteger::create(13));
73 :
74 1 : EXPECT_EQ("13", printer->getPrettyRepr(data));
75 1 : }
76 :
77 5 : TEST_F(PrettyPrinterTests,
78 : PrettyReprOfIntegerWithNegativeValueIsCorrect) {
79 2 : std::shared_ptr<BItem> data(BInteger::create(-13));
80 :
81 1 : EXPECT_EQ("-13", printer->getPrettyRepr(data));
82 1 : }
83 :
84 : //
85 : // List representation.
86 : //
87 :
88 5 : TEST_F(PrettyPrinterTests,
89 : PrettyReprOfEmptyListIsCorrect) {
90 2 : std::shared_ptr<BList> bList(BList::create());
91 :
92 1 : EXPECT_EQ("[\n]", printer->getPrettyRepr(bList));
93 1 : }
94 :
95 5 : TEST_F(PrettyPrinterTests,
96 : PrettyReprOfListWithTwoStringsIsCorrect) {
97 2 : std::shared_ptr<BList> bList = BList::create();
98 1 : bList->push_back(BString::create("test"));
99 1 : bList->push_back(BString::create("hello"));
100 :
101 1 : EXPECT_EQ("[\n \"test\",\n \"hello\"\n]",
102 1 : printer->getPrettyRepr(bList));
103 1 : }
104 :
105 : //
106 : // String representation.
107 : //
108 :
109 5 : TEST_F(PrettyPrinterTests,
110 : PrettyReprOfEmptyStringIsCorrect) {
111 2 : std::shared_ptr<BItem> data(BString::create(""));
112 :
113 1 : EXPECT_EQ(R"("")", printer->getPrettyRepr(data));
114 1 : }
115 :
116 5 : TEST_F(PrettyPrinterTests,
117 : PrettyReprOfNonemptyStringIsCorrect) {
118 2 : std::shared_ptr<BItem> data(BString::create("test"));
119 :
120 1 : EXPECT_EQ(R"("test")", printer->getPrettyRepr(data));
121 1 : }
122 :
123 5 : TEST_F(PrettyPrinterTests,
124 : QuoteInsideStringIsPrefixedWithBackslash) {
125 2 : std::shared_ptr<BItem> data(BString::create("te\"st"));
126 :
127 1 : EXPECT_EQ(R"("te\"st")", printer->getPrettyRepr(data));
128 1 : }
129 :
130 : //
131 : // Other.
132 : //
133 :
134 5 : TEST_F(PrettyPrinterTests,
135 : GetPrettyReprFunctionWorksAsCreatingPrettyPrinterAndCallingGetPrettyRepr) {
136 2 : std::shared_ptr<BDictionary> bDictionary(BDictionary::create());
137 1 : (*bDictionary)[BString::create("test")] = BInteger::create(1);
138 :
139 1 : EXPECT_EQ("{\n \"test\": 1\n}", getPrettyRepr(bDictionary));
140 1 : }
141 :
142 : } // namespace tests
143 3 : } // namespace bencoding
|