fun icon indicating copy to clipboard operation
fun copied to clipboard

Doctest are not actually tested. Only type-checked.

Open pixeleet opened this issue 3 years ago • 3 comments

I assume wrong and the examples we provide are not actually running when we call deno test --doc Therefore the following typechecks but is wrong:

/**
 * Fold away the inner Either from the `TaskEither` leaving us with the
 * result of our computation in the form of a `Task`
 *
 * ```ts
 * import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
 * import * as TE from "./task_either.ts";
 * import * as E from "./either.ts";
 * import * as T from "./task.ts";
 * import { flow, identity } from "./fns.ts";
 *
 * const hello = flow(
 *  TE.fold(() => 'World', identity),
 *  T.map(name => `Hello ${name}!`),
 * );
 *
 * assertEquals(await hello(TE.right('Functional!'))(), E.right("Hello Functional!!"));
 * assertEquals(await hello(TE.left(Error))(), E.right("Hello World!"));
 * ```
 */
export function fold<L, R, B>(
  onLeft: (left: L) => B,
  onRight: (right: R) => B,
): (ta: TaskEither<L, R>) => Task<B> {
  return (ta) => () => ta().then(eitherFold<L, R, B>(onLeft, onRight));
}

Whereas the actually valid example code is:

/**
 * Fold away the inner Either from the `TaskEither` leaving us with the
 * result of our computation in the form of a `Task`
 *
 * ```ts
 * import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
 * import * as TE from "./task_either.ts";
 * import * as T from "./task.ts";
 * import { flow, identity } from "./fns.ts";
 * 
 * const hello = flow(
 *   TE.fold(() => "World", identity),
 *   T.map((name) => `Hello ${name}!`),
 * );
 * 
 * assertEquals(await hello(TE.right("Functional!"))(), "Hello Functional!!");
 * assertEquals(await hello(TE.left(Error))(), "Hello World!");
 * ```
 */
export function fold<L, R, B>(
  onLeft: (left: L) => B,
  onRight: (right: R) => B,
): (ta: TaskEither<L, R>) => Task<B> {
  return (ta) => () => ta().then(eitherFold<L, R, B>(onLeft, onRight));
}

pixeleet avatar Mar 20 '22 17:03 pixeleet

Type checking is good enough, I think. Ultimately, test coverage should all be under ./testing anyway. It's just helpful to know our examples are borked at the type level.

baetheus avatar Mar 20 '22 17:03 baetheus

I feel bad about having submitted a non working example. We should not be able to provide examples to consumers of the library that don't actually work.

Haven't validated the idea yet, just from the top of my head, it would be interesting to look at what happens internally when we run deno test --doc maybe we could submit a PR on the deno side to add an --exec flag that also tries to execute the example code?

pixeleet avatar Mar 20 '22 17:03 pixeleet

Looks like there is some movement on this in https://github.com/denoland/deno/issues/4716.

baetheus avatar Mar 20 '22 17:03 baetheus

Closing this since it's an enhancement in deno.

baetheus avatar Jul 05 '23 18:07 baetheus