forked from cloose/CuteMarkEd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonsnippetfiletest.cpp
More file actions
165 lines (131 loc) · 5.22 KB
/
jsonsnippetfiletest.cpp
File metadata and controls
165 lines (131 loc) · 5.22 KB
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
/*
* Copyright 2013 Christian Loose <christian.loose@hamburg.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "jsonsnippetfiletest.h"
#include <QtTest>
#include <QTemporaryFile>
#include <QTextStream>
#include <jsonfile.h>
#include <snippets/jsonsnippettranslatorfactory.h>
#include <snippets/snippetcollection.h>
void JsonSnippetFileTest::loadsEmptySnippetsCollectionFromFile()
{
QTemporaryFile snippetFile(this);
if (!snippetFile.open())
QFAIL("Failed to create temporary snippet file");
QTextStream out(&snippetFile); out << "{ \"snippets\": [] }";
snippetFile.close();
SnippetCollection collection;
bool success = JsonFile<Snippet>::load(snippetFile.fileName(), &collection);
QVERIFY(success);
QCOMPARE(collection.count(), 0);
}
void JsonSnippetFileTest::loadsSnippetsCollectionFromFile()
{
QTemporaryFile snippetFile(this);
if (!snippetFile.open())
QFAIL("Failed to create temporary snippet file");
QTextStream out(&snippetFile);
out << "{ \"snippets\": ["
<< " { \"trigger\": \"abc\","
<< " \"description\": \"description xyz\","
<< " \"snippet\": \"content abc\","
<< " \"cursor\": 0,"
<< " \"builtIn\": true },"
<< " { \"trigger\": \"xyz\","
<< " \"description\": \"description xyz\","
<< " \"snippet\": \"content xyz\","
<< " \"cursor\": 1,"
<< " \"builtIn\": false } ] }";
snippetFile.close();
SnippetCollection collection;
bool success = JsonFile<Snippet>::load(snippetFile.fileName(), &collection);
QVERIFY(success);
QCOMPARE(collection.count(), 2);
QVERIFY(collection.contains("abc"));
QVERIFY(collection.contains("xyz"));
}
void JsonSnippetFileTest::savesEmptySnippetsCollectionToFile()
{
QTemporaryFile snippetFile(this);
if (!snippetFile.open())
QFAIL("Failed to create temporary snippet file");
SnippetCollection collection;
bool success = JsonFile<Snippet>::save(snippetFile.fileName(), &collection);
QVERIFY(success);
QTextStream in(&snippetFile);
QString fileContent = in.readAll().trimmed();
QVERIFY(fileContent.startsWith("{"));
QVERIFY(fileContent.contains("\"snippets\": ["));
QVERIFY(fileContent.endsWith("}"));
}
void JsonSnippetFileTest::savesSnippetsCollectionToFile()
{
QTemporaryFile snippetFile(this);
if (!snippetFile.open())
QFAIL("Failed to create temporary snippet file");
Snippet snippet1;
snippet1.trigger = "abc";
Snippet snippet2;
snippet2.trigger = "xyz";
SnippetCollection collection;
collection.insert(snippet1);
collection.insert(snippet2);
bool success = JsonFile<Snippet>::save(snippetFile.fileName(), &collection);
QVERIFY(success);
QTextStream in(&snippetFile);
QString fileContent = in.readAll().trimmed();
QVERIFY(fileContent.startsWith("{"));
QVERIFY(fileContent.contains("\"snippets\": ["));
QVERIFY(fileContent.contains("\"trigger\": \"abc\""));
QVERIFY(fileContent.contains("\"trigger\": \"xyz\""));
QVERIFY(fileContent.endsWith("}"));
}
void JsonSnippetFileTest::roundtripTest()
{
QTemporaryFile snippetFile(this);
if (!snippetFile.open())
QFAIL("Failed to create temporary snippet file");
Snippet snippet1;
snippet1.trigger = "abc";
snippet1.description = "description abc";
snippet1.snippet = "content abc";
snippet1.cursorPosition = 0;
snippet1.builtIn = true;
Snippet snippet2;
snippet2.trigger = "xyz";
snippet2.description = "description xyz";
snippet2.snippet = "content xyz";
snippet2.cursorPosition = 1;
snippet2.builtIn = false;
SnippetCollection collection1;
collection1.insert(snippet1);
collection1.insert(snippet2);
bool saveSuccess = JsonFile<Snippet>::save(snippetFile.fileName(), &collection1);
QVERIFY(saveSuccess);
SnippetCollection collection2;
bool loadSuccess = JsonFile<Snippet>::load(snippetFile.fileName(), &collection2);
QVERIFY(loadSuccess);
QCOMPARE(collection2.count(), 2);
QCOMPARE(collection2.snippet("abc").description, snippet1.description);
QCOMPARE(collection2.snippet("abc").snippet, snippet1.snippet);
QCOMPARE(collection2.snippet("abc").cursorPosition, snippet1.cursorPosition);
QCOMPARE(collection2.snippet("abc").builtIn, snippet1.builtIn);
QCOMPARE(collection2.snippet("xyz").description, snippet2.description);
QCOMPARE(collection2.snippet("xyz").snippet, snippet2.snippet);
QCOMPARE(collection2.snippet("xyz").cursorPosition, snippet2.cursorPosition);
QCOMPARE(collection2.snippet("xyz").builtIn, snippet2.builtIn);
}