1 // Copyright 2011 Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // * Neither the name of Google Inc. nor the names of its contributors 14 // may be used to endorse or promote products derived from this software 15 // without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 #include "engine/action.hpp" 30 31 #include <map> 32 #include <sstream> 33 #include <string> 34 35 #include <atf-c++.hpp> 36 37 #include "engine/context.hpp" 38 #include "utils/fs/path.hpp" 39 40 namespace fs = utils::fs; 41 42 43 namespace { 44 45 46 /// Generates a context with fake data for testing purposes only. 47 /// 48 /// \param cwd The work directory held in the context. This contains an 49 /// irrelevant default value if not provided. 50 /// 51 /// \return The fake context. 52 static engine::context 53 fake_context(const char* cwd = "/foo/bar") 54 { 55 std::map< std::string, std::string > env; 56 env["foo"] = "bar"; 57 return engine::context(fs::path(cwd), env); 58 } 59 60 61 } // anonymous namespace 62 63 64 ATF_TEST_CASE_WITHOUT_HEAD(ctor_and_getters); 65 ATF_TEST_CASE_BODY(ctor_and_getters) 66 { 67 const engine::context context = fake_context(); 68 const engine::action action(context); 69 ATF_REQUIRE(context == action.runtime_context()); 70 } 71 72 73 ATF_TEST_CASE_WITHOUT_HEAD(operators_eq_and_ne); 74 ATF_TEST_CASE_BODY(operators_eq_and_ne) 75 { 76 const engine::action action1(fake_context("foo/bar")); 77 const engine::action action2(fake_context("foo/bar")); 78 const engine::action action3(fake_context("foo/baz")); 79 ATF_REQUIRE( action1 == action2); 80 ATF_REQUIRE(!(action1 != action2)); 81 ATF_REQUIRE(!(action1 == action3)); 82 ATF_REQUIRE( action1 != action3); 83 } 84 85 86 ATF_TEST_CASE_WITHOUT_HEAD(output); 87 ATF_TEST_CASE_BODY(output) 88 { 89 const engine::context context = fake_context(); 90 const engine::action action(context); 91 92 std::ostringstream str; 93 str << action; 94 ATF_REQUIRE_EQ("action{context=context{cwd='/foo/bar', env=[foo='bar']}}", 95 str.str()); 96 } 97 98 99 ATF_INIT_TEST_CASES(tcs) 100 { 101 ATF_ADD_TEST_CASE(tcs, ctor_and_getters); 102 ATF_ADD_TEST_CASE(tcs, operators_eq_and_ne); 103 ATF_ADD_TEST_CASE(tcs, output); 104 } 105