2ch commited on
Commit
aacf917
·
verified ·
1 Parent(s): 9c76888

Update test_ts.html

Browse files
Files changed (1) hide show
  1. test_ts.html +70 -0
test_ts.html CHANGED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="ru">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>TS → Python unittest converter</title>
6
+ <style>
7
+ body { font-family: sans-serif; margin: 20px; }
8
+ textarea { width: 100%; height: 200px; font-family: monospace; }
9
+ button { margin: 10px 0; padding: 6px 12px; }
10
+ label { font-weight: bold; }
11
+ </style>
12
+ </head>
13
+ <body>
14
+ <label for="ts-input">TS тесты (массив массивов):</label><br>
15
+ <textarea id="ts-input">[
16
+ ['т.д.', 'т.\u00A0д.'],
17
+ ['т.п.', 'т.\u00A0п.']
18
+ ]</textarea>
19
+ <br>
20
+ <button id="convert">Конвертировать в Python assertEqual</button>
21
+ <br>
22
+ <label for="py-output">Python-код:</label><br>
23
+ <textarea id="py-output" readonly></textarea>
24
+
25
+ <script>
26
+ function pyEscape(s) {
27
+ // сначала экранируем обратный слеш
28
+ s = s.replace(/\\/g, '\\\\');
29
+ // потом одинарную кавычку
30
+ s = s.replace(/'/g, "\\'");
31
+ // NBSP обратно в \u00A0
32
+ s = s.replace(/\u00A0/g, '\\u00A0');
33
+ return s;
34
+ }
35
+
36
+ document.getElementById('convert').addEventListener('click', () => {
37
+ const input = document.getElementById('ts-input').value.trim();
38
+ let data;
39
+ try {
40
+ // это локальный тул, так что eval тут окей
41
+ data = eval(input);
42
+ } catch (e) {
43
+ alert('Не смог распарсить TS-массив. Проверь синтаксис.\n\n' + e);
44
+ return;
45
+ }
46
+
47
+ if (!Array.isArray(data)) {
48
+ alert('Ожидаю внешний массив.');
49
+ return;
50
+ }
51
+
52
+ const lines = [];
53
+ for (const pair of data) {
54
+ if (!Array.isArray(pair) || pair.length < 2) continue;
55
+ const src = String(pair[0]);
56
+ const dst = String(pair[1]);
57
+
58
+ const srcEsc = pyEscape(src);
59
+ const dstEsc = pyEscape(dst);
60
+
61
+ lines.push(
62
+ ` self.assertEqual(tg.execute('${srcEsc}'), '${dstEsc}')`
63
+ );
64
+ }
65
+
66
+ document.getElementById('py-output').value = lines.join('\n');
67
+ });
68
+ </script>
69
+ </body>
70
+ </html>