Skip to content

Pull request fix quick check #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 77 additions & 31 deletions src/helpers/testCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,69 @@ function shell(command, options) {
});
}

function removeMultilineComments(data) {
return data.replace(/{-[\s\S]*?-}/g, (comment) => {
return comment.replace(/\S/g, ' ');
});
}

function removeMainFunction(data) {
const dataArray = data.toString().split('\n');
const decl_regex = /^main\s*::.*$/m;
const def_regex = /^main\s*=.*$/m;
const comment_regex = /^\s*--.*$/m;
const other_regex = /^\S+.*$/m;
const dataArray = removeMultilineComments(data).split('\n');

let decl_start;
let decl_end;
let def_start;
let def_end;

let start;
let end;
for (let i = 0; i < dataArray.length; i++) {
if (dataArray[i].slice(0, 6) === 'main =') {
start = i;
end = i;
} else if (start !== undefined) {
if (dataArray[i] === '') {
break;
} else {
end++;
}
//main type signature
if (decl_regex.test(dataArray[i])) {
if (def_start !== undefined && def_end === undefined) {
def_end = i;
}
if (decl_start === undefined) {
decl_start = i;
}
}
//main definition
else if (def_regex.test(dataArray[i])) {
if (decl_start !== undefined && decl_end === undefined) {
decl_end = i;
}
if (def_start === undefined) {
def_start = i;
}
}
//skip comments
else if (comment_regex.test(dataArray[i])) {
//do nothing
}
//different expression/declaration
else if (other_regex.test(dataArray[i])) {
if (decl_start !== undefined && decl_end === undefined) {
decl_end = i;
}
if (def_start !== undefined && def_end === undefined) {
def_end = i;
}
}
}

if (decl_start !== undefined && decl_end === undefined) {
decl_end = dataArray.length;
}
if (def_start !== undefined && def_end === undefined) {
def_end = dataArray.length;
}

dataArray.splice(start, end - start + 1);
return dataArray.join('\n');
return dataArray.filter((value, index, arr) => {
return (decl_start === undefined || index < decl_start || index >= decl_end) &&
(def_start === undefined || index < def_start || index >= def_end);
}).join('\n');
}

export function testHaskellFile(filePath, stackWd) {
Expand All @@ -133,24 +176,27 @@ export function testHaskellFile(filePath, stackWd) {

// Not Stack project
if (stackWd === undefined) {
fs.createReadStream(filePath).pipe(fs.createWriteStream(newPath));
fs.readFile(newPath, 'utf-8', (err, data) => {
if (err) reject(err);

const newValue = '{-# LANGUAGE TemplateHaskell #-}\nimport Test.QuickCheck.All\n' + removeMainFunction(data)
+ '\nreturn []\nrunTests = $quickCheckAll\nmain = runTests';

fs.writeFile(newPath, newValue, 'utf-8', err => {
let writer = fs.createWriteStream(newPath);
fs.createReadStream(filePath).pipe(writer);
writer.once('finish', () => {
fs.readFile(newPath, 'utf-8', (err, data) => {
if (err) reject(err);
console.log('QuickCheking...');

shell(`stack runhaskell "${newPath}"`, {}).then(std => {
console.log(std[0]);
fs.unlinkSync(newPath);
resolve(parseStdout(std[0]));
}).catch(error => {
fs.unlinkSync(newPath);
reject(error);

const newValue = '{-# LANGUAGE TemplateHaskell #-}\nimport Test.QuickCheck.All\n' + removeMainFunction(data)
+ '\nreturn []\nrunTests = $quickCheckAll\nmain = runTests';

fs.writeFile(newPath, newValue, 'utf-8', err => {
if (err) reject(err);
console.log('QuickCheking...');

shell(`stack runhaskell "${newPath}"`, {}).then(std => {
console.log(std[0]);
fs.unlinkSync(newPath);
resolve(parseStdout(std[0]));
}).catch(error => {
fs.unlinkSync(newPath);
reject(error);
});
});
});
});
Expand Down