Skip to content

Commit

Permalink
add tests for validator.h
Browse files Browse the repository at this point in the history
  • Loading branch information
matyalatte committed Nov 7, 2024
1 parent 7f69774 commit 10e0c7b
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ test_sources = [
'json_check_test.cpp',
'main_frame_test.cpp',
'string_utils_test.cpp',
'validator_test.cpp',
]

# build tests
Expand Down
2 changes: 1 addition & 1 deletion tests/string_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void expect_tuwwstr(const wchar_t* expected, const tuwWstring& actual) {
TEST(tuwWstringTest, ConstructWithNull) {
tuwWstring str(nullptr);
expect_nullwstr(str);
tuwWstring str2("");
tuwWstring str2(L"");
expect_nullwstr(str2);
}

Expand Down
1 change: 1 addition & 0 deletions tests/test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "json_utils.h"
#include "tuw_constants.h"
#include "exe_container.h"
#include "validator.h"
#include "env_utils.h"

// you need to copy it from examples/all_keys to the json folder
Expand Down
141 changes: 141 additions & 0 deletions tests/validator_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Tests for json embedding
// Todo: Write more tests

#include "test_utils.h"

Validator GetValidator(const char* config_str) {
rapidjson::Document config(rapidjson::kObjectType);
config.Parse(config_str);
Validator validator;
validator.Initialize(config);
return validator;
}

TEST(ValidatorTest, Regex) {
const char* config =
"{"
" \"regex\": \"^[0-9]*$\""
"}";
Validator validator = GetValidator(config);
EXPECT_TRUE(validator.Validate("1234"));
EXPECT_FALSE(validator.Validate("1234abcd"));
EXPECT_STREQ("Regex match failed for pattern: ^[0-9]*$",
validator.GetError().c_str());
}

TEST(ValidatorTest, RegexCompileError) {
const char* config =
"{"
" \"regex\": \"^[0-9\""
"}";
Validator validator = GetValidator(config);
EXPECT_FALSE(validator.Validate("1234"));
EXPECT_STREQ("Failed to parse regex pattern: ^[0-9",
validator.GetError().c_str());
}

TEST(ValidatorTest, RegexGroupOp) {
const char* config =
"{"
" \"regex\": \"(a|b)\""
"}";
Validator validator = GetValidator(config);
EXPECT_FALSE(validator.Validate("1234"));
EXPECT_STREQ("Regex compile error: () operators are not supported.",
validator.GetError().c_str());
}

TEST(ValidatorTest, RegexGroupOpEscape) {
const char* config =
"{"
" \"regex\": \"^\\(a|b\\)\"$"
"}";
Validator validator = GetValidator(config);
EXPECT_TRUE(validator.Validate("(a"));
EXPECT_TRUE(validator.Validate("b)"));
}

TEST(ValidatorTest, RegexCustomError) {
const char* config =
"{"
" \"regex\": \"^[0-9]*$\","
" \"regex_error\": \"Should be numeric!\""
"}";
Validator validator = GetValidator(config);
EXPECT_FALSE(validator.Validate("abcd"));
EXPECT_STREQ("Should be numeric!",
validator.GetError().c_str());
}

TEST(ValidatorTest, Wildcard) {
const char* config =
"{"
" \"wildcard\": \"test*foo\""
"}";
Validator validator = GetValidator(config);
EXPECT_TRUE(validator.Validate("testfoo"));
EXPECT_TRUE(validator.Validate("testbarfoo"));
EXPECT_FALSE(validator.Validate("test"));
EXPECT_STREQ("Wildcard match failed for pattern: test*foo",
validator.GetError().c_str());
}

TEST(ValidatorTest, WildcardCustomError) {
const char* config =
"{"
" \"wildcard\": \"test*foo\","
" \"wildcard_error\": \"Custom message!\""
"}";
Validator validator = GetValidator(config);
EXPECT_FALSE(validator.Validate("test"));
EXPECT_STREQ("Custom message!",
validator.GetError().c_str());
}

TEST(ValidatorTest, NotEmpty) {
const char* config =
"{"
" \"not_empty\": true"
"}";
Validator validator = GetValidator(config);
EXPECT_TRUE(validator.Validate("test"));
EXPECT_FALSE(validator.Validate(""));
EXPECT_STREQ("Empty string is NOT allowed.",
validator.GetError().c_str());
}

TEST(ValidatorTest, NotEmptyCustomError) {
const char* config =
"{"
" \"not_empty\": true,"
" \"not_empty_error\": \"Custom message!\""
"}";
Validator validator = GetValidator(config);
EXPECT_FALSE(validator.Validate(""));
EXPECT_STREQ("Custom message!",
validator.GetError().c_str());
}

TEST(ValidatorTest, Exist) {
const char* config =
"{"
" \"exist\": true"
"}";
Validator validator = GetValidator(config);
EXPECT_TRUE(validator.Validate(JSON_ALL_KEYS));
EXPECT_FALSE(validator.Validate(tuwString(JSON_ALL_KEYS) + ".fake.does_not_exist"));
EXPECT_STREQ("Path does NOT exist.",
validator.GetError().c_str());
}

TEST(ValidatorTest, ExistCustomError) {
const char* config =
"{"
" \"exist\": true,"
" \"exist_error\": \"Custom message!\""
"}";
Validator validator = GetValidator(config);
EXPECT_FALSE(validator.Validate(tuwString(JSON_ALL_KEYS) + ".fake.does_not_exist"));
EXPECT_STREQ("Custom message!",
validator.GetError().c_str());
}

0 comments on commit 10e0c7b

Please sign in to comment.