Skip to content

Commit

Permalink
Get rid of multiple specifications of InvocationImpl
Browse files Browse the repository at this point in the history
next round of simplifications

introduce pass traits

Fix Windows compilation for ntsu::SocketOptionUtil, add functionality to mock methods with 4 arguments, mock new send for ntcd::StreamSocket

get rid of some MACROs

count all arguments and matchers from 0 instead of 1

Unify NTF_EXPECT macro

replace some macros with normal functions

AIX compilation fixed

Fix compilation on SunOS

Add TC28 to ntcr_streamsocket.t

Add TC29 and TC30 to ntcr_streamsocket.t

Simplify mocking framework

Add one more TC

Add more TCs

Add more TCs
  • Loading branch information
smtrfnv committed May 17, 2024
1 parent f42b8f0 commit 7d65778
Show file tree
Hide file tree
Showing 9 changed files with 2,201 additions and 642 deletions.
736 changes: 387 additions & 349 deletions groups/ntc/ntccfg/ntccfg_test.h

Large diffs are not rendered by default.

63 changes: 34 additions & 29 deletions groups/ntc/ntccfg/ntccfg_test.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ using namespace BloombergLP;
// [ 1]
//-----------------------------------------------------------------------------


#if NTCCFG_TEST_MOCK_ENABLED

namespace mock_test {
Expand Down Expand Up @@ -78,11 +77,11 @@ NTCCFG_TEST_CASE(1)
using namespace mock_test;

MyMock mock;
NTF_EXPECT_0(mock, f).ONCE();
NTF_EXPECT(mock, f).ONCE();
mock.f();

NTF_EXPECT_0(mock, f1).ONCE().RETURN(22);
NTF_EXPECT_0(mock, f1).ONCE().RETURN(33);
NTF_EXPECT(mock, f1).ONCE().RETURN(22);
NTF_EXPECT(mock, f1).ONCE().RETURN(33);

NTCCFG_TEST_EQ(mock.f1(), 22);
NTCCFG_TEST_EQ(mock.f1(), 33);
Expand All @@ -100,14 +99,14 @@ NTCCFG_TEST_CASE(2)

{
// it means we do not case what argument is used when f2 is called
NTF_EXPECT_1(mock, f2, IGNORE_ARG).ONCE();
NTF_EXPECT(mock, f2, IGNORE_ARG).ONCE();

const int val = 22;
mock.f2(val);

// here we expect that the argument used to call f2 equals `expected`
const int expected = 22;
NTF_EXPECT_1(mock, f2, NTF_EQ(expected)).ONCE();
NTF_EXPECT(mock, f2, TestMock::EQ(expected)).ONCE();
mock.f2(val);
}
{
Expand All @@ -116,17 +115,17 @@ NTCCFG_TEST_CASE(2)
int* expected_ptr = ptr;

// expect that argument used to call f3 equals `expected_ptr`
NTF_EXPECT_1(mock, f3, NTF_EQ(expected_ptr)).ONCE();
NTF_EXPECT(mock, f3, TestMock::EQ(expected_ptr)).ONCE();
mock.f3(ptr);

//expect that when argument used to call f3 is dereferenced it equals
//`expected value`
int expected_value = value;
NTF_EXPECT_1(mock, f3, NTF_EQ_DEREF(expected_value)).ONCE();
NTF_EXPECT(mock, f3, TestMock::EQ_DEREF(expected_value)).ONCE();
mock.f3(ptr);

int& ref = value;
NTF_EXPECT_1(mock, f4, NTF_EQ(value)).ONCE();
NTF_EXPECT(mock, f4, TestMock::EQ(value)).ONCE();
mock.f4(ref);
}

Expand All @@ -145,16 +144,18 @@ NTCCFG_TEST_CASE(3)
const int newValue = 55;
// when f3 is called, we do not case what arg value is, but we want to
// dereference it and set it value to `newValue`
NTF_EXPECT_1(mock, f3, IGNORE_ARG)
NTF_EXPECT(mock, f3, IGNORE_ARG)
.ONCE()
.SET_ARG_1(FROM_DEREF(newValue));
.SET_ARG_1(TestMock::FROM_DEREF(newValue));

int val = 0;
mock.f3(&val);
NTCCFG_TEST_EQ(val, newValue);

// the same can be done with references
NTF_EXPECT_1(mock, f4, IGNORE_ARG).ONCE().SET_ARG_1(FROM(newValue));
NTF_EXPECT(mock, f4, IGNORE_ARG)
.ONCE()
.SET_ARG_1(TestMock::FROM(newValue));

int data = 12;
int& data_ref = data;
Expand All @@ -176,7 +177,9 @@ NTCCFG_TEST_CASE(4)
{
// an argument can be saved to external variable to later used
int storage = 0;
NTF_EXPECT_1(mock, f2, IGNORE_ARG).ONCE().SAVE_ARG_1(TO(&storage));
NTF_EXPECT(mock, f2, IGNORE_ARG)
.ONCE()
.SAVE_ARG_1(TestMock::TO(&storage));

int val = 22;
mock.f2(val);
Expand All @@ -186,25 +189,27 @@ NTCCFG_TEST_CASE(4)
{
//the same can be done with raw pointers
int* ptr = 0;
NTF_EXPECT_1(mock, f3, IGNORE_ARG).ONCE().SAVE_ARG_1(TO(&ptr));
NTF_EXPECT(mock, f3, IGNORE_ARG).ONCE().SAVE_ARG_1(TestMock::TO(&ptr));

int val = 6;
mock.f3(&val);
NTCCFG_TEST_EQ(ptr, &val);

//pointer argument can be dereferenced before saving
int storage = 0;
NTF_EXPECT_1(mock, f3, IGNORE_ARG)
NTF_EXPECT(mock, f3, IGNORE_ARG)
.ONCE()
.SAVE_ARG_1(TO_DEREF(&storage));
.SAVE_ARG_1(TestMock::TO_DEREF(&storage));

mock.f3(&val);
NTCCFG_TEST_EQ(storage, val);
}
{
//the same can be done with references
int storage = 0;
NTF_EXPECT_1(mock, f4, IGNORE_ARG).ONCE().SAVE_ARG_1(TO(&storage));
NTF_EXPECT(mock, f4, IGNORE_ARG)
.ONCE()
.SAVE_ARG_1(TestMock::TO(&storage));

int val = 7;
mock.f4(val);
Expand All @@ -227,16 +232,16 @@ NTCCFG_TEST_CASE(5)
// `_SPEC` addition to NTF_EQ (or IGNORE_ARG_S) macro

char c = 'a';
NTF_EXPECT_2(mock, f5, IGNORE_ARG_S(int), NTF_EQ_SPEC(c, char)).ONCE();
NTF_EXPECT(mock, f5, IGNORE_ARG_S(int), NTF_EQ_SPEC(c, char)).ONCE();

mock.f5(22, c);

int val = 14;
double d = 3.14;
NTF_EXPECT_2(mock,
f5,
NTF_EQ_DEREF_SPEC(val, int*),
NTF_EQ_SPEC(d, double))
NTF_EXPECT(mock,
f5,
NTF_EQ_DEREF_SPEC(val, int*),
NTF_EQ_SPEC(d, double))
.ONCE();

mock.f5(&val, d);
Expand Down Expand Up @@ -264,14 +269,14 @@ NTCCFG_TEST_CASE(6)
long expectedLong = 100;
int* ptr = 0;
double newDouble = 8.8;
NTF_EXPECT_3(mock,
f6,
NTF_EQ_DEREF(expectedInt),
NTF_EQ(expectedDouble),
NTF_EQ(expectedLong))
NTF_EXPECT(mock,
f6,
TestMock::EQ_DEREF(expectedInt),
TestMock::EQ(expectedDouble),
TestMock::EQ(expectedLong))
.ONCE()
.SAVE_ARG_1(TO(&ptr))
.SET_ARG_2(FROM(newDouble))
.SAVE_ARG_1(TestMock::TO(&ptr))
.SET_ARG_2(TestMock::FROM(newDouble))
.RETURNREF(sptrRef);

const bsl::shared_ptr<int>& res =
Expand Down
8 changes: 8 additions & 0 deletions groups/ntc/ntcd/ntcd_datagramsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ ntsa::Error DatagramSocket::send(ntsa::SendContext* context,
return d_session_sp->send(context, data, options);
}

ntsa::Error DatagramSocket::send(ntsa::SendContext* context,
const ntsa::ConstBuffer* data,
bsl::size_t size,
const ntsa::SendOptions& options)
{
return ntsi::DatagramSocket::send(context, data, size, options);
}

ntsa::Error DatagramSocket::receive(ntsa::ReceiveContext* context,
bdlbb::Blob* data,
const ntsa::ReceiveOptions& options)
Expand Down
9 changes: 9 additions & 0 deletions groups/ntc/ntcd/ntcd_datagramsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ class DatagramSocket : public ntsi::DatagramSocket
const ntsa::Data& data,
const ntsa::SendOptions& options) BSLS_KEYWORD_OVERRIDE;

/// Enqueue the specified 'data' having the specified 'size' to the
/// socket send buffer according to the specified 'options'. Load into
/// the specified 'context' the result of the operation. Return the
/// error.
ntsa::Error send(ntsa::SendContext* context,
const ntsa::ConstBuffer* data,
bsl::size_t size,
const ntsa::SendOptions& options) BSLS_KEYWORD_OVERRIDE;

/// Dequeue from the socket receive buffer into the specified 'data'
/// according to the specified 'options'. Load into the specified
/// 'context' the result of the operation. Return the error.
Expand Down
11 changes: 11 additions & 0 deletions groups/ntc/ntcd/ntcd_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3070,6 +3070,17 @@ ntsa::Error Session::send(ntsa::SendContext* context,
return ntsa::Error();
}

ntsa::Error Session::send(ntsa::SendContext* context,
const ntsa::ConstBuffer* data,
bsl::size_t size,
const ntsa::SendOptions& options)
{
ntsa::ConstBufferArray array;
array.append(data, size);

return this->send(context, ntsa::Data(array), options);
}

ntsa::Error Session::receive(ntsa::ReceiveContext* context,
bdlbb::Blob* data,
const ntsa::ReceiveOptions& options)
Expand Down
9 changes: 9 additions & 0 deletions groups/ntc/ntcd/ntcd_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,15 @@ class Session : public ntsi::DatagramSocket,
const ntsa::Data& data,
const ntsa::SendOptions& options) BSLS_KEYWORD_OVERRIDE;

/// Enqueue the specified 'data' having the specified 'size' to the
/// socket send buffer according to the specified 'options'. Load into
/// the specified 'context' the result of the operation. Return the
/// error.
ntsa::Error send(ntsa::SendContext* context,
const ntsa::ConstBuffer* data,
bsl::size_t size,
const ntsa::SendOptions& options) BSLS_KEYWORD_OVERRIDE;

/// Dequeue from the socket receive buffer into the specified 'data'
/// according to the specified 'options'. Load into the specified
/// 'context' the result of the operation. Return the error.
Expand Down
8 changes: 8 additions & 0 deletions groups/ntc/ntcd/ntcd_streamsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ ntsa::Error StreamSocket::send(ntsa::SendContext* context,
return d_session_sp->send(context, data, options);
}

ntsa::Error StreamSocket::send(ntsa::SendContext* context,
const ntsa::ConstBuffer* data,
bsl::size_t size,
const ntsa::SendOptions& options)
{
return ntsi::StreamSocket::send(context, data, size, options);
}

ntsa::Error StreamSocket::receive(ntsa::ReceiveContext* context,
bdlbb::Blob* data,
const ntsa::ReceiveOptions& options)
Expand Down
17 changes: 16 additions & 1 deletion groups/ntc/ntcd/ntcd_streamsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class StreamSocket : public ntsi::StreamSocket
bslma::Allocator* d_allocator_p;

private:
StreamSocket(const StreamSocket&) BSLS_KEYWORD_DELETED;
StreamSocket(const StreamSocket&) BSLS_KEYWORD_DELETED;
StreamSocket& operator=(const StreamSocket&) BSLS_KEYWORD_DELETED;

public:
Expand Down Expand Up @@ -121,6 +121,15 @@ class StreamSocket : public ntsi::StreamSocket
const ntsa::Data& data,
const ntsa::SendOptions& options) BSLS_KEYWORD_OVERRIDE;

/// Enqueue the specified 'data' having the specified 'size' to the
/// socket send buffer according to the specified 'options'. Load into
/// the specified 'context' the result of the operation. Return the
/// error.
ntsa::Error send(ntsa::SendContext* context,
const ntsa::ConstBuffer* data,
bsl::size_t size,
const ntsa::SendOptions& options) BSLS_KEYWORD_OVERRIDE;

/// Dequeue from the socket receive buffer into the specified 'data'
/// according to the specified 'options'. Load into the specified
/// 'context' the result of the operation. Return the error.
Expand Down Expand Up @@ -272,6 +281,12 @@ NTF_MOCK_METHOD(ntsa::Error,
ntsa::SendContext*,
const ntsa::Data&,
const ntsa::SendOptions&)
NTF_MOCK_METHOD(ntsa::Error,
send,
ntsa::SendContext*,
const ntsa::ConstBuffer*,
bsl::size_t,
const ntsa::SendOptions&)

NTF_MOCK_METHOD(ntsa::Error,
receive,
Expand Down
Loading

0 comments on commit 7d65778

Please sign in to comment.