Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
関数のテストの追加など
Browse files Browse the repository at this point in the history
  • Loading branch information
takahash committed Oct 9, 2023
1 parent 14edee0 commit 2a486df
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 66 deletions.
10 changes: 0 additions & 10 deletions basic/func1.js

This file was deleted.

14 changes: 0 additions & 14 deletions basic/func1.test.js

This file was deleted.

30 changes: 0 additions & 30 deletions basic/func2.test.js

This file was deleted.

3 changes: 1 addition & 2 deletions basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<meta charset="utf-8">
</head>
<body></body>
<script type="module" src="./func1.js"></script>
<script type="module" src="./func2.js"></script>
<script type="module" src="./script.js"></script>
</html>

13 changes: 10 additions & 3 deletions basic/func2.js → basic/script.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// ループ・配列
export const func1 = (n) => {
const arr = [];
for (let i = 0; i < n; i++) {
arr.push(i + 1);
}
return arr;
};

// 条件分岐・データ型
const func2 = (n) => {
export const func2 = (n) => {
if (typeof n !== "number") return "error";
if (n % 3 === 0 && n % 5 === 0) return "FizzBuzz";
if (n % 3 === 0) return "Fizz";
if (n % 5 === 0) return "Buzz";
return `${n}`;
};

export default func2;
14 changes: 14 additions & 0 deletions basic/script.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test, expect } from "vitest";
import { func1, func2 } from "./script";

test("func1", () => expect(func1(5)).toEqual([1, 2, 3, 4, 5]));
test("func1", () => expect(func1(8)).toEqual([1, 2, 3, 4, 5, 6, 7, 8]));
test("func1", () => expect(func1(-1)).toEqual([]));

test("func2", () => expect(func2(1)).toEqual("1"));
test("func2", () => expect(func2(3)).toEqual("Fizz"));
test("func2", () => expect(func2(5)).toEqual("Buzz"));
test("func2", () => expect(func2(15)).toEqual("FizzBuzz"));
test("func2", () => expect(func2(22)).toEqual("22"));
test("func2", () => expect(func2(30)).toEqual("FizzBuzz"));
test("func2", () => expect(func2("hogehoge")).toEqual("error"));
2 changes: 1 addition & 1 deletion function/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<meta charset="utf-8">
</head>
<body></body>
<script src="./script.js"></script>
<script type="module" src="./script.js"></script>
</html>

14 changes: 8 additions & 6 deletions function/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ const students = [
},
];

const a1 = students.filter((x) => x.score >= 50).map((x) => x.name);
console.log(a1);
export const func1 = () => {
return students.filter((x) => x.score >= 50).map((x) => x.name);
};

const a2 = students
.filter((x) => x.gender === "male")
.reduce((a, b) => a + b.score, 0);
console.log(a2);
export const func2 = () => {
return students
.filter((x) => x.gender === "male")
.reduce((a, b) => a + b.score, 0);
};
10 changes: 10 additions & 0 deletions function/script.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test, expect } from "vitest";
import { func1, func2 } from "./script";

test("func1", () => {
expect(func1()).toEqual(["Ken", "Hana"]);
});

test("func2", () => {
expect(func2()).toEqual(125);
});

0 comments on commit 2a486df

Please sign in to comment.