Skip to content

Commit 3cc745f

Browse files
committed
Prevent singleton definition when fake objects are used
This change allows to implement and use fake objects during unit testing Classes to be mocked will be extended by fake classes. Each fake class will define a fake singleton instance
1 parent af9e096 commit 3cc745f

File tree

8 files changed

+158
-0
lines changed

8 files changed

+158
-0
lines changed

extras/test/CMakeLists.txt

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
##########################################################################
2+
3+
set(CMAKE_VERBOSE_MAKEFILE ON)
4+
cmake_minimum_required(VERSION 2.8)
5+
6+
##########################################################################
7+
8+
project(testArduinoBLE)
9+
10+
##########################################################################
11+
12+
include_directories(include)
13+
include_directories(include/util)
14+
include_directories(../../src)
15+
include_directories(../../src/local)
16+
include_directories(../../src/remote)
17+
include_directories(../../src/utility)
18+
include_directories(external/catch/v2.12.1/include)
19+
include_directories(external/fakeit/v2.0.5/include)
20+
21+
##########################################################################
22+
23+
set(CMAKE_CXX_STANDARD 11)
24+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
25+
26+
##########################################################################
27+
28+
set(TEST_TARGET_UUID_SRCS
29+
src/Arduino.cpp
30+
src/util/itoa.c
31+
src/util/TestUtil.cpp
32+
src/test_main.cpp
33+
src/test_uuid/test_uuid.cpp
34+
../../src/utility/BLEUuid.cpp
35+
src/util/String.cpp
36+
src/util/Common.cpp
37+
)
38+
39+
set(TEST_TARGET_DISC_DEVICE_SRCS
40+
src/Arduino.cpp
41+
src/util/itoa.c
42+
src/util/TestUtil.cpp
43+
src/util/String.cpp
44+
src/util/Common.cpp
45+
src/test_main.cpp
46+
src/test_discovered_device/test_discovered_device.cpp
47+
../../src/utility/BLEUuid.cpp
48+
../../src/BLEDevice.cpp
49+
../../src/BLECharacteristic.cpp
50+
../../src/BLEDescriptor.cpp
51+
../../src/BLEService.cpp
52+
../../src/BLEAdvertisingData.cpp
53+
../../src/utility/ATT.cpp
54+
#../../src/utility/GAP.cpp
55+
../../src/utility/HCI.cpp
56+
../../src/utility/GATT.cpp
57+
../../src/utility/L2CAPSignaling.cpp
58+
../../src/local/BLELocalAttribute.cpp
59+
../../src/local/BLELocalCharacteristic.cpp
60+
../../src/local/BLELocalDescriptor.cpp
61+
../../src/local/BLELocalDevice.cpp
62+
../../src/local/BLELocalService.cpp
63+
../../src/remote/BLERemoteAttribute.cpp
64+
../../src/remote/BLERemoteAdvertisingData.cpp
65+
../../src/remote/BLERemoteCharacteristic.cpp
66+
../../src/remote/BLERemoteDescriptor.cpp
67+
../../src/remote/BLERemoteDevice.cpp
68+
../../src/remote/BLERemoteService.cpp
69+
../../src/BLEStringCharacteristic.cpp
70+
../../src/BLETypedCharacteristics.cpp
71+
# Fake classes
72+
src/util/HCIFakeTransport.cpp
73+
src/test_discovered_device/FakeGAP.cpp
74+
)
75+
76+
##########################################################################
77+
78+
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "--coverage")
79+
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--coverage")
80+
81+
##########################################################################
82+
83+
add_executable(TEST_TARGET_UUID ${TEST_TARGET_UUID_SRCS})
84+
add_executable(TEST_TARGET_DISC_DEVICE ${TEST_TARGET_DISC_DEVICE_SRCS})
85+
86+
##########################################################################
87+
88+
# Build unit tests as a post build step
89+
add_custom_command(TARGET TEST_TARGET_UUID POST_BUILD
90+
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_UUID
91+
)
92+
add_custom_command(TARGET TEST_TARGET_DISC_DEVICE POST_BUILD
93+
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_DISC_DEVICE
94+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <catch.hpp>
21+
22+
#define private public
23+
#define protected public
24+
#include "BLEDevice.h"
25+
26+
SCENARIO("BLE discovered device test", "[ArduinoBLE::BLEDevice]")
27+
{
28+
29+
WHEN("Retrieve local name from advertisement packet")
30+
{
31+
// Mocking advertisement packet
32+
uint8_t advType = 0x03;
33+
uint8_t eirLength = 6;
34+
uint8_t eirData[] = {0x05, 0x09, 't', 'e', 's', 't'};
35+
uint8_t rssi = 0;
36+
37+
// Expected results
38+
String goldenName = "test";
39+
40+
// Simulate device discovery
41+
BLEDevice device = BLEDevice();
42+
device.setAdvertisingData(eirLength, eirData, rssi);
43+
44+
bool hasName = device.hasLocalName();
45+
REQUIRE(hasName);
46+
47+
String name = device.localName();
48+
REQUIRE(goldenName == name);
49+
50+
}
51+
52+
}

src/local/BLELocalDevice.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -343,5 +343,7 @@ void BLELocalDevice::noDebug()
343343
HCI.noDebug();
344344
}
345345

346+
#if !defined(FAKE_BLELOCALDEVICE)
346347
BLELocalDevice BLEObj;
347348
BLELocalDevice& BLE = BLEObj;
349+
#endif

src/utility/ATT.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1687,5 +1687,7 @@ void ATTClass::writeCmd(uint16_t connectionHandle, uint16_t handle, const uint8_
16871687
sendReq(connectionHandle, &writeReq, 3 + dataLen, NULL);
16881688
}
16891689

1690+
#if !defined(FAKE_ATT)
16901691
ATTClass ATTObj;
16911692
ATTClass& ATT = ATTObj;
1693+
#endif

src/utility/GAP.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,7 @@ bool GAPClass::matchesScanFilter(const BLEDevice& device)
258258
return true;
259259
}
260260

261+
#if !defined(FAKE_GAP)
261262
GAPClass GAPObj;
262263
GAPClass& GAP = GAPObj;
264+
#endif

src/utility/GATT.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,7 @@ void GATTClass::clearAttributes()
172172
_attributes.clear();
173173
}
174174

175+
#if !defined(FAKE_GATT)
175176
GATTClass GATTObj;
176177
GATTClass& GATT = GATTObj;
178+
#endif

src/utility/HCI.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -686,5 +686,7 @@ void HCIClass::dumpPkt(const char* prefix, uint8_t plen, uint8_t pdata[])
686686
}
687687
}
688688

689+
#if !defined(FAKE_HCI)
689690
HCIClass HCIObj;
690691
HCIClass& HCI = HCIObj;
692+
#endif

src/utility/L2CAPSignaling.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,7 @@ void L2CAPSignalingClass::connectionParameterUpdateResponse(uint16_t /*handle*/,
146146
{
147147
}
148148

149+
#if !defined(FAKE_L2CAP)
149150
L2CAPSignalingClass L2CAPSignalingObj;
150151
L2CAPSignalingClass& L2CAPSignaling = L2CAPSignalingObj;
152+
#endif

0 commit comments

Comments
 (0)