-
Notifications
You must be signed in to change notification settings - Fork 172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[2단계 - 자동차 경주 리팩터링] - 윤생(이윤성) 미션 제출합니다. #204
Changes from 60 commits
0864c02
e4448fb
80e324d
3ee13de
97cf66d
d8da869
fbfb1cb
35663a4
84db7c1
ead5f2b
658fd36
e4806a1
a6df78d
079ed07
a1457ee
06f1107
f12c5f9
93d4d82
6e0c83c
16a0534
90bd66a
f0134b8
e21ed1a
a990424
926a75b
ebf28d5
0f1e56e
c083e0d
c802c7c
29ed5f6
c0e56f3
71e97bf
57a4612
5c72db0
454395d
43913b2
78dd042
c7d7fe3
f975546
3ea8cd9
5b95d00
096d43d
bfe05fc
1aa7e7a
4af06ce
db293a1
98dabaf
1d268c1
2d741ec
abfbbc8
373abf2
fc18b5a
5a363d7
76d29e4
bd80c5f
991d7fe
8ce3d9f
e58bb91
7b2745c
293ac9b
6287e24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,21 @@ | ||
/* eslint-disable max-lines-per-function */ | ||
/* eslint-disable no-undef */ | ||
// const Car = require('../src/Car'); | ||
import GameManager from '../src/GameManager'; | ||
import RandomGenerator from '../src/utils/RandomGenerator'; | ||
|
||
const mockRandom = (number) => { | ||
RandomGenerator.pickRandomNumber = jest.fn(); | ||
RandomGenerator.pickRandomNumber.mockReturnValueOnce(number); | ||
}; | ||
|
||
const mockRandoms = (numbers) => { | ||
RandomGenerator.pickRandomNumber = jest.fn(); | ||
numbers.reduce((acc, number) => { | ||
return acc.mockReturnValueOnce(number); | ||
}, RandomGenerator.pickRandomNumber); | ||
}; | ||
import GameManager from '../src/domain/GameManager'; | ||
|
||
describe('GameManager Test', () => { | ||
test('Random mock Test', () => { | ||
mockRandom(10); | ||
expect(RandomGenerator.pickRandomNumber()).toBe(10); | ||
}); | ||
test('차 이름 입력이 유효하지 않으면 에러를 던지는 지 테스트', () => { | ||
const gameMananger = new GameManager(); | ||
const badNamesInput = 'abcdef,bad guy,nononono'; | ||
|
||
test.each([ | ||
[9, true], | ||
[4, true], | ||
[3, false], | ||
[0, false], | ||
])('isFoward Test Random value : %i', (number, expected) => { | ||
mockRandom(number); | ||
const gameManager = new GameManager(); | ||
expect(gameManager.isForward()).toEqual(expected); | ||
expect(() => gameMananger.checkCarNames(badNamesInput)).toThrow(); | ||
}); | ||
|
||
test.each([ | ||
[['yun', 'park', 'kim'], [9, 0, 1, 8, 1, 2, 7, 5, 6], ['yun']], | ||
[ | ||
['choi', 'ann', 'lee', 'gabi'], | ||
[0, 0, 5, 5, 0, 0, 9, 9], | ||
['lee', 'gabi'], | ||
], | ||
[ | ||
['aa', 'bb'], | ||
[0, 0, 5, 5, 0, 0, 9, 9], | ||
['aa', 'bb'], | ||
], | ||
])('judgeWinners Test (%#)', (carNames, moves, winners) => { | ||
const gameManager = new GameManager(); | ||
const cars = gameManager.generateCars(carNames); | ||
mockRandoms(moves); | ||
gameManager.moveCars(cars); | ||
expect(gameManager.judgeWinners([...cars])).toEqual(winners); | ||
}); | ||
test.each([0, -1, NaN])( | ||
'시도 횟수 입력이 유효하지 않으면 에러를 던지는 지 테스트', | ||
(input) => { | ||
const gameMananger = new GameManager(); | ||
expect(() => gameMananger.checkTryCount(input)).toThrow(); | ||
} | ||
); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import Console from './utils/Console.js'; | ||
import Validation from './utils/Validation.js'; | ||
import Console from '../utils/Console.js'; | ||
import Validation from '../utils/Validation.js'; | ||
import Car from './Car.js'; | ||
import RandomGenerator from './utils/RandomGenerator.js'; | ||
import OutputView from './OutputView.js'; | ||
import InputView from './InputView.js'; | ||
import constants from './utils/constants.js'; | ||
import RandomGenerator from '../utils/RandomGenerator.js'; | ||
import OutputView from '../view/OutputView.js'; | ||
import InputView from '../view/InputView.js'; | ||
import constants from '../utils/constants.js'; | ||
|
||
class GameManager { | ||
#cars = []; | ||
|
@@ -21,25 +21,24 @@ class GameManager { | |
|
||
printCars(cars) { | ||
cars.forEach((car) => { | ||
car.print(); | ||
OutputView.printCar(car.name, car.position); | ||
}); | ||
OutputView.printEmptyLine(); | ||
} | ||
|
||
tryMoveCars(tryCount, cars) { | ||
OutputView.printResult(); | ||
for (let i = 0; i < tryCount; i++) { | ||
this.moveCars(cars); | ||
this.printCars(cars); | ||
} | ||
} | ||
|
||
judgeWinners(cars) { | ||
cars.sort((a, b) => b.getPosition() - a.getPosition()); | ||
const max = cars[0].getPosition(); | ||
cars.sort((a, b) => b.position - a.position); | ||
const max = cars[0].position; | ||
const winners = cars | ||
.filter((car) => car.getPosition() === max) | ||
.map((car) => car.getName()); | ||
.filter((car) => car.position === max) | ||
.map((car) => car.name); | ||
return winners; | ||
} | ||
|
||
|
@@ -84,6 +83,7 @@ class GameManager { | |
async play() { | ||
await this.handleCarNames(); | ||
const tryCount = await this.handleTryCount(); | ||
OutputView.printResult(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 적절한 위치에 |
||
this.tryMoveCars(tryCount, this.#cars); | ||
const winners = this.judgeWinners([...this.#cars]); | ||
OutputView.printWinners(winners); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import GameManager from './GameManager.js'; | ||
import GameManager from './domain/GameManager.js'; | ||
|
||
const gameManger = new GameManager(); | ||
gameManger.play(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,26 @@ | ||
import constants from './constants'; | ||
import constants from './constants.js'; | ||
|
||
const Validation = { | ||
isValidCarNames(carNames) { | ||
return carNames.every( | ||
(name) => | ||
name.length >= constants.MIN_CAR_NAME_LENGTH && | ||
name.length <= constants.MAX_CAR_NAME_LENGTH | ||
); | ||
return carNames.every((carName) => { | ||
let count = 0; | ||
// eslint-disable-next-line no-unused-vars | ||
for (const char of carName) { | ||
count += 1; | ||
} | ||
return this.isValidCarNameLength(count); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코드를 작성하는 것은 또 다른 의미의 문서화라는 말을 들어보신 적이 있으신가요? 윤생과 저는 이모지 예외 케이스를 해결하기 위해서 작성한 코드라는 것을 알고 있지만, 처음 이 코드를 읽는 사람은 어떻게 생각할까요? 이 코드가 어떤 코드인지 알 수 있게 하려면 어떻게 하는 게 좋을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저 로직을 함수로 분리하면 좋겠다고 생각이 듭니다! 한번 시도해보겠습니다! |
||
}); | ||
}, | ||
|
||
isValidTryCount(tryCount) { | ||
return Number.isInteger(tryCount) && tryCount >= constants.MIN_TRY_COUNT; | ||
}, | ||
|
||
isValidCarNameLength(carNameLength) { | ||
return ( | ||
carNameLength >= constants.MIN_CAR_NAME_LENGTH && | ||
carNameLength <= constants.MAX_CAR_NAME_LENGTH | ||
); | ||
}, | ||
}; | ||
export default Validation; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
car가 직접 스스로에 대한 정보를 print하는 것보다 이렇게 OutputView의 메소드인 printCar가 출력하는 구조가 훨씬 깔끔하고 좋은 것 같습니다. 역할과 책임이 좀 더 명확하게 구분된 것 같네요. 👍