Files
textmate/Frameworks/regexp/tests/t_format_string.cc
2013-08-27 15:30:09 +02:00

71 lines
3.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <regexp/format_string.h>
void test_format_string ()
{
using format_string::replace;
OAK_ASSERT_EQ(replace("æbleGRØD", ".+", "»${0:/upcase}«"), "»ÆBLEGRØD«");
OAK_ASSERT_EQ(replace("æbleGRØD", ".+", "»${0:/downcase}«"), "»æblegrød«");
OAK_ASSERT_EQ(replace("æbleGRØD", ".+", "»${0:/capitalize}«"), "»Æblegrød«");
OAK_ASSERT_EQ(replace("æbleGRØD", ".+", "»${0:/asciify}«"), "»aebleGROD«");
OAK_ASSERT_EQ(replace("æbleGRØD", ".+", "»${0:/capitalize/asciify}«"), "»AEblegrod«");
}
void test_variables ()
{
std::map<std::string, std::string> variables{
{ "a", "hello" },
{ "b", " " },
{ "c", "world" },
{ "d", "hell" },
{ "dir", "/path/to" },
{ "path", "/path/to/file" },
};
OAK_ASSERT_EQ("hello world", format_string::expand("$a$b$c", variables));
OAK_ASSERT_EQ("hello world", format_string::expand("${a}${b}${c}", variables));
OAK_ASSERT_EQ("hi world", format_string::expand("${a/hello/hi/}${b}${c}", variables));
OAK_ASSERT_EQ("hiyo world", format_string::expand("${a/${d}/hiy/}${b}${c}", variables));
OAK_ASSERT_EQ("file", format_string::expand("${path/^.*\\///}", variables));
OAK_ASSERT_EQ("file", format_string::expand("${path/${dir}.//}", variables));
OAK_ASSERT_EQ("file", format_string::expand("${path/${dir}\\///}", variables));
}
void test_legacy_conditions ()
{
using format_string::replace;
OAK_ASSERT_EQ(replace("foo bar", "(foo)? bar", "(?1:baz)"), "baz");
OAK_ASSERT_EQ(replace("fud bar", "(foo)? bar", "(?1:baz)"), "fud");
OAK_ASSERT_EQ(replace("fud bar", "(foo)? bar", "(?1:baz: buz)"), "fud buz");
OAK_ASSERT_EQ(replace("foo bar", "(foo)? bar", "(?1:baz"), "(?1:baz");
OAK_ASSERT_EQ(replace("foo bar", "(foo)? bar", "(?1:baz:"), "(?1:baz:");
OAK_ASSERT_EQ(replace("foo bar", "(foo)? bar", "(?n:baz)"), "(?n:baz)");
OAK_ASSERT_EQ(replace("foo bar", "(foo)? bar", "(?n:baz:)"), "(?n:baz:)");
}
void test_escape_format_string ()
{
using format_string::escape;
using format_string::expand;
OAK_ASSERT_EQ(escape("\t\n\r\\q"), "\\t\\n\\r\\q");
OAK_ASSERT_EQ(expand(escape("${var}")), "${var}");
OAK_ASSERT_EQ(expand(escape("foo\n")), "foo\n");
OAK_ASSERT_EQ(expand(escape("foo\\n")), "foo\\n");
OAK_ASSERT_EQ(expand(escape("\\No-Escape")), "\\No-Escape");
OAK_ASSERT_EQ(expand(escape("(?bla)")), "(?bla)");
OAK_ASSERT_EQ(expand(escape("Escape\\\\me")), "Escape\\\\me");
OAK_ASSERT_EQ(expand(escape("(?1:baz: buz)")), "(?1:baz: buz)");
}
void test_control_codes ()
{
using format_string::expand;
OAK_ASSERT_EQ("\t", expand("\\t"));
OAK_ASSERT_EQ("\r", expand("\\r"));
OAK_ASSERT_EQ("\n", expand("\\n"));
OAK_ASSERT_EQ("\\x", expand("\\x"));
OAK_ASSERT_EQ("\\x{foo}", expand("\\x{foo}"));
OAK_ASSERT_EQ("\u2014", expand("\\x{2014}"));
OAK_ASSERT_EQ("\U00020EF5", expand("\\x{20EF5}"));
OAK_ASSERT_EQ("\U0010FFFF", expand("\\x{0010FFFF}"));
OAK_ASSERT_EQ("Æblegrød", expand("\\xc3\\x86blegr\\xC3\\xB8d"));
}