Revision: 7452
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7452&view=rev
Author:   gerkey
Date:     2009-03-10 18:36:28 +0000 (Tue, 10 Mar 2009)

Log Message:
-----------
Added test infrastructure, using gtest

Modified Paths:
--------------
    code/websim/CMakeLists.txt

Added Paths:
-----------
    code/websim/test/
    code/websim/test/uritest.cc

Modified: code/websim/CMakeLists.txt
===================================================================
--- code/websim/CMakeLists.txt  2009-03-10 18:30:28 UTC (rev 7451)
+++ code/websim/CMakeLists.txt  2009-03-10 18:36:28 UTC (rev 7452)
@@ -1,6 +1,10 @@
 project(WebSim)
 
+add_definitions(-g -Wall)
 cmake_minimum_required( VERSION 2.4 FATAL_ERROR )
+if(COMMAND cmake_policy)
+  cmake_policy(SET CMP0003 NEW)
+endif(COMMAND cmake_policy)
 
 include(FindPkgConfig)
 
@@ -16,7 +20,7 @@
 include_directories( ${GLIB_INCLUDE_DIRS} )
 link_directories(${GLIB_LIBRARY_DIRS} )
 
-include_directories(include)
+include_directories(src)
 add_library(websim SHARED src/websim.cc src/parser.cc src/confederate.cc 
src/puppet.cc )
 add_library(websim-static STATIC src/websim.cc src/parser.cc 
src/confederate.cc src/puppet.cc )
 # Set output name to be the same as shared lib (may not work on Windows)
@@ -31,3 +35,70 @@
         ARCHIVE DESTINATION lib)
 install(FILES src/websim.hh DESTINATION include/websim)
 
+# Everything below is test infrastructure
+link_directories(gtest/lib)
+include_directories(${PROJECT_BINARY_DIR}/gtest/include)
+add_executable(uritest EXCLUDE_FROM_ALL test/uritest.cc)
+target_link_libraries(uritest websim gtest yaml event)
+add_dependencies(uritest fetch_and_build)
+
+add_custom_target(test
+                  COMMAND uritest
+                  WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
+                  DEPENDS uritest)
+
+###############################################################################
+# Macro to dequote a string, in order to properly construct a command line.
+# There must be an easier way to do this.
+macro(_dequote_string _out _in)
+  set(${_out})
+  string(REGEX REPLACE " " ";" tmp "${_in}")
+  foreach(_item ${tmp})
+    string(LENGTH "${${_out}}" _len)
+    if(${_len} GREATER 0)
+      set(${_out} ${${_out}} ${_item})
+    else(${_len} GREATER 0)
+      set(${_out} ${_item})
+    endif(${_len} GREATER 0)
+  endforeach(_item)
+endmacro(_dequote_string)
+
+# A macro to pull in an build gtest
+macro(wget_and_build tarball tarball_url tarball_dir unpack_cmd configure_cmd 
make_cmd install_cmd)
+  find_package(Wget REQUIRED)
+
+  _dequote_string(_unpack_cmd ${unpack_cmd})
+  _dequote_string(_configure_cmd ${configure_cmd})
+  _dequote_string(_make_cmd ${make_cmd})
+  _dequote_string(_install_cmd ${install_cmd})
+
+  add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/${tarball}
+                     COMMAND ${WGET_EXECUTABLE} -r ${tarball_url} -O ${tarball}
+                     COMMAND touch -c ${PROJECT_BINARY_DIR}/${tarball}
+                    VERBATIM)
+  
+  add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/${tarball_dir}
+                     COMMAND ${_unpack_cmd} ${tarball}
+                     COMMAND touch -c ${tarball_dir}
+                    DEPENDS ${PROJECT_BINARY_DIR}/${tarball}
+                    VERBATIM)
+  
+  add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/installed
+                     COMMAND cmake -E chdir 
${PROJECT_BINARY_DIR}/${tarball_dir} ${_configure_cmd}
+                     COMMAND cmake -E chdir 
${PROJECT_BINARY_DIR}/${tarball_dir} ${_make_cmd}
+                     COMMAND cmake -E chdir 
${PROJECT_BINARY_DIR}/${tarball_dir} ${_install_cmd}
+                     COMMAND touch ${PROJECT_BINARY_DIR}/installed
+                    DEPENDS ${PROJECT_BINARY_DIR}/${tarball_dir}
+                     VERBATIM)
+  
+  add_custom_target(fetch_and_build
+                    DEPENDS ${PROJECT_BINARY_DIR}/installed)
+endmacro(wget_and_build)
+
+wget_and_build(gtest-1.0.1.tar.gz
+               http://pr.willowgarage.com/downloads/gtest-1.0.1.tar.gz
+               gtest-1.0.1
+               "tar xzf"
+               "./configure --prefix=${PROJECT_BINARY_DIR}/gtest"
+               "make"
+               "make install")

Added: code/websim/test/uritest.cc
===================================================================
--- code/websim/test/uritest.cc                         (rev 0)
+++ code/websim/test/uritest.cc 2009-03-10 18:36:28 UTC (rev 7452)
@@ -0,0 +1,87 @@
+#include "websim.hh"
+
+#include <gtest/gtest.h>
+
+// These headers must be included prior to the libevent headers
+#include <sys/types.h>
+#include <sys/queue.h>
+#include <event.h>
+
+#include <sys/time.h>
+#include <time.h>
+
+const char* g_host = "localhost";
+const unsigned short g_port = 8000;
+bool g_success;
+bool g_failure;
+struct timeval t0;
+std::string g_response;
+
+void Mark() { gettimeofday(&t0, NULL); }
+double
+ElapsedTime()
+{
+  struct timeval t1;
+  gettimeofday(&t1, NULL);
+  return (t1.tv_sec + t1.tv_usec / 1e6) - (t0.tv_sec + t0.tv_usec / 1e6);
+}
+
+void
+Loop(double dt)
+{
+  struct timespec sleeptime = {0, 1000000};
+  g_success = false;
+  g_failure = false;
+  Mark();
+  while(!g_success && !g_failure && (ElapsedTime() < dt))
+  {
+    event_loop(EVLOOP_NONBLOCK);
+    nanosleep(&sleeptime, NULL);
+  }
+}
+
+void 
+RequestCallback(evhttp_request* req, void* arg)
+{
+  if(req->input_buffer->buffer)
+    g_response = std::string((const char*)req->input_buffer->buffer);
+  else
+    g_response = "";
+  if(req->response_code == 200)
+    g_success = true;
+  else
+    g_failure = true;
+}
+
+TEST(URITest, createGetSetGet)
+{
+  struct evhttp_connection* http_con;  
+  struct evhttp_request* er;
+  ASSERT_TRUE((http_con = evhttp_connection_new(g_host, g_port)));
+
+  ASSERT_TRUE((er = evhttp_request_new(RequestCallback, NULL)));
+  ASSERT_FALSE(evhttp_make_request(http_con, er, EVHTTP_REQ_GET, 
+                                   
"/sim/factory/create?name=foo&type=pioneer2dx"));
+  Loop(5.0);
+  if(!g_success)
+    FAIL();
+
+  ASSERT_TRUE((er = evhttp_request_new(RequestCallback, NULL)));
+  ASSERT_FALSE(evhttp_make_request(http_con, er, EVHTTP_REQ_GET, 
"/foo/pva/get"));
+  Loop(5.0);
+  if(!g_success)
+    FAIL();
+
+  ASSERT_TRUE((er = evhttp_request_new(RequestCallback, NULL)));
+  ASSERT_FALSE(evhttp_make_request(http_con, er, EVHTTP_REQ_GET, 
"/foo/pva/set?pz=2.0"));
+  Loop(5.0);
+  if(!g_success)
+    FAIL();
+}
+
+int main(int argc, char **argv)
+{
+  testing::InitGoogleTest(&argc, argv);
+  event_init();
+  return RUN_ALL_TESTS();
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to