Skip to content

Commit

Permalink
version v1.0.5 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
buttercubz authored Oct 31, 2020
2 parents 88e6d95 + 3be9542 commit 9c6c5a1
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 120 deletions.
63 changes: 32 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,45 @@ Merlin is a [Jest](https://jestjs.io/en/)-inspired testing framework for deno.

### Common Matchers

- `testEqual(label: string, config)`
- `testNotEqual(label: string, config)`
- `assertEqual(label: string, config)`
- `assertNotEqual(label: string, config)`
- `evalEquals(testEqual[])`
- `stringContains(label: string, config)`
- `arrayContains(label: string, config)`
- `stringIncludes(label: string, config)`
- `arrayIncludes(label: string, config)`
- `beNull(label: string, config)`
- `beFalsy(label: string, config)`
- `beTruthy(label: string, config)`

### All Matchers

- `testEqual(label: string, config)` Compare two values and throws an error if the expect and toBe are not equal
- `testNotEqual(label: string, config)` Compare two values and throws an error if the expect and notBe are equal
- `assertEqual(label: string, config)` Compare two values and throws an error if the expect and toBe are not equal
- `assertNotEqual(label: string, config)` Compare two values and throws an error if the expect and notBe are equal
- `evalEquals(testEqual[])` evaluate multiple equality tests in an array. If the data is not the same it throws an error
- `fetchEqual(label: string, config)` evaluate if two values are equal. If the request data is not the same as expected, it throws an error
- `arrayContains(label: string, config)` evaluates that the array contains an especific data. if the array does not contain the data it throws an error
- `stringContains(label: string, config)` evaluates if a string contains an especific word. if the string does not contain the word it throws an error
- `ArrayIncludes(label: string, config)` evaluates that the array contains an specific data. if the array does not contain the data it throws an error
- `stringIncludes(label: string, config)` evaluates if a string contains an specific word. if the string does not contain the word it throws an error
- `beNull(label: string, config)` evaluates if a data is null
- `beFalsy(label: string, config)` evaluates if a data is a falsy value
- `beTruthy(label: string, config)` evaluates if a data is a truthy value
- `isBigInt(label: string, config)` evaluates if a data is a bigInt value type
- `isZero(label: string, config)` evaluates if a data is a Zero
- `isNaN(label: string, config)` evaluates if a data is NaN value
- `sameLength(label: string, config)` evaluates if data has a especific length
- `testRegExp(label: string, config)` evaluates if a regular expression match
- `sameLength(label: string, config)` evaluates if data has a specific length
- `assertRegExp(label: string, config)` evaluates if a regular expression match
- `isFunction(label: string, config)` evaluates if a data is a function
- `isSymbol(label: string, config)` evaluates if a data is a symbol
- `isUndefined(label: string, config)` evaluates if a data is undefined
- `isString(label: string, config)` evaluates if a data is string
- `isNumber(label: string, config)` evaluates if a data is number
- `testSame(label: string, config)` evaluates if two values are strictly the same
- `testGreaterOrEqual(label: string, config)` evaluates whether the expected data is greater than or equal to another
- `testGreater(label: string, config)` evaluates whether the expected data is greater than another
- `testLess(label: string, config)` evaluates if the expected data is less than another
- `testLessOrEqual(label: string, config)` evaluates if the expected data is less than or equal to another
- `testInstanceOf(label: string, config)` evaluates that one object is an instance of another
- `testFloat(label: string, config)` evaluates if two decimal numbers are equal
- `testThrows(label: string, config)` expect it throws an error
- `testThrowsSync(label: string, config)` expect it throws an async error
- `assertSame(label: string, config)` evaluates if two values are strictly the same
- `assertGreaterOrEqual(label: string, config)` evaluates whether the expected data is greater than or equal to another
- `assertGreater(label: string, config)` evaluates whether the expected data is greater than another
- `assertLess(label: string, config)` evaluates if the expected data is less than another
- `assertLessOrEqual(label: string, config)` evaluates if the expected data is less than or equal to another
- `assertInstanceOf(label: string, config)` evaluates that one object is an instance of another
- `assertFloat(label: string, config)` evaluates if two decimal numbers are equal
- `assertThrows(label: string, config)` expect it throws an error
- `assertThrowsSync(label: string, config)` expect it throws an async error
- `haveProperty(label: string, config)` expect an object to contain the properties in its value

#### Statics
Expand Down Expand Up @@ -122,7 +122,7 @@ import { Merlin } from "https://deno.land/x/merlin/mod.ts";

const test = new Merlin();

test.testEqual("two plus two is four", {
test.assertEqual("two plus two is four", {
expect() {
return 2 + 2;
},
Expand Down Expand Up @@ -186,7 +186,7 @@ async function writeSomething(): Promise<string> {
return decoder.decode(Package);
}

test.testEqual("Leak resources test", {
test.assertEqual("Leak resources test", {
expect: async () => await writeSomething(),
toBe: () => "test",
only: true,
Expand Down Expand Up @@ -250,7 +250,7 @@ test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (13ms
`example.test.ts`

```typescript
test.testNotEqual("two plus two not is five", {
test.assertNotEqual("two plus two not is five", {
expect() {
return 2 + 2;
},
Expand Down Expand Up @@ -286,21 +286,21 @@ failures:
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (2ms)
```

## stringContains
## stringIncludes

`example.test.ts`

```typescript
test.stringContains("hello world contains orld", {
Contains: () => "orld",
test.stringIncludes("hello world contains world", {
Contains: () => "world",
value: () => "Hello World",
});
```

```sh
merlin start

test hello world contains orld ... ok (8ms)
test hello world contains world ... ok (8ms)

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
```
Expand Down Expand Up @@ -328,7 +328,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
### testRegExp

```typescript
test.testRegExp("regEx match", {
test.assertRegExp("regEx match", {
expect: () => "https://google.com",
toBe: () => new RegExp("^https?://[a-z.]+.com$"),
});
Expand All @@ -342,7 +342,7 @@ test regEx match ... ok (6ms)
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (342ms)
```

### Usin async code.
### Using async code.

you can use asynchronous code by adding `async` in `expect`, `toBe` and `value` functions.

Expand All @@ -351,7 +351,7 @@ example
```typescript
const test = new Merlin();

test.testEqual("get error 404", {
test.assertEqual("get error 404", {
async expect() {
const response = await fetch("https://deno.land/std/example/examples.ts");

Expand All @@ -365,7 +365,7 @@ test.testEqual("get error 404", {
});
```

> **Note**: all the methods of the merlin class support asyn function since they have top level await
> **Note**: all the methods of the merlin class support async function since they have top level await
![merlin gif](https://cdn.discordapp.com/attachments/656976424778989602/735287285519745114/mer.gif)

Expand Down Expand Up @@ -424,10 +424,11 @@ benchmark.runBench().then(benchmark.Result());
It has a table with the detailed values

```sh

▒▒▒▒▒▒▒▒ Benchmarking finished

┌───────────────────────────────────────────────────────────────────────────────────────────┐
🚀 Benchmark name: Sorting arrays
Benchmark name: Sorting array
├───────────────────────┬──────────────────────────────┬────────────────────────────────────┤
│ Total runs: 1000 │ Total time: 1099.6591 ms │ Avg time: 1.0997 ms │
├───────────────────────┼────────────────────┬─────────┴───────────┬────────────────────────┤
Expand Down
58 changes: 47 additions & 11 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@

import { colors } from "./imports/fmt.ts";

const VERSION = "v1.0.5";

async function main() {
const [command, ...args] = Deno.args;
let [command, ...args] = Deno.args;

args = args.includes("--coverage")
? Array.from(new Set(["--unstable", ...args]))
: [...args];

if (command === "start") {
if (["start", "test"].includes(command)) {
const process = Deno.run({
cmd: ["deno", "test", "--allow-hrtime", ...args],
});
Expand All @@ -23,18 +29,48 @@ async function main() {
throw new Error(colors.red("something went wrong running the test"))
.message;
}
} else if (command === "help") {
} else if (["help", "--help", "-h"].includes(command)) {
const info = [
colors.green("merlin test runner v1.0.3 🧪\n"),
colors.green("usage:"),
`merlin ${colors.yellow("test")} ...allow-flags\n`,
colors.green("example:"),
`merlin ${colors.yellow("test")} --allow-net\n`,
colors.green(`merlin cli test runner ${VERSION} 🧪\n`),

colors.green("\nusage:\n"),
` merlin ${colors.yellow("[start or test]")} ...allow-flags\n`,

colors.green("\nexample:\n"),
` merlin ${colors.yellow("start")} --allow-net --coverage\n`,

colors.green("\ncommands:\n"),
colors.yellow(
`--help, -h, help ${colors.white("show help info\n")}`
),
colors.yellow(
`--version, -v, version ${colors.white("show merlin cli version\n")}`
),

colors.green("\ntesting flags:\n"),
`${colors.yellow(" --coverage")} ${colors.white(
"show test coverage for your code"
)}\n`,
`${colors.yellow(" --filter")} ${colors.white(
"filter the tests that contain keywords"
)}\n`,
`${colors.yellow(" --failfast")} ${colors.white(
"If you have a long running test suite and wish for it to stop on the first failure"
)}\n`,

colors.green(
`\nDirectory arguments are expanded to all contained files matching the glob\n${
colors.red("{*_,*.,}test.{js,mjs,ts,jsx,tsx}:")
}\n`)
];

for (const log of info) {
console.log(log);
}
console.log(info.join(""));
} else if (["version", "--version", "-v"].includes(command)) {
console.log(
`${colors.green(
`merlin cli: ${colors.yellow(VERSION)}`
)} \n${colors.green(`Deno: ${colors.yellow(Deno.version.deno)}`)}`
);
} else {
throw new Error(
colors.red("this command was not found, try run: merlin help")
Expand Down
2 changes: 1 addition & 1 deletion egg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "merlin",
"description": "Testing and Benchmarking framework for deno 🧙‍♂️",
"version": "1.0.3",
"version": "1.0.5",
"entry": "./mod.ts",
"stable": true,
"unlisted": false,
Expand Down
6 changes: 3 additions & 3 deletions imports/deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
},
"testing": {
"url": "https://denopkg.com/crewdevio/Trex@proxy/proxy/files/testing.ts",
"hash": "eda35f0a9575fe5d7b91a6be8eaf497d098c1273a5fc87dc3ce2390ec55af78e"
"hash": "96b3fe1112509332447d1029818dce5eb56d75c3207884dcbbfdb5d577e649a4"
},
"pretty_benching": {
"url": "https://deno.land/x/pretty_benching@v0.2.4/mod.ts",
"hash": "3d19cceffabca677358769d5487d4ae576cf4cf76c7cccb1b354ac03e625ece7"
"url": "https://deno.land/x/pretty_benching@v0.3.0/mod.ts",
"hash": "0e6a7e6ffeb7da6facfb4faa17da9556f21aeeef14486fead85461fd065c1f6c"
}
}
}
2 changes: 1 addition & 1 deletion imports/pretty_benching.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/x/pretty_benching@v0.2.4/mod.ts";
export * from "https://deno.land/x/pretty_benching@v0.3.0/mod.ts";
58 changes: 51 additions & 7 deletions src/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
import {
prettyBenchmarkProgress,
prettyBenchmarkResult,
prettyBenchmarkDown,
} from "../imports/pretty_benching.ts";
import type { Thresholds, Bench, EmitConfig } from "./types.ts";
import { colors } from "../imports/fmt.ts";
import { bench } from "../imports/testing.ts";
import type { Thresholds, Bench } from "./types.ts";

/**
* create simple and scalable benchmarks for typescript and javascript, running on deno
*/
export class Maven {
private bench = bench.bench;

Expand All @@ -31,12 +35,15 @@ export class Maven {

private runIndicator = [{ benches: /./, modFn: () => "==> " }];

private addThreasholds(name: string) {
private addThresholds(name: string) {
this.thresholds[name] = { green: 70, yellow: 90 };
}

/**
*
*/
public Bench({ fn, name, steps = 1 }: Bench) {
this.addThreasholds(name);
this.addThresholds(name);
this.bench({
name,
func(bench) {
Expand All @@ -48,6 +55,10 @@ export class Maven {
});
}

/**
* execute the benchmarks
* @param config
*/
public async runBench(config?: bench.BenchmarkRunOptions) {
this.config = config as bench.BenchmarkRunOptions;
return bench.runBenchmarks(
Expand All @@ -59,10 +70,10 @@ export class Maven {
);
}

public async success() {
return await this.runBench(this.config);
}

/**
* prints the results table at the end of the benchmarks
* @param graphBars
*/
public Result(graphBars = 5) {
return prettyBenchmarkResult({
thresholds: this.thresholds,
Expand All @@ -75,4 +86,37 @@ export class Maven {
},
});
}

/**
* create a markdown file with the results of the benchmarks, it can also be issued in json format
* @param config
*/
public static Emit(config: EmitConfig) {
const _fileName = `/${
config.fileName ? config.fileName : "out_put"
}-${new Date().getTime()}`;

return prettyBenchmarkDown(
(markdown: string) => {
const create = Deno.createSync(`${Deno.cwd()}${_fileName}.md`);
Deno.writeTextFileSync(`.${_fileName}.md`, markdown);
create.close();
},
{
title: config.title ? config.title : "Maven Benchmark output",
description: config.description ? config.description : "",
afterTables: (result: any) => {
if (config.json) {
const create = Deno.createSync(`${Deno.cwd()}${_fileName}.json`);
Deno.writeTextFileSync(
`.${_fileName}.json`,
JSON.stringify(result, null, 2)
);
create.close();
}
return "";
},
}
);
}
}
Loading

0 comments on commit 9c6c5a1

Please sign in to comment.