Skip to content

Commit

Permalink
feat(@power-doctest/markdown): add @power-doctest/markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 25, 2019
1 parent 48557ac commit d2c8014
Show file tree
Hide file tree
Showing 51 changed files with 1,359 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# monorepo

packages/*/lib
packages/*/lib/

### https://raw.github.com/github/gitignore/d2c1bb2b9c72ead618c9f6a48280ebc7a8e0dff6/Node.gitignore

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"main": "index.js",
"workspaces": {
"packages": [
"packages/*"
"packages/*",
"packages/@power-doctest/*"
]
},
"devDependencies": {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# power-doctest-runner
# @power-doctest/javascript

A Test Runner for power-doctest.
A power-doctest Runner using for JavaScript.

## Install

Install with [npm](https://www.npmjs.com/):

npm install power-doctest-runner
npm install @power-doctest/javascript

## Usage

```js
import { run } from "power-doctest-runner"
import { run } from "@power-doctest/javascript"
run(`
console.log(1); // => 1
console.log("string"); // => "string"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "power-doctest-runner",
"name": "@power-doctest/javascript",
"version": "1.0.0",
"description": "A Test Runner for power-doctest.",
"description": "A power-doctest Runner using for JavaScript.",
"keywords": [
"doctest",
"node.js",
Expand Down Expand Up @@ -70,5 +70,8 @@
"prettier --write",
"git add"
]
},
"publishConfig": {
"access": "public"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export interface PowerDoctestRunnerOptions {
// console.log(1); // => 1
// ```
console?: boolean;
// Timeout
// Default: 2000ms
timeout?: number
// Default: all
// If runMode is all, all assertions are finished and resolve it
// If runMode is any, anyone assertion is finished and resolve it
Expand All @@ -32,9 +35,21 @@ const CALLBACK_FUNCTION_NAME = "__power_doctest_runner_callback__";

export function run(code: string, options: PowerDoctestRunnerOptions = {}) {
const runMode = options.runMode || "all";
const timeout = options.timeout !== undefined ? options.timeout : 2000;
const postCallbackName = options.powerDoctestCallbackFunctionName || CALLBACK_FUNCTION_NAME;
const context = options.context || {};
return new Promise((resolve, reject) => {
let isSettled = false;
const timeoutId = setTimeout(() => {
if (isSettled) {
return;
}
restoreListener();
reject(new Error(`Timeout error
${runMode === "all" ? `If you use { "runMode": "all" }, you should check all condition flow is passed. Maybe
Also, you should consider to use { "runMode": "any" }` : ""}`));
}, timeout);
// Test Runner like mocha listen unhandledRejection and uncaughtException
// Disable these listener before running code
const originalUnhandledRejection = process.listeners("unhandledRejection");
Expand All @@ -50,6 +65,7 @@ export function run(code: string, options: PowerDoctestRunnerOptions = {}) {
reject(error);
};
const restoreListener = () => {
isSettled = true;
process.off("uncaughtException", uncaughtException);
process.off("unhandledRejection", unhandledRejection);
// restore
Expand All @@ -59,6 +75,8 @@ export function run(code: string, options: PowerDoctestRunnerOptions = {}) {
originalUnhandledRejection.forEach(listener => {
process.addListener("unhandledRejection", listener);
});
// clearTimeout
clearTimeout(timeoutId);
};
process.on("uncaughtException", uncaughtException);
process.on("unhandledRejection", unhandledRejection as any);
Expand All @@ -71,6 +89,7 @@ export function run(code: string, options: PowerDoctestRunnerOptions = {}) {
let countOfExecutedAssertion = 0;
const vm = new NodeVM({
console: options.console ? "inherit" : "off",
timeout: timeout,
sandbox: {
[postCallbackName]: (_id: string) => {
countOfExecutedAssertion++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,15 @@ setTimeout(() => {
runMode: "any"
}));
});

it("does timeout because all assertion never called", () => {
return assert.rejects(run(`
if( true ) {
1; // => 1
} else{
2; // => 2
}
`, {
timeout: 100 // 100ms
}));
});
});
File renamed without changes.
File renamed without changes.
155 changes: 155 additions & 0 deletions packages/@power-doctest/markdown/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
### https://raw.github.com/github/gitignore/d2c1bb2b9c72ead618c9f6a48280ebc7a8e0dff6/Node.gitignore

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/


### https://raw.github.com/github/gitignore/d2c1bb2b9c72ead618c9f6a48280ebc7a8e0dff6/Global/JetBrains.gitignore

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser


/lib
19 changes: 19 additions & 0 deletions packages/@power-doctest/markdown/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019 azu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit d2c8014

Please sign in to comment.