test / test_ts.html
2ch's picture
Update test_ts.html
3363645 verified
<!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>