GCC Code Coverage Report


Directory: ./
File: test/unittest/src/validators/header_param_validator.cpp
Date: 2024-07-09 12:21:25
Exec Total Coverage
Lines: 15 15 100.0%
Functions: 6 6 100.0%
Branches: 33 79 41.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2024 Muhammad Nawaz
3 * Licensed under the MIT License. See LICENSE file for more information.
4 */
5 // [ END OF LICENSE c6bd0f49d040fca8d8a9cb05868e66aa63f0e2e0 ]
6
7 #include "validators/param_validators.hpp"
8 #include <gtest/gtest.h>
9
10 enum : size_t
11 {
12 INPUT,
13 EXPLODE,
14 TYPE,
15 EXPECTED_ERROR
16 };
17
18 class HeaderParamValidatorTest
19 : public ::testing::TestWithParam<std::tuple<std::string, bool, std::string, ValidationError>>
20 {
21 protected:
22 8 void SetUp() override
23 {
24
2/4
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 const auto param = GetParam();
25
1/2
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 input_ = std::get<INPUT>(param);
26
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
8 const auto* const explode = std::get<EXPLODE>(param) ? "true" : "false";
27 8 const auto& type = std::get<TYPE>(param);
28 8 expected_error_ = std::get<EXPECTED_ERROR>(param);
29 std::string json = R"({
30 "name": "param",
31 "in": "header",
32 "required": true,
33 "style": "simple",
34
3/6
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
16 "explode": )" + std::string(explode) +
35 R"(,
36 "schema": {
37
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
16 "type": ")" +
38
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 type + R"("
39 }
40 })";
41
1/2
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 doc_.Parse(json.data());
42
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 validator_ = std::make_unique<HeaderParamValidator>(doc_, keys_);
43 8 }
44
45 std::unique_ptr<HeaderParamValidator> validator_;
46 std::string input_;
47 rapidjson::Document doc_;
48 std::vector<std::string> keys_{"paths", "/pets/xyz", "get", "parameters", "param"};
49 std::string error_msg_;
50 ValidationError expected_error_;
51 };
52
53
6/12
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
✓ Branch 24 taken 1 times.
✗ Branch 25 not taken.
34 TEST_P(HeaderParamValidatorTest, ValidateParam)
54 {
55
4/14
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 8 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 26 taken 8 times.
✗ Branch 27 not taken.
16 ASSERT_EQ(expected_error_, validator_->ValidateParam(input_.data(), input_.data() + input_.size(), error_msg_));
56 }
57
58
11/31
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
20 INSTANTIATE_TEST_SUITE_P(HeaderParamValidatorTest, HeaderParamValidatorTest,
59 ::testing::Values(std::make_tuple("123", false, "integer", ValidationError::NONE),
60 std::make_tuple("123", true, "integer", ValidationError::NONE),
61 std::make_tuple("true", false, "boolean", ValidationError::NONE),
62 std::make_tuple("true", true, "boolean", ValidationError::NONE),
63 std::make_tuple("123.456", false, "number", ValidationError::NONE),
64 std::make_tuple("123.456", true, "number", ValidationError::NONE),
65 std::make_tuple("abc", false, "string", ValidationError::NONE),
66 std::make_tuple("abc", true, "string", ValidationError::NONE)));
67