Anonymous avatar Anonymous committed fe7804a

Added platform tests

Comments (0)

Files changed (6)

neglect/cpplib/include/neglect/ntpath.hpp

 
 namespace neglect { namespace ntpath {
 
+    /* checks if a path is absolute */
     bool is_abs(const neglect::string &filename);
+
+    /* joins two paths */
     neglect::string join(const neglect::string &a, const neglect::string &b);
+
+    /* returns the directory name from a path */
     neglect::string dirname(const neglect::string &filename);
+
+    /* returns the filename or last folder from a path */
     neglect::string basename(const neglect::string &filename);
 
     // the following functions will only work if compiled for
     // windows (obviously)
 #if NEGLECT_PLATFORM == NEGLECT_PLATFORM_WINDOWS
+
+    /* makes a path absolute */
     neglect::string abspath(const neglect::string &filename);
+
+    /* returns the current working directory */
     neglect::string getcwd();
+
+    /* checks if a path exists */
     bool exists(const neglect::string &filename);
+
+    /* checks if a path is a file */
     bool is_file(const neglect::string &filename);
+
+    /* checks if a path is a directory */
     bool is_dir(const neglect::string &filename);
+
+    /* creates a directory.  returns true if the directory exists
+       after this call (it either was created or existed already) */
     bool mkdir(const neglect::string &filename);
+
+    /* removes a directory or file.  When removing directories, the
+       directory has to be empty.  Returns true if the file does
+       not exist after this call (either did not exist to begin with
+       or was successfully removed)*/
     bool remove(const neglect::string &filename);
+
+    /* renames a file or directory.  Returns true if this
+       worked out well.  This overrites the target file if it
+       existed already. */
     bool rename(const neglect::string &filename,
                 const neglect::string &new_name);
 #endif

neglect/cpplib/include/neglect/platform.hpp

         INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
 #elif NEGLECT_PLATFORM == NEGLECT_PLATFORM_OSX
 #   define NEGLECT_PLATFORM_MAIN() \
-        int neglect_app_main(int, char **)
+        int neglect_app_main(int argc, char **argv)
 #endif
 
 namespace neglect {
     namespace platform {
 
         /* name of the platform */
-        extern const string name;
+        extern const neglect::string name;
 
         /* returns the path to the users's home directory */
-        const string &get_home_folder();
+        const neglect::string &get_home_folder();
 
         /* returns the path to the users's settings directory */
-        string get_settings_folder(const string &appname);
+        neglect::string get_settings_folder(const string &appname, bool exposed=false);
+
+        /* the name of the executable */
+        const neglect::string &get_executable();
+
+        /* the arguments passed to the application */
+        const std::vector<neglect::string> &get_arguments();
     }
 }
 

neglect/cpplib/src/platform.cpp

 #include <neglect/platform.hpp>
+#include <neglect/path.hpp>
 
 #if NEGLECT_PLATFORM == NEGLECT_PLATFORM_WINDOWS
 #   include "shlobj.h"
 namespace {
     bool _home_folder_cached = false;
     neglect::string _home_folder;
+
+    bool _executable_found = false;
+    neglect::string _executable;
+    bool _arguments_found = false;
+    std::vector<std::string> _arguments;
 }
 
 const neglect::string &platform::get_home_folder()
     return _home_folder;
 }
 
-neglect::string platform::get_settings_folder(const neglect::string &appname)
+neglect::string platform::get_settings_folder(const neglect::string &appname, bool exposed)
 {
 #if NEGLECT_PLATFORM == NEGLECT_PLATFORM_WINDOWS
-    char path[MAX_PATH], *pathptr;
-    GetModuleFileNameA(0, path, MAX_PATH);
-    pathptr = strrchr(path, '\\');
+    char path[MAX_PATH];
     LPITEMIDLIST pidl;
-    SHGetFolderLocation(0, CSIDL_APPDATA, 0, 0, &pidl);
+    SHGetFolderLocation(0, exposed ? CSIDL_MYDOCUMENTS : CSIDL_LOCAL_APPDATA, 0, 0, &pidl);
     SHGetPathFromIDList(pidl, path);
     CoTaskMemFree(pidl);
     return neglect::string(path) + "\\" + appname;
 #else
 #   error "unsupported platform for platform::get_settings_folder()"
 #endif
+}
+
+const neglect::string &platform::get_executable()
+{
+    if (_executable_found)
+        return _executable;
+#if NEGLECT_PLATFORM == NEGLECT_PLATFORM_WINDOWS
+    char path[MAX_PATH];
+    GetModuleFileNameA(0, path, MAX_PATH);
+    _executable = neglect::string(path);
+#else
+#   error "unsupported platform for platform::get_executable()
+#endif
+    _executable_found = true;
+    return _executable;
+}
+
+const std::vector<neglect::string> &platform::get_arguments()
+{
+    if (_arguments_found)
+        return _arguments;
+
+#if NEGLECT_PLATFORM == NEGLECT_PLATFORM_WINDOWS
+    for (int i = 1; i < __argc; i++)
+        _arguments.push_back(__argv[i]);
+#else
+#   error "unsupported platform for platform::get_arguments()
+#endif
+    _arguments_found = true;
+    return _arguments;
 }

neglect/tests/CMakeLists.txt

     src/test_vectors.cpp
     src/test_format.cpp
     src/test_path.cpp
+    src/test_platform.cpp
 )
 set(HEADERS
 )

neglect/tests/src/test_path.cpp

     {
         neglect::string folder = "mkdir_test_folder";
         neglect::string folder2 = folder + "2";
-        NUT_CHECK(!path::exists(folder));
-        NUT_CHECK(path::mkdir(folder));
-        NUT_CHECK(path::is_dir(folder));
-        NUT_CHECK(path::rename(folder, folder2));
-        NUT_CHECK(!path::is_dir(folder));
-        NUT_CHECK(path::is_dir(folder2));
-        NUT_CHECK(path::remove(folder2));
-        NUT_CHECK(!path::is_dir(folder2));
+        NUT_REQUIRE(!path::exists(folder));
+        NUT_REQUIRE(path::mkdir(folder));
+        NUT_REQUIRE(path::is_dir(folder));
+        NUT_REQUIRE(path::rename(folder, folder2));
+        NUT_REQUIRE(!path::is_dir(folder));
+        NUT_REQUIRE(path::is_dir(folder2));
+        NUT_REQUIRE(path::remove(folder2));
+        NUT_REQUIRE(!path::is_dir(folder2));
     }
 
     NUT_TESTCASE(abspath)
     {
         neglect::string filename = "filetest_test_file.txt";
         neglect::string filename2 = filename + "2";
-        NUT_CHECK(!path::is_file(filename));
-        NUT_CHECK(!path::is_file(filename));
+        NUT_REQUIRE(!path::is_file(filename));
+        NUT_REQUIRE(!path::is_file(filename));
         {
             std::fstream test(filename.c_str(), std::fstream::out);
             test << "Hello World" << std::endl;
         }
-        NUT_CHECK(path::exists(filename));
-        NUT_CHECK(path::is_file(filename));
-        NUT_CHECK(path::rename(filename, filename2));
-        NUT_CHECK(!path::is_file(filename));
-        NUT_CHECK(path::is_file(filename2));
-        NUT_CHECK(path::remove(filename2));
-        NUT_CHECK(!path::is_file(filename2));
-        NUT_CHECK(!path::is_file(filename2));
+        NUT_REQUIRE(path::exists(filename));
+        NUT_REQUIRE(path::is_file(filename));
+        NUT_REQUIRE(path::rename(filename, filename2));
+        NUT_REQUIRE(!path::is_file(filename));
+        NUT_REQUIRE(path::is_file(filename2));
+        NUT_REQUIRE(path::remove(filename2));
+        NUT_REQUIRE(!path::is_file(filename2));
+        NUT_REQUIRE(!path::is_file(filename2));
     }
 }

neglect/tests/src/test_platform.cpp

+#include <neglect/unittest.hpp>
+#include <neglect/platform.hpp>
+#include <neglect/path.hpp>
+
+
+using namespace neglect;
+
+NUT_TESTGROUP(platform) {
+
+    NUT_TESTCASE(name)
+    {
+#if NEGLECT_PLATFORM == NEGLECT_PLATFORM_WINDOWS
+        NUT_CHECK_EQUAL(platform::name, "Windows");
+#else
+#error  "unsupported platform in platform::name test"
+#endif
+    }
+
+    NUT_TESTCASE(get_home_folder)
+    {
+        NUT_CHECK(path::is_dir(platform::get_home_folder()));
+    }
+
+    NUT_TESTCASE(get_settings_folder)
+    {
+        neglect::string dir = platform::get_settings_folder("Neglect Test Application", true);
+        NUT_REQUIRE(!path::exists(dir));
+        NUT_REQUIRE(path::mkdir(dir));
+        NUT_REQUIRE(path::remove(dir));
+        dir = platform::get_settings_folder("Neglect Test Application", false);
+        NUT_REQUIRE(!path::exists(dir));
+        NUT_REQUIRE(path::mkdir(dir));
+        NUT_REQUIRE(path::remove(dir));
+    }
+
+    NUT_TESTCASE(get_executable)
+    {
+        std::string executable = platform::get_executable();
+        NUT_REQUIRE(path::is_file(executable));
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.