Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jul 9, 2017
1 parent 1f03cf0 commit 3b304d0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
21 changes: 21 additions & 0 deletions tests/src/manipulator_conditions/json/frontmost_application.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"type": "frontmost_application_if",
"bundle_identifiers": [
"^com\\.apple\\.Terminal$",
"^com\\.googlecode\\.iterm2$"
],
"file_paths": [
"/Terminal\\.app/"
]
},
{
"type": "unknown"
},
{
"type": "frontmost_application_unless",
"file_paths": [
"^/Users/tekezo/Applications/iTerm\\.app"
]
}
]
47 changes: 46 additions & 1 deletion tests/src/manipulator_conditions/test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define CATCH_CONFIG_RUNNER
#include "../../vendor/catch/catch.hpp"

#include "manipulator/manipulator_factory.hpp"
#include "manipulator/condition_manager.hpp"
#include "thread_utility.hpp"
#include <boost/optional/optional_io.hpp>

Expand Down Expand Up @@ -40,6 +40,51 @@ TEST_CASE("manipulator.manipulator_factory") {
}
}

namespace {
class actual_examples_helper final {
public:
actual_examples_helper(const std::string& file_name) {
std::ifstream input(std::string("json/") + file_name);
auto json = nlohmann::json::parse(input);
for (const auto& j : json) {
condition_manager_.push_back_condition(j);
}
}

const krbn::manipulator::condition_manager& get_condition_manager(void) const {
return condition_manager_;
}

private:
krbn::manipulator::condition_manager condition_manager_;
};
} // namespace

TEST_CASE("manipulator.frontmost_application") {
actual_examples_helper helper("frontmost_application.json");
krbn::manipulator_environment manipulator_environment;

// bundle_identifiers matching
manipulator_environment.get_frontmost_application().set_bundle_identifier("com.apple.Terminal");
manipulator_environment.get_frontmost_application().set_file_path("/not_found");
REQUIRE(helper.get_condition_manager().is_fulfilled(manipulator_environment) == true);

// Test regex escape works properly
manipulator_environment.get_frontmost_application().set_bundle_identifier("com/apple/Terminal");
REQUIRE(helper.get_condition_manager().is_fulfilled(manipulator_environment) == false);

// file_path matching
manipulator_environment.get_frontmost_application().set_file_path("/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal");
REQUIRE(helper.get_condition_manager().is_fulfilled(manipulator_environment) == true);

// frontmost_application_not
manipulator_environment.get_frontmost_application().set_bundle_identifier("com.googlecode.iterm2");
manipulator_environment.get_frontmost_application().set_bundle_identifier("/Applications/iTerm.app");
REQUIRE(helper.get_condition_manager().is_fulfilled(manipulator_environment) == true);
manipulator_environment.get_frontmost_application().set_bundle_identifier("/Users/tekezo/Applications/iTerm.app");
REQUIRE(helper.get_condition_manager().is_fulfilled(manipulator_environment) == true);
}

int main(int argc, char* const argv[]) {
krbn::thread_utility::register_main_thread();
return Catch::Session().run(argc, argv);
Expand Down

0 comments on commit 3b304d0

Please sign in to comment.