Browse Source

Update and add unit tests

- update Makefile.am to add new test files
- add test_elt.h, a doc_elt to help with testing
- add tests for merge_print
- add tests for org_text and org_heading
- change the format of test org_documents
Andrew Young 8 years ago
parent
commit
56c2de3270

+ 3 - 1
tests/Makefile.am

@@ -7,7 +7,9 @@ check_org_merge_driver_SOURCES = \
 	check_org-merge-driver.c \
 	check_parser.c \
 	check_list_diff.c \
-	check_doc_merge.c
+	check_doc_merge.c \
+	check_org_elements.c \
+	check_merge_print.c
 
 check_org_merge_driver_CFLAGS = @CHECK_CFLAGS@ -I$(top_srcdir)/src \
 	-I$(top_srcdir)/gnulib/lib 

+ 65 - 0
tests/check_merge_print.c

@@ -0,0 +1,65 @@
+/**
+ * @file check_merge_print.c
+ */
+#include "config.h"
+#include <check.h>
+#include "gl_list.h"
+#include "merge_print.c"
+#include "test_elt.h"
+
+int next_val = 0;
+
+merge_node *
+make_merge_node ()
+{
+  merge_node *node = ltree_node_create_empty ();
+  merge_delta *delta = merge_delta_create_empty ();
+  test_elt *elt = test_elt_create (next_val++);
+  
+  merge_delta_set_elt (delta, (doc_elt *)elt);
+  merge_node_set_delta (node, delta);
+  return node;
+}
+
+merge_node *
+make_merge_node_with_children (size_t num)
+{
+  merge_node *node = make_merge_node ();
+  merge_list list = merge_node_get_children (node);
+  size_t i;
+  for (i = 0; i < num; i++)
+    {
+      gl_list_nx_add_last (list, make_merge_node ());
+    }
+}
+
+START_TEST (check_merge_print)
+{
+
+  merge_node *root = make_merge_node_with_children (2);
+
+  merge_print_ctxt ctxt;
+  ctxt.depth = 0;
+  doc_stream *out = stdout;
+  merge_print (root, &ctxt, out);
+
+  merge_node *child     = (merge_node *)gl_list_get_at (merge_node_get_children (root), 1);
+  test_elt   *child_elt = (test_elt *)merge_delta_get_elt(merge_node_get_delta (child));
+
+  fail_if (  child_elt->merge_print_depth != 1
+	   && test_elt_get_value(child_elt) != 2);
+} END_TEST
+
+
+Suite *
+make_merge_print_suite (void)
+{
+  Suite *s = suite_create ("Merge Print");
+
+  /* Core test case */
+  TCase *tc_core = tcase_create ("Core");
+  tcase_add_test (tc_core, check_merge_print);
+
+  suite_add_tcase (s, tc_core);
+  return s;
+}

+ 2 - 1
tests/check_org-merge-driver.c

@@ -10,8 +10,9 @@ main (void)
   SRunner *sr = srunner_create (make_parser_suite ());
    srunner_set_fork_status (sr, CK_NOFORK);
   srunner_add_suite (sr, make_list_diff_suite());
-  //srunner_add_suite (sr, make_org_elements_suite());
+  srunner_add_suite (sr, make_org_elements_suite());
   srunner_add_suite (sr, make_doc_merge_suite());
+  srunner_add_suite (sr, make_merge_print_suite());
   srunner_run_all (sr, CK_NORMAL);
   number_failed = srunner_ntests_failed (sr);
   srunner_free (sr);

+ 2 - 1
tests/check_org-merge-driver.h

@@ -6,5 +6,6 @@
 Suite * make_parser_suite (void);
 Suite * make_list_diff_suite (void);
 Suite * make_org_elements_suite (void);
-Suite *make_doc_merge_suite (void);
+Suite * make_doc_merge_suite (void);
+Suite * make_merge_print_suite (void);
 #endif

+ 46 - 1
tests/check_org_elements.c

@@ -1,3 +1,5 @@
+#if 0
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdbool.h>
@@ -11,6 +13,7 @@
 #include "org_phony.h"
 #include "org_phony.c"
 
+
 /*
  * Test Case Org Phony
  */
@@ -23,9 +26,11 @@ START_TEST (check_org_phony_compare_op)
 }
 END_TEST
 
+
 /*
  * Test Case Org Heading
  */
+
 START_TEST (check_compare_org_header_are_same)
 {
   char heading_text[] = "Test Heading Text";
@@ -69,6 +74,43 @@ START_TEST (check_compare_org_header_are_different_text)
 }
 END_TEST
 
+#endif
+
+/*
+ * Org Text
+ */
+#include <check.h>
+#include <string.h>
+#include "org_text.h"
+#include "org_text.c"
+
+START_TEST (check_org_text_strncat)
+{
+  char *msg_one = "Message 1";
+  char *msg_two = "Message 2";
+  org_text *text = org_text_create_empty();
+  org_text_strncat (text, msg_one, 9);
+  org_text_strncat (text, msg_two, 9);
+  bool text_eql = (0 == (strcmp (org_text_get_text(text), "Message1Message2")));
+  fail_if (!text_eql);
+}
+END_TEST
+
+
+START_TEST (check_org_text_create_and_print)
+{
+  char *msg_one = "Message 1";
+  char *msg_two = "Message 2";
+  org_text *text = org_text_create_empty();
+  //org_text_strncat (text, msg_one, 9);
+  //org_text_strncat (text, msg_two, 9);
+
+  org_text_print_op ((doc_elt *)text, NULL, stdout);
+  doc_elt_print ((doc_elt *) text, NULL, stdout);
+
+  fail_if (0);
+}
+END_TEST
 
 /*
  * Org Elements Suite
@@ -81,11 +123,14 @@ make_org_elements_suite (void)
   Suite *s = suite_create ("Org Elements");
   /* Core test case */
   TCase *tc_core = tcase_create ("Core");
- 
+  /*
   tcase_add_test (tc_core, check_org_phony_compare_op);
   tcase_add_test (tc_core, check_compare_org_header_are_same);
   tcase_add_test (tc_core, check_compare_org_header_are_different_levels);
   tcase_add_test (tc_core, check_compare_org_header_are_different_text);
+  */
+  tcase_add_test (tc_core, check_org_text_strncat);
+  tcase_add_test (tc_core, check_org_text_create_and_print);
 
   suite_add_tcase (s, tc_core);
   return s;

+ 1 - 1
tests/t1-1.org

@@ -7,4 +7,4 @@
 :PROPERTIES:
 :ID: 100
 :END:
-- this is just a test
+- this is just a test111

+ 0 - 7
tests/test1.org

@@ -1,15 +1,8 @@
-- this is going to be a test
 * Heading 1
-- this is such a test
 * Heading 2
-- we are testing this
 *** Heading 3
-- I'm wondering what this will do
-
 * Heading 4
-- testing this is so much fun 
 ** Heading 5
 *** Heading 6
 *** Heading 7
 ** Heading 8
-- some text

+ 116 - 0
tests/test_elt.h

@@ -0,0 +1,116 @@
+/**
+ * @file test_elt.h
+ */
+
+#ifndef TEST_ELT_H
+#define TEST_ELT_H
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include "merge_print_ctxt.h"
+#include "doc_stream.h"
+#include "merge_delta.h"
+#include "doc_elt.h"
+#include "doc_elt_ops.h"
+
+static void test_elt_print_op (doc_elt *elt, void *ctxt, doc_stream *out);
+static void test_elt_merge_print_op (merge_delta *delta, merge_print_ctxt *ctxt, 
+				     doc_stream *out);
+static bool test_elt_compare_op (doc_elt *a, doc_src a_src, 
+				 doc_elt *b, doc_src b_src, void *ctxt);
+/**
+ * @struct test_elt
+ * @brief An extremely simple doc_elt used for testing.
+ */
+typedef struct test_elt
+{
+  doc_elt elt;
+  int value;
+  int merge_print_depth;
+} test_elt;
+
+static doc_elt_ops test_elt_ops = {
+  .print = test_elt_print_op,
+  .merge_print = test_elt_merge_print_op,
+  .compare = test_elt_compare_op
+};
+
+static int
+test_elt_get_value (test_elt *elt)
+{
+  return elt->value;
+}
+
+static void
+test_elt_set_value (test_elt *elt, int value)
+{
+  elt->value = value;
+  return;
+}
+
+static int
+test_elt_get_merge_print_depth (test_elt *elt)
+{
+  return elt->merge_print_depth;
+}
+
+static void
+test_elt_set_merge_print_depth (test_elt *elt, int depth)
+{
+  elt->merge_print_depth = depth;
+  return;
+}
+
+/**
+ * Reset values in elt changed by doc_elt_ops to zero.
+ */
+static void
+test_elt_reset (test_elt *elt)
+{
+  test_elt_set_merge_print_depth (elt, 0);
+}
+
+static void
+test_elt_print_op (doc_elt *elt, void *ctxt, doc_stream *out)
+{
+  /* do nothing */
+}
+
+static void
+test_elt_merge_print_op (merge_delta *delta, merge_print_ctxt *ctxt, doc_stream *out)
+{
+  doc_elt *elt = merge_delta_get_elt (delta);
+  test_elt_set_merge_print_depth ((test_elt *)elt, ctxt->depth);
+  ctxt->depth++;
+  return;
+}
+
+static bool
+test_elt_compare_op (doc_elt *a, doc_src a_src, 
+		     doc_elt *b, doc_src b_src, void *ctxt)
+{
+  doc_elt_ops *a_ops = doc_elt_get_ops (a);
+  doc_elt_ops *b_ops = doc_elt_get_ops (b);
+  assert (a_ops == b_ops);
+  assert (a_ops == &test_elt_ops);
+  assert (b_ops == &test_elt_ops);
+  return (test_elt_get_value ((test_elt*)a) == test_elt_get_value ((test_elt *)b));
+}
+
+
+static test_elt *
+test_elt_create (int value)
+{
+  test_elt *elt = malloc (sizeof (test_elt));
+  doc_elt_set_ops (&(elt->elt), &test_elt_ops);
+  test_elt_set_value (elt, value);
+  test_elt_set_merge_print_depth (elt, 0);
+}
+
+static void 
+test_elt_free (test_elt *elt)
+{
+  free (elt);
+}
+
+#endif

+ 12 - 5
tests/tout.org

@@ -1,10 +1,7 @@
-<<<<<<< yours: t1-1.org
-=======
 * heading 2
 - new text under heading 2
 - this will conflict since heading 2
   deleted in file 1
->>>>>>> theirs: t1-2.org
 * heading 3
 ** heading 4
 ** test heading
@@ -12,7 +9,17 @@
 :ID: 100
 :END:
 - updated line in file 2
+this is some more text
+
+
+
+aoeuaoeua
+aoeuaeou
 * heading 1
 ** test heading
-- this is a different heading
-- this line added in file 2
+ooeueou
+aoeuaeou
+*** aoetnh
+- andrew
+aeou
+