1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:07:35 +00:00

AK: Use new Formatter for each element in Formatter<Vector<T>>

The state of the formatter for the previous element should be thrown
away for each iteration. This showed up when trying to format a
Vector<String>, since Formatter<StringView> was unhappy about some state
that gets set when it's called. Add a test for Formatter<Vector>.
This commit is contained in:
Andrew Kaster 2021-07-17 16:27:12 -06:00 committed by Ali Mohammad Pur
parent 4842c8c902
commit 64aac345d3
2 changed files with 20 additions and 1 deletions

View file

@ -8,6 +8,7 @@
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Vector.h>
TEST_CASE(is_integral_works_properly)
{
@ -306,3 +307,19 @@ TEST_CASE(hex_dump)
EXPECT_EQ(String::formatted("{:>2hex-dump}", "0000"), "3030 00\n3030 00");
EXPECT_EQ(String::formatted("{:*>4hex-dump}", "0000"), "30303030****0000");
}
TEST_CASE(vector_format)
{
{
Vector<int> v { 1, 2, 3, 4 };
EXPECT_EQ(String::formatted("{}", v), "[ 1, 2, 3, 4 ]");
}
{
Vector<StringView> v { "1"sv, "2"sv, "3"sv, "4"sv };
EXPECT_EQ(String::formatted("{}", v), "[ 1, 2, 3, 4 ]");
}
{
Vector<Vector<String>> v { { "1"sv, "2"sv }, { "3"sv, "4"sv } };
EXPECT_EQ(String::formatted("{}", v), "[ [ 1, 2 ], [ 3, 4 ] ]");
}
}