Skip to content

Commit cd45ca2

Browse files
committed
updated logic
1 parent 19a0bc1 commit cd45ca2

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

src/extension.ts

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -116,43 +116,14 @@ export function activate(context: vscode.ExtensionContext) {
116116
}
117117

118118
const boilerplate = language === 'python'
119-
? `import os
120-
121-
def run_test_case(test_case_number, function):
122-
# Determine the base directory dynamically\n
123-
base_directory = os.path.dirname(os.path.dirname(__file__))\n
124-
file_path = os.path.join(base_directory, 'test_cases', f"input_{test_case_number}.txt")\n
125-
126-
try:\n
127-
# Read the input file\n
128-
with open(file_path, "r") as file:\n
129-
content = file.read().strip().splitlines() # Read file and split lines\n
130-
131-
# Dynamically parse all lines as arguments\n
132-
args = [eval(line.strip()) for line in content] # Parse each line in the file\n
133-
134-
# Call the provided function with all parsed arguments\n
135-
result = function(*args) # Unpack the arguments dynamically\n
136-
print(result) # Output the result\n
137-
138-
except FileNotFoundError:\n
139-
print(f"Error: File not found at {file_path}. Check the file path and try again.")\n
140-
except ValueError as e:\n
141-
print(f"Error: {e}")\n
142-
except SyntaxError as e:\n
143-
print(f"Error: Check your input file format. {e}")\n
144-
except Exception as e:\n
145-
print(f"Unexpected Error: {e}")\n
146-
147-
148-
# WRITE YOUR FUNCTION HERE
149-
#-------------------------------------------------------------------------------\n
150-
151-
#-------------------------------------------------------------------------------\n
152-
153-
#run_test_case(ENTER THE TESTCASE NUMBER, ENTER THE FUNCTION NAME)\n
154-
# EXAMPLE:\n
155-
run_test_case() # Replace with the relevant test case number and function name\n`
119+
? `import os; run_test_case=lambda n,f: print((lambda p: (lambda a: f(*a))(eval(l.strip()) for l in open(p).read().strip().splitlines()) if os.path.isfile(p) else f"Error: File not found at {p}. Check the file path and try again.")((lambda d: os.path.join(d, 'test_cases', f"input_{n}.txt"))(os.path.dirname(os.path.dirname(file)))))
120+
#WRITE YOUR CODE LOGIC HERE
121+
122+
#---------------------------------------------------------------------------------------
123+
# Example usage
124+
# Provide the test case number and function name
125+
#run_test_case(TEST_CASE_NUMBER, FUNCTION_NAME);
126+
run_test_case()`
156127

157128

158129
: `#include <bits/stdc++.h>

src/fetchTestCases.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ import * as fs from 'fs';
33
import * as path from 'path';
44
import * as vscode from 'vscode';
55

6-
const RETRY_DELAY = 100; // 0.1 seconds
6+
interface TestCase {
7+
input: string | null;
8+
output: string | null;
9+
}
10+
11+
const RETRY_DELAY = 3000; // 0.1 seconds
712
const MAX_RETRIES = 5; // Maximum retry attempts
813

914
export async function fetchTestCases(url: string): Promise<void> {
1015
const browser = await puppeteer.launch({ headless: false }); // Run in visible mode for debugging
1116
const page = await browser.newPage();
1217

1318
let attempts = 0;
14-
let testCases = [];
19+
let testCases: TestCase[] = [];
1520

1621
const baseDirectory = path.join(__dirname, 'dist', 'test_cases');
1722

@@ -83,7 +88,6 @@ export async function fetchTestCases(url: string): Promise<void> {
8388
console.log('Failed to fetch test cases after maximum retries.');
8489
}
8590
}
86-
8791
async function fetchTestCasesWithRetry(page: puppeteer.Page, url: string): Promise<any[]> {
8892
// Load cookies if available
8993
await loadCookies(page);
@@ -96,24 +100,50 @@ async function fetchTestCasesWithRetry(page: puppeteer.Page, url: string): Promi
96100
}
97101

98102
console.log('Waiting for test cases to load...');
103+
let testCases: TestCase[] = [];
99104
try {
100105
await page.waitForSelector('pre', { timeout: 20000 }); // Wait up to 20 seconds for the <pre> tags
106+
// Extract test cases
107+
testCases = await page.$$eval('pre', (elements) => {
108+
return elements.map(pre => {
109+
const inputMatch = pre.innerText.match(/Input:\s*(.+)/);
110+
const outputMatch = pre.innerText.match(/Output:\s*(.+)/);
111+
return {
112+
input: inputMatch ? inputMatch[1].trim() : null,
113+
output: outputMatch ? outputMatch[1].trim() : null,
114+
};
115+
});
116+
});
101117
} catch (error) {
102-
console.error('Failed to find <pre> tags in time:', error);
103-
return []; // Return empty array if fetching fails
118+
console.error('Failed to extract test cases using <pre> selector:', error);
104119
}
105120

106-
// Extract test cases
107-
const testCases = await page.$$eval('pre', (elements) => {
108-
return elements.map(pre => {
109-
const inputMatch = pre.innerText.match(/Input:\s*(.+)/);
110-
const outputMatch = pre.innerText.match(/Output:\s*(.+)/);
111-
return {
112-
input: inputMatch ? inputMatch[1].trim() : null,
113-
output: outputMatch ? outputMatch[1].trim() : null,
114-
};
115-
});
116-
});
121+
if (testCases.length === 0) {
122+
try {
123+
// Fallback to the .example-block structure
124+
console.log('Trying to extract test cases using .example-block structure...');
125+
await page.waitForSelector('.example-block', { timeout: 5000 }); // Adjust timeout as needed
126+
testCases = await page.$$eval('.example-block', (blocks) => {
127+
return blocks.map(block => {
128+
const inputElement = block.querySelector('strong:contains("Input:") + span.example-io');
129+
const outputElement = block.querySelector('strong:contains("Output:") + span.example-io');
130+
131+
return {
132+
input: inputElement ? (inputElement as HTMLElement).innerText.trim() : null,
133+
output: outputElement ? (outputElement as HTMLElement).innerText.trim() : null,
134+
};
135+
});
136+
});
137+
138+
if (testCases.length > 0) {
139+
console.log('Test cases successfully extracted using .example-block structure.');
140+
} else {
141+
console.log('No test cases found using .example-block structure.');
142+
}
143+
} catch (error) {
144+
console.error('Failed to extract test cases using .example-block structure:', error);
145+
}
146+
}
117147

118148
return testCases;
119149
}

0 commit comments

Comments
 (0)