<commit_before>#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include <Arduino.h>
#include <Wire.h>
#include <functional>

#define MOCK_GLOBAL_METHOD(RETURN_TYPE, FUNCTION_NAME) \
template< typename... Args>\
RETURN_TYPE FUNCTION_NAME (Args... args)\
{\
  return mockPointer->FUNCTION_NAME(args...);\
}\


#define REAL_MOCK_IMPLEMENTATION(MockName)\
struct MockName##_Implementation\

REAL_MOCK_IMPLEMENTATION(Serial)
{
  MOCK_METHOD0(foo, int() );
  MOCK_METHOD1(foo, int(int) );
  MOCK_METHOD2(foo, int(int, int) );
  MOCK_METHOD0(boo, int() );
  MOCK_METHOD1(boo, int(int) );
  MOCK_METHOD2(boo, int(int, int) );
};


#define GLOBAL_MOCK_IMPLEMENTATION(MockName)\
struct MockName##_PointerClass\
{\
  MockName##_Implementation* mockPointer = nullptr;\
};\
struct MockName##_GlobalObjectMethodImplementation : public MockName##_PointerClass\

GLOBAL_MOCK_IMPLEMENTATION(Serial)
{
  MOCK_GLOBAL_METHOD(int, foo)
  MOCK_GLOBAL_METHOD(int, boo)
};


//#define GLOBAL_MOCK_DEFINISIONS(MockName)
class MockGlobalObject : public Serial_GlobalObjectMethodImplementation
{
  MockGlobalObject& operator=(const MockGlobalObject& other) = delete;
  MockGlobalObject(const MockGlobalObject& other) = delete;

public:
  MockGlobalObject(){}


  static MockGlobalObject& getGlobalInstanceReference()
  {
    static MockGlobalObject globalInstance;
    return globalInstance;
  }

};


class MockLocalObject : public Serial_Implementation
{

  MockGlobalObject& globalObj = MockGlobalObject::getGlobalInstanceReference();

public:
  MockLocalObject()
  {
    EXPECT_EQ(nullptr, globalObj.mockPointer)
      << "Are you trying to instantiate multiple local mock objects in one scope?";
    globalObj.mockPointer = this;
  }

  ~MockLocalObject()
  {
    EXPECT_NE(nullptr, globalObj.mockPointer);
    globalObj.mockPointer = nullptr;
  }
};

using ::testing::Return;
using ::testing::_;

auto& globalObj = MockGlobalObject::getGlobalInstanceReference();

TEST(SomeMock, FirstTest)
{
  MockLocalObject obj;

  EXPECT_CALL(obj, foo()).WillOnce(Return(1) );
  EXPECT_CALL(obj, foo(2)).WillOnce(Return(2) );
  EXPECT_CALL(obj, foo(3, 3)).WillOnce(Return(3) );
  EXPECT_CALL(obj, boo(3, 3)).WillOnce(Return(4) );

  EXPECT_EQ(1, globalObj.foo());
  EXPECT_EQ(2, globalObj.foo(2));
  EXPECT_EQ(3, globalObj.foo(3, 3));
  EXPECT_EQ(4, globalObj.boo(3, 3));
}
TEST(SomeMock, SecondTest)
{
  MockLocalObject obj;

  EXPECT_CALL(obj, foo()).WillOnce(Return(1) );
  EXPECT_CALL(obj, foo(2)).WillOnce(Return(2) );
  EXPECT_CALL(obj, foo(3, 3)).WillOnce(Return(3) );

  EXPECT_EQ(1, globalObj.foo());
  EXPECT_EQ(2, globalObj.foo(2));
  EXPECT_EQ(3, globalObj.foo(3, 3));
}

<commit_msg>Refactoring, got working with some defines<commit_after>#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include <Arduino.h>
#include <Wire.h>
#include <functional>

#define MOCK_GLOBAL_METHOD(RETURN_TYPE, FUNCTION_NAME) \
template< typename... Args>\
RETURN_TYPE FUNCTION_NAME (Args... args)\
{\
  return mockPointer->FUNCTION_NAME(args...);\
}\


#define REAL_MOCK_IMPLEMENTATION(MockName)\
struct MockName##_Implementation\

REAL_MOCK_IMPLEMENTATION(Serial)
{
  MOCK_METHOD0(foo, int() );
  MOCK_METHOD1(foo, int(int) );
  MOCK_METHOD2(foo, int(int, int) );
  MOCK_METHOD0(boo, int() );
  MOCK_METHOD1(boo, int(int) );
  MOCK_METHOD2(boo, int(int, int) );
};


#define GLOBAL_MOCK_IMPLEMENTATION(MockName)\
struct MockName##_PointerClass\
{\
  MockName##_Implementation* mockPointer = nullptr;\
};\
struct MockName##_GlobalObjectMethodImplementation : public MockName##_PointerClass\

GLOBAL_MOCK_IMPLEMENTATION(Serial)
{
  MOCK_GLOBAL_METHOD(int, foo)
  MOCK_GLOBAL_METHOD(int, boo)
};

#define GLOBAL_MOCK_DEFINISIONS(MockName)\
class MockName##_GlobalObject : public Serial_GlobalObjectMethodImplementation\
{\
  MockName##_GlobalObject& operator=(const MockName##_GlobalObject& other) = delete;\
  MockName##_GlobalObject(const MockName##_GlobalObject& other) = delete;\
\
public:\
  MockName##_GlobalObject(){}\
\
  static MockName##_GlobalObject& getGlobalInstanceReference()\
  {\
    static MockName##_GlobalObject globalInstance;\
    return globalInstance;\
  }\
};\
class MockName##_LocalObject : public Serial_Implementation\
{\
  MockName##_GlobalObject& globalObj = MockName##_GlobalObject::getGlobalInstanceReference();\
\
public:\
  MockName##_LocalObject()\
  {\
    EXPECT_EQ(nullptr, globalObj.mockPointer)\
      << "Are you trying to instantiate multiple local mock objects in one scope?";\
    globalObj.mockPointer = this;\
  }\
\
  ~MockName##_LocalObject()\
  {\
    EXPECT_NE(nullptr, globalObj.mockPointer);\
    globalObj.mockPointer = nullptr;\
  }\
};\
auto& MockName = MockName##_GlobalObject::getGlobalInstanceReference()\

GLOBAL_MOCK_DEFINISIONS(Serial);

using ::testing::Return;
using ::testing::_;

TEST(SomeMock, FirstTest)
{
  Serial_LocalObject obj;

  EXPECT_CALL(obj, foo()).WillOnce(Return(1) );
  EXPECT_CALL(obj, foo(2)).WillOnce(Return(2) );
  EXPECT_CALL(obj, foo(3, 3)).WillOnce(Return(3) );
  EXPECT_CALL(obj, boo(3, 3)).WillOnce(Return(4) );

  EXPECT_EQ(1, Serial.foo());
  EXPECT_EQ(2, Serial.foo(2));
  EXPECT_EQ(3, Serial.foo(3, 3));
  EXPECT_EQ(4, Serial.boo(3, 3));
}

TEST(SomeMock, SecondTest)
{
  Serial_LocalObject obj;

  EXPECT_CALL(obj, foo()).WillOnce(Return(1) );
  EXPECT_CALL(obj, foo(2)).WillOnce(Return(2) );
  EXPECT_CALL(obj, foo(3, 3)).WillOnce(Return(3) );

  EXPECT_EQ(1, Serial.foo());
  EXPECT_EQ(2, Serial.foo(2));
  EXPECT_EQ(3, Serial.foo(3, 3));
}


{'fraction_non_alphanumeric': 0.14781818181818182, 'fraction_numerical': 0.01309090909090909, 'mean_word_length': 5.708536585365854, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5084938', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1974, 'n_words': 328}