Skip to content

Commit

Permalink
Validator: implement eachCharacterOf(S).isBetween()
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar committed Nov 1, 2024
1 parent 4885370 commit 003c9ed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
36 changes: 28 additions & 8 deletions include/tcframe/validator/core.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#pragma once

#include <string>
#include <type_traits>
#include <vector>

using std::enable_if_t;
using std::is_arithmetic_v;
using std::size_t;
using std::string;
using std::vector;

namespace tcframe {
Expand All @@ -25,7 +29,7 @@ struct ScalarValidator {
};

template<typename T, typename = ScalarType<T>>
ScalarValidator<T> valueOf(T val) {
inline ScalarValidator<T> valueOf(T val) {
return ScalarValidator(val);
}

Expand All @@ -48,12 +52,7 @@ struct VectorElementValidator {
};

template<typename T, typename = ScalarType<T>>
VectorElementValidator<T> eachElementOf(const vector<T>& val) {
return VectorElementValidator(val);
}

template<typename T, typename = ScalarType<T>>
VectorElementValidator<T> eachElementOf(vector<T>&& val) {
inline VectorElementValidator<T> eachElementOf(const vector<T>& val) {
return VectorElementValidator(val);
}

Expand Down Expand Up @@ -110,8 +109,29 @@ struct VectorElementsValidator {
};

template<typename T, typename = ScalarType<T>>
VectorElementsValidator<T> elementsOf(const vector<T>& val) {
inline VectorElementsValidator<T> elementsOf(const vector<T>& val) {
return VectorElementsValidator(val);
}

struct StringElementValidator {
private:
const string& val;

public:
explicit StringElementValidator(const string& _val) : val(_val) {}

bool isBetween(char minVal, char maxVal) {
for (char v : val) {
if (!valueOf(v).isBetween(minVal, maxVal)) {
return false;
}
}
return true;
}
};

inline StringElementValidator eachCharacterOf(const string& val) {
return StringElementValidator(val);
}

}
7 changes: 7 additions & 0 deletions test/unit/tcframe/validator/CoreValidatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ TEST_F(CoreValidatorTests, elementsOf_areUnique) {
EXPECT_TRUE(elementsOf(vector<char>{'a', 'x', 'd', 'g', 'h'}).areUnique());
}

TEST_F(CoreValidatorTests, eachCharacterOf_isBetween) {
EXPECT_FALSE(eachCharacterOf("BCDEF").isBetween('B', 'E'));
EXPECT_FALSE(eachCharacterOf("BCDEF").isBetween('C', 'F'));
EXPECT_TRUE(eachCharacterOf("BCDEF").isBetween('B', 'F'));
EXPECT_TRUE(eachCharacterOf("BCDEF").isBetween('A', 'G'));
}

}

0 comments on commit 003c9ed

Please sign in to comment.