11/*
22 -------------------------------------------------------------------------------
33 This file is part of the Private Message Database test suite.
4- Copyright (C) 2015, 2022 Dirk Stolle
4+ Copyright (C) 2015, 2022, 2025 Dirk Stolle
55
66 This program is free software: you can redistribute it and/or modify
77 it under the terms of the GNU General Public License as published by
@@ -26,84 +26,149 @@ TEST_CASE("TableBBCode")
2626 // some rather standard table BB code
2727 TableBBCode table (" table" );
2828
29- // Populate map with strings for testing.
30- // Keys: input string
31- // Values: expected output string
32- std::map<std::string, std::string> tests;
33- // empty string stays unchanged
34- tests[" " ] = " " ;
35- // unchanged, no code
36- tests[" There is no table here." ] = " There is no table here." ;
37- // simple table, one cell only
38- tests[" [table][tr][td]Content goes here.[/td][/tr][/table]" ]
39- = " <table><tr><td>Content goes here.</td></tr></table>" ;
40- // simple table, two cells in one row
41- tests[" [table][tr][td]Content[/td][td]more text[/td][/tr][/table]" ]
42- = " <table><tr><td>Content</td><td>more text</td></tr></table>" ;
43- // simple table, two cells only in two rows
44- tests[" [table][tr][td]Content[/td][/tr][tr][td]more text[/td][/tr][/table]" ]
45- = " <table><tr><td>Content</td></tr><tr><td>more text</td></tr></table>" ;
46- // four cells in two rows: two cells in each row
47- tests[" [table][tr][td]one[/td][td]two[/td][/tr][tr][td]three[/td][td]four[/td][/tr][/table]" ]
48- = " <table><tr><td>one</td><td>two</td></tr><tr><td>three</td><td>four</td></tr></table>" ;
49- // same, but with colspan
50- tests[" [table][tr][td=\" colspan: 2\" ]one[/td][/tr][tr][td]three[/td][td]four[/td][/tr][/table]" ]
51- = " <table><tr><td colspan=\" 2\" >one</td></tr><tr><td>three</td><td>four</td></tr></table>" ;
52-
53- // table with width
54- tests[" [table=\" width: 250\" ][tr][td]Content goes here.[/td][/tr][/table]" ]
55- = " <table width=\" 250\" ><tr><td>Content goes here.</td></tr></table>" ;
56- // table with width in percent
57- tests[" [table=\" width: 25%\" ][tr][td]Content goes here.[/td][/tr][/table]" ]
58- = " <table width=\" 25%\" ><tr><td>Content goes here.</td></tr></table>" ;
59-
60- // cell with width
61- tests[" [table][tr][td=\" width: 250\" ]Content goes here.[/td][/tr][/table]" ]
62- = " <table><tr><td width=\" 250\" >Content goes here.</td></tr></table>" ;
63- // cell with width in percent
64- tests[" [table][tr][td=\" width: 25%\" ]Content goes here.[/td][/tr][/table]" ]
65- = " <table><tr><td width=\" 25%\" >Content goes here.</td></tr></table>" ;
66-
67- // table with align left
68- tests[" [table=\" align: left\" ][tr][td]Content goes here.[/td][/tr][/table]" ]
69- = " <table align=\" left\" ><tr><td>Content goes here.</td></tr></table>" ;
70- // table with align right
71- tests[" [table=\" align: right\" ][tr][td]Content goes here.[/td][/tr][/table]" ]
72- = " <table align=\" right\" ><tr><td>Content goes here.</td></tr></table>" ;
73- // table with align center
74- tests[" [table=\" align: center\" ][tr][td]Content goes here.[/td][/tr][/table]" ]
75- = " <table align=\" center\" ><tr><td>Content goes here.</td></tr></table>" ;
76-
77- // cell with align left
78- tests[" [table][tr][td=\" align: left\" ]Content goes here.[/td][/tr][/table]" ]
79- = " <table><tr><td align=\" left\" >Content goes here.</td></tr></table>" ;
80- // cell with align right
81- tests[" [table][tr][td=\" align: right\" ]Content goes here.[/td][/tr][/table]" ]
82- = " <table><tr><td align=\" right\" >Content goes here.</td></tr></table>" ;
83- // cell with align center
84- tests[" [table][tr][td=\" align: center\" ]Content goes here.[/td][/tr][/table]" ]
85- = " <table><tr><td align=\" center\" >Content goes here.</td></tr></table>" ;
86-
87- // cell with border
88- tests[" [table][tr][td=\" class: grid\" ]Content goes here.[/td][/tr][/table]" ]
89- = " <table><tr><td style=\" border: 1px solid #000000; border-collapse: collapse;\" >Content goes here.</td></tr></table>" ;
90- // row with border - border style gets applied to child elements, too
91- tests[" [table][tr=\" class: grid\" ][td]Content goes here.[/td][/tr][/table]" ]
92- = " <table>"
93- + std::string (" <tr style=\" border: 1px solid #000000; border-collapse: collapse;\" >" )
94- + " <td style=\" border: 1px solid #000000; border-collapse: collapse;\" >Content goes here.</td></tr></table>" ;
95- // table with border - border style gets applied to child elements, too
96- tests[" [table=\" class: grid\" ][tr][td]Content goes here.[/td][/tr][/table]" ]
97- = " <table style=\" border: 1px solid #000000; border-collapse: collapse;\" >"
29+ SECTION (" empty string stays unchanged" )
30+ {
31+ std::string text = " " ;
32+ table.applyToText (text);
33+ REQUIRE ( text == " " );
34+ }
35+
36+ SECTION (" text without table code is not changed" )
37+ {
38+ std::string text = " There is no table here." ;
39+ table.applyToText (text);
40+ REQUIRE ( text == " There is no table here." );
41+ }
42+
43+ SECTION (" simple table, one cell only" )
44+ {
45+ std::string text = " [table][tr][td]Content goes here.[/td][/tr][/table]" ;
46+ table.applyToText (text);
47+ REQUIRE ( text == " <table><tr><td>Content goes here.</td></tr></table>" );
48+ }
49+
50+ SECTION (" simple table, two cells in one row" )
51+ {
52+ std::string text = " [table][tr][td]Content[/td][td]more text[/td][/tr][/table]" ;
53+ table.applyToText (text);
54+ REQUIRE ( text == " <table><tr><td>Content</td><td>more text</td></tr></table>" );
55+ }
56+
57+ SECTION (" simple table, two cells only in two rows" )
58+ {
59+ std::string text = " [table][tr][td]Content[/td][/tr][tr][td]more text[/td][/tr][/table]" ;
60+ table.applyToText (text);
61+ REQUIRE ( text == " <table><tr><td>Content</td></tr><tr><td>more text</td></tr></table>" );
62+ }
63+
64+ SECTION (" four cells in two rows: two cells in each row" )
65+ {
66+ std::string text = " [table][tr][td]one[/td][td]two[/td][/tr][tr][td]three[/td][td]four[/td][/tr][/table]" ;
67+ table.applyToText (text);
68+ REQUIRE ( text == " <table><tr><td>one</td><td>two</td></tr><tr><td>three</td><td>four</td></tr></table>" );
69+ }
70+
71+ SECTION (" four cells in two rows: two cells in each row, but with colspan" )
72+ {
73+ std::string text = " [table][tr][td=\" colspan: 2\" ]one[/td][/tr][tr][td]three[/td][td]four[/td][/tr][/table]" ;
74+ table.applyToText (text);
75+ REQUIRE ( text == " <table><tr><td colspan=\" 2\" >one</td></tr><tr><td>three</td><td>four</td></tr></table>" );
76+ }
77+
78+ SECTION (" table with width in pixels" )
79+ {
80+ std::string text = " [table=\" width: 250\" ][tr][td]Content goes here.[/td][/tr][/table]" ;
81+ table.applyToText (text);
82+ REQUIRE ( text == " <table width=\" 250\" ><tr><td>Content goes here.</td></tr></table>" );
83+ }
84+
85+ SECTION (" table with width in percent" )
86+ {
87+ std::string text = " [table=\" width: 25%\" ][tr][td]Content goes here.[/td][/tr][/table]" ;
88+ table.applyToText (text);
89+ REQUIRE ( text == " <table width=\" 25%\" ><tr><td>Content goes here.</td></tr></table>" );
90+ }
91+
92+ SECTION (" cell with width in pixels" )
93+ {
94+ std::string text = " [table][tr][td=\" width: 250\" ]Content goes here.[/td][/tr][/table]" ;
95+ table.applyToText (text);
96+ REQUIRE ( text == " <table><tr><td width=\" 250\" >Content goes here.</td></tr></table>" );
97+ }
98+
99+ SECTION (" cell with width in percent" )
100+ {
101+ std::string text = " [table][tr][td=\" width: 25%\" ]Content goes here.[/td][/tr][/table]" ;
102+ table.applyToText (text);
103+ REQUIRE ( text == " <table><tr><td width=\" 25%\" >Content goes here.</td></tr></table>" );
104+ }
105+
106+ SECTION (" table with align left" )
107+ {
108+ std::string text = " [table=\" align: left\" ][tr][td]Content goes here.[/td][/tr][/table]" ;
109+ table.applyToText (text);
110+ REQUIRE ( text == " <table align=\" left\" ><tr><td>Content goes here.</td></tr></table>" );
111+ }
112+
113+ SECTION (" table with align right" )
114+ {
115+ std::string text = " [table=\" align: right\" ][tr][td]Content goes here.[/td][/tr][/table]" ;
116+ table.applyToText (text);
117+ REQUIRE ( text == " <table align=\" right\" ><tr><td>Content goes here.</td></tr></table>" );
118+ }
119+
120+ SECTION (" table with align center" )
121+ {
122+ std::string text = " [table=\" align: center\" ][tr][td]Content goes here.[/td][/tr][/table]" ;
123+ table.applyToText (text);
124+ REQUIRE ( text == " <table align=\" center\" ><tr><td>Content goes here.</td></tr></table>" );
125+ }
126+
127+ SECTION (" cell with align left" )
128+ {
129+ std::string text = " [table][tr][td=\" align: left\" ]Content goes here.[/td][/tr][/table]" ;
130+ table.applyToText (text);
131+ REQUIRE ( text == " <table><tr><td align=\" left\" >Content goes here.</td></tr></table>" );
132+ }
133+
134+ SECTION (" cell with align right" )
135+ {
136+ std::string text = " [table][tr][td=\" align: right\" ]Content goes here.[/td][/tr][/table]" ;
137+ table.applyToText (text);
138+ REQUIRE ( text == " <table><tr><td align=\" right\" >Content goes here.</td></tr></table>" );
139+ }
140+
141+ SECTION (" cell with align center" )
142+ {
143+ std::string text = " [table][tr][td=\" align: center\" ]Content goes here.[/td][/tr][/table]" ;
144+ table.applyToText (text);
145+ REQUIRE ( text == " <table><tr><td align=\" center\" >Content goes here.</td></tr></table>" );
146+ }
147+
148+ SECTION (" cell with border" )
149+ {
150+ std::string text = " [table][tr][td=\" class: grid\" ]Content goes here.[/td][/tr][/table]" ;
151+ table.applyToText (text);
152+ REQUIRE ( text == " <table><tr><td style=\" border: 1px solid #000000; border-collapse: collapse;\" >Content goes here.</td></tr></table>" );
153+ }
154+
155+ SECTION (" row with border - border style gets applied to child elements, too" )
156+ {
157+ std::string text = " [table][tr=\" class: grid\" ][td]Content goes here.[/td][/tr][/table]" ;
158+ table.applyToText (text);
159+ const std::string expected = " <table>"
98160 + std::string (" <tr style=\" border: 1px solid #000000; border-collapse: collapse;\" >" )
99161 + " <td style=\" border: 1px solid #000000; border-collapse: collapse;\" >Content goes here.</td></tr></table>" ;
162+ REQUIRE ( text == expected );
163+ }
100164
101-
102- // iterate over all given strings and check, if they get the expected result
103- for (const auto & [key, value]: tests)
165+ SECTION (" table with border - border style gets applied to child elements, too" )
104166 {
105- std::string text = key ;
167+ std::string text = " [table= \" class: grid \" ][tr][td]Content goes here.[/td][/tr][/table] " ;
106168 table.applyToText (text);
107- REQUIRE ( text == value );
169+ const std::string expected = " <table style=\" border: 1px solid #000000; border-collapse: collapse;\" >"
170+ + std::string (" <tr style=\" border: 1px solid #000000; border-collapse: collapse;\" >" )
171+ + " <td style=\" border: 1px solid #000000; border-collapse: collapse;\" >Content goes here.</td></tr></table>" ;
172+ REQUIRE ( text == expected );
108173 }
109174}
0 commit comments