Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoABCardoso committed May 28, 2021
0 parents commit ff97879
Show file tree
Hide file tree
Showing 15 changed files with 12,688 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .eslintrc
@@ -0,0 +1,30 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true,
"jest": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 12
},
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
}
}
67 changes: 67 additions & 0 deletions .github/workflows/codeql-analysis.yml
@@ -0,0 +1,67 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '40 3 * * 0'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
language: [ 'javascript' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
34 changes: 34 additions & 0 deletions .github/workflows/node.js.yml
@@ -0,0 +1,34 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.vscode
node_modules
coverage
.DS_Store
6 changes: 6 additions & 0 deletions .npmignore
@@ -0,0 +1,6 @@
.vscode
.github
node_modules
coverage
.DS_Store
test
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,5 @@
### Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
7 changes: 7 additions & 0 deletions LICENSE
@@ -0,0 +1,7 @@
# [ISC License](https://spdx.org/licenses/ISC)

Copyright (c) 2021, Marco Cardoso <macardoso95@gmail.com>

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
78 changes: 78 additions & 0 deletions README.md
@@ -0,0 +1,78 @@
<h1 align="center">iteration-test</h1>
<p>
<a href="https://www.npmjs.com/package/iteration-test" target="_blank">
<img alt="Version" src="https://img.shields.io/npm/v/iteration-test.svg">
</a>
<a href="#" target="_blank">
<img alt="License: ISC" src="https://img.shields.io/badge/License-ISC-yellow.svg" />
</a>
<a href='https://coveralls.io/github/MarcoABCardoso/iteration-test?branch=master'>
<img src='https://coveralls.io/repos/github/MarcoABCardoso/iteration-test/badge.svg?branch=master' alt='Coverage Status' />
</a>
<a href="#" target="_blank">
<img alt="Node.js CI" src="https://github.com/MarcoABCardoso/iteration-test/workflows/Node.js%20CI/badge.svg" />
</a>
</p>

> Runs automated testing for a given function to be run iteratively.
## Install

```sh
npm install iteration-test
```

## Usage

```js
const TestSuite = require('iteration-test')

let testSuite = new TestSuite({
testFunction: payload => ({ output: payload.current + payload.previous }),
tests: [
{
name: 'Adds current and previous fields at each turn',
rounds: [
{ inputExpression: '{ current: 3, previous: 0 }', evaluateExpression: 'output == 3' },
{ inputExpression: '{ current: 4, previous: output }', evaluateExpression: 'output == 7' },
{ inputExpression: '{ current: 5, previous: output }', evaluateExpression: 'output == 11' }, // Fails
]
}
]
})

let results = await testSuite.run()
```

## Sample results

```json
{
"success": false,
"passed": 0,
"failed": 1,
"details": [
{
"name": "Adds current and previous fields at each turn",
"success": false,
"error": "Expression [output == 11] is false for obtained output {\"output\":12}"
}
]
}

## Run tests

```sh
npm run test
```

## Author

👤 **Marco Cardoso**

* Github: [@MarcoABCardoso](https://github.com/MarcoABCardoso)
* LinkedIn: [@marco-cardoso](https://linkedin.com/in/marco-cardoso)

## Show your support

Give a ⭐️ if this project helped you!
41 changes: 41 additions & 0 deletions lib/index.d.ts
@@ -0,0 +1,41 @@

/**
* @module iteration-test
*/

declare class TestSuite {
constructor(options: TestSuiteOptions)
run(): Promise<TestResults>
}

interface TestSuiteOptions {
testFunction: () => any
tests: Test[]

VERBOSE?: Number
}

interface Test {
name: string
rounds: Round[]
}

interface Round {
inputExpression: string
evaluateExpression: string
}

interface TestResults {
success: boolean
passed: number
failed: number
details: ResultDetail[]
}

interface ResultDetail {
name: string
success: boolean
error?: any
}

export = TestSuite
1 change: 1 addition & 0 deletions lib/index.js
@@ -0,0 +1 @@
module.exports = require('./test-suite')
20 changes: 20 additions & 0 deletions lib/test-analysis.js
@@ -0,0 +1,20 @@
function passedCount(tests) {
return tests.filter(test => test.success).length
}

function failedCount(tests) {
return tests.length - passedCount(tests)
}

function generateReports(tests) {
return {
success: failedCount(tests) == 0,
passed: passedCount(tests),
failed: failedCount(tests),
details: tests
}
}

module.exports = {
generateReports
}
51 changes: 51 additions & 0 deletions lib/test-suite.js
@@ -0,0 +1,51 @@
const { generateReports } = require('./test-analysis')
const { SpelExpressionEvaluator } = require('spel2js')

class TestSuite {
constructor(options) {
this.testFunction = options.testFunction

this.config = {
...TestSuite.getDefaultConfig(),
...options
}

this.log = this.config.VERBOSE ? console.log : () => { }
}

async run() {
this.log('[ITERATION TEST] STARTING TEST')
let testData = await Promise.all(
this.config.tests
.map(async (test, testIndex) =>
test.rounds
.reduce(
async (currentContextPromise, round, roundIndex) => {
let currentContext = await currentContextPromise
this.log(`[ITERATION TEST] EXECUTING TEST ${testIndex} ROUND ${roundIndex}`)
return this.executeRound(round, currentContext)
},
null
)
.then(() => ({ name: test.name, success: true }))
.catch(err => ({ name: test.name, success: false, error: err }))
)
)
this.log('[ITERATION TEST] ALL DONE')
return generateReports(testData)
}

executeRound(options, context) {
let input = SpelExpressionEvaluator.eval(options.inputExpression, context)
let output = this.testFunction(input)
let valid = SpelExpressionEvaluator.eval(options.evaluateExpression, output)
if (!valid) throw `Expression [${options.evaluateExpression}] is false for obtained output ${JSON.stringify(output)}`
return output
}

}

TestSuite.getDefaultConfig = () => ({
})

module.exports = TestSuite

0 comments on commit ff97879

Please sign in to comment.