-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.js
More file actions
executable file
·144 lines (122 loc) · 4.86 KB
/
Copy pathrun_tests.js
File metadata and controls
executable file
·144 lines (122 loc) · 4.86 KB
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
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import { execSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import chalk from 'chalk';
import * as diff from 'diff';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function displayDiff(expected, actual) {
console.log(chalk.bold('\n📊 Visual Diff:'));
console.log('─'.repeat(60));
const changes = diff.diffLines(expected, actual);
changes.forEach((part) => {
const prefix = part.added ? '+ ' : part.removed ? '- ' : ' ';
const color = part.added ? chalk.green : part.removed ? chalk.red : chalk.gray;
part.value.split('\n').forEach((line, index, lines) => {
if (index === lines.length - 1 && line === '') return;
console.log(color(`${prefix}${line}`));
});
});
console.log('─'.repeat(60));
}
function runTests() {
const testDir = path.join(__dirname, './test_data');
const mainScript = path.join(__dirname, './main.js');
console.log(chalk.bold.blue('🧪 Test Results\n'));
try {
const subdirs = fs
.readdirSync(testDir, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
.sort();
if (subdirs.length === 0) {
console.log(chalk.yellow('⚠️ No test directories found in ./test_data/'));
return;
}
let passedTests = 0;
const totalTests = subdirs.length;
for (const subdir of subdirs) {
const testPath = path.join(testDir, subdir);
const inFile = path.join(testPath, 'in.js');
const outFile = path.join(testPath, 'out.js');
if (!fs.existsSync(inFile)) {
console.log(chalk.red.bold(`❌ Failed on test ${subdir}`));
console.log(chalk.yellow(`Missing input file: ${inFile}\n`));
continue;
}
if (!fs.existsSync(outFile)) {
console.log(chalk.red.bold(`❌ Failed on test ${subdir}`));
console.log(chalk.yellow(`Missing output file: ${outFile}\n`));
continue;
}
try {
const inputContent = fs.readFileSync(inFile, 'utf8');
const expectedOutput = fs.readFileSync(outFile, 'utf8').trim();
const actualOutput = execSync(`node ${mainScript} ${inFile}`, {
encoding: 'utf8',
timeout: 10000,
}).trim();
if (actualOutput === expectedOutput) {
passedTests++;
console.log(chalk.green.bold(`✅ Success on test ${subdir}`));
console.log(chalk.dim('Input:'));
console.log(chalk.gray('```'));
console.log(chalk.dim(inputContent));
console.log(chalk.gray('```'));
console.log(chalk.dim('Result:'));
console.log(chalk.gray('```'));
console.log(chalk.green(actualOutput));
console.log(chalk.gray('```\n'));
} else {
console.log(chalk.red.bold(`❌ Failed on test ${subdir}`));
console.log(chalk.dim('Input:'));
console.log(chalk.gray('```'));
console.log(chalk.dim(inputContent));
console.log(chalk.gray('```'));
displayDiff(expectedOutput, actualOutput);
console.log(chalk.dim('\n📋 Full outputs for reference:'));
console.log(chalk.yellow('Expected:'));
console.log(chalk.gray('```'));
console.log(chalk.yellow(expectedOutput));
console.log(chalk.gray('```'));
console.log(chalk.red('Actual:'));
console.log(chalk.gray('```'));
console.log(chalk.red(actualOutput));
console.log(chalk.gray('```\n'));
}
} catch (error) {
console.log(chalk.red.bold(`❌ Failed on test ${subdir}`));
console.log(chalk.dim('Input:'));
console.log(chalk.gray('```'));
const inputContent = fs.readFileSync(inFile, 'utf8');
console.log(chalk.dim(inputContent));
console.log(chalk.gray('```'));
console.log(chalk.dim('Expected:'));
console.log(chalk.gray('```'));
const expectedOutput = fs.readFileSync(outFile, 'utf8');
console.log(chalk.yellow(expectedOutput));
console.log(chalk.gray('```'));
console.log(chalk.red.bold('💥 Error:'));
console.log(chalk.gray('```'));
console.log(chalk.red(error.message));
console.log(chalk.gray('```\n'));
}
}
console.log('═'.repeat(60));
const successRate = Math.round((passedTests / totalTests) * 100);
const summaryColor =
passedTests === totalTests
? chalk.green.bold
: successRate >= 50
? chalk.yellow.bold
: chalk.red.bold;
console.log(summaryColor(`📊 Test Summary: ${passedTests}/${totalTests} passed (${successRate}%)`));
console.log('═'.repeat(60));
} catch (error) {
console.error(chalk.red.bold('💥 Error reading test directory:'), chalk.red(error.message));
process.exit(1);
}
}
runTests();