1 // ==========================================================================
2 //                 SeqAn - The Library for Sequence Analysis
3 // ==========================================================================
4 // Copyright (c) 2006-2018, Knut Reinert, FU Berlin
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 //       notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above copyright
13 //       notice, this list of conditions and the following disclaimer in the
14 //       documentation and/or other materials provided with the distribution.
15 //     * Neither the name of Knut Reinert or the FU Berlin nor the names of
16 //       its contributors may be used to endorse or promote products derived
17 //       from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
23 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 //
31 // ==========================================================================
32 // Author: Stephan Aiche <stephan.aiche@fu-berlin.de>
33 // ==========================================================================
34 // Tests for arg_parse/arg_parse_argument.h.
35 // ==========================================================================
36 
37 #ifndef SEQAN_TESTS_ARG_PARSE_TEST_ARG_PARSE_ARGUMENT_H_
38 #define SEQAN_TESTS_ARG_PARSE_TEST_ARG_PARSE_ARGUMENT_H_
39 
40 #include <seqan/basic.h>
41 
42 #include "test_extensions.h"
43 #include <seqan/arg_parse/arg_parse_argument.h>
44 
45 using namespace seqan;
46 
SEQAN_DEFINE_TEST(test_argument_string_type)47 SEQAN_DEFINE_TEST(test_argument_string_type)
48 {
49     ArgParseArgument arg(ArgParseArgument::STRING);
50     SEQAN_ASSERT_EQ(getArgumentType(arg), ArgParseArgument::STRING);
51 }
52 
SEQAN_DEFINE_TEST(test_argument_bool_type)53 SEQAN_DEFINE_TEST(test_argument_bool_type)
54 {
55     ArgParseArgument arg(ArgParseArgument::BOOL);
56     SEQAN_ASSERT_EQ(getArgumentType(arg), ArgParseArgument::BOOL);
57 }
SEQAN_DEFINE_TEST(test_argument_int_type)58 SEQAN_DEFINE_TEST(test_argument_int_type)
59 {
60     ArgParseArgument arg(ArgParseArgument::INTEGER);
61     SEQAN_ASSERT_EQ(getArgumentType(arg), ArgParseArgument::INTEGER);
62 }
63 
SEQAN_DEFINE_TEST(test_argument_int64_type)64 SEQAN_DEFINE_TEST(test_argument_int64_type)
65 {
66     ArgParseArgument arg(ArgParseArgument::INT64);
67     SEQAN_ASSERT_EQ(getArgumentType(arg), ArgParseArgument::INT64);
68 }
69 
SEQAN_DEFINE_TEST(test_argument_double_type)70 SEQAN_DEFINE_TEST(test_argument_double_type)
71 {
72     ArgParseArgument arg(ArgParseArgument::DOUBLE);
73     SEQAN_ASSERT_EQ(getArgumentType(arg), ArgParseArgument::DOUBLE);
74 }
75 
SEQAN_DEFINE_TEST(test_argument_inputfile_type)76 SEQAN_DEFINE_TEST(test_argument_inputfile_type)
77 {
78     ArgParseArgument arg(ArgParseArgument::INPUT_FILE);
79     SEQAN_ASSERT_EQ(getArgumentType(arg), ArgParseArgument::INPUT_FILE);
80 }
81 
SEQAN_DEFINE_TEST(test_argument_outputfile_type)82 SEQAN_DEFINE_TEST(test_argument_outputfile_type)
83 {
84     ArgParseArgument arg(ArgParseArgument::OUTPUT_FILE);
85     SEQAN_ASSERT_EQ(getArgumentType(arg), ArgParseArgument::OUTPUT_FILE);
86 }
87 
SEQAN_DEFINE_TEST(test_argument_inputprefix_type)88 SEQAN_DEFINE_TEST(test_argument_inputprefix_type)
89 {
90     ArgParseArgument arg(ArgParseArgument::INPUT_PREFIX);
91     SEQAN_ASSERT_EQ(getArgumentType(arg), ArgParseArgument::INPUT_PREFIX);
92 }
93 
SEQAN_DEFINE_TEST(test_argument_outputprefix_type)94 SEQAN_DEFINE_TEST(test_argument_outputprefix_type)
95 {
96     ArgParseArgument arg(ArgParseArgument::OUTPUT_PREFIX);
97     SEQAN_ASSERT_EQ(getArgumentType(arg), ArgParseArgument::OUTPUT_PREFIX);
98 }
99 
SEQAN_DEFINE_TEST(test_argument_inputdirectory_type)100 SEQAN_DEFINE_TEST(test_argument_inputdirectory_type)
101 {
102     ArgParseArgument arg(ArgParseArgument::INPUT_DIRECTORY);
103     SEQAN_ASSERT_EQ(getArgumentType(arg), ArgParseArgument::INPUT_DIRECTORY);
104 }
105 
SEQAN_DEFINE_TEST(test_argument_outputdirectory_type)106 SEQAN_DEFINE_TEST(test_argument_outputdirectory_type)
107 {
108     ArgParseArgument arg(ArgParseArgument::OUTPUT_DIRECTORY);
109     SEQAN_ASSERT_EQ(getArgumentType(arg), ArgParseArgument::OUTPUT_DIRECTORY);
110 }
111 
SEQAN_DEFINE_TEST(test_argument_label)112 SEQAN_DEFINE_TEST(test_argument_label)
113 {
114     ArgParseArgument arg(ArgParseArgument::STRING, "my_label");
115     SEQAN_ASSERT_EQ(getArgumentLabel(arg), "my_label");
116 }
117 
SEQAN_DEFINE_TEST(test_argument_invalid_cast)118 SEQAN_DEFINE_TEST(test_argument_invalid_cast)
119 {
120     ArgParseArgument doubleArg(ArgParseArgument::DOUBLE);
121 
122     _checkValue(doubleArg, "6.0221418e23");
123     _assignArgumentValue(doubleArg, "6.0221418e23");
124     _checkValue(doubleArg);
125     SEQAN_ASSERT_EQ(getArgumentValue(doubleArg), "6.0221418e23");
126 
127     _checkValue(doubleArg, "-6.022");
128     _assignArgumentValue(doubleArg, "-6.022");
129     _checkValue(doubleArg);
130     SEQAN_ASSERT_EQ(getArgumentValue(doubleArg), "-6.022");
131 
132     // Test _checkValue() with value.
133     SEQAN_TEST_EXCEPTION_WITH_MESSAGE(ParseError,
134                          _checkValue(doubleArg, "-6.022aaa"),
135                          "the given value '-6.022aaa' cannot be casted to double");
136 
137     // Test _checkValue() after assignment.
138     _assignArgumentValue(doubleArg, "-6.022aaa");
139     SEQAN_TEST_EXCEPTION_WITH_MESSAGE(ParseError,
140                          _checkValue(doubleArg),
141                          "the given value '-6.022aaa' cannot be casted to double");
142 }
143 
SEQAN_DEFINE_TEST(test_argument_min_max_boundaries)144 SEQAN_DEFINE_TEST(test_argument_min_max_boundaries)
145 {
146     ArgParseArgument arg(ArgParseArgument::DOUBLE);
147     setMinValue(arg, "1");
148     setMaxValue(arg, "6.0221418e23");
149 
150     _checkValue(arg, "1");
151     _assignArgumentValue(arg, "1"); // should just work without throwing any exception
152     _checkValue(arg);
153     SEQAN_ASSERT_EQ(getArgumentValue(arg), "1");
154     // Test _checkValue() with argument and value.
155     SEQAN_TEST_EXCEPTION_WITH_MESSAGE(ParseError,
156                          _checkValue(arg, "-1"),
157                          "the given value '-1' is not in the interval [1:6.0221418e23]");
158     // Test _checkValue() after assignment.
159     _assignArgumentValue(arg, "-1");
160     SEQAN_TEST_EXCEPTION_WITH_MESSAGE(ParseError,
161                          _checkValue(arg),
162                          "the given value '-1' is not in the interval [1:6.0221418e23]");
163 
164     // Tests _checkValue() with argument and value.
165     SEQAN_TEST_EXCEPTION_WITH_MESSAGE(ParseError,
166                          _checkValue(arg, "6.0321418e23"),
167                          "the given value '6.0321418e23' is not in the interval [1:6.0221418e23]");
168     // Test _checkValue() after assignment.
169     _assignArgumentValue(arg, "6.0321418e23");
170     SEQAN_TEST_EXCEPTION_WITH_MESSAGE(ParseError,
171                          _checkValue(arg),
172                          "the given value '6.0321418e23' is not in the interval [1:6.0221418e23]");
173 }
174 
SEQAN_DEFINE_TEST(test_argument_valid_values)175 SEQAN_DEFINE_TEST(test_argument_valid_values)
176 {
177     ArgParseArgument arg(ArgParseArgument::STRING);
178     setValidValues(arg, "this that");
179 
180     _checkValue(arg, "this");
181     _assignArgumentValue(arg, "this");
182     _checkValue(arg);
183     SEQAN_ASSERT_EQ(getArgumentValue(arg), "this");
184     // Test _checkValue() with argument and value.
185     SEQAN_TEST_EXCEPTION_WITH_MESSAGE(ParseError,
186                          _checkValue(arg, "not-this-or-that"),
187                          "the given value 'not-this-or-that' is not in the list of allowed values [this, that]");
188     // Test _checkValue after assignment.
189     _assignArgumentValue(arg, "not-this-or-that");
190     SEQAN_TEST_EXCEPTION_WITH_MESSAGE(ParseError,
191                          _checkValue(arg),
192                          "the given value 'not-this-or-that' is not in the list of allowed values [this, that]");
193 
194     ArgParseArgument filearg(ArgParseArgument::INPUT_FILE);
195     setValidValues(filearg, ".txt .fasta");
196 
197     _assignArgumentValue(filearg, "textfile.txt");
198     SEQAN_ASSERT_EQ(value(filearg.value, 0), "textfile.txt");
199 
200     // Test getFileExtension() function.
201     SEQAN_ASSERT_EQ(getFileExtension(filearg), ".txt");
202 
203     // different case should also work
204     _assignArgumentValue(filearg, "textfile.tXT");
205     SEQAN_ASSERT_EQ(value(filearg.value, 0), "textfile.tXT");
206 
207     // Test getFileExtension() function.
208     SEQAN_ASSERT_EQ(getFileExtension(filearg), ".txt");
209 
210     // Test getFileExtension() function with explicit file extension.
211     filearg._fileExtensions.push_back(".fa");
212     SEQAN_ASSERT_EQ(getFileExtension(filearg), ".fa");
213     SEQAN_ASSERT_EQ(getFileExtension(filearg, 0), ".fa");
214 
215     // Test _checkValue() with argument and value.
216     SEQAN_TEST_EXCEPTION_WITH_MESSAGE(ParseError,
217                          _checkValue(filearg, "not-a-validfile.qxt"),
218                          "the given path 'not-a-validfile.qxt' does not have one of the valid file extensions [*.txt, *.fasta]; the file extension was overridden to be '.fa'");
219     // Test _checkValue after assignment.
220     _assignArgumentValue(filearg, "not-a-validfile.qxt");
221     SEQAN_TEST_EXCEPTION_WITH_MESSAGE(ParseError,
222                          _checkValue(filearg),
223                          "the given path 'not-a-validfile.qxt' does not have one of the valid file extensions [*.txt, *.fasta]; the file extension was overridden to be '.fa'");
224 }
225 
SEQAN_DEFINE_TEST(test_argument_valid_values_directories)226 SEQAN_DEFINE_TEST(test_argument_valid_values_directories)
227 {
228     ArgParseArgument dirarg(ArgParseArgument::INPUT_DIRECTORY);
229     setValidValues(dirarg, ".dir1 .dir2");
230 
231     _assignArgumentValue(dirarg, "directory.dir1");
232     SEQAN_ASSERT_EQ(value(dirarg.value, 0), "directory.dir1");
233 
234     // Test getFileExtension() function.
235     SEQAN_ASSERT_EQ(getFileExtension(dirarg), ".dir1");
236 
237     // different case should also work
238     _assignArgumentValue(dirarg, "directory.DIR1");
239     SEQAN_ASSERT_EQ(value(dirarg.value, 0), "directory.DIR1");
240 
241     // also accept a trailing '/'
242     _assignArgumentValue(dirarg, "directory.dir2/");
243     SEQAN_ASSERT_EQ(value(dirarg.value, 0), "directory.dir2/");
244 
245     // Test getFileExtension() function.
246     SEQAN_ASSERT_EQ(getFileExtension(dirarg), ".dir2");
247 
248     // different case should also work
249     _assignArgumentValue(dirarg, "directory.DIR2/");
250     SEQAN_ASSERT_EQ(value(dirarg.value, 0), "directory.DIR2/");
251 
252 }
253 
254 #endif // SEQAN_TESTS_ARG_PARSE_TEST_ARG_PARSE_ARGUMENT_H_
255