-
Notifications
You must be signed in to change notification settings - Fork 6
[정찬욱] 로또 미션 Step2 #8
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
base: seeyoujeong
Are you sure you want to change the base?
Changes from all commits
ef743b3
9f95561
de30d4e
d579f9b
e855bbf
e3e8ecc
f33d640
aced74a
5a7c226
56d244f
4afa7f0
fd17d31
86dd7f4
e776555
cda3a77
6f8ddc9
eca3349
5f550f1
10e8c4d
40f1f10
ce82980
c4d6d2b
eb7368d
bc07e24
82d0a01
a85d026
d66eca8
d4e99f2
89b8eaf
e9f317e
fbf2d4f
a9471c4
7ebaa96
56a4598
c41a1fc
4f78672
df5b826
a5c07c1
0bc5f15
3ccfbbc
88004ac
502416e
783e136
2ec79a3
b7c3d93
0e4cade
48d0c42
4bd886c
9b86217
b0a1390
e4e4f4f
ee9f635
43c8791
ddaf645
2b4897a
046974b
49641e8
af050c1
85b4a72
6e5ac02
bc8c1a7
60d2154
6dcaa4f
26037c2
8fb164f
0fd967b
f787d18
92a4f4f
58fd431
074b130
d5721d7
258249e
f3c451b
b41cc97
15933cb
daf3e24
bbf6649
59db060
ff3fb40
42f46b7
7006658
e0cd64f
de23f68
4477059
d38145f
1956965
19ab847
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,4 +1,4 @@ | ||
| # 자동차 경주 미션 | ||
| # 로또 미션 | ||
|
|
||
| ## 참고링크 및 저장소 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { describe, test, expect } from "vitest"; | ||
| import { Lotto } from "../model/index.js"; | ||
|
|
||
| describe("Lotto 클래스 테스트.", () => { | ||
| test.each([ | ||
| { value: 1 }, | ||
| { value: "1" }, | ||
| { value: true }, | ||
| { value: null }, | ||
| { value: undefined }, | ||
| { value: {} }, | ||
| ])( | ||
| "로또 번호로 적합하지 않은 값($value)을 받으면 오류가 발생한다.", | ||
| ({ value }) => { | ||
| expect(() => new Lotto(value)).toThrowError( | ||
| "[ERR_002] Lotto 클래스의 생성자 인수는 배열이어야 합니다." | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| test("로또 번호가 6개 초과이면 오류가 발생한다.", () => { | ||
| expect(() => new Lotto([1, 2, 3, 4, 5, 6, 7])).toThrowError( | ||
| "[ERR_002] Lotto 클래스의 생성자 인수의 길이는 6이어야 합니다." | ||
| ); | ||
| }); | ||
|
|
||
| test("로또 번호가 6개 미만이면 오류가 발생한다.", () => { | ||
| expect(() => new Lotto([1, 2, 3, 4, 5])).toThrowError( | ||
| "[ERR_002] Lotto 클래스의 생성자 인수의 길이는 6이어야 합니다." | ||
| ); | ||
| }); | ||
|
|
||
| test("로또 번호를 가져옵니다.", () => { | ||
| const lotto = new Lotto([1, 2, 3, 4, 5, 6]); | ||
|
|
||
| expect(lotto.numbers).toEqual([1, 2, 3, 4, 5, 6]); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { describe, test, expect } from "vitest"; | ||
| import { LottoMachine } from "../model/index.js"; | ||
|
|
||
| describe("LottoMachine 클래스 테스트", () => { | ||
| test("로또를 구입할 때 정수가 아닌 값을 받으면 오류가 발생한다.", () => { | ||
| expect(() => new LottoMachine("1")).toThrowError( | ||
| "[ERR_004] LottoMachine 클래스의 생성자 인수는 정수여야 합니다." | ||
| ); | ||
| }); | ||
|
|
||
| test("로또를 구입할 때 1000원미만의 값을 받으면 오류가 발생한다.", () => { | ||
| expect(() => new LottoMachine(999)).toThrowError( | ||
| "[ERR_004] LottoMachine 클래스의 생성자 인수는 1000이상이어야 합니다." | ||
| ); | ||
| }); | ||
|
|
||
| test.each([ | ||
| { price: 1001, count: 1 }, | ||
| { price: 2500, count: 2 }, | ||
| { price: 3999, count: 3 }, | ||
| { price: 10999, count: 10 }, | ||
| ])( | ||
| "로또를 $price원만큼 구매하면 $count장을 발행한다.", | ||
| ({ price, count }) => { | ||
| const lottoMachine = new LottoMachine(price); | ||
|
|
||
| expect(lottoMachine.count).toBe(count); | ||
| } | ||
| ); | ||
|
|
||
| test("무작위 번호 6개가 적힌 로또를 받는다.", () => { | ||
| const lottoMachine = new LottoMachine(1000); | ||
|
|
||
| const lotto = lottoMachine.lottos[0]; | ||
|
|
||
| expect(lotto.numbers).toHaveLength(6); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { describe, expect, test, vi } from "vitest"; | ||
| import { | ||
| LottoMachine, | ||
| WinningLotto, | ||
| LottoMatcher, | ||
| Lotto, | ||
| } from "../model/index.js"; | ||
| import { LOTTO } from "../constants/index.js"; | ||
|
|
||
| describe("LottoMatcher 클래스 테스트", () => { | ||
| test("1등부터 5등까지 당첨된 수를 가져온다.", () => { | ||
| const { lottos } = new LottoMachine(6000); | ||
| const winningLotto = new WinningLotto([1, 2, 3, 4, 5, 6], 7); | ||
|
|
||
| const { winningCounts } = new LottoMatcher(lottos, winningLotto); | ||
|
|
||
| expect(winningCounts).toHaveProperty("1"); | ||
| expect(winningCounts).toHaveProperty("2"); | ||
| expect(winningCounts).toHaveProperty("3"); | ||
| expect(winningCounts).toHaveProperty("4"); | ||
| expect(winningCounts).toHaveProperty("5"); | ||
|
|
||
| expect(typeof winningCounts[1]).toBe("number"); | ||
| expect(typeof winningCounts[2]).toBe("number"); | ||
| expect(typeof winningCounts[3]).toBe("number"); | ||
| expect(typeof winningCounts[4]).toBe("number"); | ||
| expect(typeof winningCounts[5]).toBe("number"); | ||
| }); | ||
|
|
||
| test("로또 번호 중에 1등 당첨이 하나있다.", () => { | ||
| const { lottos } = createLottoMachineMock([[1, 2, 3, 4, 5, 6]]); | ||
| const winningLotto = new WinningLotto([1, 2, 3, 4, 5, 6], 7); | ||
|
|
||
| const { winningCounts } = new LottoMatcher(lottos, winningLotto); | ||
|
|
||
| expect(winningCounts).toHaveProperty("1", 1); | ||
| }); | ||
|
|
||
| test("로또 번호 중에 2등 당첨이 하나있다.", () => { | ||
| const { lottos } = createLottoMachineMock([[1, 2, 3, 4, 5, 7]]); | ||
| const winningLotto = new WinningLotto([1, 2, 3, 4, 5, 6], 7); | ||
|
|
||
| const { winningCounts } = new LottoMatcher(lottos, winningLotto); | ||
|
|
||
| expect(winningCounts).toHaveProperty("2", 1); | ||
| }); | ||
|
|
||
| test("로또 번호 중에 3등 당첨이 하나있다.", () => { | ||
| const { lottos } = createLottoMachineMock([[1, 2, 3, 4, 5, 8]]); | ||
| const winningLotto = new WinningLotto([1, 2, 3, 4, 5, 6], 7); | ||
|
|
||
| const { winningCounts } = new LottoMatcher(lottos, winningLotto); | ||
|
|
||
| expect(winningCounts).toHaveProperty("3", 1); | ||
| }); | ||
|
|
||
| test("로또 번호 중에 4등 당첨이 하나있다.", () => { | ||
| const { lottos } = createLottoMachineMock([[1, 2, 3, 4, 8, 9]]); | ||
| const winningLotto = new WinningLotto([1, 2, 3, 4, 5, 6], 7); | ||
|
|
||
| const { winningCounts } = new LottoMatcher(lottos, winningLotto); | ||
|
|
||
| expect(winningCounts).toHaveProperty("4", 1); | ||
| }); | ||
|
|
||
| test("로또 번호 중에 5등 당첨이 하나있다.", () => { | ||
| const { lottos } = createLottoMachineMock([[1, 2, 3, 8, 9, 10]]); | ||
| const winningLotto = new WinningLotto([1, 2, 3, 4, 5, 6], 7); | ||
|
|
||
| const { winningCounts } = new LottoMatcher(lottos, winningLotto); | ||
|
|
||
| expect(winningCounts).toHaveProperty("5", 1); | ||
| }); | ||
| }); | ||
|
|
||
| function createLottoMachineMock(numbersArray) { | ||
| const lottos = numbersArray.map((numbers) => new Lotto(numbers)); | ||
| const lottoMachineMock = vi.fn(); | ||
| lottoMachineMock.mockReturnValueOnce({ | ||
| price: lottos.length * LOTTO.PRICE, | ||
| lottos, | ||
| }); | ||
|
|
||
| return lottoMachineMock(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { LottoNumber } from "../model/index.js"; | ||
|
|
||
| describe("LottoNumber 클래스 테스트", () => { | ||
| test.each([ | ||
| { value: "a" }, | ||
| { value: true }, | ||
| { value: false }, | ||
| { value: undefined }, | ||
| { value: null }, | ||
| { value: [] }, | ||
| { value: {} }, | ||
| ])( | ||
| "로또 번호로 적합하지 않은 값($value)을 할당하면 오류가 발생한다.", | ||
| ({ value }) => { | ||
| expect(() => new LottoNumber(value)).toThrowError( | ||
| "[ERR_001] LottoNumber 클래스의 생성자 인수는 정수여야 합니다." | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| test("로또 번호가 1보다 작으면 오류가 발생한다.", () => { | ||
| expect(() => new LottoNumber(0)).toThrowError( | ||
| "[ERR_001] LottoNumber 클래스의 생성자 인수는 1이상이어야 합니다." | ||
| ); | ||
| }); | ||
|
|
||
| test("로또 번호가 45보다 크면 오류가 발생한다.", () => { | ||
| expect(() => new LottoNumber(46)).toThrowError( | ||
| "[ERR_001] LottoNumber 클래스의 생성자 인수는 45이하여야 합니다." | ||
| ); | ||
| }); | ||
|
|
||
| test("로또 번호를 정한다.", () => { | ||
| const number = 1; | ||
|
|
||
| const lottoNumber = new LottoNumber(number); | ||
|
|
||
| expect(lottoNumber.value).toBe(1); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { LottoReturnCalculator } from "../model/index.js"; | ||
|
|
||
| describe("LottoReturnCalculator 클래스 테스트", () => { | ||
| test("수익률을 가져온다.", () => { | ||
| const winningCounts = { | ||
| 1: 0, | ||
| 2: 0, | ||
| 3: 0, | ||
| 4: 0, | ||
| 5: 1, | ||
| }; | ||
| const price = 2000; | ||
|
|
||
| const { rateOfReturn } = new LottoReturnCalculator(winningCounts, price); | ||
|
|
||
| expect(rateOfReturn).toBe(250); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { WinningLotto } from "../model/index.js"; | ||
|
|
||
| describe("WinningLotto 클래스 테스트", () => { | ||
| test("당첨 번호 중에 보너스 번호와 중복되는 번호가 있으면 오류가 발생한다.", () => { | ||
| const lottoNumbers = [1, 2, 3, 4, 5, 6]; | ||
| const bonusNumber = 6; | ||
|
|
||
| expect(() => new WinningLotto(lottoNumbers, bonusNumber)).toThrowError( | ||
| "[ERR_003] WinningLotto 클래스의 생성자 인수인 winningNumbers 중에 bonusNumber와 중복됩니다." | ||
| ); | ||
| }); | ||
|
|
||
| test("보너스 번호를 가져온다.", () => { | ||
| const lottoNumbers = [1, 2, 3, 4, 5, 6]; | ||
| const bonusNumber = 45; | ||
|
|
||
| const winningLotto = new WinningLotto(lottoNumbers, bonusNumber); | ||
|
|
||
| expect(winningLotto.bonusNumber).toBe(45); | ||
| }); | ||
| }); |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export const USER_FRIENDLY_ERROR_MESSAGES = { | ||
| ERR_001: | ||
| "입력하신 값은 로또 번호로 사용할 수 없습니다. 1부터 45 사이의 숫자를 입력해 주세요.", | ||
| ERR_002: "로또 번호는 중복 없이 6개의 서로 다른 숫자를 입력해 주세요.", | ||
| ERR_003: | ||
| "보너스 번호가 당첨 번호와 중복되었습니다. 당첨 번호 6개와 중복되지 않는 보너스 번호 1개를 포함하여 총 7개의 번호를 다시 입력해 주세요.", | ||
| ERR_004: "로또 구매 금액은 1,000원 단위로 입력해 주세요.", | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from "./lotto.js"; | ||
| export * from "./errorMessage.js"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| export const LOTTO = { | ||
| NUMBERS_SIZE: 6, | ||
| MIN_NUMBER: 1, | ||
| MAX_NUMBER: 45, | ||
| PRICE: 1000, | ||
| RANKING_INFO: [ | ||
| { | ||
| ranking: 1, | ||
| matchingCount: 6, | ||
| prizeMoney: 2_000_000_000, | ||
| }, | ||
| { | ||
| ranking: 2, | ||
| matchingCount: 5, | ||
| isBonusMatch: true, | ||
| prizeMoney: 30_000_000, | ||
| }, | ||
|
Comment on lines
+7
to
+17
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. isBonusMatch를 사용하지 않는다면
Author
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. 저도 처음에는 그렇게 작성했는데 ai들이 없어도 된다고 해서... |
||
| { | ||
| ranking: 3, | ||
| matchingCount: 5, | ||
| prizeMoney: 1_500_000, | ||
| }, | ||
| { | ||
| ranking: 4, | ||
| matchingCount: 4, | ||
| prizeMoney: 50_000, | ||
| }, | ||
| { | ||
| ranking: 5, | ||
| matchingCount: 3, | ||
| prizeMoney: 5_000, | ||
| }, | ||
| ], | ||
| }; | ||
|
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. controller를 여러개로 분리해놓으셨군요...! 이런 방법도 있을 수 있겠네요 🤔
Author
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. 사실 좋은 방법인지는 모르겠지만 지금거에서 좀더 수정해서 하나의 작업단위로 분리를 해볼까 합니다! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { LOTTO } from "../constants/index.js"; | ||
| import { LottoMatcher, LottoReturnCalculator } from "../model/index.js"; | ||
|
|
||
| const calculateWinningResults = ({ lottos, winningLotto }) => { | ||
| const { winningCounts } = new LottoMatcher(lottos, winningLotto); | ||
|
|
||
| const price = lottos.length * LOTTO.PRICE; | ||
|
|
||
| const { rateOfReturn } = new LottoReturnCalculator(winningCounts, price); | ||
|
|
||
| return { winningCounts, rateOfReturn }; | ||
| }; | ||
|
|
||
| export default calculateWinningResults; |
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.
에러를 번호로 구분하게 되면 에러를 명시적으로 작성하는 것과 달리 어떤 장점이 있나요?
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.
이렇게하면 개발자용 에러 메세지랑 사용자용 에러 메세지를 에러 코드를 통해 관리할 수 있어요!