This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathset-command_test.cpp
81 lines (65 loc) · 2.05 KB
/
set-command_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "Firebase.h"
#include "gtest/gtest.h"
#include "modem/db/commands.h"
#include "modem/json_util.h"
#include "test/modem/mock-input-stream.h"
#include "test/modem/mock-output-stream.h"
#include "test/mock-firebase.h"
namespace firebase {
namespace modem {
using ::testing::Return;
using ::testing::ByMove;
using ::testing::ReturnRef;
using ::testing::_;
class SetCommandTest : public ::testing::Test {
protected:
void SetUp() override {
set_.reset(new MockFirebaseSet());
}
void FeedCommand(const String& path, const String& data) {
const String data_fragment(data);
EXPECT_CALL(in_, readStringUntil(' '))
.WillOnce(Return(path));
EXPECT_CALL(in_, readLine())
.WillOnce(Return(data_fragment));
}
void ExpectOutput(const String& output) {
EXPECT_CALL(out_, println(output))
.WillOnce(Return(3));
}
void ExpectErrorOutput(const String& error_message) {
EXPECT_CALL(out_, print(String("-FAIL ")))
.WillOnce(Return(5));
EXPECT_CALL(out_, println(error_message))
.WillOnce(Return(error_message.length()));
}
bool RunExpectingData(const String& data, const FirebaseError& error) {
EXPECT_CALL(*set_, error())
.WillRepeatedly(ReturnRef(error));
EXPECT_CALL(fbase_, setPtr(_, EncodeForJson(data)))
.WillOnce(Return(ByMove(std::move(set_))));
SetCommand setCmd(&fbase_);
return setCmd.execute("SET", &in_, &out_);
}
MockInputStream in_;
MockOutputStream out_;
MockFirebase fbase_;
std::unique_ptr<MockFirebaseSet> set_;
};
TEST_F(SetCommandTest, sendsData) {
const String path("/test/path");
const String data("This is a test payload.");
FeedCommand(path, data);
ExpectOutput("+OK");
ASSERT_TRUE(RunExpectingData(data, FirebaseError()));
}
TEST_F(SetCommandTest, HandlesError) {
const String path("/test/path");
const String data("This is a test payload.");
FirebaseError error(-200, "Test error.");
FeedCommand(path, data);
ExpectErrorOutput(error.message());
ASSERT_FALSE(RunExpectingData(data, error));
}
} // modem
} // firebase