Skip to content

Commit b8e7cbf

Browse files
committed
Add unit test for DocoptParser::StaticGetRequiredSizeT
1 parent 0c41bb5 commit b8e7cbf

3 files changed

Lines changed: 47 additions & 10 deletions

File tree

libFileArb/Components/Docopt/DocoptParser.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,14 @@ size_t DocoptParser::StaticGetRequiredSizeT(
148148
const map<string, docopt::value>& docoptArgs, const string& argName)
149149
{
150150
const docopt::value& docoptValue = Map::At(docoptArgs, argName);
151-
size_t sizeTValue = docoptValue.asSizeT();
152-
return sizeTValue;
151+
if (docoptValue.isSizeT())
152+
{
153+
size_t sizeTArgumentValue = docoptValue.asSizeT();
154+
return sizeTArgumentValue;
155+
}
156+
const string exceptionMessage = Utils::String::ConcatStrings(
157+
"Key[", argName, "] found in map but with non-size_t value");
158+
throw invalid_argument(exceptionMessage);
153159
}
154160

155161
string DocoptParser::StaticGetRequiredString(const map<string, docopt::value>& docoptArgs, const string& argName)
@@ -160,6 +166,7 @@ string DocoptParser::StaticGetRequiredString(const map<string, docopt::value>& d
160166
const string& stringArgumentValue = docoptValue.asString();
161167
return stringArgumentValue;
162168
}
163-
const string exceptionMessage = Utils::String::ConcatValues("String key not found in map: [", argName, "]");
169+
const string exceptionMessage = Utils::String::ConcatStrings(
170+
"Key[", argName, "] found in map but with non-string value");
164171
throw invalid_argument(exceptionMessage);
165172
}

libFileArb/docopt/docopt_value.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ namespace docopt
380380
}
381381
return ret;
382382
}
383-
throwIfNotKind(Kind::Long);
384-
return variant_.longValue;
383+
throwIfNotKind(Kind::SizeT);
384+
return variant_.sizeTValue;
385385
}
386386

387387
inline const std::string& value::asString() const

libFileArbTests/Components/Docopt/DocoptParserTests.cpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ AFACT(GetRequiredSizeT_DoesSo)
3434
AFACT(GetRequiredFilePathWhichMustExist_DoesSo)
3535
AFACT(GetRequiredFolderPathWhichNeedNotExist_DoesSo)
3636
// Private Functions
37-
AFACT(StaticGetRequiredString_ArgNotInMap_ThrowsOutOfRangeException)
37+
AFACT(StaticGetRequiredSizeT_ArgNotInMap_ThrowsInvalidArgumentException)
38+
AFACT(StaticGetRequiredSizeT_ArgInMapAsNoneValue_ThrowsInvalidArgumentException)
39+
AFACT(StaticGetRequiredSizeT_ArgInMapAsSizeTValue_ReturnsValue)
40+
41+
AFACT(StaticGetRequiredString_ArgNotInMap_ThrowsInvalidArgumentException)
3842
AFACT(StaticGetRequiredString_ArgInMapAsNoneValue_ThrowsInvalidArgumentException)
3943
AFACT(StaticGetRequiredString_ArgInMapAsStringValue_ReturnsValue)
4044
EVIDENCE
@@ -50,7 +54,6 @@ Utils::FileSystemPatherMock* _fileSystemPatherMock = nullptr;
5054
map<string, docopt::value> _docoptArgs;
5155
string _argName;
5256
string _expectedKeyNotFoundInMapExceptionMessage;
53-
string _expectedStringKeyNotFoundInMapExceptionMessage;
5457

5558
STARTUP
5659
{
@@ -63,7 +66,6 @@ STARTUP
6366
_docoptArgs = ZenUnit::RandomOrderedMap<string, docopt::value>();
6467
_argName = ZenUnit::Random<string>() + "_argName";
6568
_expectedKeyNotFoundInMapExceptionMessage = Utils::String::ConcatStrings("Error: Key not found in map: [", _argName, "]");
66-
_expectedStringKeyNotFoundInMapExceptionMessage = Utils::String::ConcatStrings("String key not found in map: [", _argName, "]");
6769
}
6870

6971
TEST(DefaultConstructor_SetsFieldsToDefaultValues)
@@ -289,7 +291,33 @@ TEST(GetRequiredFolderPathWhichNeedNotExist_DoesSo)
289291

290292
// Private Functions
291293

292-
TEST(StaticGetRequiredString_ArgNotInMap_ThrowsOutOfRangeException)
294+
TEST(StaticGetRequiredSizeT_ArgNotInMap_ThrowsInvalidArgumentException)
295+
{
296+
THROWS_EXCEPTION(_docoptParser.StaticGetRequiredSizeT(_docoptArgs, _argName),
297+
invalid_argument, _expectedKeyNotFoundInMapExceptionMessage);
298+
}
299+
300+
TEST(StaticGetRequiredSizeT_ArgInMapAsNoneValue_ThrowsInvalidArgumentException)
301+
{
302+
_docoptArgs[_argName] = docopt::value();
303+
//
304+
const string expectedExceptionMessage = Utils::String::ConcatStrings(
305+
"Key[", _argName, "] found in map but with non-size_t value");
306+
THROWS_EXCEPTION(_docoptParser.StaticGetRequiredSizeT(_docoptArgs, _argName),
307+
invalid_argument, expectedExceptionMessage);
308+
}
309+
310+
TEST(StaticGetRequiredSizeT_ArgInMapAsSizeTValue_ReturnsValue)
311+
{
312+
const size_t sizeTArgumentValue = ZenUnit::Random<size_t>();
313+
_docoptArgs[_argName] = docopt::value(sizeTArgumentValue);
314+
//
315+
const size_t returnedSizeTArgumentValue = _docoptParser.StaticGetRequiredSizeT(_docoptArgs, _argName);
316+
//
317+
ARE_EQUAL(sizeTArgumentValue, returnedSizeTArgumentValue);
318+
}
319+
320+
TEST(StaticGetRequiredString_ArgNotInMap_ThrowsInvalidArgumentException)
293321
{
294322
THROWS_EXCEPTION(_docoptParser.StaticGetRequiredString(_docoptArgs, _argName),
295323
invalid_argument, _expectedKeyNotFoundInMapExceptionMessage);
@@ -299,8 +327,10 @@ TEST(StaticGetRequiredString_ArgInMapAsNoneValue_ThrowsInvalidArgumentException)
299327
{
300328
_docoptArgs[_argName] = docopt::value();
301329
//
330+
const string expectedExceptionMessage = Utils::String::ConcatStrings(
331+
"Key[", _argName, "] found in map but with non-string value");
302332
THROWS_EXCEPTION(_docoptParser.StaticGetRequiredString(_docoptArgs, _argName),
303-
invalid_argument, _expectedStringKeyNotFoundInMapExceptionMessage);
333+
invalid_argument, expectedExceptionMessage);
304334
}
305335

306336
TEST(StaticGetRequiredString_ArgInMapAsStringValue_ReturnsValue)

0 commit comments

Comments
 (0)