Skip to content
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

remove indentN #178

Merged
merged 1 commit into from
Jan 4, 2024
Merged
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
67 changes: 31 additions & 36 deletions lib/doctest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,21 @@ import {Line} from './Line.js';
import require from './require.js';


// indentN :: Integer -> String -> String
const indentN = n => s => s.replace (/^(?!$)/gm, ' '.repeat (n));

// formatLines :: Integer -> NonEmpty (Array Line) -> String
// formatLines :: String -> NonEmpty (Array Line) -> String
const formatLines = indent => lines => (
lines
.map (line => `{number: ${show (line.number)}, text: ${show (line.text)}},`)
.join ('\n' + ' '.repeat (indent))
.join (`\n${indent}`)
);

// formatInput :: Integer -> NonEmpty (Array Line) -> String
// formatInput :: String -> NonEmpty (Array Line) -> String
const formatInput = indent => lines => (
lines
.map (line => line.text.replace (/^\s*([>]|[.]+)[ ]?/, ''))
.join ('\n' + ' '.repeat (indent))
.join (`\n${indent}`)
);

// formatOutput :: Integer -> NonEmpty (Array Line) -> String
// formatOutput :: String -> NonEmpty (Array Line) -> String
const formatOutput = indent => lines => {
const [head, ...tail] = lines.map (line => line.text.replace (/^\s*/, ''));
const match = /^![ ]?([^:]*)(?::[ ]?(.*))?$/.exec (head);
Expand All @@ -50,11 +47,11 @@ const formatOutput = indent => lines => {
` ${match == null ? head : `new ${match[1]}(${show (match[2] ?? '')})`}`,
...(tail.map (text => ' ' + text.replace (/^[.]+[ ]?/, ''))),
')',
].join ('\n' + ' '.repeat (indent));
].join (`\n${indent}`);
};

const wrapJs = sourceType => test => {
const source = formatInput (0) (test.input.lines);
const wrapJs = sourceType => ({input, output}) => {
const source = formatInput ('') (input.lines);
const ast = acorn.parse (
source.startsWith ('{') && source.endsWith ('}') ? `(${source})` : source,
{ecmaVersion: 2023, sourceType}
Expand All @@ -66,43 +63,43 @@ const wrapJs = sourceType => test => {
__doctest.enqueue({
input: {
lines: [
${formatLines (6) (test.input.lines)}
${formatLines (' ') (input.lines)}
],
thunk: () => {
return (
${formatInput (8) (test.input.lines)}
${formatInput (' ') (input.lines)}
);
},
},
output: ${test.output && `{
output: ${output && `{
lines: [
${formatLines (6) (test.output.lines)}
${formatLines (' ') (output.lines)}
],
thunk: () => {
${formatOutput (6) (test.output.lines)};
${formatOutput (' ') (output.lines)};
},
}`},
});
`;
};

const wrapCoffee = test => `
__doctest.enqueue {
input: {
lines: [
${formatLines (6) (test.input.lines)}
]
thunk: ->
${formatInput (6) (test.input.lines)}
}
output: ${test.output && `{
lines: [
${formatLines (6) (test.output.lines)}
]
thunk: ->
${formatOutput (6) (test.output.lines)}
}`}
}
const wrapCoffee = ({indent, input, output}) => `
${indent}__doctest.enqueue {
${indent} input: {
${indent} lines: [
${indent} ${formatLines (`${indent} `) (input.lines)}
${indent} ]
${indent} thunk: ->
${indent} ${formatInput (`${indent} `) (input.lines)}
${indent} }
${indent} output: ${output && `{
${indent} lines: [
${indent} ${formatLines (`${indent} `) (output.lines)}
${indent} ]
${indent} thunk: ->
${indent} ${formatOutput (`${indent} `) (output.lines)}
${indent} }`}
${indent}}
`;

// contiguous :: Line -> NonEmpty (Array Line) -> Boolean
Expand Down Expand Up @@ -381,9 +378,7 @@ const rewriteCoffee = ({
return accum;
}, {state: openingDelimiter == null ? 'open' : 'closed', tests: []});

return result.tests.map (
test => indentN (test.indent.length) (wrapCoffee (test))
);
return result.tests.map (wrapCoffee);
});

return CoffeeScript.compile (
Expand Down