Skip to content

Commit 1c06cbc

Browse files
authored
Merge pull request #3 from NHibiki-NYU/master
add fibonacci computing function and test
2 parents db98644 + 3945084 commit 1c06cbc

4 files changed

Lines changed: 43 additions & 2 deletions

File tree

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ jobs:
1212
steps:
1313
- checkout
1414
- run: npm install
15+
- run: npm install node-fetch
1516
- run: npm test

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unit Testing and Continuous Integration
22

3+
[![CircleCI](https://circleci.com/gh/NHibiki-NYU/UnitTestingCI.svg?style=svg)](https://circleci.com/gh/NHibiki-NYU/UnitTestingCI)
4+
35
This repository is for sudents in the course to experiment with unit testing and continuous integration. It uses the testing framework [Jest](https://jestjs.io/) and [CircleCI](https://circleci.com/) as described in the video tutorials linked below.
46

57
This repo is forked from [CodingTrain/TestingTestTest](https://github.com/CodingTrain/TestingTestTest).

sketch.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ function anomalyCode(x) {
2929
return '5' + x - x;
3030
}
3131

32+
function nOfFibonacci(x) {
33+
let n = parseInt(x, 10);
34+
return (!n || n < 1) ? -1 : (n < 3 ? 1 : (nOfFibonacci(n-1) + nOfFibonacci(n-2)));
35+
}
36+
3237
module.exports = {
3338
sum: sum,
3439
sub: sub,
@@ -37,5 +42,6 @@ module.exports = {
3742
digital_root: digital_root,
3843
sayHelloTo: sayHelloTo,
3944
answer: answer,
40-
anomalyCode: anomalyCode
45+
anomalyCode: anomalyCode,
46+
nOfFibonacci: nOfFibonacci
4147
}

sketch.test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
const { sum, sub, prod, digital_root, sum42, sayHelloTo, anomalyCode } = require('./sketch');
1+
const { sum, sub, prod, digital_root, sum42, sayHelloTo, anomalyCode, nOfFibonacci } = require('./sketch');
2+
const fs = require("fs");
3+
const path = require("path");
4+
const fetch = require("node-fetch");
25

36
// test('adds 1 + 2 to equal 3', () => {
47
// expect(sum(1, 2)).toBe(3);
@@ -93,3 +96,32 @@ test('anomalyCode hundred should be 5000', () => {
9396
test('anomalyCode thousand should be 50000', () => {
9497
expect(anomalyCode(7891)).toBe(50000);
9598
})
99+
100+
test('the 20th number of fibonacci should be 6765', () => {
101+
expect(nOfFibonacci(20)).toBe(6765);
102+
})
103+
104+
const testUrlInMarkdown = (file) => {
105+
let markdown = fs.readFileSync(file).toString();
106+
let urls = [];
107+
let regExp = /\[([^\]]+)\]\(([^)]+)\)/g;
108+
let result = null;
109+
while (result = regExp.exec(markdown)) urls.push(result[2]);
110+
return [urls.length, async () => {
111+
for (let url of urls) {
112+
if (url.includes("://") && !url.split("://")[0].includes("/")) {
113+
// An External url
114+
let res = await fetch(url);
115+
if (![200, 301, 302].includes(res.status)) throw new Error(`External Link Test: ${url} with bad response code ${res.status}`);
116+
} else {
117+
if (!fs.existsSync(path.join(path.dirname(file), url))) throw new Error(`Internal Link Test: ${url} with not found error`);
118+
}
119+
}
120+
}];
121+
}
122+
123+
it('tests url avaliability in README.md', async () => {
124+
let [testCount, testFunc] = testUrlInMarkdown("README.md");
125+
jest.setTimeout(testCount * 5000);
126+
return await testFunc();
127+
});

0 commit comments

Comments
 (0)