Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / options / tests / abstractoptionstorage.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Tests proper handling of option storage.
38  *
39  * These tests check that methods in storage objects are called properly in all
40  * situations, and also that the OptionStorageTemplate class behaves properly.
41  *
42  * \author Teemu Murtola <teemu.murtola@gmail.com>
43  * \ingroup module_options
44  */
45 #include <vector>
46 #include <string>
47
48 #include <gmock/gmock.h>
49 #include <gtest/gtest.h>
50
51 #include "gromacs/options/abstractoption.h"
52 #include "gromacs/options/options.h"
53 #include "gromacs/options/optionstoragetemplate.h"
54 #include "gromacs/options/optionsassigner.h"
55 #include "gromacs/utility/exceptions.h"
56
57 #include "testutils/testasserts.h"
58 #include "testutils/testexceptions.h"
59
60 namespace
61 {
62
63 class MockOption;
64 class MockOptionStorage;
65
66 class MockOptionInfo : public gmx::OptionInfo
67 {
68     public:
69         //! Creates an option info object for the given option.
70         explicit MockOptionInfo(MockOptionStorage *option);
71
72         MockOptionStorage &option();
73 };
74
75 /*! \brief
76  * Mock implementation of an option storage class for unit testing.
77  *
78  * Provides facilities for checking that correct methods are called, and for
79  * controlling how they add values using the base class methods.
80  *
81  * \ingroup module_options
82  */
83 class MockOptionStorage : public gmx::OptionStorageTemplate<std::string>
84 {
85     public:
86         /*! \brief
87          * Initializes the storage from option settings.
88          *
89          * \param[in] settings   Storage settings.
90          */
91         MockOptionStorage(const MockOption &settings);
92
93         /*! \brief
94          * Calls addValue("dummy") in the base class.
95          */
96         void addDummyValue()
97         {
98             addValue("dummy");
99         }
100         // using MyBase::markAsSet;
101         // using MyBase::addValue;
102         // using MyBase::commitValues;
103         // "using" is correct but MSVC gives error C2248. Workaround:
104         //! \copydoc gmx::OptionStorageTemplate::markAsSet()
105         void markAsSet()
106         {
107             MyBase::markAsSet();
108         }
109         //! \copydoc gmx::OptionStorageTemplate::addValue()
110         void addValue(const std::string &value)
111         {
112             MyBase::addValue(value);
113         }
114         //! \copydoc gmx::OptionStorageTemplate::commitValues()
115         void commitValues()
116         {
117             MyBase::commitValues();
118         }
119
120         virtual gmx::OptionInfo &optionInfo() { return info_; }
121         // These are not used.
122         virtual const char *typeString() const { return "mock"; }
123         virtual std::string formatSingleValue(const std::string & /*value*/) const
124         {
125             return "";
126         }
127
128         MOCK_METHOD1(convertValue, void(const std::string &value));
129         MOCK_METHOD1(processSetValues, void(ValueList *values));
130         MOCK_METHOD0(processAll, void());
131
132     private:
133         MockOptionInfo          info_;
134 };
135
136 /*! \internal \brief
137  * Specifies an option that has a mock storage object for unit testing.
138  *
139  * \ingroup module_options
140  */
141 class MockOption : public gmx::OptionTemplate<std::string, MockOption>
142 {
143     public:
144         //! OptionInfo subclass corresponding to this option type.
145         typedef MockOptionInfo InfoType;
146
147         //! Initializes an option with the given name.
148         explicit MockOption(const char *name)
149             : MyBase(name)
150         {
151         }
152
153     private:
154         virtual gmx::AbstractOptionStoragePointer createStorage() const
155         {
156             return gmx::AbstractOptionStoragePointer(new MockOptionStorage(*this));
157         }
158 };
159
160 MockOptionStorage::MockOptionStorage(const MockOption &settings)
161     : MyBase(settings), info_(this)
162 {
163     using ::testing::_;
164     using ::testing::Invoke;
165     using ::testing::WithArg;
166     ON_CALL(*this, convertValue(_))
167         .WillByDefault(WithArg<0>(Invoke(this, &MockOptionStorage::addValue)));
168 }
169
170 MockOptionInfo::MockOptionInfo(MockOptionStorage *option)
171     : gmx::OptionInfo(option)
172 {
173 }
174
175 MockOptionStorage &MockOptionInfo::option()
176 {
177     return static_cast<MockOptionStorage &>(gmx::OptionInfo::option());
178 }
179
180 /*
181  * Tests that finish() can set a required option even if the user has not
182  * provided it.
183  */
184 TEST(AbstractOptionStorageTest, HandlesSetInFinish)
185 {
186     gmx::Options                options(NULL, NULL);
187     std::vector<std::string>    values;
188     MockOptionInfo             *info = options.addOption(
189                 MockOption("name").required().storeVector(&values));
190     MockOptionStorage          *mock = &info->option();
191     {
192         ::testing::InSequence dummy;
193         using ::testing::DoAll;
194         using ::testing::InvokeWithoutArgs;
195         EXPECT_CALL(*mock, processAll())
196             .WillOnce(DoAll(InvokeWithoutArgs(mock, &MockOptionStorage::markAsSet),
197                             InvokeWithoutArgs(mock, &MockOptionStorage::addDummyValue),
198                             InvokeWithoutArgs(mock, &MockOptionStorage::commitValues)));
199     }
200
201     gmx::OptionsAssigner assigner(&options);
202     EXPECT_NO_THROW_GMX(assigner.start());
203     EXPECT_NO_THROW_GMX(assigner.finish());
204     EXPECT_NO_THROW_GMX(options.finish());
205
206     ASSERT_EQ(1U, values.size());
207     EXPECT_EQ("dummy", values[0]);
208 }
209
210 /*
211  * Tests that storage works if the storage object does not add a value in a
212  * call to appendValue().
213  */
214 TEST(AbstractOptionStorageTest, HandlesValueRemoval)
215 {
216     gmx::Options                options(NULL, NULL);
217     std::vector<std::string>    values;
218     MockOptionInfo             *info = options.addOption(
219                 MockOption("name").storeVector(&values).multiValue());
220     MockOptionStorage          *mock = &info->option();
221     {
222         ::testing::InSequence dummy;
223         using ::testing::ElementsAre;
224         using ::testing::Pointee;
225         using ::testing::Return;
226         EXPECT_CALL(*mock, convertValue("a"));
227         EXPECT_CALL(*mock, convertValue("b"))
228             .WillOnce(Return());
229         EXPECT_CALL(*mock, convertValue("c"));
230         EXPECT_CALL(*mock, processSetValues(Pointee(ElementsAre("a", "c"))));
231         EXPECT_CALL(*mock, processAll());
232     }
233
234     gmx::OptionsAssigner assigner(&options);
235     EXPECT_NO_THROW_GMX(assigner.start());
236     ASSERT_NO_THROW_GMX(assigner.startOption("name"));
237     EXPECT_NO_THROW_GMX(assigner.appendValue("a"));
238     EXPECT_NO_THROW_GMX(assigner.appendValue("b"));
239     EXPECT_NO_THROW_GMX(assigner.appendValue("c"));
240     EXPECT_NO_THROW_GMX(assigner.finishOption());
241     EXPECT_NO_THROW_GMX(assigner.finish());
242     EXPECT_NO_THROW_GMX(options.finish());
243
244     ASSERT_EQ(2U, values.size());
245     EXPECT_EQ("a", values[0]);
246     EXPECT_EQ("c", values[1]);
247 }
248
249 /*
250  * Tests that storage works if the storage object adds more than one value in
251  * one call to appendValue().
252  */
253 TEST(AbstractOptionStorageTest, HandlesValueAddition)
254 {
255     gmx::Options                options(NULL, NULL);
256     std::vector<std::string>    values;
257     MockOptionInfo             *info = options.addOption(
258                 MockOption("name").storeVector(&values).multiValue());
259     MockOptionStorage          *mock = &info->option();
260     {
261         ::testing::InSequence dummy;
262         using ::testing::DoAll;
263         using ::testing::ElementsAre;
264         using ::testing::InvokeWithoutArgs;
265         using ::testing::Pointee;
266         EXPECT_CALL(*mock, convertValue("a"));
267         EXPECT_CALL(*mock, convertValue("b"))
268             .WillOnce(DoAll(InvokeWithoutArgs(mock, &MockOptionStorage::addDummyValue),
269                             InvokeWithoutArgs(mock, &MockOptionStorage::addDummyValue)));
270         EXPECT_CALL(*mock, processSetValues(Pointee(ElementsAre("a", "dummy", "dummy"))));
271         EXPECT_CALL(*mock, processAll());
272     }
273
274     gmx::OptionsAssigner assigner(&options);
275     EXPECT_NO_THROW_GMX(assigner.start());
276     ASSERT_NO_THROW_GMX(assigner.startOption("name"));
277     EXPECT_NO_THROW_GMX(assigner.appendValue("a"));
278     EXPECT_NO_THROW_GMX(assigner.appendValue("b"));
279     EXPECT_NO_THROW_GMX(assigner.finishOption());
280     EXPECT_NO_THROW_GMX(assigner.finish());
281     EXPECT_NO_THROW_GMX(options.finish());
282
283     ASSERT_EQ(3U, values.size());
284     EXPECT_EQ("a", values[0]);
285     EXPECT_EQ("dummy", values[1]);
286     EXPECT_EQ("dummy", values[2]);
287 }
288
289 /*
290  * Tests that storage works if the storage object adds more than one value in
291  * one call to appendValue(), and this results in too many values.
292  */
293 TEST(AbstractOptionStorageTest, HandlesTooManyValueAddition)
294 {
295     gmx::Options                options(NULL, NULL);
296     std::vector<std::string>    values;
297     MockOptionInfo             *info = options.addOption(
298                 MockOption("name").storeVector(&values).valueCount(2));
299     MockOptionStorage          *mock = &info->option();
300     {
301         ::testing::InSequence dummy;
302         using ::testing::DoAll;
303         using ::testing::ElementsAre;
304         using ::testing::InvokeWithoutArgs;
305         using ::testing::Pointee;
306         EXPECT_CALL(*mock, convertValue("a"));
307         EXPECT_CALL(*mock, convertValue("b"))
308             .WillOnce(DoAll(InvokeWithoutArgs(mock, &MockOptionStorage::addDummyValue),
309                             InvokeWithoutArgs(mock, &MockOptionStorage::addDummyValue)));
310         EXPECT_CALL(*mock, processAll());
311     }
312
313     gmx::OptionsAssigner assigner(&options);
314     EXPECT_NO_THROW_GMX(assigner.start());
315     ASSERT_NO_THROW_GMX(assigner.startOption("name"));
316     EXPECT_NO_THROW_GMX(assigner.appendValue("a"));
317     EXPECT_THROW_GMX(assigner.appendValue("b"), gmx::InvalidInputError);
318     EXPECT_NO_THROW_GMX(assigner.finishOption());
319     EXPECT_NO_THROW_GMX(assigner.finish());
320     EXPECT_NO_THROW_GMX(options.finish());
321
322     ASSERT_TRUE(values.empty());
323 }
324
325 /*
326  * Tests that the storage object is properly invoked even if no output values
327  * should be produced.
328  */
329 TEST(AbstractOptionStorageTest, AllowsEmptyValues)
330 {
331     gmx::Options                options(NULL, NULL);
332     std::vector<std::string>    values;
333     MockOptionInfo             *info = options.addOption(
334                 MockOption("name").storeVector(&values).valueCount(0));
335     MockOptionStorage          *mock = &info->option();
336     {
337         ::testing::InSequence dummy;
338         using ::testing::DoAll;
339         using ::testing::ElementsAre;
340         using ::testing::Pointee;
341         using ::testing::Return;
342         EXPECT_CALL(*mock, convertValue("a"))
343             .WillOnce(Return());
344         EXPECT_CALL(*mock, processSetValues(Pointee(ElementsAre())));
345         EXPECT_CALL(*mock, processAll());
346     }
347
348     gmx::OptionsAssigner assigner(&options);
349     EXPECT_NO_THROW_GMX(assigner.start());
350     EXPECT_NO_THROW_GMX(assigner.startOption("name"));
351     EXPECT_NO_THROW_GMX(assigner.appendValue("a"));
352     EXPECT_NO_THROW_GMX(assigner.finishOption());
353     EXPECT_NO_THROW_GMX(assigner.finish());
354     EXPECT_NO_THROW_GMX(options.finish());
355
356     ASSERT_EQ(0U, values.size());
357 }
358
359 } // namespace