-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47dcad8
commit c2de101
Showing
4 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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글자 이하여야 합니다.') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |