-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a mock initiator implementation that allows tests to specify a list of expected transactions. Change-Id: Ifb5c1bedd9468202f11a380766e1ec4091055dae Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/92147 Reviewed-by: Mark Slevinsky <markslevinsky@google.com> Commit-Queue: Austin Foxley <afoxley@google.com>
- Loading branch information
Showing
7 changed files
with
401 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2022 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. | ||
|
||
#include "pw_spi/initiator_mock.h" | ||
|
||
#include "gtest/gtest.h" | ||
#include "pw_assert/check.h" | ||
|
||
namespace pw::spi { | ||
|
||
Status MockInitiator::WriteRead(ConstByteSpan tx_buffer, ByteSpan rx_buffer) { | ||
PW_CHECK_INT_LT(expected_transaction_index_, expected_transactions_.size()); | ||
|
||
ConstByteSpan expected_tx_buffer = | ||
expected_transactions_[expected_transaction_index_].write_buffer(); | ||
EXPECT_TRUE(std::equal(expected_tx_buffer.begin(), | ||
expected_tx_buffer.end(), | ||
tx_buffer.begin(), | ||
tx_buffer.end())); | ||
|
||
ConstByteSpan expected_rx_buffer = | ||
expected_transactions_[expected_transaction_index_].read_buffer(); | ||
PW_CHECK_INT_EQ(expected_rx_buffer.size(), rx_buffer.size()); | ||
|
||
std::copy( | ||
expected_rx_buffer.begin(), expected_rx_buffer.end(), rx_buffer.begin()); | ||
|
||
// Do not directly return this value as expected_transaction_index_ should be | ||
// incremented. | ||
const Status expected_return_value = | ||
expected_transactions_[expected_transaction_index_].return_value(); | ||
|
||
expected_transaction_index_ += 1; | ||
|
||
return expected_return_value; | ||
} | ||
|
||
MockInitiator::~MockInitiator() { EXPECT_EQ(Finalize(), OkStatus()); } | ||
|
||
} // namespace pw::spi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2022 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. | ||
|
||
#include "pw_spi/initiator_mock.h" | ||
|
||
#include <array> | ||
#include <span> | ||
|
||
#include "gtest/gtest.h" | ||
#include "pw_bytes/array.h" | ||
#include "pw_bytes/span.h" | ||
|
||
namespace pw::spi { | ||
namespace { | ||
|
||
TEST(Transaction, Read) { | ||
constexpr auto kExpectRead1 = bytes::Array<1, 2, 3, 4, 5>(); | ||
constexpr auto kExpectRead2 = bytes::Array<3, 4, 5>(); | ||
|
||
auto expected_transactions = MakeExpectedTransactionArray( | ||
{MockReadTransaction(OkStatus(), kExpectRead1), | ||
MockReadTransaction(OkStatus(), kExpectRead2)}); | ||
|
||
MockInitiator mocked_spi(expected_transactions); | ||
|
||
std::array<std::byte, kExpectRead1.size()> read1; | ||
EXPECT_EQ(mocked_spi.WriteRead(ConstByteSpan(), read1), OkStatus()); | ||
EXPECT_TRUE(std::equal( | ||
read1.begin(), read1.end(), kExpectRead1.begin(), kExpectRead1.end())); | ||
|
||
std::array<std::byte, kExpectRead2.size()> read2; | ||
EXPECT_EQ(mocked_spi.WriteRead(ConstByteSpan(), read2), OkStatus()); | ||
EXPECT_TRUE(std::equal( | ||
read2.begin(), read2.end(), kExpectRead2.begin(), kExpectRead2.end())); | ||
|
||
EXPECT_EQ(mocked_spi.Finalize(), OkStatus()); | ||
} | ||
|
||
TEST(Transaction, Write) { | ||
constexpr auto kExpectWrite1 = bytes::Array<1, 2, 3, 4, 5>(); | ||
constexpr auto kExpectWrite2 = bytes::Array<3, 4, 5>(); | ||
|
||
auto expected_transactions = MakeExpectedTransactionArray( | ||
{MockWriteTransaction(OkStatus(), kExpectWrite1), | ||
MockWriteTransaction(OkStatus(), kExpectWrite2)}); | ||
|
||
MockInitiator mocked_spi(expected_transactions); | ||
|
||
EXPECT_EQ(mocked_spi.WriteRead(kExpectWrite1, ByteSpan()), OkStatus()); | ||
|
||
EXPECT_EQ(mocked_spi.WriteRead(kExpectWrite2, ByteSpan()), OkStatus()); | ||
|
||
EXPECT_EQ(mocked_spi.Finalize(), OkStatus()); | ||
} | ||
|
||
TEST(Transaction, WriteRead) { | ||
constexpr auto kExpectWrite1 = bytes::Array<1, 2, 3, 4, 5>(); | ||
constexpr auto kExpectRead1 = bytes::Array<1, 2>(); | ||
|
||
constexpr auto kExpectWrite2 = bytes::Array<3, 4, 5>(); | ||
constexpr auto kExpectRead2 = bytes::Array<3, 4>(); | ||
|
||
auto expected_transactions = MakeExpectedTransactionArray({ | ||
MockTransaction(OkStatus(), kExpectWrite1, kExpectRead1), | ||
MockTransaction(OkStatus(), kExpectWrite2, kExpectRead2), | ||
}); | ||
|
||
MockInitiator mocked_spi(expected_transactions); | ||
|
||
std::array<std::byte, kExpectRead1.size()> read1; | ||
EXPECT_EQ(mocked_spi.WriteRead(kExpectWrite1, read1), OkStatus()); | ||
EXPECT_TRUE(std::equal( | ||
read1.begin(), read1.end(), kExpectRead1.begin(), kExpectRead1.end())); | ||
|
||
std::array<std::byte, kExpectRead2.size()> read2; | ||
EXPECT_EQ(mocked_spi.WriteRead(kExpectWrite2, read2), OkStatus()); | ||
EXPECT_TRUE(std::equal( | ||
read2.begin(), read2.end(), kExpectRead2.begin(), kExpectRead2.end())); | ||
|
||
EXPECT_EQ(mocked_spi.Finalize(), OkStatus()); | ||
} | ||
|
||
} // namespace | ||
} // namespace pw::spi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2022 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 <cstdint> | ||
|
||
#include "pw_spi/chip_selector.h" | ||
#include "pw_status/status.h" | ||
|
||
namespace pw::spi { | ||
|
||
class MockChipSelector : public ChipSelector { | ||
public: | ||
bool is_active() { return active_; } | ||
|
||
private: | ||
Status SetActive(bool active) override { | ||
active_ = active; | ||
return pw::OkStatus(); | ||
} | ||
bool active_ = false; | ||
}; | ||
|
||
} // namespace pw::spi |
Oops, something went wrong.