File size: 2,433 Bytes
aacf917
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3363645
 
 
 
 
 
 
aacf917
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>TS → Python unittest converter</title>
    <style>
        body { font-family: sans-serif; margin: 20px; }
        textarea { width: 100%; height: 200px; font-family: monospace; }
        button { margin: 10px 0; padding: 6px 12px; }
        label { font-weight: bold; }
    </style>
</head>
<body>
    <label for="ts-input">TS тесты (массив массивов):</label><br>
    <textarea id="ts-input">[
    ['т.д.', 'т.\u00A0д.'],
    ['т.п.', 'т.\u00A0п.']
]</textarea>
    <br>
    <button id="convert">Конвертировать в Python assertEqual</button>
    <br>
    <label for="py-output">Python-код:</label><br>
    <textarea id="py-output" readonly></textarea>

    <script>
        function pyEscape(s) {
    return s
        .replace(/\\/g, '\\\\')      // экранируем обратный слеш
        .replace(/'/g, "\\'")        // экранируем одинарную кавычку
        .replace(/\u00A0/g, '\\u00A0') // NBSP → \u00A0
        .replace(/\r/g, '\\r')       // CR → \r
        .replace(/\n/g, '\\n');      // LF → \n (главное!)
}

        document.getElementById('convert').addEventListener('click', () => {
            const input = document.getElementById('ts-input').value.trim();
            let data;
            try {
                // это локальный тул, так что eval тут окей
                data = eval(input);
            } catch (e) {
                alert('Не смог распарсить TS-массив. Проверь синтаксис.\n\n' + e);
                return;
            }

            if (!Array.isArray(data)) {
                alert('Ожидаю внешний массив.');
                return;
            }

            const lines = [];
            for (const pair of data) {
                if (!Array.isArray(pair) || pair.length < 2) continue;
                const src = String(pair[0]);
                const dst = String(pair[1]);

                const srcEsc = pyEscape(src);
                const dstEsc = pyEscape(dst);

                lines.push(
                    `        self.assertEqual(tg.execute('${srcEsc}'), '${dstEsc}')`
                );
            }

            document.getElementById('py-output').value = lines.join('\n');
        });
    </script>
</body>
</html>