-
Notifications
You must be signed in to change notification settings - Fork 0
/
openquestionfactory.cpp
185 lines (164 loc) · 4.24 KB
/
openquestionfactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "openquestionfactory.h"
#include <cassert>
#include <stdexcept>
#include <boost/algorithm/string/split.hpp>
#include <boost/make_shared.hpp>
#include "container.h"
#include "openquestion.h"
ribi::OpenQuestionFactory::OpenQuestionFactory()
{
}
boost::shared_ptr<ribi::OpenQuestion>
ribi::OpenQuestionFactory::Create(const std::string& s
) const
{
if (s.empty())
{
throw std::logic_error("An open question must contain text");
}
if (s[0] == ',')
{
throw std::logic_error("An open question must not start with a comma");
}
if (s[s.size() - 1] == ',')
{
throw std::logic_error("An open question must not end with a comma");
}
if (s.find(",,") != std::string::npos)
{
throw std::logic_error("An open question cannot contain two consecutive commas");
}
const auto v = Container().SeperateString(s,',');
if (v.size() != 3)
{
throw std::logic_error("An open question has exactly three comma-seperated elements");
}
const auto filename = v[0];
const auto question = v[1];
const auto answers = Container().SeperateString(v[2],'/');
if (answers.size() == 0)
{
throw std::logic_error("An open question has at least one correct answer");
}
return Create(filename,question,answers);
}
boost::shared_ptr<ribi::OpenQuestion>
ribi::OpenQuestionFactory::Create(
const std::string& filename,
const std::string& question,
const std::vector<std::string>& answers
) const noexcept
{
boost::shared_ptr<OpenQuestion> openquestion(
new OpenQuestion(
filename,question,answers
)
);
return openquestion;
//assert(!filename.empty() && "Filename must not be empty");
//assert(FileExists(filename) == true && "File must exists");
//assert(!question.empty() && "OpenQuestion must not be empty");
//assert(!correct_answers.empty() && "Correct answer must not be empty");
}
std::vector<std::string>
ribi::OpenQuestionFactory::GetInvalidOpenQuestionStrings() const noexcept
{
return {
"-,1+1=,2,3", //Incorrect options are
"-,1+1=", //No answer
"-", //No question
"tmp.png", //No question
"", //Nothing
",tmp.png,1+1=,2", //Start with comma
"tmp.png,,1+1=,2", //Two consecutive comma's
"tmp.png,1+1=,,2", //Two consecutive comma's
"tmp.png,1+1=,2,", //Two consecutive comma's
"tmp.png,1+1=,2,", //End with comma
",tmp.png,1+1=,2,", //Start and end with comma
",,tmp.png,1+1=,2,",
",tmp.png,,1+1=,2,",
",tmp.png,1+1=,,2,",
",tmp.png,1+1=,2,,",
",",
",,",
",,,",
",,,,",
",,,,,",
",,,,,,"
};
}
std::vector<boost::shared_ptr<ribi::OpenQuestion>>
ribi::OpenQuestionFactory::GetTestOpenQuestions() const noexcept
{
std::vector<boost::shared_ptr<OpenQuestion>> v;
for (const auto& s: GetValidOpenQuestionStrings())
{
v.push_back(Create(s));
}
return v;
}
std::vector<std::string>
ribi::OpenQuestionFactory::GetValidOpenQuestionStrings() const noexcept
{
return {
"-,1+1=,2",
"tmp.png,1+1=,2/Two",
"-,1+1=,Two/2/two"
};
}
std::string ribi::OpenQuestionFactory::GetVersion() noexcept
{
return "1.0";
}
std::vector<std::string> ribi::OpenQuestionFactory::GetVersionHistory() noexcept
{
return {
"2014-06-05: version 1.0: initial version"
};
}
void ribi::TestOpenQuestionFactory() noexcept
{
OpenQuestionFactory f;
try
{
const auto q = f.Create(f.GetExampleOpenQuestionString());
assert(q);
}
catch (std::exception& e)
{
assert("OpenQuestionFactory::GetExampleOpenQuestion()"
&& "must yield a valid OpenQuestion");
}
//Test valid multiple choice questions for validity
{
for (const std::string& s: f.GetValidOpenQuestionStrings())
{
try
{
const auto q = f.Create(s);
assert(q); //To make the compiler happy
//OK
}
catch (std::exception& e)
{
assert(!"Valid questions must be accepted");
}
}
}
//Test invalid multiple choice questions for invalidity
{
for (const std::string& s: f.GetInvalidOpenQuestionStrings())
{
try
{
const auto q = f.Create(s);
assert(!"Invalid questions must be rejected");
assert(q); //To make the compiler happy
}
catch (std::exception& e)
{
//OK
}
}
}
}