Re: [libvirt] [PATCH v3 3/3] vsh: Added tests

2018-08-16 Thread Ján Tomko

On Thu, Aug 16, 2018 at 12:56:26PM +0200, Simon Kobyda wrote:

For now, there are 5 test cases
- testVshTableNew: Creating table with empty header
- testVshTableHeader: Printing table with/without header
- testVshTableRowAppend: Appending row with various number of cells.
 Only row with same number of cells as in header is accepted.
- testVshTableNewUnicode: Printing table with unicode characters.
 Checking correct alignment.
- testNTables: Create and print various types of tables - one column,
 one row table, table without content, standard table...

Signed-off-by: Simon Kobyda 
---
tests/Makefile.am|   8 ++
tests/vshtabletest.c | 247 +++
2 files changed, 255 insertions(+)
create mode 100644 tests/vshtabletest.c
+static int
+testVshTableNew(const void *opaque ATTRIBUTE_UNUSED)
+{
+int ret = 0;


no need for a 'ret' variable if you don't have a cleanup section.


+
+if (vshTableNew(NULL)) {
+fprintf(stderr, "expected failure when passing null to"
+"vshtablenew\n");


Missing space between 'to' and 'vshtablenew'


+ret = -1;
+}
+
+return ret;
+}
+
+static int
+testVshTableHeader(const void *opaque ATTRIBUTE_UNUSED)
+{
+int ret = 0;
+char *out;
+const char *exp = "\
+ 1   fedora28   running  \n\
+ 2   rhel7.5running  \n";
+const char *exp2 = "\
+ Id   Name   State\n\
+--\n\
+ 1fedora28   running  \n\
+ 2rhel7.5running  \n";


Please use one string literal per line in new code. That way you can align them.


+
+vshTablePtr table = vshTableNew("Id", "Name", "State",
+NULL); //to ask about return
+if (!table)
+goto cleanup;
+
+vshTableRowAppend(table, "1", "fedora28", "running", NULL);
+vshTableRowAppend(table, "2", "rhel7.5", "running",
+  NULL);
+


[...]


+static int
+testNTables(const void *opaque ATTRIBUTE_UNUSED)
+{
+int ret = 0;
+vshTablePtr table1;
+vshTablePtr table2;
+vshTablePtr table3;
+const char *exp1 = "\
+ Id   Name   Status   \n\
+--\n\
+ 1fedora28   running  \n\
+ 2rhel7.5running  \n";
+const char *exp2 = "\
+ Id   Name   Status  \n\
+-\n";
+const char *exp3 = "\
+ Id  \n\
+-\n\
+ 1   \n\
+ 2   \n\
+ 3   \n\
+ 4   \n";
+char *out1;
+char *out2;
+char *out3;
+
+table1 = vshTableNew("Id", "Name", "Status", NULL);
+if (!table1)
+goto cleanup;
+vshTableRowAppend(table1, "1", "fedora28", "running", NULL);
+vshTableRowAppend(table1, "2", "rhel7.5", "running", NULL);
+out1 = vshTablePrintToString(table1, true);
+
+table2 = vshTableNew("Id", "Name", "Status", NULL);
+if (!table2)
+goto cleanup;


out2 and out3 are unitialized if you jump to cleanup here.

Jano


+out2 = vshTablePrintToString(table2, true);
+
+table3 = vshTableNew("Id", NULL);


signature.asc
Description: Digital signature
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v3 3/3] vsh: Added tests

2018-08-16 Thread Simon Kobyda
For now, there are 5 test cases
- testVshTableNew: Creating table with empty header
- testVshTableHeader: Printing table with/without header
- testVshTableRowAppend: Appending row with various number of cells.
  Only row with same number of cells as in header is accepted.
- testVshTableNewUnicode: Printing table with unicode characters.
  Checking correct alignment.
- testNTables: Create and print various types of tables - one column,
  one row table, table without content, standard table...

Signed-off-by: Simon Kobyda 
---
 tests/Makefile.am|   8 ++
 tests/vshtabletest.c | 247 +++
 2 files changed, 255 insertions(+)
 create mode 100644 tests/vshtabletest.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 21a6c823d9..136fe16f71 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -206,6 +206,7 @@ test_programs = virshtest sockettest \
virhostdevtest \
virnetdevtest \
virtypedparamtest \
+   vshtabletest \
$(NULL)
 
 test_libraries = libshunload.la \
@@ -938,6 +939,13 @@ metadatatest_SOURCES = \
testutils.c testutils.h
 metadatatest_LDADD = $(LDADDS) $(LIBXML_LIBS)
 
+vshtabletest_SOURCES = \
+   vshtabletest.c \
+   testutils.c testutils.h
+vshtabletest_LDADD = \
+   $(LDADDS) \
+   ../tools/libvirt_shell.la
+
 virshtest_SOURCES = \
virshtest.c \
testutils.c testutils.h
diff --git a/tests/vshtabletest.c b/tests/vshtabletest.c
new file mode 100644
index 00..b41a205761
--- /dev/null
+++ b/tests/vshtabletest.c
@@ -0,0 +1,247 @@
+/*
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * .
+ */
+
+#include 
+
+#include 
+#include 
+#include 
+
+#include "internal.h"
+#include "testutils.h"
+#include "viralloc.h"
+#include "../tools/vsh-table.h"
+
+static int
+testVshTableNew(const void *opaque ATTRIBUTE_UNUSED)
+{
+int ret = 0;
+
+if (vshTableNew(NULL)) {
+fprintf(stderr, "expected failure when passing null to"
+"vshtablenew\n");
+ret = -1;
+}
+
+return ret;
+}
+
+static int
+testVshTableHeader(const void *opaque ATTRIBUTE_UNUSED)
+{
+int ret = 0;
+char *out;
+const char *exp = "\
+ 1   fedora28   running  \n\
+ 2   rhel7.5running  \n";
+const char *exp2 = "\
+ Id   Name   State\n\
+--\n\
+ 1fedora28   running  \n\
+ 2rhel7.5running  \n";
+
+vshTablePtr table = vshTableNew("Id", "Name", "State",
+NULL); //to ask about return
+if (!table)
+goto cleanup;
+
+vshTableRowAppend(table, "1", "fedora28", "running", NULL);
+vshTableRowAppend(table, "2", "rhel7.5", "running",
+  NULL);
+
+out = vshTablePrintToString(table, false);
+if (virTestCompareToString(exp, out) < 0)
+ret = -1;
+
+VIR_FREE(out);
+out = vshTablePrintToString(table, true);
+if (virTestCompareToString(exp2, out) < 0)
+ret = -1;
+
+ cleanup:
+VIR_FREE(out);
+vshTableFree(table);
+return ret;
+}
+
+static int
+testVshTableNewUnicode(const void *opaque ATTRIBUTE_UNUSED)
+{
+
+int ret = 0;
+char *out;
+
+char *locale = setlocale(LC_CTYPE, NULL);
+if (!setlocale(LC_CTYPE, "en_US.UTF-8"))
+return EXIT_AM_SKIP;
+
+const char *exp = "\
+ Id   名稱  государство  \n\
+-\n\
+ 1fedora28  running  \n\
+ 2rhel7.5   running  \n";
+vshTablePtr table;
+
+table = vshTableNew("Id", "名稱", "государство", NULL);
+if (!table)
+goto cleanup;
+
+vshTableRowAppend(table, "1", "fedora28", "running", NULL);
+vshTableRowAppend(table, "2", "rhel7.5", "running",
+  NULL);
+
+out = vshTablePrintToString(table, true);
+if (virTestCompareToString(exp, out) < 0)
+ret = -1;
+
+ cleanup:
+setlocale(LC_CTYPE, locale);
+VIR_FREE(out);
+vshTableFree(table);
+return ret;
+}
+
+static int
+testVshTableRowAppend(const void *opaque ATTRIBUTE_UNUSED)
+{
+int ret = 0;
+
+vshTablePtr table = vshTableNew("Id", "Name", NULL);
+if (!table)
+goto cleanup;
+
+if (vshTableRowAppend(table, NULL) >= 0) {
+fprintf(stderr, "Appending