Skip to content

Commit

Permalink
Integrated gmock
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed Jun 14, 2024
1 parent a12a0c0 commit e2008e2
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,7 @@
url = https://github.com/Infineon/optiga-trust-m.git
branch = matter_support
platforms = infineon
[submodule "third_party/googletest/repo"]
path = third_party/googletest/repo
url = https://github.com/google/googletest.git
branch = v1.14.x
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("//build_overrides/googletest.gni")
import("//build_overrides/mbedtls.gni")
import("//build_overrides/nlassert.gni")
import("//build_overrides/nlunit_test.gni")
Expand Down
18 changes: 18 additions & 0 deletions build_overrides/googletest.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2024 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

declare_args() {
# Root directory for googletest.
googletest_root = "//third_party/googletest"
}
18 changes: 18 additions & 0 deletions examples/build_overrides/googletest.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2024 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

declare_args() {
# Root directory for nlassert.
googletest_root = "//third_party/googletest"
}
3 changes: 3 additions & 0 deletions src/lib/support/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("//build_overrides/googletest.gni")
import("//build_overrides/nlunit_test.gni")
import("//build_overrides/pigweed.gni")

Expand Down Expand Up @@ -44,6 +45,7 @@ chip_test_suite("tests") {
"TestErrorStr.cpp",
"TestFixedBufferAllocator.cpp",
"TestFold.cpp",
"TestGmockUsage.cpp",
"TestIniEscaping.cpp",
"TestIntrusiveList.cpp",
"TestJsonToTlv.cpp",
Expand Down Expand Up @@ -93,5 +95,6 @@ chip_test_suite("tests") {
"${chip_root}/src/lib/support:testing",
"${chip_root}/src/lib/support/jsontlv",
"${chip_root}/src/platform",
"${googletest_root}:googlemock",
]
}
40 changes: 40 additions & 0 deletions src/lib/support/tests/TestGmockUsage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

using ::testing::Return;

// Define the interface
class Foo
{
public:
virtual ~Foo() = default;
virtual int DoSomething(int a) = 0;
};

// Create the mock class
class MockFoo : public Foo
{
public:
MOCK_METHOD(int, DoSomething, (int a), (override));
};

// Test function that uses Foo
int UseFoo(Foo * foo, int value)
{
return foo->DoSomething(value);
}

// Test case
TEST(GMockTest, BasicValidation)
{
MockFoo mock_foo;

// Set up the expectation
EXPECT_CALL(mock_foo, DoSomething(5)).WillOnce(Return(10));

// Call the function with the mock object
int result = UseFoo(&mock_foo, 5);

// Validate the result
EXPECT_EQ(result, 11);
}
78 changes: 78 additions & 0 deletions third_party/googletest/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright (c) 2024 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/build.gni")
import("${build_root}/config/compiler/compiler.gni")

config("googletest_config") {
include_dirs = [
"repo/googletest/include",
"repo/googletest",
]
}

static_library("googletest") {
output_name = "libgoogletest"

include_dirs = [
"repo/googletest/include",
"repo/googletest",
]

sources = [
"repo/googletest/src/gtest-all.cc",
"repo/googletest/src/gtest-assertion-result.cc",
"repo/googletest/src/gtest-death-test.cc",
"repo/googletest/src/gtest-filepath.cc",
"repo/googletest/src/gtest-internal-inl.h",
"repo/googletest/src/gtest-matchers.cc",
"repo/googletest/src/gtest-port.cc",
"repo/googletest/src/gtest-printers.cc",
"repo/googletest/src/gtest-test-part.cc",
"repo/googletest/src/gtest-typed-test.cc",
"repo/googletest/src/gtest.cc",
"repo/googletest/src/gtest_main.cc",
]

public_configs = [ ":googletest_config" ]
}

config("googlemock_config") {
include_dirs = [ "repo/googlemock/include" ]
}

static_library("googlemock") {
output_name = "libgooglemock"

include_dirs = [
"repo/googlemock/include",
"repo/googlemock",
]

sources = [
"repo/googlemock/src/gmock-all.cc",
"repo/googlemock/src/gmock-cardinalities.cc",
"repo/googlemock/src/gmock-internal-utils.cc",
"repo/googlemock/src/gmock-matchers.cc",
"repo/googlemock/src/gmock-spec-builders.cc",
"repo/googlemock/src/gmock.cc",
"repo/googlemock/src/gmock_main.cc",
]

deps = [ ":googletest" ]
public_configs = [
":googlemock_config",
":googletest_config",
]
}
1 change: 1 addition & 0 deletions third_party/googletest/repo
Submodule repo added at f8d7d7

0 comments on commit e2008e2

Please sign in to comment.