Catch up with Gearman's libtest
[m6w6/libmemcached] / tests / parser.cc
index 823970ede167514716512c446e309082febaa4e4..7078305336ea3f1c267b96e5a590606231e729f4 100644 (file)
@@ -1,9 +1,8 @@
 /*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  * 
- *  Gearmand client and server library.
+ *  Libmemcached library
  *
- *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
- *  All rights reserved.
+ *  Copyright (C) 2012 Data Differential, http://datadifferential.com/
  *
  *  Redistribution and use in source and binary forms, with or without
  *  modification, are permitted provided that the following conditions are
 
 #include <config.h>
 
-#include <libmemcached/memcached.h>
+/*
+  C++ interface test
+*/
+#include <libmemcached-1.0/memcached.hpp>
+#include <libtest/test.hpp>
 
-#include "tests/parser.h"
-#include "tests/print.h"
+using namespace libtest;
 
-struct scanner_string_st {
-  const char *c_ptr;
-  size_t size;
-};
+static test_return_t memcached_NULL_string_TEST(void*)
+{
+  test_null(memcached(NULL, 75));
+  return TEST_SUCCESS;
+}
+
+static test_return_t memcached_zero_string_length_TEST(void*)
+{
+  test_null(memcached("value", 0));
+  return TEST_SUCCESS;
+}
+
+static test_return_t putenv_localhost_quoted_TEST(void*)
+{
+  char set_env[1024];
+
+  snprintf(set_env, sizeof(set_env), "LIBMEMCACHED=\"--server=localhost\"");
+  test_zero(putenv(set_env));
+  test_null(memcached(NULL, 0));
+
+  return TEST_SUCCESS;
+}
 
-test_return_t server_test(memcached_st *junk)
+static test_return_t putenv_NULL_TEST(void*)
 {
-  (void)junk;
-  memcached_return_t rc;
-  memcached_st *memc;
-  memc= memcached_create(NULL);
-
-  scanner_string_st test_strings[]= {
-    { STRING_WITH_LEN("--server=localhost") },
-    { STRING_WITH_LEN("--server=10.0.2.1") },
-    { STRING_WITH_LEN("--server=example.com") },
-    { STRING_WITH_LEN("--server=localhost:30") },
-    { STRING_WITH_LEN("--server=10.0.2.1:20") },
-    { STRING_WITH_LEN("--server=example.com:1024") },
-    { NULL, 0}
-  };
-
-  for (scanner_string_st *ptr= test_strings; ptr->size; ptr++)
-  {
-    rc= memcached_parse_options(memc, ptr->c_ptr, ptr->size);
-    test_true(rc == MEMCACHED_SUCCESS);
-    memcached_servers_reset(memc);
-  }
+  char set_env[1024];
+
+  snprintf(set_env, sizeof(set_env), "LIBMEMCACHED");
+  test_zero(putenv(set_env));
+  memcached_st *memc= memcached(NULL, 0);
+  test_true(memc);
 
   memcached_free(memc);
 
   return TEST_SUCCESS;
 }
 
-test_return_t servers_test(memcached_st *junk)
+static test_return_t putenv_NULL_TEST2(void*)
 {
-  (void)junk;
-  memcached_st *memc;
-  memc= memcached_create(NULL);
+  char set_env[1024];
 
-  scanner_string_st test_strings[]= {
-    { STRING_WITH_LEN("--servers=localhost:11221,localhost:11222,localhost:11223,localhost:11224,localhost:11225") },
-    { STRING_WITH_LEN("--servers=a.example.com:81,localhost:82,b.example.com") },
-    { STRING_WITH_LEN("--servers=localhost,localhost:80") },
-    { NULL, 0}
-  };
+  snprintf(set_env, sizeof(set_env), "LIBMEMCACHED=");
+  test_zero(putenv(set_env));
+  memcached_st *memc= memcached(NULL, 0);
+  test_true(memc);
 
-  for (scanner_string_st *ptr= test_strings; ptr->size; ptr++)
-  {
-    memcached_return_t rc;
-    rc= memcached_parse_options(memc, ptr->c_ptr, ptr->size);
+  memcached_free(memc);
 
-    test_true(rc == MEMCACHED_SUCCESS);
+  return TEST_SUCCESS;
+}
 
-    memcached_server_fn callbacks[1];
-    callbacks[0]= server_print_callback;
-    memcached_server_cursor(memc, callbacks, NULL,  1);
+static test_return_t putenv_localhost_TEST(void*)
+{
+  char set_env[1024];
 
-    memcached_servers_reset(memc);
-  }
+  snprintf(set_env, sizeof(set_env), "LIBMEMCACHED=--server=localhost");
+  test_zero(putenv(set_env));
+  memcached_st *memc= memcached(NULL, 0);
+  test_true(memc);
 
   memcached_free(memc);
 
   return TEST_SUCCESS;
 }
+
+test_st memcached_TESTS[] ={
+  {"memcached(NULL, 75)", false, (test_callback_fn*)memcached_NULL_string_TEST },
+  {"memcached(\"value\", 0)", false, (test_callback_fn*)memcached_zero_string_length_TEST },
+  {"putenv(LIBMEMCACHED=--server=localhost)", false, (test_callback_fn*)putenv_localhost_TEST },
+  {"putenv(LIBMEMCACHED)", false, (test_callback_fn*)putenv_NULL_TEST },
+  {"putenv(LIBMEMCACHED=)", false, (test_callback_fn*)putenv_NULL_TEST2 },
+  {"putenv(LIBMEMCACHED=--server=\"localhost\")", false, (test_callback_fn*)putenv_localhost_quoted_TEST },
+  {0, 0, 0}
+};
+
+collection_st collection[] ={
+  {"memcached()", 0, 0, memcached_TESTS},
+  {0, 0, 0, 0}
+};
+
+void get_world(Framework *world)
+{
+  world->collections(collection);
+}
+