File: tst_neovimconnector.cpp

package info (click to toggle)
neovim-qt 0.2.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,808 kB
  • sloc: cpp: 45,441; python: 234; makefile: 24; sh: 19; xml: 5
file content (170 lines) | stat: -rw-r--r-- 4,616 bytes parent folder | download
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <QTest>
#include <QSignalSpy>
#include <QTcpServer>
#include <QTcpSocket>
#include <QRegularExpression>
#include <QBuffer>

#include <neovimconnector.h>
#include "common.h"

namespace NeovimQt {

class Test: public QObject
{
	Q_OBJECT
private slots:

	void reconnect() {
		NeovimConnector c(new QBuffer());
		QCOMPARE(c.canReconnect(), false);

		NeovimConnector *spawned = NeovimConnector::spawn({"-u", "NONE"});
		spawned->setParent(this);
		QCOMPARE(spawned->connectionType(), NeovimConnector::SpawnedConnection);
		QCOMPARE(spawned->canReconnect(), true);

		spawned->reconnect();
	}

	void isReady() {

		NeovimConnector *c = NeovimConnector::spawn({"-u", "NONE"});
		c->setParent(this);
		QSignalSpy onReady(c, SIGNAL(ready()));
		QVERIFY(onReady.isValid());

		QVERIFY(SPYWAIT(onReady));
		QVERIFY(c->isReady());
	}

	void encodeDecode() {
		NeovimConnector *c = NeovimConnector::spawn({"-u", "NONE"});
		c->setParent(this);
		// This will print a warning, but should succeed
		QString s = "ç日本語";
		QByteArray bytes = c->encode(s);
		QCOMPARE(c->decode(bytes), s);

		QSignalSpy onReady(c, SIGNAL(ready()));
		QVERIFY(onReady.isValid());
		QVERIFY(SPYWAIT(onReady));

		bytes = c->encode(s);
		QCOMPARE(c->decode(bytes), s);

	}

	void connectToNeovimTCP() {
		NeovimConnector *c = NeovimConnector::connectToNeovim("127.0.0.1:64999");
		c->setParent(this);
		QCOMPARE(c->connectionType(), NeovimConnector::HostConnection);
		QSignalSpy onError(c, SIGNAL(error(NeovimError)));
		QVERIFY(onError.isValid());

		// Pull Request #612: On MacOS, this test can fail without a long timeout value.
		QVERIFY(SPYWAIT(onError, 120000 /*msec*/));

		QCOMPARE(c->errorCause(), NeovimConnector::SocketError);
		c->deleteLater();
	}

	void connectToNeovimSocket() {
		NeovimConnector *c = NeovimConnector::connectToNeovim("NoSuchFile");
		c->setParent(this);
		QCOMPARE(c->connectionType(), NeovimConnector::SocketConnection);
		QSignalSpy onError(c, SIGNAL(error(NeovimError)));
		QVERIFY(onError.isValid());

		// Test Performance: timeout expected, set value carefully.
		QVERIFY(!SPYWAIT(onError, 5000 /*msec*/));

		QCOMPARE(c->errorCause(), NeovimConnector::SocketError);
		c->deleteLater();
	}

	void connectToNeovimEnvEmpty() {
		// This is the same as ::spawn()
		NeovimConnector *c = NeovimConnector::connectToNeovim("");
		c->setParent(this);
		QSignalSpy onReady(c, SIGNAL(ready()));
		QVERIFY(onReady.isValid());
		QVERIFY(SPYWAIT(onReady));

		c->deleteLater();
	}


#ifdef Q_OS_UNIX
	void connectToSocket_data() {
		QTest::addColumn<QString>("socketname");

		QTest::newRow("relative") << "relnvimsock";
		QTest::newRow("./relative") << "./relnvimsock";
		QTest::newRow("absolute") << QFileInfo("absnvimsock").absoluteFilePath();
	}

	// https://github.com/equalsraf/neovim-qt/issues/936
	// QLocalSocket cannot open relative paths. This happens
	// both in Linux and Mac.
	void connectToSocket() {
		QFETCH(QString, socketname);

		QDir().remove(socketname);
		// Start nvim
		QProcess p;
		p.setProgram("nvim");
		QProcessEnvironment env = QProcessEnvironment::systemEnvironment();

		auto path_info = QFileInfo(socketname);
		if (path_info.isAbsolute()) {
			env.insert("NVIM_LISTEN_ADDRESS", socketname);
		}
		else {
			env.insert("NVIM_LISTEN_ADDRESS", "./" + socketname);
		}
		p.setProcessEnvironment(env);
		p.setArguments({"--headless", "-u", "NONE"});
		p.start();
		p.waitForStarted();

		QTest::qWait(1500);
		// connect
		NeovimConnector *c = NeovimConnector::connectToSocket(socketname);
		qDebug() << c->connectionDescription();
		QSignalSpy onReady(c, SIGNAL(ready()));
		QVERIFY(onReady.isValid());
		QVERIFY(SPYWAIT(onReady, 5000 /*msec*/));

		QCOMPARE(c->connectionType(), NeovimConnector::SocketConnection);
	}
#endif

	void metadataTimeout() {
		// Connect to a TCP socket that will never respond, should trigger
		// a timeout for the discoverMetadata call
		QTcpServer *server = new QTcpServer();
		server->listen(QHostAddress::LocalHost);
		QVERIFY(server->isListening());

		NeovimConnector* c =
			NeovimConnector::connectToNeovim(QStringLiteral("%1:%2")
												 .arg(server->serverAddress().toString())
												 .arg(server->serverPort()));

		// Test Performance: timeout expected, set value carefully.
		c->setRequestTimeout(1000 /*msec*/);

		QSignalSpy onError(c, SIGNAL(error(NeovimError)));
		QVERIFY(onError.isValid());

		QVERIFY(SPYWAIT(onError, 5000 /*msec*/));

		QCOMPARE(c->errorCause(), NeovimConnector::RuntimeMsgpackError);
		c->deleteLater();
	}
};

} // Namespace NeovimQt
QTEST_MAIN(NeovimQt::Test)
#include "tst_neovimconnector.moc"