Skip to content

Commit

Permalink
fix: abandon adding to and remove be in behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Jun 4, 2024
1 parent 9c6edc9 commit 87aa28c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { parse, transform } from "jsr:@chiezo/bddoc";

### `transform`

To transform tests to documentation:
Transform tests to documentation:

```typescript
assertEquals(
Expand All @@ -30,7 +30,7 @@ assertEquals(

### `parse`

To parse `describe` and `it`:
Parse `describe` and `it` from tests:

```typescript
const describes = await parse(
Expand Down
6 changes: 3 additions & 3 deletions fixtures/output.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
### `foo`

To do something:
Do something:

```typescript
assertEquals("foo", "foo");
```

### `bar`

To do something else:
Do something else:

```typescript
assertEquals("bar", "bar");
```

Not to do something more:
Won't do something more:

```typescript
assertNotEquals("bar", "baz");
Expand Down
2 changes: 1 addition & 1 deletion src/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, it } from "@std/testing/bdd";
import parse from "./parse.ts";

describe("parse", () => {
it("should parse `describe` and `it`", async () => {
it("should parse `describe` and `it` from tests", async () => {
const describes = await parse(
await Deno.readTextFile("./fixtures/input.ts"),
);
Expand Down
25 changes: 10 additions & 15 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,20 @@ export interface StringifyOptions {
format?: (behavior: string) => string;
}

const toTitleCase = (word: string) =>
word[0].toLocaleUpperCase() + word.slice(1);

export const defaultFormatter = (behavior: string) => {
let words = behavior.split(" ").filter((word) => word.trim().length);
if (!words.length) {
if (behavior.startsWith("should")) {
behavior = behavior.slice(6).trimStart();
} else {
return behavior;
}
const first = () => words[0].toLocaleLowerCase();
if (first() === "should") {
words.shift();
}
if (first() === "shouldn't" || first() === "not") {
words = ["not", "to", ...words.slice(1)];
} else {
words = ["to", ...words];
if (behavior.startsWith("be ")) {
behavior = behavior.slice(3);
} else if (behavior.startsWith("n't be ") || behavior.startsWith("not be ")) {
behavior = "not " + behavior.slice(7);
} else if (behavior.startsWith("n't ") || behavior.startsWith("not ")) {
behavior = "won't " + behavior.slice(4);
}
words[0] = toTitleCase(first());
return words.join(" ");
return behavior[0].toLocaleUpperCase() + behavior.slice(1);
};

export function stringifyDescribe(
Expand Down
50 changes: 39 additions & 11 deletions src/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,52 @@ describe("defaultFormatter", () => {
);
});

it("should format a positive sentence", () => {
it("should do nothing to a string without `should`", () => {
assertEquals(
defaultFormatter("do something"),
"do something",
);
});

it("should replace `should` with `to`", () => {
assertEquals(
defaultFormatter("should do something"),
"To do something",
"Do something",
);
});

it("should format a negative sentence", () => {
it("should replace `should not do` with `Won't`", () => {
assertEquals(
defaultFormatter("should not do something"),
"Not to do something",
"Won't do something",
);
});

it("should format a conjugated negative sentence", () => {
it("should replace `shouldn't do` with `Won't do`", () => {
assertEquals(
defaultFormatter("shouldn't do something"),
"Not to do something",
"Won't do something",
);
});

it("should remove `should be`", () => {
assertEquals(
defaultFormatter("should be testable"),
"Testable",
);
});

it("should replace `should not be` with `Not`", () => {
assertEquals(
defaultFormatter("should not be testable"),
"Not testable",
);
});

it("should replace `shouldn't be` with `Not`", () => {
assertEquals(
defaultFormatter("shouldn't be testable"),
"Not testable",
);
});
});
Expand All @@ -48,7 +76,7 @@ describe("stringifyIt", () => {
`,
}),
dedent`
To do something:
Do something:
\`\`\`typescript
console.log("foo");
Expand Down Expand Up @@ -84,14 +112,14 @@ describe("stringifyDescribe", () => {
dedent`
### \`foo\`
To do something:
Do something:
\`\`\`typescript
console.log("foo");
assert("foo" !== "bar");
\`\`\`
Not to do something else:
Won't do something else:
\`\`\`typescript
console.log("bar");
Expand Down Expand Up @@ -134,7 +162,7 @@ describe("stringify", () => {
dedent`
### \`foo\`
To do something:
Do something:
\`\`\`typescript
console.log("foo");
Expand All @@ -143,7 +171,7 @@ describe("stringify", () => {
### \`bar\`
Not to do something else:
Won't do something else:
\`\`\`typescript
console.log("bar");
Expand Down

0 comments on commit 87aa28c

Please sign in to comment.