1
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9 // Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28 //
29
30 // appleseed.foundation headers.
31 #include "foundation/utility/containers/dictionary.h"
32 #include "foundation/utility/log.h"
33 #include "foundation/utility/settings.h"
34 #include "foundation/utility/test.h"
35 #include "foundation/utility/testutils.h"
36
37 // Standard headers.
38 #include <string>
39
40 using namespace foundation;
41 using namespace std;
42
TEST_SUITE(Foundation_Utility_SettingsFileReader)43 TEST_SUITE(Foundation_Utility_SettingsFileReader)
44 {
45 struct Fixture
46 {
47 Logger m_logger;
48 SettingsFileReader m_reader;
49 Dictionary m_dictionary;
50
51 Fixture()
52 : m_reader(m_logger)
53 {
54 }
55
56 bool read(const char* filename)
57 {
58 return
59 m_reader.read(
60 filename,
61 "../../../schemas/settings.xsd", // path relative to input file
62 m_dictionary);
63 }
64 };
65
66 TEST_CASE_F(Read_GivenEmptySettingsFile_ReturnsEmptyDictionary, Fixture)
67 {
68 const bool succeeded = read("unit tests/inputs/test_settings_emptysettingsfile.xml");
69 ASSERT_TRUE(succeeded);
70
71 EXPECT_TRUE(m_dictionary.empty());
72 }
73
74 TEST_CASE_F(Read_GivenSettingsFileWithTwoScalarParameters_ReturnsDictionaryWithTwoScalarParameters, Fixture)
75 {
76 const bool succeeded = read("unit tests/inputs/test_settings_settingsfilewithtwoscalarparameters.xml");
77 ASSERT_TRUE(succeeded);
78
79 ASSERT_EQ(2, m_dictionary.strings().size());
80
81 EXPECT_EQ(42, m_dictionary.get<int>("x"));
82 EXPECT_EQ("foo", m_dictionary.get<string>("y"));
83 }
84
85 TEST_CASE_F(Read_GivenSettingsFileWithTwoDictionaryParameters_ReturnsDictionaryWithTwoDictionaryParameters, Fixture)
86 {
87 const bool succeeded = read("unit tests/inputs/test_settings_settingsfilewithtwodictionaryparameters.xml");
88 ASSERT_TRUE(succeeded);
89
90 ASSERT_EQ(0, m_dictionary.strings().size());
91 ASSERT_EQ(2, m_dictionary.dictionaries().size());
92
93 const Dictionary& sub1 = m_dictionary.dictionaries().get("sub1");
94 EXPECT_EQ(42, sub1.get<int>("x"));
95 EXPECT_EQ("foo", sub1.get<string>("y"));
96
97 const Dictionary& sub2 = m_dictionary.dictionaries().get("sub2");
98 EXPECT_EQ("aa", sub2.get<string>("a"));
99 EXPECT_EQ("bb", sub2.get<string>("b"));
100 }
101
102 TEST_CASE_F(Read_GivenSettingsFileWithNewlinesInParameters_ReturnsDictionaryWithNewlinesInParameters, Fixture)
103 {
104 const bool succeeded = read("unit tests/inputs/test_settings_settingsfilewithnewlinesinparameters.xml");
105 ASSERT_TRUE(succeeded);
106
107 ASSERT_EQ(2, m_dictionary.strings().size());
108 ASSERT_EQ(0, m_dictionary.dictionaries().size());
109
110 EXPECT_EQ("aa", m_dictionary.get<string>("a"));
111 EXPECT_EQ("bb\nbb\nbb", m_dictionary.get<string>("b"));
112 }
113 }
114
TEST_SUITE(Foundation_Utility_SettingsFileWriter)115 TEST_SUITE(Foundation_Utility_SettingsFileWriter)
116 {
117 TEST_CASE(Write_GivenEmptyDictionary_WriteEmptySettingsFile)
118 {
119 Dictionary dictionary;
120
121 SettingsFileWriter writer;
122 writer.write("unit tests/outputs/test_settings_emptysettingsfile.xml", dictionary);
123
124 const bool identical =
125 compare_text_files(
126 "unit tests/inputs/test_settings_emptysettingsfile.xml",
127 "unit tests/outputs/test_settings_emptysettingsfile.xml");
128
129 EXPECT_TRUE(identical);
130 }
131
132 TEST_CASE(Write_GivenDictionaryWithTwoScalarParameters_WritesSettingsFileWithTwoScalarParameters)
133 {
134 Dictionary dictionary;
135 dictionary.insert("x", 42);
136 dictionary.insert("y", "foo");
137
138 SettingsFileWriter writer;
139 writer.write("unit tests/outputs/test_settings_settingsfilewithtwoscalarparameters.xml", dictionary);
140
141 const bool identical =
142 compare_text_files(
143 "unit tests/inputs/test_settings_settingsfilewithtwoscalarparameters.xml",
144 "unit tests/outputs/test_settings_settingsfilewithtwoscalarparameters.xml");
145
146 EXPECT_TRUE(identical);
147 }
148
149 TEST_CASE(Write_GivenDictionaryWithTwoDictionaryParameters_WritesSettingsFileWithTwoDictionaryParameters)
150 {
151 Dictionary sub1;
152 sub1.insert("x", 42);
153 sub1.insert("y", "foo");
154
155 Dictionary sub2;
156 sub2.insert("a", "aa");
157 sub2.insert("b", "bb");
158
159 Dictionary dictionary;
160 dictionary.insert("sub1", sub1);
161 dictionary.insert("sub2", sub2);
162
163 SettingsFileWriter writer;
164 writer.write("unit tests/outputs/test_settings_settingsfilewithtwodictionaryparameters.xml", dictionary);
165
166 const bool identical =
167 compare_text_files(
168 "unit tests/inputs/test_settings_settingsfilewithtwodictionaryparameters.xml",
169 "unit tests/outputs/test_settings_settingsfilewithtwodictionaryparameters.xml");
170
171 EXPECT_TRUE(identical);
172 }
173
174 TEST_CASE(Write_GivenDictionaryWithNewlinesInParameters_WriteSettingsFileWithNewlinesInParameters)
175 {
176 Dictionary dictionary;
177 dictionary.insert("a", "aa");
178 dictionary.insert("b", "bb\nbb\nbb");
179
180 SettingsFileWriter writer;
181 writer.write("unit tests/outputs/test_settings_settingsfilewithnewlinesinparameters.xml", dictionary);
182
183 const bool identical =
184 compare_text_files(
185 "unit tests/inputs/test_settings_settingsfilewithnewlinesinparameters.xml",
186 "unit tests/outputs/test_settings_settingsfilewithnewlinesinparameters.xml");
187
188 EXPECT_TRUE(identical);
189 }
190 }
191