|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as url from "url"; |
| 4 | + |
| 5 | +const functionsWithSingleCase = [ |
| 6 | + ["id :: a -> a", "id x = x"], |
| 7 | + ["const :: a -> b -> a", "const x y = x"], |
| 8 | + ["fst :: (a, b) -> a", "fst (x, y) = x"], |
| 9 | +]; |
| 10 | + |
| 11 | +const functionsWithMultipleCases = [ |
| 12 | + [ |
| 13 | + "fib :: Integer -> Integer", |
| 14 | + "fib 0 = 0", |
| 15 | + "fib 1 = 1", |
| 16 | + "fib n = fib (n-1) + fib (n-2)", |
| 17 | + ], |
| 18 | + [ |
| 19 | + "map :: (a -> b) -> [a] -> [b]", |
| 20 | + "map f [] = []", |
| 21 | + "map f (x:xs) = f x : map f xs", |
| 22 | + ], |
| 23 | + ["not :: Bool -> Bool", "not True = False", "not False = True"], |
| 24 | +]; |
| 25 | + |
| 26 | +const nonFunctions = [ |
| 27 | + ["type Point = (Double, Double)"], |
| 28 | + ["data Maybe a = Nothing | Just a"], |
| 29 | +]; |
| 30 | + |
| 31 | +interface FunctionParameters { |
| 32 | + singleCase: boolean; |
| 33 | + signature: boolean; |
| 34 | +} |
| 35 | + |
| 36 | +function* functionParameterCases(): Generator<FunctionParameters, void, void> { |
| 37 | + for (const singleCase of [true, false]) { |
| 38 | + for (const signature of [true, false]) { |
| 39 | + yield { singleCase, signature }; |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +type DelimiterParameters = |
| 45 | + | { kind: "anchor" } |
| 46 | + | ({ kind: "function" } & FunctionParameters) |
| 47 | + | { kind: "otherwise" }; |
| 48 | + |
| 49 | +const delimiterParameterCases: DelimiterParameters[] = [ |
| 50 | + { kind: "anchor" }, |
| 51 | + ...[...functionParameterCases()].map( |
| 52 | + (functionParameters: FunctionParameters): DelimiterParameters => { |
| 53 | + return { kind: "function", ...functionParameters }; |
| 54 | + }, |
| 55 | + ), |
| 56 | + { kind: "otherwise" }, |
| 57 | +]; |
| 58 | + |
| 59 | +interface TestParameters { |
| 60 | + startDelimiterParameters: DelimiterParameters; |
| 61 | + functionParameters: FunctionParameters; |
| 62 | + endDelimiterParameters: DelimiterParameters; |
| 63 | +} |
| 64 | + |
| 65 | +function* testParameterCases(): Generator<TestParameters, void, void> { |
| 66 | + for (const startDelimiterParameters of delimiterParameterCases) { |
| 67 | + for (const functionParameters of functionParameterCases()) { |
| 68 | + for (const endDelimiterParameters of delimiterParameterCases) { |
| 69 | + yield { |
| 70 | + startDelimiterParameters, |
| 71 | + functionParameters, |
| 72 | + endDelimiterParameters, |
| 73 | + }; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +function renderFunction( |
| 80 | + [functionSignature, ...functionBody]: string[], |
| 81 | + { signature }: FunctionParameters, |
| 82 | +): string { |
| 83 | + if (signature) { |
| 84 | + return [functionSignature, ...functionBody].join("\n"); |
| 85 | + } else { |
| 86 | + return functionBody.join("\n"); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function* testCases(): Generator<string, void, void> { |
| 91 | + for (const testParameters of testParameterCases()) { |
| 92 | + const functionsWithSingleCaseIter = functionsWithSingleCase.values(); |
| 93 | + const functionsWithMultipleCasesIter = functionsWithMultipleCases.values(); |
| 94 | + const nonFunctionsIter = nonFunctions.values(); |
| 95 | + // eslint-disable-next-line no-inner-declarations |
| 96 | + function generateFunction(functionParameters: FunctionParameters): string { |
| 97 | + if (functionParameters.singleCase) { |
| 98 | + return renderFunction( |
| 99 | + functionsWithSingleCaseIter.next().value, |
| 100 | + functionParameters, |
| 101 | + ); |
| 102 | + } else { |
| 103 | + return renderFunction( |
| 104 | + functionsWithMultipleCasesIter.next().value, |
| 105 | + functionParameters, |
| 106 | + ); |
| 107 | + } |
| 108 | + } |
| 109 | + const testCase: string[] = []; |
| 110 | + if (testParameters.startDelimiterParameters.kind === "function") { |
| 111 | + testCase.push(generateFunction(testParameters.startDelimiterParameters)); |
| 112 | + } else if (testParameters.startDelimiterParameters.kind === "otherwise") { |
| 113 | + testCase.push(nonFunctionsIter.next().value.join("\n")); |
| 114 | + } |
| 115 | + testCase.push(generateFunction(testParameters.functionParameters)); |
| 116 | + if (testParameters.endDelimiterParameters.kind === "function") { |
| 117 | + testCase.push(generateFunction(testParameters.endDelimiterParameters)); |
| 118 | + } else if (testParameters.endDelimiterParameters.kind === "otherwise") { |
| 119 | + testCase.push(nonFunctionsIter.next().value.join("\n")); |
| 120 | + } |
| 121 | + testCase.push("---"); |
| 122 | + yield testCase.join("\n\n"); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +const scopeTypes = [ |
| 127 | + "branch.match.iteration", |
| 128 | + "name.function", |
| 129 | + "namedFunction", |
| 130 | + "functionName", |
| 131 | +]; |
| 132 | + |
| 133 | +function generateTestsFor(scopeType: string): void { |
| 134 | + const dirname = path.join( |
| 135 | + "src", |
| 136 | + "suite", |
| 137 | + "fixtures", |
| 138 | + "scopes", |
| 139 | + "haskell", |
| 140 | + "generated", |
| 141 | + scopeType, |
| 142 | + ); |
| 143 | + fs.mkdirSync(dirname, { recursive: true }); |
| 144 | + |
| 145 | + let index: number = 1; |
| 146 | + for (const testCase of testCases()) { |
| 147 | + fs.writeFileSync( |
| 148 | + path.join(dirname, `./${scopeType}${index}.scope`), |
| 149 | + testCase, |
| 150 | + { encoding: "utf-8" }, |
| 151 | + ); |
| 152 | + index++; |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +for (const scopeType of scopeTypes) { |
| 157 | + generateTestsFor(scopeType); |
| 158 | +} |
0 commit comments