blob: f03bccc2380bc281e0aa6ec734979350b96ab111 [file] [log] [blame]
// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SHILL_TESTING_H_
#define SHILL_TESTING_H_
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace shill {
// A Google Mock action (similar to testing::ReturnPointee) that takes a pointer
// to a scoped_ptr object, releases and returns the raw pointer managed by the
// scoped_ptr object when the action is invoked.
//
// Example usage:
//
// TEST(FactoryTest, CreateStuff) {
// MockFactory factory;
// scoped_ptr<Stuff> stuff(new Stuff());
// EXPECT_CALL(factory, CreateStuff())
// .WillOnce(ReturnAndReleasePointee(&stuff));
// }
//
// If |factory.CreateStuff()| is called, the ownership of the Stuff object
// managed by |stuff| is transferred to the caller of |factory.CreateStuff()|.
// Otherwise, the Stuff object will be destroyed once |stuff| goes out of
// scope when the test completes.
ACTION_P(ReturnAndReleasePointee, scoped_pointer) {
return scoped_pointer->release();
}
MATCHER(IsSuccess, "") {
return arg.IsSuccess();
}
MATCHER(IsFailure, "") {
return arg.IsFailure();
}
MATCHER_P2(ErrorIs, error_type, error_message, "") {
return error_type == arg.type() && error_message == arg.message();
}
MATCHER(IsNullRefPtr, "") {
return !arg.get();
}
MATCHER(NotNullRefPtr, "") {
return arg.get();
}
// Use this matcher instead of passing RefPtrs directly into the arguments
// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
// system teardown.
MATCHER_P(IsRefPtrTo, ref_address, "") {
return arg.get() == ref_address;
}
} // namespace shill
#endif // SHILL_TESTING_H_