forked from YujiSoftware/ac-unit-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
248 lines (201 loc) · 5.77 KB
/
content.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
chrome.runtime.onMessage.addListener(function (message, sender, callback) {
if (message.functiontoInvoke == "onClick") {
var io = onClick();
chrome.storage.sync.get({
language: 'Java',
}, function(items) {
switch(items.language){
case "Java":
copy(createJUnit(io));
break;
case "CSharp":
copy(createMSTest(io));
break;
case "Python3":
copy(createPyUnittest(io));
break;
case "Kotlin":
copy(createJUnitKotlin(io));
break;
default:
throw new Error("Unknown language. [items.language=" + items.language + "]");
}
});
}
});
function onClick(){
var name = null;
var input = null;
var output = null;
var io = [];
var sample_tests = $("#pageContent > div.problemindexholder > div.ttypography > div > div.sample-tests > div.sample-test > div");
var num = 0;
for (var i = 0; i < sample_tests.length; i++) {
var sample_test = $(sample_tests[i]);
var children = sample_test[0].children;
var header = children[0].firstChild.textContent.trim();
var data = children[1].innerText.trim();
if (header.indexOf("Input") == 0 || header.indexOf("Входные данные") == 0) {
// Input0
name = header.replace(/\s+/g, "_") + num;
input = data;
num += 1;
} else if (header.indexOf("Output") == 0 || header.indexOf("Выходные данные") == 0) {
output = data;
}
if (name != null && input != null && output != null) {
io.push({ name: name, input: input, output: output });
name = input = output = null;
}
}
return io;
}
function createJUnit(io){
var text =
`import static org.hamcrest.CoreMatchers.is;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Assert;
import org.junit.Test;
public class MainTest {
`;
for(var i = 0; i < io.length; i++){
text += `
@Test
public void ${io[i].name}() throws Exception {
String input =
"${io[i].input.trim("\n").replace(/\n/g, '" + System.lineSeparator() +\n "')}";
String output =
"${io[i].output.trim("\n").replace(/\n/g, '" + System.lineSeparator() +\n "')}";
assertIO(input, output);
}
`;
}
text += `
private void assertIO(String input, String output) throws Exception {
ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
System.setIn(in);
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
Main.main(new String[0]);
Assert.assertThat(out.toString(), is(output + System.lineSeparator()));
}
}
`;
return text;
};
function createMSTest(io){
var text =
`using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
namespace Codeforces
{
[TestClass]
public class ProgramTest
{`;
for(var i = 0; i < io.length; i++){
text += `
[TestMethod]
public void ${io[i].name}()
{
string input =
@"${io[i].input.trim("\n").replace(/\n/g, '\r\n')}";
string output =
@"${io[i].output.trim("\n").replace(/\n/g, '\r\n')}";
AssertIO(input, output);
}
`;
}
text += `
private void AssertIO(string input, string output)
{
StringReader reader = new StringReader(input);
Console.SetIn(reader);
StringWriter writer = new StringWriter();
Console.SetOut(writer);
Program.Main(new string[0]);
Assert.AreEqual(output + Environment.NewLine, writer.ToString());
}
}
}
`;
return text;
}
function createPyUnittest(io) {
var text =
`import sys
from io import StringIO
import unittest
class TestClass(unittest.TestCase):
def assertIO(self, input, output):
stdout, stdin = sys.stdout, sys.stdin
sys.stdout, sys.stdin = StringIO(), StringIO(input)
resolve()
sys.stdout.seek(0)
out = sys.stdout.read()[:-1]
sys.stdout, sys.stdin = stdout, stdin
self.assertEqual(out, output)
`;
for(var i = 0; i < io.length; i++){
text +=
` def test_${io[i].name}(self):
input = """${io[i].input.trim("\n").replace(/\n/g, '\r\n')}"""
output = """${io[i].output.trim("\n").replace(/\n/g, '\r\n')}"""
self.assertIO(input, output)
`;
}
text += `
if __name__ == "__main__":
unittest.main()
`;
return text;
}
function createJUnitKotlin(io){
var text =
`import org.hamcrest.CoreMatchers.equalTo
import org.junit.Assert
import org.junit.Test
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.PrintStream
class MainTest {
`;
for(var i = 0; i < io.length; i++){
text +=`
@Test
fun ${io[i].name}() {
val input =
"${io[i].input.trim("\n").replace(/\n/g, '" + System.lineSeparator() +\n "')}";
val output =
"${io[i].output.trim("\n").replace(/\n/g, '" + System.lineSeparator() +\n "')}";
assertIO(input, output);
}
`;
}
text +=`
private fun assertIO(input: String, output: String) {
val sysIn = ByteArrayInputStream(input.toByteArray())
System.setIn(sysIn)
val sysOut = ByteArrayOutputStream()
System.setOut(PrintStream(sysOut))
cf0000X()
Assert.assertThat(sysOut.toString(), equalTo(output + System.lineSeparator()))
}
}
`;
return text;
};
function copy(text){
var textArea = document.createElement("textarea");
textArea.style.cssText = "position: absolute; left: -100%;";
try{
document.body.appendChild(textArea);
textArea.value = text;
textArea.select();
document.execCommand("copy");
}finally{
document.body.removeChild(textArea);
}
}