Skip to content

Commit f6f510e

Browse files
committed
Add multi-line test cases
1 parent ca7325f commit f6f510e

5 files changed

Lines changed: 155 additions & 12 deletions

File tree

index.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { program } from 'commander'
88
import { hash } from './hash.js';
99
import url from 'url'
1010
import { transpile } from './typescriptTranspiler.js';
11-
11+
import { parse } from './parser/dsl.js';
1212

1313
program
1414
.name('pineapple')
15-
.version('0.6.2')
15+
.version('0.6.3')
1616
.option('-i, --include <files...>', 'Comma separated globs of files.')
1717
.option('-a, --accept-all', 'Accept all snapshots.')
1818
.option('-u, --update-all', 'Update all snapshots.')
@@ -164,6 +164,28 @@ const tagTypes = [
164164
'afterEach'
165165
]
166166

167+
function multiLine(fileText, start, type) {
168+
let end = start + 1;
169+
170+
console.log(fileText[start], type)
171+
let text = (fileText[start].split(`@${type} `)[1] || '').trim()
172+
let lastSuccess = text
173+
174+
// while there isn't a * @ on the line
175+
while (fileText[end] && !fileText[end].includes('* @')) {
176+
if(fileText[end].includes('*/')) break;
177+
const addition = fileText[end].substring(fileText[end].indexOf('*') + 1).trim()
178+
text += ' ' + addition
179+
try {
180+
parse(text) // attempt a parse
181+
lastSuccess = text
182+
} catch(err) {}
183+
end++;
184+
}
185+
186+
return lastSuccess.trim()
187+
}
188+
167189
function getFunctions(file, fileText, fileName) {
168190

169191

@@ -182,13 +204,10 @@ function getFunctions(file, fileText, fileName) {
182204
while (current > 0 && !fileText[current].includes('/*')) {
183205
current--;
184206
for (const type of tagTypes) {
185-
if (fileText[current].includes(`@${type} `))
186-
tags.push(
187-
{
188-
type,
189-
text: fileText[current].split(`@${type} `)[1].trim()
190-
}
191-
);
207+
if (fileText[current].includes(`@${type} `)) tags.push({
208+
type,
209+
text: multiLine(fileText, current, type)
210+
});
192211
}
193212
}
194213
}
@@ -253,7 +272,7 @@ function getFunctions(file, fileText, fileName) {
253272
const tags = item[1].filter(i => tagTypes.includes(i.getTagName()))
254273
.map(tag => {
255274
const tagName = tag.getTagName()
256-
return { type: tagName, text: fileText[tag.getStartLineNumber() - 1].split(`@${tagName}`)[1].trim() }
275+
return { type: tagName, text: multiLine(fileText, tag.getStartLineNumber() - 1, tagName) }
257276
});
258277

259278
return {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pineapple",
3-
"version": "0.6.2",
3+
"version": "0.6.3",
44
"description": "A testing framework for humans.",
55
"main": "index.js",
66
"scripts": {

test/class.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,39 @@ export class Adder {
8181
static getPrevious() {
8282
return this.previousResult
8383
}
84-
}
84+
}
85+
86+
87+
/**
88+
* @pineapple_import
89+
*/
90+
export function setPersonCount (to = 0) {
91+
Person.count = to
92+
}
93+
94+
/**
95+
* @test 'Jesse', 24
96+
* ~> $.grow(3)
97+
* ~> $.grow(2) returns 29
98+
*
99+
* @test 'Rick', 62
100+
* ~> $.grow(1) returns 63
101+
* ~> $.grow(2) returns 65
102+
* ~> $.getName() returns 'Rick'
103+
* ~> $.grow() returns $.age === 66
104+
*/
105+
export class Person {
106+
constructor(name, age) {
107+
this.name = name
108+
this.age = age
109+
}
110+
111+
grow(amount = 1) {
112+
return this.age += amount
113+
}
114+
115+
getName() {
116+
return this.name
117+
}
118+
}
119+

test/rental.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,23 @@
88
if (typeof buyer !== 'string' || typeof length !== 'number') throw new Error('Types do not match.')
99
return { type, buyer, length }
1010
}
11+
12+
13+
/**
14+
* @test {
15+
* tenant: 'Rick',
16+
* length: 10,
17+
* type: 'boat'
18+
* } resolves
19+
*
20+
* @test {
21+
* tenant: 10,
22+
* length: 'Rick',
23+
* type: 'boat'
24+
* } rejects
25+
*/
26+
export async function createLease({ tenant, length, type = 'boat' }) {
27+
if (typeof tenant !== 'string' || typeof length !== 'number')
28+
throw new Error('Types do not match.')
29+
return { type, tenant, length }
30+
}

website/blog/v0.6.3.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
tags: [v0.6.3, patch]
3+
sidebar_position: 2
4+
date: 2022-05-10
5+
authors:
6+
- name: Jesse Mitchell
7+
title: Developer of Pineapple
8+
url: https://github.com/TotalTechGeek
9+
image_url: https://github.com/TotalTechGeek.png
10+
---
11+
12+
# Minor Patch: v0.6.3
13+
14+
This patch introduces a small quality of life improvement, which I felt was particularly necessary after introducing class-based testing:
15+
16+
**Multiline Test Cases!**
17+
18+
19+
```js
20+
/**
21+
* @test 'Jesse', 24
22+
* ~> $.grow(3)
23+
* ~> $.grow(2) returns 29
24+
*
25+
* @test 'Rick', 62
26+
* ~> $.grow(1) returns 63
27+
* ~> $.grow(2) returns 65
28+
* ~> $.getName() returns 'Rick'
29+
* ~> $.grow() returns $.age === 66
30+
*/
31+
export class Person {
32+
constructor(name, age) {
33+
this.name = name
34+
this.age = age
35+
}
36+
37+
grow(amount = 1) {
38+
return this.age += amount
39+
}
40+
41+
getName() {
42+
return this.name
43+
}
44+
}
45+
```
46+
47+
If you write a test case on multiple lines, Pineapple will now automatically concatenate it to the test case. This is not exclusive to class / higher-order function syntax.
48+
49+
50+
```js
51+
/**
52+
* @test {
53+
* tenant: 'Rick',
54+
* length: 10,
55+
* type: 'boat'
56+
* } resolves
57+
*
58+
* @test {
59+
* tenant: 10,
60+
* length: 'Rick',
61+
* type: 'boat'
62+
* } rejects
63+
*/
64+
export async function createLease({ tenant, length, type = 'boat' }) {
65+
if (typeof tenant !== 'string' || typeof length !== 'number')
66+
throw new Error('Types do not match.')
67+
return { type, tenant, length }
68+
}
69+
```

0 commit comments

Comments
 (0)