Writing more Unit tests

Add InRangeVerifier and NotInRangeVerifier
Add static_asserts to prohibit nonsensical verifier combinations
This commit is contained in:
Alexander Bock
2016-09-13 19:29:00 +02:00
parent 9c7bd2af67
commit faf180ab7d
3 changed files with 158 additions and 21 deletions
+59 -1
View File
@@ -32,6 +32,7 @@
#include <map>
#include <memory>
#include <string>
#include <type_traits>
#include <vector>
namespace openspace {
@@ -99,7 +100,7 @@ struct StringVerifier : public TemplateVerifier<std::string> {
};
struct TableVerifier : public TemplateVerifier<ghoul::Dictionary> {
TableVerifier(Documentation d);
TableVerifier(Documentation d = {});
TestResult operator()(const ghoul::Dictionary& dict, const std::string& key) const override;
@@ -112,6 +113,10 @@ struct TableVerifier : public TemplateVerifier<ghoul::Dictionary> {
template <typename T>
struct LessVerifier : public T {
static_assert(!std::is_base_of_v<BoolVerifier, T>, "T cannot be BoolVerifier");
static_assert(!std::is_base_of_v<StringVerifier, T>, "T cannot be StringVerifier");
static_assert(!std::is_base_of_v<TableVerifier, T>, "T cannot be TableVerifier");
LessVerifier(typename T::Type value);
bool test(const ghoul::Dictionary& dict, const std::string& key) const override;
@@ -123,6 +128,10 @@ struct LessVerifier : public T {
template <typename T>
struct LessEqualVerifier : public T {
static_assert(!std::is_base_of_v<BoolVerifier, T>, "T cannot be BoolVerifier");
static_assert(!std::is_base_of_v<StringVerifier, T>, "T cannot be StringVerifier");
static_assert(!std::is_base_of_v<TableVerifier, T>, "T cannot be TableVerifier");
LessEqualVerifier(typename T::Type value);
bool test(const ghoul::Dictionary& dict, const std::string& key) const override;
@@ -134,6 +143,10 @@ struct LessEqualVerifier : public T {
template <typename T>
struct GreaterVerifier : public T {
static_assert(!std::is_base_of_v<BoolVerifier, T>, "T cannot be BoolVerifier");
static_assert(!std::is_base_of_v<StringVerifier, T>, "T cannot be StringVerifier");
static_assert(!std::is_base_of_v<TableVerifier, T>, "T cannot be TableVerifier");
GreaterVerifier(typename T::Type value);
bool test(const ghoul::Dictionary& dict, const std::string& key) const override;
@@ -145,6 +158,10 @@ struct GreaterVerifier : public T {
template <typename T>
struct GreaterEqualVerifier : public T {
static_assert(!std::is_base_of_v<BoolVerifier, T>, "T cannot be BoolVerifier");
static_assert(!std::is_base_of_v<StringVerifier, T>, "T cannot be StringVerifier");
static_assert(!std::is_base_of_v<TableVerifier, T>, "T cannot be TableVerifier");
GreaterEqualVerifier(typename T::Type value);
bool test(const ghoul::Dictionary& dict, const std::string& key) const override;
@@ -156,6 +173,8 @@ struct GreaterEqualVerifier : public T {
template <typename T>
struct EqualVerifier : public T {
static_assert(!std::is_base_of_v<TableVerifier, T>, "T cannot be TableVerifier");
EqualVerifier(typename T::Type value);
bool test(const ghoul::Dictionary& dict, const std::string& key) const override;
@@ -167,6 +186,8 @@ struct EqualVerifier : public T {
template <typename T>
struct UnequalVerifier : public T {
static_assert(!std::is_base_of_v<TableVerifier, T>, "T cannot be TableVerifier");
UnequalVerifier(typename T::Type value);
bool test(const ghoul::Dictionary& dict, const std::string& key) const override;
@@ -180,6 +201,8 @@ struct UnequalVerifier : public T {
template <typename T>
struct InListVerifier : public T {
static_assert(!std::is_base_of_v<TableVerifier, T>, "T cannot be TableVerifier");
InListVerifier(std::vector<typename T::Type> values);
bool test(const ghoul::Dictionary& dict, const std::string& key) const override;
@@ -191,6 +214,8 @@ struct InListVerifier : public T {
template <typename T>
struct NotInListVerifier : public T {
static_assert(!std::is_base_of_v<TableVerifier, T>, "T cannot be TableVerifier");
NotInListVerifier(std::vector<typename T::Type> values);
bool test(const ghoul::Dictionary& dict, const std::string& key) const override;
@@ -200,6 +225,39 @@ struct NotInListVerifier : public T {
std::vector<typename T::Type> values;
};
// Range Verifiers
template <typename T>
struct InRangeVerifier : public T {
static_assert(!std::is_base_of_v<BoolVerifier, T>, "T cannot be BoolVerifier");
static_assert(!std::is_base_of_v<StringVerifier, T>, "T cannot be StringVerifier");
static_assert(!std::is_base_of_v<TableVerifier, T>, "T cannot be TableVerifier");
InRangeVerifier(typename T::Type lower, typename T::Type upper);
bool test(const ghoul::Dictionary& dict, const std::string& key) const override;
std::string documentation() const override;
typename T::Type lower;
typename T::Type upper;
};
template <typename T>
struct NotInRangeVerifier : public T {
static_assert(!std::is_base_of_v<BoolVerifier, T>, "T cannot be BoolVerifier");
static_assert(!std::is_base_of_v<StringVerifier, T>, "T cannot be StringVerifier");
static_assert(!std::is_base_of_v<TableVerifier, T>, "T cannot be TableVerifier");
NotInRangeVerifier(typename T::Type lower, typename T::Type upper);
bool test(const ghoul::Dictionary& dict, const std::string& key) const override;
std::string documentation() const override;
typename T::Type lower;
typename T::Type upper;
};
// Misc Verifiers
template <typename T>
+81 -9
View File
@@ -22,11 +22,18 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
namespace std {
std::string to_string(std::string value);
}
namespace openspace {
namespace documentation {
template <typename T>
LessVerifier<T>::LessVerifier(typename T::Type value) : value(std::move(value)) {}
LessVerifier<T>::LessVerifier(typename T::Type value)
: value(std::move(value))
{
}
template <typename T>
bool LessVerifier<T>::test(const ghoul::Dictionary& dict, const std::string& key) const {
@@ -40,7 +47,9 @@ std::string LessVerifier<T>::documentation() const {
template <typename T>
LessEqualVerifier<T>::LessEqualVerifier(typename T::Type value) : value(std::move(value)) {}
LessEqualVerifier<T>::LessEqualVerifier(typename T::Type value)
: value(std::move(value))
{}
template <typename T>
bool LessEqualVerifier<T>::test(const ghoul::Dictionary& dict, const std::string& key) const {
@@ -53,7 +62,9 @@ std::string LessEqualVerifier<T>::documentation() const {
}
template <typename T>
GreaterVerifier<T>::GreaterVerifier(typename T::Type value) : value(std::move(value)) {}
GreaterVerifier<T>::GreaterVerifier(typename T::Type value)
: value(std::move(value))
{}
template <typename T>
bool GreaterVerifier<T>::test(const ghoul::Dictionary& dict, const std::string& key) const {
@@ -66,7 +77,9 @@ std::string GreaterVerifier<T>::documentation() const {
}
template <typename T>
GreaterEqualVerifier<T>::GreaterEqualVerifier(typename T::Type value) : value(std::move(value)) {}
GreaterEqualVerifier<T>::GreaterEqualVerifier(typename T::Type value)
: value(std::move(value))
{}
template <typename T>
bool GreaterEqualVerifier<T>::test(const ghoul::Dictionary& dict, const std::string& key) const {
@@ -79,7 +92,9 @@ std::string GreaterEqualVerifier<T>::documentation() const {
}
template <typename T>
EqualVerifier<T>::EqualVerifier(typename T::Type value) : value(std::move(value)) {}
EqualVerifier<T>::EqualVerifier(typename T::Type value)
: value(std::move(value))
{}
template <typename T>
bool EqualVerifier<T>::test(const ghoul::Dictionary& dict, const std::string& key) const {
@@ -92,7 +107,9 @@ std::string EqualVerifier<T>::documentation() const {
}
template <typename T>
UnequalVerifier<T>::UnequalVerifier(typename T::Type value) : value(std::move(value)) {}
UnequalVerifier<T>::UnequalVerifier(typename T::Type value)
: value(std::move(value))
{}
template <typename T>
bool UnequalVerifier<T>::test(const ghoul::Dictionary& dict, const std::string& key) const {
@@ -100,12 +117,14 @@ bool UnequalVerifier<T>::test(const ghoul::Dictionary& dict, const std::string&
}
template <typename T>
std::string UnequalVerifier<T>::documentation() const override {
std::string UnequalVerifier<T>::documentation() const {
return T::documentation() + '\n' + "Unequal to: " + std::to_string(value);
}
template <typename T>
InListVerifier<T>::InListVerifier(std::vector<typename T::Type> values) : values(std::move(values)) {}
InListVerifier<T>::InListVerifier(std::vector<typename T::Type> values)
: values(std::move(values))
{}
template <typename T>
bool InListVerifier<T>::test(const ghoul::Dictionary& dict, const std::string& key) const {
@@ -136,7 +155,9 @@ std::string InListVerifier<T>::documentation() const {
}
template <typename T>
NotInListVerifier<T>::NotInListVerifier(std::vector<typename T::Type> values) : values(std::move(values)) {}
NotInListVerifier<T>::NotInListVerifier(std::vector<typename T::Type> values)
: values(std::move(values))
{}
template <typename T>
bool NotInListVerifier<T>::test(const ghoul::Dictionary& dict, const std::string& key) const {
@@ -167,6 +188,57 @@ std::string NotInListVerifier<T>::documentation() const {
return result;
}
template <typename T>
InRangeVerifier<T>::InRangeVerifier(typename T::Type lower, typename T::Type upper)
: lower(std::move(lower))
, upper(std::move(upper))
{
ghoul_assert(lower <= upper, "Lower value must be smaller or equal to upper value");
}
template <typename T>
bool InRangeVerifier<T>::test(const ghoul::Dictionary& d, const std::string& key) const {
if (T::test(d, key)) {
typename T::Type val = d.value<typename T::Type>(key);
return val >= lower && val <= upper;
}
else {
return false;
}
}
template <typename T>
std::string InRangeVerifier<T>::documentation() const {
return T::documentation() + '\n' + "In range: (" + std::to_string(lower) + "," +
std::to_string(upper) + ")";
}
template <typename T>
NotInRangeVerifier<T>::NotInRangeVerifier(typename T::Type lower, typename T::Type upper)
: lower(std::move(lower))
, upper(std::move(upper))
{
ghoul_assert(lower <= upper, "Lower value must be smaller or equal to upper value");
}
template <typename T>
bool NotInRangeVerifier<T>::test(const ghoul::Dictionary& d, const std::string& k) const {
if (T::test(d, k)) {
typename T::Type val = d.value<typename T::Type>(k);
return !(val >= lower && val <= upper);
}
else {
return false;
}
}
template <typename T>
std::string NotInRangeVerifier<T>::documentation() const {
return T::documentation() + '\n' + "Not in range: (" + std::to_string(lower) + "," +
std::to_string(upper) + ")";
}
template <typename T>
AnnotationVerifier<T>::AnnotationVerifier(std::string annotation)
: annotation(std::move(annotation))