tests: make tests pass with non-utf8 locale
authorŁukasz Stelmach <l.stelmach@samsung.com>
Tue, 19 Mar 2024 15:48:08 +0000 (16:48 +0100)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Tue, 19 Mar 2024 15:48:08 +0000 (16:48 +0100)
Change-Id: Id9056e66b5b3619f7f30a8e1a12b184bf82b1991
Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
src/test/test-format-table.c

index 7d544b1..30fae3d 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "alloc-util.h"
 #include "format-table.h"
+#include "locale-util.h"
 #include "string-util.h"
 #include "strv.h"
 #include "tests.h"
@@ -27,10 +28,16 @@ TEST(issue_9549) {
         assert_se(table_format(table, &formatted) >= 0);
 
         printf("%s\n", formatted);
-        assert_se(streq(formatted,
-                        "NAME  TYPE RO  USAGE CREATED                    MODIFIED\n"
-                        "foooo raw  no 673.6M Wed 2018-07-11 00:10:33 J… Wed 2018-07-11 00:16:00 JST\n"
-                        ));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "NAME  TYPE RO  USAGE CREATED                    MODIFIED\n"
+                                "foooo raw  no 673.6M Wed 2018-07-11 00:10:33 J… Wed 2018-07-11 00:16:00 JST\n"
+                                ));
+        else
+                assert_se(streq(formatted,
+                                "NAME  TYPE RO  USAGE CREATED                    MODIFIED\n"
+                                "foooo raw  no 673.6M Wed 2018-07-11 00:10:33... Wed 2018-07-11 00:16:00 JST\n"
+                                ));
 }
 
 TEST(multiline) {
@@ -48,28 +55,47 @@ TEST(multiline) {
         table_set_cell_height_max(table, 1);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO     BAR\n"
-                        "three… two…\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO     BAR\n"
+                                "three… two…\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO         BAR\n"
+                                "three... two...\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, 2);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO          BAR\n"
-                        "three        two\n"
-                        "different… lines\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO          BAR\n"
+                                "three        two\n"
+                                "different… lines\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO            BAR\n"
+                                "three          two\n"
+                                "different... lines\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, 3);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO         BAR\n"
-                        "three       two\n"
-                        "different lines\n"
-                        "lines     \n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO         BAR\n"
+                                "three       two\n"
+                                "different lines\n"
+                                "lines     \n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO         BAR\n"
+                                "three       two\n"
+                                "different lines\n"
+                                "lines     \n"));
+
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, SIZE_MAX);
@@ -93,39 +119,68 @@ TEST(multiline) {
         table_set_cell_height_max(table, 1);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO     BAR\n"
-                        "three… two…\n"
-                        "short    a…\n"
-                        "short2   a…\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO     BAR\n"
+                                "three… two…\n"
+                                "short    a…\n"
+                                "short2   a…\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO         BAR\n"
+                                "three... two...\n"
+                                "short      a...\n"
+                                "short2     a...\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, 2);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO          BAR\n"
-                        "three        two\n"
-                        "different… lines\n"
-                        "short          a\n"
-                        "            pair\n"
-                        "short2         a\n"
-                        "           four…\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO          BAR\n"
+                                "three        two\n"
+                                "different… lines\n"
+                                "short          a\n"
+                                "            pair\n"
+                                "short2         a\n"
+                                "           four…\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO              BAR\n"
+                                "three            two\n"
+                                "different...   lines\n"
+                                "short              a\n"
+                                "                pair\n"
+                                "short2             a\n"
+                                "             four...\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, 3);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO         BAR\n"
-                        "three       two\n"
-                        "different lines\n"
-                        "lines     \n"
-                        "short         a\n"
-                        "           pair\n"
-                        "short2        a\n"
-                        "           four\n"
-                        "          line…\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO         BAR\n"
+                                "three       two\n"
+                                "different lines\n"
+                                "lines     \n"
+                                "short         a\n"
+                                "           pair\n"
+                                "short2        a\n"
+                                "           four\n"
+                                "          line…\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO           BAR\n"
+                                "three         two\n"
+                                "different   lines\n"
+                                "lines     \n"
+                                "short           a\n"
+                                "             pair\n"
+                                "short2          a\n"
+                                "             four\n"
+                                "          line...\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, SIZE_MAX);
@@ -160,18 +215,29 @@ TEST(strv) {
         table_set_cell_height_max(table, 1);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO     BAR\n"
-                        "three… two…\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO     BAR\n"
+                                "three… two…\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO         BAR\n"
+                                "three... two...\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, 2);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO          BAR\n"
-                        "three        two\n"
-                        "different… lines\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO          BAR\n"
+                                "three        two\n"
+                                "different… lines\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO            BAR\n"
+                                "three          two\n"
+                                "different... lines\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, 3);
@@ -205,39 +271,68 @@ TEST(strv) {
         table_set_cell_height_max(table, 1);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO     BAR\n"
-                        "three… two…\n"
-                        "short    a…\n"
-                        "short2   a…\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO     BAR\n"
+                                "three… two…\n"
+                                "short    a…\n"
+                                "short2   a…\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO         BAR\n"
+                                "three... two...\n"
+                                "short      a...\n"
+                                "short2     a...\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, 2);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO          BAR\n"
-                        "three        two\n"
-                        "different… lines\n"
-                        "short          a\n"
-                        "            pair\n"
-                        "short2         a\n"
-                        "           four…\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO          BAR\n"
+                                "three        two\n"
+                                "different… lines\n"
+                                "short          a\n"
+                                "            pair\n"
+                                "short2         a\n"
+                                "           four…\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO              BAR\n"
+                                "three            two\n"
+                                "different...   lines\n"
+                                "short              a\n"
+                                "                pair\n"
+                                "short2             a\n"
+                                "             four...\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, 3);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO         BAR\n"
-                        "three       two\n"
-                        "different lines\n"
-                        "lines     \n"
-                        "short         a\n"
-                        "           pair\n"
-                        "short2        a\n"
-                        "           four\n"
-                        "          line…\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO         BAR\n"
+                                "three       two\n"
+                                "different lines\n"
+                                "lines     \n"
+                                "short         a\n"
+                                "           pair\n"
+                                "short2        a\n"
+                                "           four\n"
+                                "          line…\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO           BAR\n"
+                                "three         two\n"
+                                "different   lines\n"
+                                "lines     \n"
+                                "short           a\n"
+                                "             pair\n"
+                                "short2          a\n"
+                                "             four\n"
+                                "          line...\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, SIZE_MAX);
@@ -313,23 +408,39 @@ TEST(strv_wrapped) {
         table_set_cell_height_max(table, 1);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO                             BAR\n"
-                        "three different…          two lines\n"
-                        "short                        a pair\n"
-                        "short2           a eight line ćęłł…\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO                             BAR\n"
+                                "three different…          two lines\n"
+                                "short                        a pair\n"
+                                "short2           a eight line ćęłł…\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO                                 BAR\n"
+                                "three different...            two lines\n"
+                                "short                            a pair\n"
+                                "short2             a eight line ćęłł...\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, 2);
         assert_se(table_format(table, &formatted) >= 0);
         fputs(formatted, stdout);
-        assert_se(streq(formatted,
-                        "FOO                           BAR\n"
-                        "three different         two lines\n"
-                        "lines           \n"
-                        "short                      a pair\n"
-                        "short2          a eight line ćęłł\n"
-                        "                 ___5___ ___6___…\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "FOO                           BAR\n"
+                                "three different         two lines\n"
+                                "lines           \n"
+                                "short                      a pair\n"
+                                "short2          a eight line ćęłł\n"
+                                "                 ___5___ ___6___…\n"));
+        else
+                assert_se(streq(formatted,
+                                "FOO                            BAR\n"
+                                "three different          two lines\n"
+                                "lines           \n"
+                                "short                       a pair\n"
+                                "short2           a eight line ćęłł\n"
+                                "                ___5___ ___6___...\n"));
         formatted = mfree(formatted);
 
         table_set_cell_height_max(table, 3);
@@ -439,10 +550,16 @@ TEST(table) {
         assert_se(table_format(t, &formatted) >= 0);
         printf("%s\n", formatted);
 
-        assert_se(streq(formatted,
-                        "ONE TWO TH… FO…\n"
-                        "xxx yyy yes  -1\n"
-                        "a … YYY no  -9…\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "ONE TWO TH… FO…\n"
+                                "xxx yyy yes  -1\n"
+                                "a … YYY no  -9…\n"));
+        else
+                assert_se(streq(formatted,
+                                "ONE TWO ... ...\n"
+                                "xxx yyy yes  -1\n"
+                                "... YYY no  ...\n"));
 
         formatted = mfree(formatted);
 
@@ -450,10 +567,16 @@ TEST(table) {
         assert_se(table_format(t, &formatted) >= 0);
         printf("%s\n", formatted);
 
-        assert_se(streq(formatted,
-                        "… … … …\n"
-                        "… … … …\n"
-                        "… … … …\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "… … … …\n"
+                                "… … … …\n"
+                                "… … … …\n"));
+        else
+                assert_se(streq(formatted,
+                                ". . . .\n"
+                                ". . . .\n"
+                                ". . . .\n"));
 
         formatted = mfree(formatted);
 
@@ -461,10 +584,16 @@ TEST(table) {
         assert_se(table_format(t, &formatted) >= 0);
         printf("%s\n", formatted);
 
-        assert_se(streq(formatted,
-                        "… … … …\n"
-                        "… … … …\n"
-                        "… … … …\n"));
+        if (is_locale_utf8())
+                assert_se(streq(formatted,
+                                "… … … …\n"
+                                "… … … …\n"
+                                "… … … …\n"));
+        else
+                assert_se(streq(formatted,
+                                ". . . .\n"
+                                ". . . .\n"
+                                ". . . .\n"));
 
         formatted = mfree(formatted);
 
@@ -519,12 +648,20 @@ TEST(table) {
         printf("%s\n", formatted);
 
         if (isatty(STDOUT_FILENO))
-                assert_se(streq(formatted,
-                                "no   a long f… no   a long f… a long fi…\n"
-                                "no   fäää      no   fäää      fäää\n"
-                                "yes  fäää      yes  fäää      fäää\n"
-                                "yes  xxx       yes  xxx       xxx\n"
-                                "5min           5min           \n"));
+                if (is_locale_utf8())
+                        assert_se(streq(formatted,
+                                        "no   a long f… no   a long f… a long fi…\n"
+                                        "no   fäää      no   fäää      fäää\n"
+                                        "yes  fäää      yes  fäää      fäää\n"
+                                        "yes  xxx       yes  xxx       xxx\n"
+                                        "5min           5min           \n"));
+                else
+                        assert_se(streq(formatted,
+                                        "no   a long... no   a long... a long ...\n"
+                                        "no   fäää      no   fäää      fäää\n"
+                                        "yes  fäää      yes  fäää      fäää\n"
+                                        "yes  xxx       yes  xxx       xxx\n"
+                                        "5min           5min           \n"));
         else
                 assert_se(streq(formatted,
                                 "no   a long field no   a long field a long field\n"