Przeglądaj źródła

[Project] Css: Move some of the tests to the doctest

tags/3.0
Vsevolod Stakhov 3 lat temu
rodzic
commit
10fb4d1a18

+ 1
- 1
CMakeLists.txt Wyświetl plik

@@ -251,7 +251,7 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_OPT_FLAGS} ${CMAKE_C_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_C_OPT_FLAGS} ${CMAKE_CXX_FLAGS}")

ADD_DEFINITIONS(-DHAVE_CONFIG_H)
ADD_DEFINITIONS(-DDOCTEST_CONFIG_SUPER_FAST_ASSERTS)
ADD_DEFINITIONS(-DDOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS)

# Check platform specific includes
CHECK_INCLUDE_FILES(sys/types.h HAVE_SYS_TYPES_H)

+ 5
- 0
src/libserver/css/css.cxx Wyświetl plik

@@ -28,6 +28,8 @@ rspamd_css_parse_style (rspamd_mempool_t *pool, const guchar *begin, gsize len,
{
auto parse_res = rspamd::css::parse_css(pool, {(const char* )begin, len});

#if 0
/* Return once semantical parsing is done */
if (parse_res.has_value()) {
return reinterpret_cast<rspamd_css>(parse_res.value().release());
}
@@ -37,6 +39,9 @@ rspamd_css_parse_style (rspamd_mempool_t *pool, const guchar *begin, gsize len,
"parse error");
return nullptr;
}
#else
return nullptr;
#endif
}

namespace rspamd::css {

+ 55
- 6
src/libserver/css/css_parser.cxx Wyświetl plik

@@ -19,9 +19,12 @@
#include "css_selector.hxx"
#include "css_rule.hxx"
#include "fmt/core.h"

#include <vector>
#include <unicode/utf8.h>

#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#include "doctest/doctest.h"

namespace rspamd::css {

@@ -117,9 +120,9 @@ auto css_consumed_block::debug_str(void) -> std::string {
ret += R"("empty")";
}
else if constexpr (std::is_same_v<T, css_function_block>) {
/* Empty block */
ret += fmt::format(R"({ "content": {"token": "{}", "arguments": [)",
arg.function.debug_token_str());
ret += R"({ "content": {"token": )";
ret += "\"" + arg.function.debug_token_str() + "\", ";
ret += R"("arguments": [)";

for (const auto &block : arg.args) {
ret += "{";
@@ -619,13 +622,59 @@ bool css_parser::consume_input(const std::string_view &sv)
* Wrapper for the parser
*/
auto parse_css(rspamd_mempool_t *pool, const std::string_view &st) ->
tl::expected<std::unique_ptr<css_style_sheet>,css_parse_error>
bool
{
css_parser parser(pool);

parser.consume_input(st);
if (parser.consume_input(st)) {
return true;
}

return false;
}

return parser.get_object_maybe();
TEST_SUITE("css parser") {
TEST_CASE("parse colors") {
const std::vector<const char *> cases{
"p { color: rgb(100%, 50%, 0%); opacity: -1; width: 1em; display: none; } /* very transparent solid orange */",
"p { color: rgb(100%, 50%, 0%); opacity: 2; display: inline; } /* very transparent solid orange */",
"p { color: rgb(100%, 50%, 0%); opacity: 0.5; } /* very transparent solid orange */\n",
"p { color: rgb(100%, 50%, 0%); opacity: 1; width: 99%; } /* very transparent solid orange */\n",
"p { color: rgb(100%, 50%, 0%); opacity: 10%; width: 99%; } /* very transparent solid orange */\n",
"p { color: rgb(100%, 50%, 0%); opacity: 10%; width: 100px; } /* very transparent solid orange */\n",
"p { color: rgb(100%, 50%, 0%); opacity: 10% } /* very transparent solid orange */\n",
"* { color: hsl(0, 100%, 50%) !important } /* red */\n",
"* { color: hsl(120, 100%, 50%) important } /* lime */\n",
"* { color: hsl(120, 100%, 25%) } /* dark green */\n",
"* { color: hsl(120, 100%, 75%) } /* light green */\n",
"* { color: hsl(120, 75%, 75%) } /* pastel green, and so on */\n",
"em { color: #f00 } /* #rgb */\n",
"em { color: #ff0000 } /* #rrggbb */\n",
"em { color: rgb(255,0,0) }\n",
"em { color: rgb(100%, 0%, 0%) }\n",
"body {color: black; background: white }\n",
"h1 { color: maroon }\n",
"h2 { color: olive }\n",
"em { color: rgb(255,0,0) } /* integer range 0 - 255 */\n",
"em { color: rgb(300,0,0) } /* clipped to rgb(255,0,0) */\n",
"em { color: rgb(255,-10,0) } /* clipped to rgb(255,0,0) */\n",
"em { color: rgb(110%, 0%, 0%) } /* clipped to rgb(100%,0%,0%) */\n",
"em { color: rgb(255,0,0) } /* integer range 0 - 255 */\n",
"em { color: rgba(255,0,0,1) /* the same, with explicit opacity of 1 */\n",
"em { color: rgb(100%,0%,0%) } /* float range 0.0% - 100.0% */\n",
"em { color: rgba(100%,0%,0%,1) } /* the same, with explicit opacity of 1 */\n",
"p { color: rgba(0,0,255,0.5) } /* semi-transparent solid blue */\n",
"p { color: rgba(100%, 50%, 0%, 0.1) } /* very transparent solid orange */",
};

rspamd_mempool_t *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(),
"css", 0);
for (const auto &c : cases) {
CHECK_UNARY(parse_css(pool, c));
}

rspamd_mempool_delete(pool);
}
}

}

+ 1
- 1
src/libserver/css/css_parser.hxx Wyświetl plik

@@ -186,7 +186,7 @@ extern const css_consumed_block css_parser_eof_block;
using blocks_gen_functor = std::function<const css_consumed_block &(void)>;

auto parse_css (rspamd_mempool_t *pool, const std::string_view &st) ->
tl::expected<std::unique_ptr<css_style_sheet>,css_parse_error>;
bool;

}


+ 12
- 3
src/libserver/css/css_value.cxx Wyświetl plik

@@ -386,10 +386,19 @@ TEST_SUITE("css values") {
}
}
TEST_CASE("css colors strings") {
auto passed = 0;
for (const auto &p : css_colors_map) {
auto col_parsed = css_value::maybe_color_from_string(p.first);
auto final_col = col_parsed.value().to_color().value();
CHECK(final_col == p.second);
/* Match some of the colors selected randomly */
if (rspamd_random_double_fast() > 0.9) {
auto col_parsed = css_value::maybe_color_from_string(p.first);
auto final_col = col_parsed.value().to_color().value();
CHECK_MESSAGE(final_col == p.second, p.first.data());
passed ++;

if (passed > 20) {
break;
}
}
}
}
};

+ 15
- 0
test/rspamd_cxx_unit.cxx Wyświetl plik

@@ -15,12 +15,27 @@
*/

#include "config.h"
#include "rspamd.h"
#include <memory>
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#include "doctest/doctest.h"

int
main(int argc, char** argv)
{
std::unique_ptr<struct rspamd_main> rspamd_main{new struct rspamd_main};
struct rspamd_config *cfg;

rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0);
cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT);
cfg->libs_ctx = rspamd_init_libs ();
rspamd_main->cfg = cfg;
cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0);

rspamd_main->logger = rspamd_log_open_emergency (rspamd_main->server_pool,
RSPAMD_LOG_FLAG_RSPAMADM);
rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_MESSAGE);

doctest::Context context(argc, argv);
int res = context.run();


Ładowanie…
Anuluj
Zapisz