Initial commit 🎉
fbshipit-source-id: b6fc29740c6875d2e78953b8a7123890a67930f2 Co-authored-by: Sebastian McKenzie <sebmck@fb.com> Co-authored-by: John Knox <jknox@fb.com> Co-authored-by: Emil Sjölander <emilsj@fb.com> Co-authored-by: Pritesh Nandgaonkar <prit91@fb.com>
This commit is contained in:
34
xplat/SonarTestLib/SonarConnectionMock.h
Normal file
34
xplat/SonarTestLib/SonarConnectionMock.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Sonar/SonarConnection.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace facebook {
|
||||
namespace sonar {
|
||||
|
||||
class SonarConnectionMock : public SonarConnection {
|
||||
public:
|
||||
void send(const std::string& method, const folly::dynamic& params) override {
|
||||
sent_[method] = params;
|
||||
}
|
||||
|
||||
void receive(const std::string& method, const SonarReceiver& receiver)
|
||||
override {
|
||||
receivers_[method] = receiver;
|
||||
}
|
||||
|
||||
std::map<std::string, folly::dynamic> sent_;
|
||||
std::map<std::string, SonarReceiver> receivers_;
|
||||
};
|
||||
|
||||
} // namespace sonar
|
||||
} // namespace facebook
|
||||
62
xplat/SonarTestLib/SonarPluginMock.h
Normal file
62
xplat/SonarTestLib/SonarPluginMock.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Sonar/SonarPlugin.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace sonar {
|
||||
namespace test {
|
||||
|
||||
class SonarPluginMock : public SonarPlugin {
|
||||
using ConnectionCallback =
|
||||
std::function<void(std::shared_ptr<SonarConnection>)>;
|
||||
using DisconnectionCallback = std::function<void()>;
|
||||
|
||||
public:
|
||||
SonarPluginMock(const std::string& identifier) : identifier_(identifier) {}
|
||||
|
||||
SonarPluginMock(
|
||||
const std::string& identifier,
|
||||
const ConnectionCallback& connectionCallback)
|
||||
: identifier_(identifier), connectionCallback_(connectionCallback) {}
|
||||
|
||||
SonarPluginMock(
|
||||
const std::string& identifier,
|
||||
const ConnectionCallback& connectionCallback,
|
||||
const DisconnectionCallback& disconnectionCallback)
|
||||
: identifier_(identifier),
|
||||
connectionCallback_(connectionCallback),
|
||||
disconnectionCallback_(disconnectionCallback) {}
|
||||
|
||||
std::string identifier() const override {
|
||||
return identifier_;
|
||||
}
|
||||
|
||||
void didConnect(std::shared_ptr<SonarConnection> conn) override {
|
||||
if (connectionCallback_) {
|
||||
connectionCallback_(conn);
|
||||
}
|
||||
}
|
||||
|
||||
void didDisconnect() override {
|
||||
if (disconnectionCallback_) {
|
||||
disconnectionCallback_();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::string identifier_;
|
||||
ConnectionCallback connectionCallback_;
|
||||
DisconnectionCallback disconnectionCallback_;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
} // namespace sonar
|
||||
} // namespace facebook
|
||||
43
xplat/SonarTestLib/SonarResponderMock.h
Normal file
43
xplat/SonarTestLib/SonarResponderMock.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Sonar/SonarResponder.h>
|
||||
#include <folly/json.h>
|
||||
#include <vector>
|
||||
|
||||
namespace facebook {
|
||||
namespace sonar {
|
||||
|
||||
class SonarResponderMock : public SonarResponder {
|
||||
public:
|
||||
SonarResponderMock(
|
||||
std::vector<folly::dynamic>* successes = nullptr,
|
||||
std::vector<folly::dynamic>* errors = nullptr)
|
||||
: successes_(successes), errors_(errors) {}
|
||||
|
||||
void success(const folly::dynamic& response) const override {
|
||||
if (successes_) {
|
||||
successes_->push_back(response);
|
||||
}
|
||||
}
|
||||
|
||||
void error(const folly::dynamic& response) const override {
|
||||
if (errors_) {
|
||||
errors_->push_back(response);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<folly::dynamic>* successes_;
|
||||
std::vector<folly::dynamic>* errors_;
|
||||
};
|
||||
|
||||
} // namespace sonar
|
||||
} // namespace facebook
|
||||
55
xplat/SonarTestLib/SonarWebSocketMock.h
Normal file
55
xplat/SonarTestLib/SonarWebSocketMock.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Sonar/SonarWebSocket.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace sonar {
|
||||
namespace test {
|
||||
|
||||
class SonarWebSocketMock : public SonarWebSocket {
|
||||
public:
|
||||
SonarWebSocketMock() : callbacks(nullptr) {}
|
||||
|
||||
void start() override {
|
||||
open = true;
|
||||
if (callbacks) {
|
||||
callbacks->onConnected();
|
||||
}
|
||||
}
|
||||
|
||||
void stop() override {
|
||||
open = false;
|
||||
if (callbacks) {
|
||||
callbacks->onDisconnected();
|
||||
}
|
||||
}
|
||||
|
||||
bool isOpen() const override {
|
||||
return open;
|
||||
}
|
||||
|
||||
void sendMessage(const folly::dynamic& message) override {
|
||||
messages.push_back(message);
|
||||
}
|
||||
|
||||
void setCallbacks(Callbacks* aCallbacks) override {
|
||||
callbacks = aCallbacks;
|
||||
}
|
||||
|
||||
public:
|
||||
bool open = false;
|
||||
Callbacks* callbacks;
|
||||
std::vector<folly::dynamic> messages;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
} // namespace sonar
|
||||
} // namespace facebook
|
||||
Reference in New Issue
Block a user