فهرست منبع

Overwrite the mainBundle for use in unit tests.

Sam Jaffe 6 سال پیش
والد
کامیت
0908c0d98e
2فایلهای تغییر یافته به همراه23 افزوده شده و 3 حذف شده
  1. 9 0
      graphics/test/manager_test.cxx
  2. 14 3
      util/src/osx_env.mm

+ 9 - 0
graphics/test/manager_test.cxx

@@ -15,6 +15,12 @@
 #include "game/graphics/shader_program.hpp"
 #include "game/graphics/texture.hpp"
 
+#ifdef __APPLE__
+namespace env { namespace detail {
+  extern void bundle(std::string const &);
+}}
+#endif
+
 using testing::AllOf;
 using testing::AnyNumber;
 using testing::Eq;
@@ -78,6 +84,9 @@ private:
 };
 
 void ManagerTest::SetUp() {
+#ifdef __APPLE__
+  env::detail::bundle("leumasjaffe.graphics-test");
+#endif
   using testing::Invoke;
   using testing::WithArg;
   // Start with a new set of IDs

+ 14 - 3
util/src/osx_env.mm

@@ -20,6 +20,17 @@ namespace {
 }
 
 namespace env {
+  namespace detail {
+    NSString * bundle_name = nil;
+    void bundle(std::string const & str) {
+      bundle_name = str.empty() ? nil : translate(str);
+    }
+    NSBundle * bundle() {
+      return bundle_name == nil ? [NSBundle mainBundle] :
+          [NSBundle bundleWithIdentifier:bundle_name];
+    }
+  }
+  
   std::string resource_file(std::string const& path) {
     size_t dir_idx = path.find_last_of("/");
     size_t ext_idx = path.find_first_of(".");
@@ -27,9 +38,9 @@ namespace env {
     std::string name = path.substr(dir_idx + 1, ext_idx - (dir_idx + 1));
     std::string type = path.substr(ext_idx + 1);
     
-    NSString* url = [[NSBundle mainBundle] pathForResource:translate(name)
-                                                    ofType:translate(type)
-                                               inDirectory:translate(base)];
+    NSString* url = [detail::bundle() pathForResource:translate(name)
+                                               ofType:translate(type)
+                                          inDirectory:translate(base)];
     
     char const* abs_path = [url cStringUsingEncoding:encoding];
     return abs_path ? std::string(abs_path) : std::string();