Skip to content

Commit

Permalink
feat: 자동차 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JunilHwang committed Jun 18, 2024
1 parent 47dcad8 commit c2de101
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/__tests__/Car.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, test, expect } from "vitest";
import Car from "../domain/Car.js";

describe('Car >', () => {
test('자동차의 이름이 5글자보다 길면 오류가 발생한다.', () => {
expect(() => new Car('123456')).toThrow();
})

test.each([
{ name: '', },
{ name: '1', },
{ name: '12', },
{ name: '1 2', },
{ name: '123', },
{ name: '1 2 3', },
{ name: '1234', },
{ name: '12345', },
{ name: ' 12345 ', },
])('자동차의 이름이 5글자 이하($name)이면 오류가 발생하지 않는다.', ({ name }) => {
expect(() => new Car(name)).not.toThrow();
})

test('자동차는 전진할 수 있다.', () => {
const car = new Car('이름');
car.move();
expect(car.position).toBe(1);
})
})
26 changes: 26 additions & 0 deletions src/domain/Car.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default class Car {
static NAME_SIZE = 5;

#position;
#name;

constructor(name) {
Car.#validateName(name);
this.#position = 0;
this.#name = name;
}

move() {
this.#position += 1;
}

get position() {
return this.#position;
}

static #validateName(name) {
if (name.trim().length > this.NAME_SIZE) {
throw new Error('자동차의 이름은 5글자 이하여야 합니다.')
}
}
}
9 changes: 7 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function main() {
console.log('main의 내용을 채워주세요');
import readLineAsync from "./readLineAsync.js";



async function main() {
const name = await readLineAsync("자동차 이름을 입력하세요 > ");
console.log(`자동차 이름은 ${name}입니다.`);
}

main();
25 changes: 25 additions & 0 deletions src/readLineAsync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import readline from "readline";

function readLineAsync(query) {
return new Promise((resolve, reject) => {
if (arguments.length !== 1) {
reject(new Error("arguments must be 1"));
}

if (typeof query !== "string") {
reject(new Error("query must be string"));
}

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

rl.question(query, (input) => {
rl.close();
resolve(input);
});
});
}

export default readLineAsync

0 comments on commit c2de101

Please sign in to comment.