From fb88887d6a4d4da58a7208c5388267d89ca6db22 Mon Sep 17 00:00:00 2001 From: Kevin Zeng Date: Tue, 8 Jun 2021 10:39:06 -0700 Subject: [PATCH] pw_analog: Add gmocks for interfaces Change-Id: I43c5a555ff6ee8983ab4f78bf9b3dfb00f06df37 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/48321 Commit-Queue: Kevin Zeng Reviewed-by: Wyatt Hepler --- pw_analog/BUILD | 24 ++++++++++++++ pw_analog/BUILD.gn | 29 ++++++++++++++-- pw_analog/docs.rst | 8 +++++ .../public/pw_analog/analog_input_gmock.h | 31 +++++++++++++++++ .../public/pw_analog/microvolt_input_gmock.h | 33 +++++++++++++++++++ 5 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 pw_analog/public/pw_analog/analog_input_gmock.h create mode 100644 pw_analog/public/pw_analog/microvolt_input_gmock.h diff --git a/pw_analog/BUILD b/pw_analog/BUILD index d60252107..37fc74117 100644 --- a/pw_analog/BUILD +++ b/pw_analog/BUILD @@ -48,6 +48,30 @@ pw_cc_library( ], ) +pw_cc_library( + name = "microvolt_input_gmock", + hdrs = [ + "public/pw_analog/microvolt_input_gmock.h", + ], + includes = ["public"], + deps = [ + ":microvolt_input", + "$dir_pw_third_party/googletest", + ], +) + +pw_cc_library( + name = "analog_input_gmock", + hdrs = [ + "public/pw_analog/analog_input_gmock.h", + ], + includes = ["public"], + deps = [ + ":analog_input", + "$dir_pw_third_party/googletest", + ], +) + pw_cc_test( name = "analog_input_test", srcs = [ diff --git a/pw_analog/BUILD.gn b/pw_analog/BUILD.gn index 360fc935a..dd2761acc 100644 --- a/pw_analog/BUILD.gn +++ b/pw_analog/BUILD.gn @@ -23,7 +23,14 @@ config("public_include_path") { include_dirs = [ "public" ] } -pw_source_set("pw_analog") { +group("pw_analog") { + public_deps = [ + ":analog_input", + ":microvolt_input", + ] +} + +pw_source_set("analog_input") { public_configs = [ ":public_include_path" ] public_deps = [ "$dir_pw_chrono:system_clock", @@ -35,7 +42,7 @@ pw_source_set("pw_analog") { pw_source_set("microvolt_input") { public_configs = [ ":public_include_path" ] public_deps = [ - ":pw_analog", + ":analog_input", "$dir_pw_chrono:system_clock", "$dir_pw_result", "$dir_pw_status", @@ -43,6 +50,24 @@ pw_source_set("microvolt_input") { public = [ "public/pw_analog/microvolt_input.h" ] } +pw_source_set("analog_input_gmock") { + public_configs = [ ":public_include_path" ] + public_deps = [ + ":analog_input", + "$dir_pw_third_party/googletest", + ] + public = [ "public/pw_analog/analog_input_gmock.h" ] +} + +pw_source_set("microvolt_input_gmock") { + public_configs = [ ":public_include_path" ] + public_deps = [ + ":microvolt_input", + "$dir_pw_third_party/googletest", + ] + public = [ "public/pw_analog/microvolt_input_gmock.h" ] +} + pw_test_group("tests") { tests = [ ":analog_input_test", diff --git a/pw_analog/docs.rst b/pw_analog/docs.rst index 5176465b5..19285e09f 100644 --- a/pw_analog/docs.rst +++ b/pw_analog/docs.rst @@ -28,3 +28,11 @@ own ADC driver implementation in order to configure and enable the ADC peripheral in order to provide the reference voltages and to configure and enable the ADC peripheral where needed. Users are responsible for managing multithreaded access to the ADC driver if the ADC services multiple channels. + +pw::analog::GmockAnalogInput +------------------------------- +gMock of AnalogInput used for testing and mocking out the AnalogInput. + +pw::analog::GmockMicrovoltInput +------------------------------- +gMock of MicrovoltInput used for testing and mocking out the MicrovoltInput. diff --git a/pw_analog/public/pw_analog/analog_input_gmock.h b/pw_analog/public/pw_analog/analog_input_gmock.h new file mode 100644 index 000000000..f328c93c1 --- /dev/null +++ b/pw_analog/public/pw_analog/analog_input_gmock.h @@ -0,0 +1,31 @@ +// Copyright 2021 The Pigweed 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 +// +// https://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. +#pragma once + +#include "gmock/gmock.h" +#include "pw_analog/analog_input.h" + +namespace pw::analog { + +class GmockAnalogInput : public AnalogInput { + public: + MOCK_METHOD(pw::Result, + TryReadUntil, + (pw::chrono::SystemClock::time_point deadline), + (override)); + + MOCK_METHOD(Limits, GetLimits, (), (const, override)); +}; + +} // namespace pw::analog diff --git a/pw_analog/public/pw_analog/microvolt_input_gmock.h b/pw_analog/public/pw_analog/microvolt_input_gmock.h new file mode 100644 index 000000000..a6ce9227e --- /dev/null +++ b/pw_analog/public/pw_analog/microvolt_input_gmock.h @@ -0,0 +1,33 @@ +// Copyright 2021 The Pigweed 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 +// +// https://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. +#pragma once + +#include "gmock/gmock.h" +#include "pw_analog/microvolt_input.h" + +namespace pw::analog { + +class GmockMicrovoltInput : public MicrovoltInput { + public: + MOCK_METHOD(pw::Result, + TryReadUntil, + (pw::chrono::SystemClock::time_point deadline), + (override)); + + MOCK_METHOD(Limits, GetLimits, (), (const, override)); + + MOCK_METHOD(References, GetReferences, (), (const, override)); +}; + +} // namespace pw::analog