aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2023-01-13 19:40:15 +0530
committerAkshay Nair <phenax5@gmail.com>2023-01-13 19:59:56 +0530
commit42f8c401dc9519bc8b2d386ce9eda072144a46d0 (patch)
treee886538eccbd47beaaf27deff69267bd8c7dd67d
parent8ba316461d2dc0a1372af16836ce14ceabc2bf4f (diff)
downloadts-types-lang-42f8c401dc9519bc8b2d386ce9eda072144a46d0.tar.gz
ts-types-lang-42f8c401dc9519bc8b2d386ce9eda072144a46d0.zip
feat: refactors test runner
-rw-r--r--examples/test-runner.ts50
-rw-r--r--src/eval-env/builtins.ts1
-rw-r--r--src/eval-env/test.ts13
-rw-r--r--src/eval.ts19
-rw-r--r--src/index.ts3
-rw-r--r--src/util.ts11
-rw-r--r--stdlib/test.ts1
7 files changed, 57 insertions, 41 deletions
diff --git a/examples/test-runner.ts b/examples/test-runner.ts
index 3302fbb..081dede 100644
--- a/examples/test-runner.ts
+++ b/examples/test-runner.ts
@@ -1,30 +1,40 @@
-import { Do, Kind1 } from '../stdlib/effect'
-import { Print, PutStringLn } from '../stdlib/stdio'
+import { Do } from '../stdlib/effect'
+import { PutStringLn } from '../stdlib/stdio'
import { SetEvalEnvironment } from '../stdlib/sys'
import { Test, Assert } from '../stdlib/test'
import { Equals, Not } from '../stdlib/util'
-interface PrintK extends Kind1 {
- return: Print<this['input']>
-}
+type testSomeStuff = Do<[
+ PutStringLn<"Some Stuff">,
-export type main = [
- SetEvalEnvironment<'test.node'>,
-
- PutStringLn<"Running tests...">,
+ Test<"should do some stuff", [
+ Assert<Equals<1, 1>>,
+ Assert<Not<Equals<2, 1>>>,
+ ]>,
- Do<[
- Test<"should do some stuff", [
- Assert<Equals<1, 2>>,
- Assert<Not<Equals<2, 1>>>,
- ]>,
+ Test<"bing bong, bing bing bong", [
+ Assert<Equals<1, 1>>,
+ ]>,
+]>
- Test<"hello world", [
- Assert<Equals<1, 1>>,
- ]>,
+type testSomeMoreStuff = Do<[
+ PutStringLn<"Other stuff?">,
- Test<"should do some other stuff", [
- Assert<Equals<1, 1>>,
- ]>,
+ Test<"should do some other stuff", [
+ Assert<Equals<[1, 2, 3], [1, 2, 3]>>,
]>,
+
+ // Test<"this'll fail fo sho", [
+ // Assert<Equals<[1, 2, 3], [4, 5, 6]>>,
+ // ]>,
+]>
+
+export type main = [
+ // Update env to node with test helpers
+ SetEvalEnvironment<'test.node'>,
+
+ PutStringLn<"=== Running tests... ===\n">,
+
+ testSomeStuff,
+ testSomeMoreStuff,
]
diff --git a/src/eval-env/builtins.ts b/src/eval-env/builtins.ts
index 3adde06..f9c4ce8 100644
--- a/src/eval-env/builtins.ts
+++ b/src/eval-env/builtins.ts
@@ -1,5 +1,6 @@
import { Type } from 'ts-morph'
import { Ctx } from "../types"
+import { evalList } from '../util'
export default (ctx: Ctx, args: Type[]) => ({
SetEvalEnvironment: async () => {
diff --git a/src/eval-env/test.ts b/src/eval-env/test.ts
index c394752..eddf366 100644
--- a/src/eval-env/test.ts
+++ b/src/eval-env/test.ts
@@ -1,31 +1,30 @@
import { Type } from 'ts-morph'
import { Ctx } from '../types'
-export const cleanup = () => {}
-
export default (ctx: Ctx, args: Type[]) => ({
Test: async () => {
const [msg, effs] = args
- process.stdout.write(` - ${ctx.getTypeValue(msg)}`)
+ process.stdout.write(` - ${ctx.getTypeValue(msg)} `)
try {
for (const eff of effs?.getTupleElements() ?? []) {
await ctx.evaluateType(ctx, eff)
}
- console.log(' [✓]')
+ console.log('[✓]')
} catch(e) {
- console.log(' [TEST FAILED]')
+ console.log('[TEST FAILED]')
throw e
}
+
return []
},
Assert: async () => {
- const [b] = args
- if (!ctx.getTypeValue(b)) {
+ if (!ctx.getTypeValue(args[0])) {
throw new Error('Assertion failed')
}
+
return []
},
})
diff --git a/src/eval.ts b/src/eval.ts
index fdffed7..2b93e49 100644
--- a/src/eval.ts
+++ b/src/eval.ts
@@ -6,7 +6,7 @@ import * as builtins from './eval-env/builtins'
type EffDefn = {
default: (ctx: Ctx, args: Type[]) => Record<string, () => Promise<string[]>>,
- cleanup: () => void,
+ cleanup?: () => void,
}
const mergeEffDefns = (a: EffDefn, b: EffDefn): EffDefn => ({
default: (ctx: Ctx, args: Type[]) => ({
@@ -14,13 +14,13 @@ const mergeEffDefns = (a: EffDefn, b: EffDefn): EffDefn => ({
...b.default(ctx, args),
}),
cleanup: () => {
- a.cleanup()
- b.cleanup()
+ a.cleanup?.()
+ b.cleanup?.()
},
})
-const cleanupActions = new Set<() => void>()
-export const cleanup = () => cleanupActions.forEach(f => f())
+const cleanupActions = new Set<undefined | (() => void)>()
+export const cleanup = () => cleanupActions.forEach(f => f?.())
let prevEnv: string
@@ -28,6 +28,7 @@ export const evaluateType = async (
ctx: Ctx,
effTyp: Type
): Promise<string[]> => {
+ // TODO: base type check
const name = effTyp.getSymbol()?.getName()
const args = effTyp.getTypeArguments()
@@ -67,11 +68,3 @@ export const evaluateType = async (
},
})
}
-
-export const evalList = async (ctx: Ctx, effectTyps: Type[]) => {
- const effectResults: string[] = []
- for (const item of effectTyps ?? []) {
- effectResults.push(...(await evaluateType(ctx, item)))
- }
- return effectResults
-}
diff --git a/src/index.ts b/src/index.ts
index 01420c8..a144c57 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,6 +1,7 @@
import { program } from 'commander'
import { createContext } from './context'
-import { cleanup, evalList } from './eval'
+import { cleanup } from './eval'
+import { evalList } from './util'
const main = () => {
program
diff --git a/src/util.ts b/src/util.ts
index 136815e..13bdb35 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -1,4 +1,15 @@
+import { Type } from "ts-morph"
+import { Ctx } from "./types"
+
export const match = <K extends string, R>(
k: K | undefined,
pattern: { [key in K | '_']: () => R }
) => (k && pattern[k] ? pattern[k]() : pattern._())
+
+export const evalList = async (ctx: Ctx, effectTyps: Type[]) => {
+ const effectResults: string[] = []
+ for (const item of effectTyps ?? []) {
+ effectResults.push(...(await ctx.evaluateType(ctx, item)))
+ }
+ return effectResults
+}
diff --git a/stdlib/test.ts b/stdlib/test.ts
index 49c6e0e..7497f7b 100644
--- a/stdlib/test.ts
+++ b/stdlib/test.ts
@@ -2,6 +2,7 @@ import { Effect } from "./effect";
export interface Config {
compileTimeTestFailures: false
+ stopAtFailure: true // TODO: stopAtFailure
}
type Assertion = Config['compileTimeTestFailures'] extends true ? true : boolean