feat:add test
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { runCode } from '@/actions'; // 导入 Server Action
|
||||
|
||||
// import './styles.css'; // 引入样式
|
||||
//
|
||||
// const root = document.getElementById('root');
|
||||
// if (root) {
|
||||
// root.innerHTML = 'Hello, world!';
|
||||
// }
|
||||
//
|
||||
// /// <reference types="webpack-env" />
|
||||
//
|
||||
// if (module.hot) {
|
||||
// module.hot.accept(() => {
|
||||
// console.log('HMR is working!');
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// // 启用 HMR
|
||||
// if (module.hot) {
|
||||
// module.hot.accept('./styles.css', () => {
|
||||
// console.log('CSS updated!');
|
||||
// });
|
||||
// }
|
||||
|
||||
|
||||
const CodeRunner = () => {
|
||||
const [code, setCode] = useState('');
|
||||
const [language, setLanguage] = useState('c'); // 默认语言为 C
|
||||
const [output, setOutput] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const runCodeHandler = async () => {
|
||||
try {
|
||||
// 调用 Server Action 执行代码
|
||||
const result = await runCode({ code, language });
|
||||
|
||||
setOutput(result.output || ''); // 如果没有输出,设置为空字符串
|
||||
setError(result.error || ''); // 如果没有错误,设置为空字符串
|
||||
} catch (err) {
|
||||
setError((err as Error).message);
|
||||
setOutput('');
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>Code Runner</h1>
|
||||
<textarea
|
||||
value={code}
|
||||
onChange={(e) => setCode(e.target.value)}
|
||||
placeholder="Write your code here"
|
||||
rows={10}
|
||||
cols={50}
|
||||
/>
|
||||
<div>
|
||||
<label>
|
||||
Language:
|
||||
<select value={language} onChange={(e) => setLanguage(e.target.value)}>
|
||||
<option value="c">C</option>
|
||||
<option value="java">Java</option>
|
||||
<option value="python">Python</option> {/* 添加 Python */}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<button onClick={runCodeHandler}>Run Code</button>
|
||||
|
||||
{output && (
|
||||
<div>
|
||||
<h3>Output:</h3>
|
||||
<pre>{output}</pre>
|
||||
</div>
|
||||
)}
|
||||
{error && (
|
||||
<div>
|
||||
<h3>Error:</h3>
|
||||
<pre>{error}</pre>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default CodeRunner;
|
||||
+88
-64
@@ -1,86 +1,110 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { runCode } from '@/actions'; // 导入 Server Action
|
||||
import React, { useState } from 'react';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { runMultiFileCodeWithOptionalTestData } from '@/actions/enhancedmultiFileRunner';
|
||||
|
||||
// import './styles.css'; // 引入样式
|
||||
//
|
||||
// const root = document.getElementById('root');
|
||||
// if (root) {
|
||||
// root.innerHTML = 'Hello, world!';
|
||||
// }
|
||||
//
|
||||
// /// <reference types="webpack-env" />
|
||||
//
|
||||
// if (module.hot) {
|
||||
// module.hot.accept(() => {
|
||||
// console.log('HMR is working!');
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// // 启用 HMR
|
||||
// if (module.hot) {
|
||||
// module.hot.accept('./styles.css', () => {
|
||||
// console.log('CSS updated!');
|
||||
// });
|
||||
// }
|
||||
const TestPage = () => {
|
||||
const [code, setCode] = useState<string>('');
|
||||
const [language, setLanguage] = useState<string>('c');
|
||||
const [result, setResult] = useState<string>('');
|
||||
const [loading, setLoading] = useState<boolean>(false);
|
||||
|
||||
const handleRun = async () => {
|
||||
setLoading(true);
|
||||
setResult('');
|
||||
|
||||
const CodeRunner = () => {
|
||||
const [code, setCode] = useState('');
|
||||
const [language, setLanguage] = useState('c'); // 默认语言为 C
|
||||
const [output, setOutput] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const testFiles = [
|
||||
{ name: 'input1.txt', content: '1 2 3' },
|
||||
{ name: 'input2.txt', content: '4 5 6' },
|
||||
];
|
||||
|
||||
const expectedFiles = [
|
||||
{ name: 'output1.txt', content: '6' },
|
||||
{ name: 'output2.txt', content: '15' },
|
||||
];
|
||||
|
||||
const codeFile = {
|
||||
name: language === 'c' ? 'main.c' : language === 'python' ? 'script.py' : 'Main.java',
|
||||
content: code,
|
||||
};
|
||||
|
||||
const runCodeHandler = async () => {
|
||||
try {
|
||||
// 调用 Server Action 执行代码
|
||||
const result = await runCode({ code, language });
|
||||
const result = await runMultiFileCodeWithOptionalTestData({
|
||||
files: [codeFile],
|
||||
language,
|
||||
testDataFiles: testFiles.map((file) => file.name),
|
||||
expectedAnswerFiles: expectedFiles.map((file) => file.name),
|
||||
resultOutputFile: 'result.txt',
|
||||
});
|
||||
|
||||
setOutput(result.output || ''); // 如果没有输出,设置为空字符串
|
||||
setError(result.error || ''); // 如果没有错误,设置为空字符串
|
||||
} catch (err) {
|
||||
setError((err as Error).message);
|
||||
setOutput('');
|
||||
setResult(JSON.stringify(result, null, 2));
|
||||
} catch (error) {
|
||||
// 使用类型断言确保 error 是 Error 类型
|
||||
setResult(`Error: ${(error as Error).message}`);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>Code Runner</h1>
|
||||
<textarea
|
||||
value={code}
|
||||
onChange={(e) => setCode(e.target.value)}
|
||||
placeholder="Write your code here"
|
||||
rows={10}
|
||||
cols={50}
|
||||
/>
|
||||
<div>
|
||||
<div style={{ padding: '20px', fontFamily: 'Arial, sans-serif' }}>
|
||||
<h1>Test Page</h1>
|
||||
<div style={{ marginBottom: '20px' }}>
|
||||
<label>
|
||||
Language:
|
||||
<select value={language} onChange={(e) => setLanguage(e.target.value)}>
|
||||
<strong>Language:</strong>
|
||||
<select
|
||||
value={language}
|
||||
onChange={(e) => setLanguage(e.target.value)}
|
||||
style={{ marginLeft: '10px' }}
|
||||
>
|
||||
<option value="c">C</option>
|
||||
<option value="java">Java</option>
|
||||
<option value="python">Python</option> {/* 添加 Python */}
|
||||
<option value="python">Python</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<button onClick={runCodeHandler}>Run Code</button>
|
||||
|
||||
{output && (
|
||||
<div>
|
||||
<h3>Output:</h3>
|
||||
<pre>{output}</pre>
|
||||
</div>
|
||||
)}
|
||||
{error && (
|
||||
<div>
|
||||
<h3>Error:</h3>
|
||||
<pre>{error}</pre>
|
||||
</div>
|
||||
)}
|
||||
<div style={{ marginBottom: '20px' }}>
|
||||
<label>
|
||||
<strong>Code:</strong>
|
||||
<textarea
|
||||
value={code}
|
||||
onChange={(e) => setCode(e.target.value)}
|
||||
rows={10}
|
||||
style={{ width: '100%', fontFamily: 'monospace', fontSize: '14px' }}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<button
|
||||
onClick={handleRun}
|
||||
style={{
|
||||
padding: '10px 20px',
|
||||
background: 'blue',
|
||||
color: 'white',
|
||||
border: 'none',
|
||||
borderRadius: '5px',
|
||||
cursor: 'pointer',
|
||||
}}
|
||||
disabled={loading}
|
||||
>
|
||||
{loading ? 'Running...' : 'Run Code'}
|
||||
</button>
|
||||
<div style={{ marginTop: '20px' }}>
|
||||
<h2>Result</h2>
|
||||
<pre
|
||||
style={{
|
||||
background: '#f4f4f4',
|
||||
padding: '10px',
|
||||
border: '1px solid #ddd',
|
||||
borderRadius: '5px',
|
||||
}}
|
||||
>
|
||||
{result || 'No results yet.'}
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default CodeRunner;
|
||||
export default TestPage;
|
||||
|
||||
Reference in New Issue
Block a user