Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validator: implement eachCharacterOf(S).isBetween() #205

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
}

}
Loading