You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@subversion.apache.org by ju...@apache.org on 2010/12/22 12:52:11 UTC

svn commit: r1051864 - /subversion/trunk/subversion/tests/libsvn_subr/subst_translate-test.c

Author: julianfoad
Date: Wed Dec 22 11:52:11 2010
New Revision: 1051864

URL: http://svn.apache.org/viewvc?rev=1051864&view=rev
Log:
Simplify the new test file 'subst_translate-test.c' added in r1051322.

* subversion/tests/libsvn_subr/subst_translate-test.c
  (s_buf, strtocsrc): Delete.
  (translate_string2_data_t, translate_cstring2_data_t): New structs.
  (test_svn_subst_translate_string2, test_svn_subst_translate_cstring2):
    Re-write in a data-driven manner, use helpers such as
    SVN_TEST_STRING_ASSERT(), and other changes to maximize readability.

Modified:
    subversion/trunk/subversion/tests/libsvn_subr/subst_translate-test.c

Modified: subversion/trunk/subversion/tests/libsvn_subr/subst_translate-test.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/tests/libsvn_subr/subst_translate-test.c?rev=1051864&r1=1051863&r2=1051864&view=diff
==============================================================================
--- subversion/trunk/subversion/tests/libsvn_subr/subst_translate-test.c (original)
+++ subversion/trunk/subversion/tests/libsvn_subr/subst_translate-test.c Wed Dec 22 11:52:11 2010
@@ -21,233 +21,66 @@
  * ====================================================================
  */
 
-#include <stddef.h>
-#include <string.h>
-
-#include <apr.h>
-#include <apr_errno.h>
-#include <apr_general.h>
-
 #include "../svn_test.h"
 
 #include "svn_types.h"
-#include "svn_error_codes.h"
-#include "svn_error.h"
-#include "svn_pools.h"
 #include "svn_string.h"
 #include "svn_subst.h"
 
-static char s_buf[2053];
-
-/**
- * Converts the string STR to C code for a string literal that represents it.
- *
- * Not thread safe. The returned pointer is to a static buffer, so it does
- * not need to be freed.
- */
-static char *
-strtocsrc(const char *str)
+/* Test inputs and expected output for svn_subst_translate_string2(). */
+struct translate_string2_data_t
 {
-  char *p = s_buf;
-  size_t i;
-
-  *p++ = '"';
-
-  for (i = 0; str[i] != '\0' && i < 512; ++i) {
-    sprintf(p, "\\x%02x", (int) str[i]);
-    p += 4;
-  }
-
-  if (i < 512) {
-    *p++ = '"';
-  } else {
-    *p++ = '.';
-    *p++ = '.';
-    *p++ = '.';
-    /* no terminating double quote character */
-  }
-
-  *p = '\0';
-
-  return s_buf;
-}
+  const char *source;
+  const char *expected_str;
+  svn_boolean_t translated_to_utf8;
+  svn_boolean_t translated_line_endings;
+};
 
 static svn_error_t *
 test_svn_subst_translate_string2(apr_pool_t *pool)
 {
-  svn_string_t *new_value;
-  svn_boolean_t translated_to_utf8, translated_line_endings;
-
-  {
-  /* No reencoding, no translation of line endings */
-  const char data0[] = "abcdefz";
-  svn_string_t *string0 = svn_string_create(data0, pool);
-  new_value = NULL;
-  translated_line_endings = TRUE;
-  SVN_ERR(svn_subst_translate_string2(&new_value,
-                                      NULL, &translated_line_endings,
-                                      string0, "UTF-8", pool, pool));
-  if (strcmp(new_value->data, data0) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING0 should "
-                             "yield \"abcdefz\".");
-  if (translated_line_endings)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING0 should "
-                             "reset TRANSLATED_LINE_ENDINGS to FALSE because "
-                             "the string does not contain a new line to "
-                             "translate.");
-  new_value = NULL;
-  translated_to_utf8 = TRUE;
-  translated_line_endings = TRUE;
-  SVN_ERR(svn_subst_translate_string2(&new_value, &translated_to_utf8,
-                                      &translated_line_endings,
-                                      string0, "ISO-8859-1", pool, pool));
-  if (strcmp(new_value->data, data0) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "(2) svn_subst_translate_string2() on STRING0 "
-                             "should yield \"abcdefz\".");
-  if (translated_to_utf8)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING0 should "
-                             "reset TRANSLATED_TO_UTF8 to FALSE because "
-                             "the string should not be changed as a result of "
-                             "reencoding to UTF-8.");
-  if (translated_line_endings)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "(2) svn_subst_translate_string2() on STRING0 "
-                             "should reset TRANSLATED_LINE_ENDINGS to FALSE "
-                             "because the string does not contain a new line "
-                             "to translate.");
-  }
-
-  {
-  /* No reencoding, translation of line endings */
-  const char data1[] = "     \r\n\r\n      \r\n        \r\n";
-  svn_string_t *string1 = svn_string_create(data1, pool);
-  const char expected_result1[] = "     \n\n      \n        \n";
-  new_value = NULL;
-  translated_line_endings = FALSE;
-  SVN_ERR(svn_subst_translate_string2(&new_value,
-                                      NULL, &translated_line_endings,
-                                      string1, "UTF-8", pool, pool));
-  if (strcmp(new_value->data, expected_result1) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING1 should "
-                             "yield \"     \\n\\n      \\n        \\n\".");
-  if (! translated_line_endings)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING1 should "
-                             "set TRANSLATED_LINE_ENDINGS to TRUE because the "
-                             "string has Windows-style line endings that "
-                             "were translated.");
-  new_value = NULL;
-  translated_to_utf8 = TRUE;
-  translated_line_endings = FALSE;
-  SVN_ERR(svn_subst_translate_string2(&new_value, &translated_to_utf8,
-                                      &translated_line_endings,
-                                      string1, "ISO-8859-1", pool, pool));
-  if (strcmp(new_value->data, expected_result1) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "(2) svn_subst_translate_string2() on STRING1 "
-                             "should yield \"     \\n\\n      \\n        \\n\""
-                             ".");
-  if (translated_to_utf8)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING1 should "
-                             "reset TRANSLATED_TO_UTF8 to FALSE because "
-                             "the string should not be changed as a result of "
-                             "reencoding to UTF-8.");
-  if (! translated_line_endings)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "(2) svn_subst_translate_string2() on STRING1 "
-                             "should set TRANSLATED_LINE_ENDINGS to TRUE "
-                             "because the string has Windows-style line "
-                             "endings that were translated.");
-  }
-
-  {
-  /* Reencoding, no translation of line endings */
-  const char data2[] = "\xc7\xa9\xf4\xdf";
-  svn_string_t *string2 = svn_string_create(data2, pool);
-  const char expected_result2[] = "\xc3\x87\xc2\xa9\xc3\xb4\xc3\x9f";
-  new_value = NULL;
-  translated_to_utf8 = FALSE;
-  SVN_ERR(svn_subst_translate_string2(&new_value,
-                                      &translated_to_utf8, NULL,
-                                      string2, "ISO-8859-1", pool, pool));
-  if (strcmp(new_value->data, expected_result2) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING2 should "
-                             "yield \"\\xc3\\x87\\xc2\\xa9\\xc3\\xb4\\xc3\\x9f"
-                             "\". Instead, got %s.",
-                             strtocsrc(new_value->data));
-  if (! translated_to_utf8)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING2 should "
-                             "set TRANSLATED_TO_UTF8 to TRUE.");
-  new_value = NULL;
-  translated_to_utf8 = FALSE;
-  translated_line_endings = TRUE;
-  SVN_ERR(svn_subst_translate_string2(&new_value, &translated_to_utf8,
-                                      &translated_line_endings,
-                                      string2, "ISO-8859-1", pool, pool));
-  if (strcmp(new_value->data, expected_result2) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "(2) svn_subst_translate_string2() on STRING2 "
-                             "should yield \"\\xc3\\x87\\xc2\\xa9\\xc3\\xb4"
-                             "\\xc3\\x9f\". Instead, got %s.",
-                             strtocsrc(new_value->data));
-  if (! translated_to_utf8)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "(2) svn_subst_translate_string2() on STRING2 "
-                             "should set TRANSLATED_TO_UTF8 to TRUE.");
-  if (translated_line_endings)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING2 should "
-                             "reset TRANSLATED_LINE_ENDINGS to FALSE.");
-  }
-
-  {
-  /* Reencoding, translation of line endings */
-  const char data3[] = "\xc7\xa9\xf4\xdf\r\n";
-  svn_string_t *string3 = svn_string_create(data3, pool);
-  const char expected_result3[] = "\xc3\x87\xc2\xa9\xc3\xb4\xc3\x9f\n";
-  new_value = NULL;
-  translated_to_utf8 = FALSE;
-  SVN_ERR(svn_subst_translate_string2(&new_value,
-                                      &translated_to_utf8, NULL,
-                                      string3, "ISO-8859-1", pool, pool));
-  if (strcmp(new_value->data, expected_result3) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING3 should "
-                             "yield \"\\xc3\\x87\\xc2\\xa9\\xc3\\xb4\\xc3\\x9f"
-                             "\\x0a\". Instead, got %s.",
-                             strtocsrc(new_value->data));
-  if (! translated_to_utf8)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING3 should "
-                             "set TRANSLATED_TO_UTF8 to TRUE.");
-  new_value = NULL;
-  translated_to_utf8 = FALSE;
-  translated_line_endings = FALSE;
-  SVN_ERR(svn_subst_translate_string2(&new_value, &translated_to_utf8,
-                                      &translated_line_endings,
-                                      string3, "ISO-8859-1", pool, pool));
-  if (strcmp(new_value->data, expected_result3) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "(2) svn_subst_translate_string2() on STRING3 "
-                             "should yield \"\\xc3\\x87\\xc2\\xa9\\xc3\\xb4"
-                             "\\xc3\\x9f\\x0a\". Instead, got %s.",
-                             strtocsrc(new_value->data));
-  if (! translated_to_utf8)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "(2) svn_subst_translate_string2() on STRING3 "
-                             "should set TRANSLATED_TO_UTF8 to TRUE.");
-  if (! translated_line_endings)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_string2() on STRING3 should "
-                             "set TRANSLATED_LINE_ENDINGS to TRUE.");
+  static const struct translate_string2_data_t tests[] =
+    {
+      /* No reencoding, no translation of line endings */
+      { "abcdefz",
+        "abcdefz", FALSE, FALSE },
+      /* No reencoding, translation of line endings */
+      { "     \r\n\r\n      \r\n        \r\n",
+        "     \n\n      \n        \n", FALSE, TRUE },
+      /* Reencoding, no translation of line endings */
+      { "\xc7\xa9\xf4\xdf",
+        "\xc3\x87\xc2\xa9\xc3\xb4\xc3\x9f", TRUE, FALSE },
+      /* Reencoding, translation of line endings */
+      { "\xc7\xa9\xf4\xdf\r\n",
+        "\xc3\x87\xc2\xa9\xc3\xb4\xc3\x9f\n", TRUE, TRUE },
+      { NULL, NULL, FALSE, FALSE }
+    };
+  const struct translate_string2_data_t *t;
+
+  for (t = tests; t->source != NULL; t++)
+  {
+    svn_string_t *source_string = svn_string_create(t->source, pool);
+    svn_string_t *new_value = NULL;
+    svn_boolean_t translated_line_endings = ! t->translated_line_endings;
+    svn_boolean_t translated_to_utf8;
+
+    SVN_ERR(svn_subst_translate_string2(&new_value,
+                                        NULL, &translated_line_endings,
+                                        source_string, "ISO-8859-1",
+                                        pool, pool));
+    SVN_TEST_STRING_ASSERT(new_value->data, t->expected_str);
+    SVN_TEST_ASSERT(translated_line_endings == t->translated_line_endings);
+
+    new_value = NULL;
+    translated_to_utf8 = ! t->translated_to_utf8;
+    translated_line_endings = ! t->translated_line_endings;
+    SVN_ERR(svn_subst_translate_string2(&new_value, &translated_to_utf8,
+                                        &translated_line_endings,
+                                        source_string, "ISO-8859-1",
+                                        pool, pool));
+    SVN_TEST_STRING_ASSERT(new_value->data, t->expected_str);
+    SVN_TEST_ASSERT(translated_to_utf8 == t->translated_to_utf8);
+    SVN_TEST_ASSERT(translated_line_endings == t->translated_line_endings);
   }
 
   return SVN_NO_ERROR;
@@ -265,56 +98,41 @@ test_svn_subst_stream_translated(apr_poo
   return SVN_NO_ERROR;
 }*/
 
+/* Test inputs and expected output for svn_subst_translate_cstring2(). */
+struct translate_cstring2_data_t
+{
+  const char *source;
+  const char *eol_str;
+  svn_boolean_t repair;
+  const char *expected_str;
+};
+
 static svn_error_t *
 test_svn_subst_translate_cstring2(apr_pool_t *pool)
 {
-  {
-  /* Test the unusual case where EOL_STR is an empty string. */
-  const char src0[] = "   \r   \n\r\n     \n\n\n";
-  const char *dest0 = NULL;
-  const char expected_result0[] = "           ";
-  SVN_ERR(svn_subst_translate_cstring2(src0, &dest0, "", TRUE, NULL, FALSE,
-                                       pool));
-  if (strcmp(dest0, expected_result0) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_cstring2() on SRC0 should "
-                             "yield \"           \".");
-  }
-
-  {
-  /* Test the unusual case where EOL_STR is not a standard EOL string. */
-  const char src1[] = "   \r   \n\r\n     \n\n\n";
-  const char *dest1 = NULL;
-  const char expected_result1[] = "   z   zz     zzz";
-  SVN_ERR(svn_subst_translate_cstring2(src1, &dest1, "z", TRUE, NULL, FALSE,
-                                       pool));
-  if (strcmp(dest1, expected_result1) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_cstring2() on SRC1 should "
-                             "yield \"   z   zz     zzz\".");
-  }
-  {
-  const char src2[] = "    \n    \n ";
-  const char *dest2 = NULL;
-  const char expected_result2[] = "    buzz    buzz ";
-  SVN_ERR(svn_subst_translate_cstring2(src2, &dest2, "buzz", FALSE, NULL, FALSE,
-                                       pool));
-  if (strcmp(dest2, expected_result2) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_cstring2() on SRC2 should "
-                             "yield \"    buzz    buzz \".");
-  }
-  {
-  const char src3[] = "    \r\n    \n";
-  const char *dest3 = NULL;
-  const char expected_result3[] = "    buzz    buzz";
-  SVN_ERR(svn_subst_translate_cstring2(src3, &dest3, "buzz", TRUE, NULL, FALSE,
-                                       pool));
-  if (strcmp(dest3, expected_result3) != 0)
-    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
-                             "svn_subst_translate_cstring2() on SRC3 should "
-                             "yield \"    buzz    buzz\".");
-  }
+  static const struct translate_cstring2_data_t tests[] =
+    {
+      /* Test the unusual case where EOL_STR is an empty string. */
+      { "   \r   \n\r\n     \n\n\n", "", TRUE,
+        "           " },
+      /* Test the unusual case where EOL_STR is not a standard EOL string. */
+      { "   \r   \n\r\n     \n\n\n", "z", TRUE,
+        "   z   zz     zzz" },
+      { "    \n    \n ", "buzz", FALSE,
+        "    buzz    buzz " },
+      { "    \r\n    \n", "buzz", TRUE ,
+        "    buzz    buzz"},
+      { NULL, NULL, FALSE, NULL }
+    };
+  const struct translate_cstring2_data_t *t;
+
+  for (t = tests; t->source != NULL; t++)
+    {
+      const char *result = NULL;
+      SVN_ERR(svn_subst_translate_cstring2(t->source, &result, t->eol_str,
+                                           t->repair, NULL, FALSE, pool));
+      SVN_TEST_STRING_ASSERT(result, t->expected_str);
+    }
 
   return SVN_NO_ERROR;
 }