ladybird/Tests/AK/TestSlugify.cpp
Gurkirat Singh f1b79e0cd3 AK: Implement slugify function for URL slug generation
The slugify function is used to convert input into URL-friendly slugs.
It processes each character in the input, keeping ascii alpha characters
after lowercase and replacing non-alphanum characters with the glue
character or a space if multiple spaces are encountered consecutively.
The resulting string is trimmed of leading and trailing whitespace, and
any internal whitespace is replaced with the glue character.

It is currently used in LibMarkdown headings generation code.
2023-10-30 10:39:59 +00:00

44 lines
962 B
C++

/*
* Copyright (c) 2023, Gurkirat Singh <tbhaxor@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Slugify.h>
#include <LibTest/TestCase.h>
TEST_CASE(ignore_unicode_characters)
{
EXPECT_EQ(MUST(slugify("Hello World!🎉"_string)), "hello-world"_string);
}
TEST_CASE(all_whitespace_empty_string)
{
EXPECT_EQ(MUST(slugify(" "_string)), ""_string);
}
TEST_CASE(squeeze_multiple_whitespace)
{
EXPECT_EQ(MUST(slugify("Hello World"_string)), "hello-world"_string);
}
TEST_CASE(trim_trailing_whitelist)
{
EXPECT_EQ(MUST(slugify("Hello World "_string)), "hello-world"_string);
}
TEST_CASE(lowercase_all_result)
{
EXPECT_EQ(MUST(slugify("HelloWorld"_string)), "helloworld"_string);
}
TEST_CASE(slug_glue_change)
{
EXPECT_EQ(MUST(slugify("Hello World"_string, '|')), "hello|world"_string);
}
TEST_CASE(multiple_glue_squeeze)
{
EXPECT_EQ(MUST(slugify("Hello_ World"_string, '_')), "hello_world"_string);
}