Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed ⚡ to ✽ to fix parse input issue. #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
346 changes: 175 additions & 171 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,171 +1,175 @@
# pico-test

<p>
<a href="https://www.npmjs.com/package/pico-test">
<img alt="npm version" src="https://img.shields.io/npm/v/pico-test.svg" />
</a>
<a href="https://travis-ci.org/jozanza/pico-test">
<img alt="build status" src="https://travis-ci.org/jozanza/pico-test.svg" />
</a>
<a href="http://standardjs.com/">
<img alt="code style" src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg" />
</a>
</p>

> **Note:** This project is still in its initial stages, so I'd love feedback about the API and issue reports.

### Intro

PICO-8 is great but debugging your code in this little vm can be a chore.

If you're tired of riddling your carts with `printh`s or have given up on test-driven development, this tool should help you out.

### Installation

npm i -g pico-test

> **Note:** you can also download it directly from the [releases section](https://github.com/jozanza/pico-test/releases)

### Usage

Copy/paste the following snippet into the cart you wish to test:

```lua
function test(title,f)
local desc=function(msg,f)
printh('⚡:desc:'..msg)
f()
end
local it=function(msg,f)
printh('⚡:it:'..msg)
local xs={f()}
for i=1,#xs do
if xs[i] == true then
printh('⚡:assert:true')
else
printh('⚡:assert:false')
end
end
printh('⚡:it_end')
end
printh('⚡:test:'..title)
f(desc,it)
printh('⚡:test_end')
end
```

Next, be sure PICO-8 is aliased properly in your terminal. You may have to do something like the following:

alias pico-8='/Applications/PICO-8.app/Contents/MacOS/pico8'

Last, run Pico-8 from your terminal and pipe its output to `pico-test`.

pico-8 | pico-test

Each time your run your cart, test results will be printed to `stdout`. Now, you just have to write some tests! :)

### API

`pico-test`'s api is will be pretty familiar if you've ever used [mocha](https://mochajs.org/). There are only 3 functions to learn: `test()`, `desc()`, and `it()`

#### test(title:string, fn:function)

initiates testing, wraps around test descriptions and tests, providing the callback `fn` with two args: `desc` and `it` – the other two functions in this API.

| Type | Param | Description |
|----------|-------|-------------|
| String | title | title of test suite
| Function | fn | callback to call with `desc` and `it`

#### desc(description:string, fn:function)

Describes a set of tests. This function is applied as the first argument of the callback function passed to `test`

| Type | Param | Description |
|----------|-------------|-------------|
| String | description | description for tests to be run inside of param `fn`
| Function | fn | callback to call with `desc` and `it`


#### it(message:string, fn:function)

Returns one or more boolean values representing test assertions. all returned values must be `true` or your test will fail. This function is applied as the second argument of the callback function passed to `test`

| Type | Param | Description |
|----------|---------|-------------|
| String | message | message starting with "should"
| Function | fn | callback to return assertions from


### Example

Here's what it looks like in action:

```lua
-- here's an object with methods we want to test
local math={
gt=function(a,b) return a>b end,
lt=function(a,b) return a<b end,
mul=function(a,b) return a*b end,
div=function(a,b) return a/b end
}

test('math functions', function(desc,it)
desc('math.gt()', function()
local gt = math.gt
it('should return type boolean', function()
return 'boolean' == type(gt(1,0))
end)
it('should give same result as > operator', function()
return gt(1,0)
end)
end)

desc('math.lt()', function()
local lt = math.lt
it('should return type boolean',function()
return 'boolean' == type(lt(1,0))
end)
it('should give same result as < operator',function()
return lt(1, 0) == false
end)
end)

desc('math.mul()', function()
local mul = math.mul
it('should return type number', function()
local a = rnd(time())
local b = rnd(time())
return 'number' == type(mul(a,b))
end)
it('should give same result as * operator', function()
local x=rnd(time())
return
x*1 == mul(x,1),
x*2 == mul(x,2),
x*3 == mul(x,3)
end)
end)

desc('math.div()', function()
local div = math.div
it('should return type number', function()
local a = rnd(time())
local b = rnd(time())
return 'number' == type(div(a,b))
end)
it('should give same result as / operator', function()
local x=1+rnd(time())
return
x/1 == div(x,1),
x/2 == div(x,2),
x/3 == div(x,3)
end)
end)

end)
```

### License

Copyright (c) 2015 Josiah Savary. Made available under The MIT License (MIT).
# pico-test

<p>
<a href="https://www.npmjs.com/package/pico-test">
<img alt="npm version" src="https://img.shields.io/npm/v/pico-test.svg" />
</a>
<a href="https://travis-ci.org/jozanza/pico-test">
<img alt="build status" src="https://travis-ci.org/jozanza/pico-test.svg" />
</a>
<a href="http://standardjs.com/">
<img alt="code style" src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg" />
</a>
</p>

> **Note:** This project is still in its initial stages, so I'd love feedback about the API and issue reports.

### Intro

PICO-8 is great but debugging your code in this little vm can be a chore.

If you're tired of riddling your carts with `printh`s or have given up on test-driven development, this tool should help you out.

### Installation

npm i -g pico-test

> **Note:** you can also download it directly from the [releases section](https://github.com/jozanza/pico-test/releases)

### Usage

Copy/paste `testrunner.lua` into the cart you wish to test:

```lua
test = function (title,f)
local desc=function(msg,f)
printh('✽:desc:'..msg)
f()
end
local it=function(msg,f)
printh('✽:it:'..msg)
local xs=f and {f()} or {}
if #xs>0 then
for i=1,#xs do
if xs[i]==true then
printh('✽:assert:true')
else
printh('✽:assert:false')
end
end
else
printh('✽:pend:pend')
end
printh('✽:it_end')
end
printh('✽:test:'..title)
f(desc,it)
printh('✽:test_end')
end
```

Next, be sure PICO-8 is aliased properly in your terminal. You may have to do something like the following:

alias pico-8='/Applications/PICO-8.app/Contents/MacOS/pico8'

Last, run Pico-8 from your terminal and pipe its output to `pico-test`.

pico-8 | pico-test

Each time your run your cart, test results will be printed to `stdout`. Now, you just have to write some tests! :)

### API

`pico-test`'s api is will be pretty familiar if you've ever used [mocha](https://mochajs.org/). There are only 3 functions to learn: `test()`, `desc()`, and `it()`

#### test(title:string, fn:function)

initiates testing, wraps around test descriptions and tests, providing the callback `fn` with two args: `desc` and `it` – the other two functions in this API.

| Type | Param | Description |
|----------|-------|-------------|
| String | title | title of test suite
| Function | fn | callback to call with `desc` and `it`

#### desc(description:string, fn:function)

Describes a set of tests. This function is applied as the first argument of the callback function passed to `test`

| Type | Param | Description |
|----------|-------------|-------------|
| String | description | description for tests to be run inside of param `fn`
| Function | fn | callback to call with `desc` and `it`


#### it(message:string, fn:function)

Returns one or more boolean values representing test assertions. all returned values must be `true` or your test will fail. This function is applied as the second argument of the callback function passed to `test`

| Type | Param | Description |
|----------|---------|-------------|
| String | message | message starting with "should"
| Function | fn | callback to return assertions from


### Example

Here's what it looks like in action:

```lua
-- here's an object with methods we want to test
local math={
gt=function(a,b) return a>b end,
lt=function(a,b) return a<b end,
mul=function(a,b) return a*b end,
div=function(a,b) return a/b end
}

test('math functions', function(desc,it)
desc('math.gt()', function()
local gt = math.gt
it('should return type boolean', function()
return 'boolean' == type(gt(1,0))
end)
it('should give same result as > operator', function()
return gt(1,0)
end)
end)

desc('math.lt()', function()
local lt = math.lt
it('should return type boolean',function()
return 'boolean' == type(lt(1,0))
end)
it('should give same result as < operator',function()
return lt(1, 0) == false
end)
end)

desc('math.mul()', function()
local mul = math.mul
it('should return type number', function()
local a = rnd(time())
local b = rnd(time())
return 'number' == type(mul(a,b))
end)
it('should give same result as * operator', function()
local x=rnd(time())
return
x*1 == mul(x,1),
x*2 == mul(x,2),
x*3 == mul(x,3)
end)
end)

desc('math.div()', function()
local div = math.div
it('should return type number', function()
local a = rnd(time())
local b = rnd(time())
return 'number' == type(div(a,b))
end)
it('should give same result as / operator', function()
local x=1+rnd(time())
return
x/1 == div(x,1),
x/2 == div(x,2),
x/3 == div(x,3)
end)
end)

end)
```

### License

Copyright (c) 2015 Josiah Savary. Made available under The MIT License (MIT).
27 changes: 21 additions & 6 deletions index → index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const parseInput = curry((dispatch, write) => {
let line = ''
let concat = false
return x => {
if (x[0] === '') concat = true
if (x[0] === '') concat = true
if (!concat) write(x)
else {
line += x
Expand Down Expand Up @@ -189,6 +189,9 @@ function reducer (state, action) {
nextState.failCount += 1
}
break
case 'pend':
nextState.suites[i].cases[j].assertions.push(_[0])
break
default: break
}
return nextState
Expand Down Expand Up @@ -228,16 +231,28 @@ function reporter (state, action) {
const kase = test.cases[test.cases.length - 1]
// const duration = kase.stopped - kase.started
const failed = kase.assertions.reduce((next, x) => next || !x, false)
const pended = kase.assertions[0] == 'pend'
const fails = kase.assertions.map((passed, i) => passed
? ''
: `\n • failed assertion #${i + 1}`
).join('')
out = '\n ' + [
(failed ? '✖' : '✔'),
kase.name,
''// `(${!duration ? '<1' : duration}ms)\n`,
].join(' ') + fails
if (pended) {
out = '\n ' + [
'-',
kase.name,
'(pending)',
''// `(${!duration ? '<1' : duration}ms)\n`,
].join(' ') + fails
} else {
out = '\n ' + [
(failed ? '✖' : '✔'),
kase.name + ' (+' + kase.assertions.length + ')',
'',
''// `(${!duration ? '<1' : duration}ms)\n`,
].join(' ') + fails
}
if (failed) out = format('red')(out)
else if (pended) out = format('yellow')(out)
else out = format('green')(out)
break
default: break
Expand Down
Loading