1//
2// ValidatorTest.cpp
3//
4// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "ValidatorTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Util/RegExpValidator.h"
15#include "Poco/Util/IntValidator.h"
16#include "Poco/Util/Option.h"
17#include "Poco/Util/OptionException.h"
18#include "Poco/AutoPtr.h"
19
20
21using Poco::Util::Validator;
22using Poco::Util::RegExpValidator;
23using Poco::Util::IntValidator;
24using Poco::Util::Option;
25using Poco::Util::InvalidArgumentException;
26using Poco::AutoPtr;
27
28
29ValidatorTest::ValidatorTest(const std::string& name): CppUnit::TestCase(name)
30{
31}
32
33
34ValidatorTest::~ValidatorTest()
35{
36}
37
38
39void ValidatorTest::testRegExpValidator()
40{
41 Option option("option", "o");
42 AutoPtr<Validator> pVal(new RegExpValidator("[0-9]+"));
43
44 pVal->validate(option, "0");
45 pVal->validate(option, "12345");
46
47 try
48 {
49 pVal->validate(option, " 234");
50 fail("does not match - must throw");
51 }
52 catch (InvalidArgumentException& exc)
53 {
54 std::string s(exc.message());
55 assertTrue (s.find("argument for option does not match regular expression [0-9]+") != std::string::npos);
56 }
57
58 try
59 {
60 pVal->validate(option, "234asdf");
61 fail("does not match - must throw");
62 }
63 catch (InvalidArgumentException& exc)
64 {
65 std::string s(exc.message());
66 assertTrue (s.find("argument for option does not match regular expression [0-9]+") != std::string::npos);
67 }
68
69 try
70 {
71 pVal->validate(option, "abc");
72 fail("does not match - must throw");
73 }
74 catch (InvalidArgumentException& exc)
75 {
76 std::string s(exc.message());
77 assertTrue (s.find("argument for option does not match regular expression [0-9]+") != std::string::npos);
78 }
79
80 try
81 {
82 pVal->validate(option, "");
83 fail("does not match - must throw");
84 }
85 catch (InvalidArgumentException& exc)
86 {
87 std::string s(exc.message());
88 assertTrue (s.find("argument for option does not match regular expression [0-9]+") != std::string::npos);
89 }
90}
91
92
93void ValidatorTest::testIntValidator()
94{
95 Option option("option", "o");
96 AutoPtr<Validator> pVal(new IntValidator(0, 100));
97
98 pVal->validate(option, "0");
99 pVal->validate(option, "100");
100 pVal->validate(option, "55");
101
102 try
103 {
104 pVal->validate(option, "-1");
105 fail("out of range - must throw");
106 }
107 catch (InvalidArgumentException& exc)
108 {
109 std::string s(exc.message());
110 assertTrue (s.find("argument for option must be in range 0 to 100") != std::string::npos);
111 }
112
113 try
114 {
115 pVal->validate(option, "101");
116 fail("out of range - must throw");
117 }
118 catch (InvalidArgumentException& exc)
119 {
120 std::string s(exc.message());
121 assertTrue (s.find("argument for option must be in range 0 to 100") != std::string::npos);
122 }
123
124 try
125 {
126 pVal->validate(option, "asdf");
127 fail("not a number - must throw");
128 }
129 catch (InvalidArgumentException& exc)
130 {
131 std::string s(exc.message());
132 assertTrue (s.find("argument for option must be an integer") != std::string::npos);
133 }
134}
135
136
137void ValidatorTest::setUp()
138{
139}
140
141
142void ValidatorTest::tearDown()
143{
144}
145
146
147CppUnit::Test* ValidatorTest::suite()
148{
149 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ValidatorTest");
150
151 CppUnit_addTest(pSuite, ValidatorTest, testRegExpValidator);
152 CppUnit_addTest(pSuite, ValidatorTest, testIntValidator);
153
154 return pSuite;
155}
156