Skip to content

Commit f0df74a

Browse files
authored
Merge pull request #4 from kanziw/add-test
테스트케이스 추가
2 parents fcb3186 + 6f0b6f9 commit f0df74a

File tree

5 files changed

+122
-5
lines changed

5 files changed

+122
-5
lines changed

.babelrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"plugins": [
3+
"transform-async-to-generator"
4+
]
5+
}

.gitignore

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
#
2+
node_modules
13

2-
.idea/functional.es.iml
4+
# for IntelliJ IDE
5+
.idea/*.iml
36
.idea/markdown-navigator.xml
47
.idea/markdown-navigator/profiles_settings.xml
58
.idea/misc.xml
69
.idea/modules.xml
710
.idea/vcs.xml
811
.idea/watcherTasks.xml
912
.idea/workspace.xml
10-
articles/ES6, 함수형 프로그래밍, 비동기, 동시성 프로그래밍/html/.DS_Store
11-
articles/ES6, 함수형 프로그래밍, 비동기, 동시성 프로그래밍/.DS_Store
13+
14+
.DS_Store
15+
1216
*.zip

functional.es.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
let root = {};
2+
13
!function() {
24
const curry2 = f => (..._) => _.length < 2 ? (..._2) => f(..._, ..._2) : f(..._);
35

@@ -382,7 +384,6 @@
382384

383385
const sel = baseSel(' > ');
384386

385-
const root = typeof global == 'object' ? global : window;
386387
root.Functional = {
387388
curry2, flip,
388389
then, identity, noop,
@@ -399,4 +400,10 @@
399400
negate, complement, not, isAny, isUndefined,
400401
each, log,
401402
};
402-
} ();
403+
} ();
404+
405+
if (typeof global == 'object') {
406+
module.exports = root.Functional || {};
407+
} else {
408+
window.Functional = root.Functional;
409+
}

package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "functional.es",
3+
"version": "1.0.0",
4+
"description": "ES6 +, Functional Programming, Asynchronous, Concurrent Programming",
5+
"main": "functional.es.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "mocha -R spec --require babel-register test/**/*.js"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/Functional-JavaScript/FunctionalES.git"
15+
},
16+
"keywords": [
17+
"es6+",
18+
"functional",
19+
"programming",
20+
"javascript"
21+
],
22+
"author": "[email protected]",
23+
"license": "",
24+
"bugs": {
25+
"url": "https://github.com/Functional-JavaScript/FunctionalES/issues"
26+
},
27+
"homepage": "https://github.com/Functional-JavaScript/FunctionalES#readme",
28+
"devDependencies": {
29+
"babel-cli": "^6.26.0",
30+
"babel-plugin-transform-async-to-generator": "^6.24.1",
31+
"babel-register": "^6.26.0",
32+
"chai": "^4.1.2",
33+
"mocha": "^5.1.0"
34+
}
35+
}

test/common.spec.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const expect = require('chai').expect
2+
const { curry2, pipe } = require('../functional.es')
3+
4+
describe('[ curry2 ]', function () {
5+
it('curry2 는 함수를 반환한다.', () => {
6+
const adder = curry2((a, b) => a + b)
7+
expect(adder).to.be.a('function')
8+
})
9+
10+
it('curry2 를 통해 반환된 함수는 인자가 1개이면 함수를, 1개보다 많으면 해당 함수를 실행한다.', () => {
11+
const adder = curry2((a, b) => a + b)
12+
const addWith5 = adder(5)
13+
14+
expect(addWith5).to.be.a('function')
15+
expect(addWith5(3)).to.eql(8)
16+
expect(addWith5(5)).to.eql(10)
17+
})
18+
19+
it('curry2 에 함수가 아닌 인자를 입력하는 경우 TypeError 가 발생할 수 있다.', () => {
20+
const unknown = curry2('abc')
21+
22+
expect(unknown).to.be.a('function')
23+
expect(unknown(1)).to.be.a('function')
24+
expect(() => unknown(1)(2)).to.throw(TypeError, 'Function.prototype.apply was called on abc, which is a string and not a function')
25+
unknown(1)(2)
26+
})
27+
})
28+
29+
describe('[ pipe ]', function () {
30+
const add1 = a => a + 1
31+
const add1P = a => new Promise(resolve => setTimeout(() => resolve(a + 1), 1000))
32+
const mul2 = a => a * 2
33+
34+
it('pipe 는 인자로 받은 함수들을 순차적으로 실행하는 함수를 반환한다.', () => {
35+
const f = pipe(add1, mul2)
36+
37+
expect(f).to.be.a('function')
38+
expect(f(1)).to.eql(4)
39+
expect(f(2)).to.eql(6)
40+
})
41+
42+
it('pipe 에 promise 를 반환하는 비동키 함수가 포함되는 경우 pipe 함수는 인자로 들어온 함수를 순차적으로 실행하지만 promise 를 반환한다.', async () => {
43+
this.timeout(2000)
44+
45+
const fP = pipe(add1P, mul2)
46+
expect(fP).to.be.a('function')
47+
48+
const p1 = fP(1)
49+
const p2 = fP(2)
50+
51+
expect(p1).to.be.a('Promise')
52+
expect(p2).to.be.a('Promise')
53+
54+
await p1.then(result => expect(result).to.eql(4))
55+
await p2.then(result => expect(result).to.eql(6))
56+
})
57+
58+
it('pipe 중간에 함수가 아닌 인자가 들어가는 경우 TypeError 가 발생할 수 있다.', () => {
59+
const f = pipe(add1, mul2, 3)
60+
61+
expect(f).to.be.a('function')
62+
expect(() => f(1)).to.throw(TypeError, 'f is not a function')
63+
expect(() => f(2)).to.throw(TypeError, 'f is not a function')
64+
f(1)
65+
})
66+
})

0 commit comments

Comments
 (0)