Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
a1b0bbd
style: 불필요한 주석 제거
bar-d May 17, 2022
e858cb9
feat: struct구조체 생성
bar-d May 17, 2022
2a8aa87
refactor: CalculatorItemQueue 타입을 제너릭 타입으로 변경
bar-d May 17, 2022
3c5b512
style: 네이밍 변경
bar-d May 17, 2022
5e976a6
refactor: xCode내 에러로 인해 info.plist삭제 후 폴더에 재추가
bar-d May 17, 2022
d75b2fb
style: test코드 내 네이밍 변경
bar-d May 17, 2022
f1d1c8b
style: 불필요한 여백 제거
bar-d May 17, 2022
01eef00
refactor: QueueEror열거형 생성 및 함수 리팩토링
bar-d May 17, 2022
a0a86f5
refactor: peek이라는 프로퍼티가 반환하는 값을 검증하는 테스트 리팩토링
bar-d May 17, 2022
9046d8a
refactor: enQueue함수를 호출했을 때 왼쪽 스택에 값을 더하는지 검증하는 테스트코드 리팩토링
bar-d May 17, 2022
a55f714
refactor: 테스트 코드 리팩토링 및 중복되는 테스트 코드 제거
bar-d May 17, 2022
f5dcff8
style: test함수 내 들여쓰기 통일
bar-d May 17, 2022
923d247
docs: README.md 파일 수정
bar-d May 17, 2022
af5f010
refactor: 접근제한자 생성
bar-d May 17, 2022
4ab5bd4
refactor: Queue 프로토콜 및 CalculatorItemQueue 구조체 리팩토링
bar-d May 17, 2022
a5b027b
refactor: test코드 리팩토링
bar-d May 17, 2022
762d3d2
refactor: test코드 구현 완료 후 접근제한 설정
bar-d May 17, 2022
996565b
refactor: 테스트 코드 sut의 타입 옵셔널 CalculatorItemQueue로 변경
bar-d May 18, 2022
246833b
refactor: sut의 타입을 CalculatorItemQueue옵셔널타입으로 변경
bar-d May 18, 2022
251a16a
style: QueueError열거형 내 케이스 네이밍 변경
bar-d May 18, 2022
8889676
refactor: CalculatorItemQueue 구조체 내 함수 및 테스트코드 리팩토링
bar-d May 18, 2022
908efaa
add: Formula구조체 ExpressionParser열거형 생성
bar-d May 18, 2022
db2bcf2
add: UML을 토대로 파일 생성 및 타입 프레임 생성
bar-d May 18, 2022
95a8f39
refactor: Operator 내 함수 내부 구현
bar-d May 19, 2022
63f21fb
refactor: extension 제거
bar-d May 19, 2022
47fef64
refactor: UML에 따른 접근제한자 설정
bar-d May 19, 2022
6e498d7
refactor: Queue, CalculatorItemQueue 구조체 리팩토링
bar-d May 19, 2022
cd51a0f
refactor: CalculatorItemQueue 구조체 리팩토링
bar-d May 19, 2022
0b3abdc
refactor: test코드 리팩토링
bar-d May 19, 2022
1cebe3b
refactor: 에러처리 및 들여쓰기 수정
bar-d May 19, 2022
ea890bf
refactor: Formula구조체 구현 및 리팩토링
bar-d May 19, 2022
470118b
add: String extension TDD를 통해 구현
bar-d May 20, 2022
300891a
refactor: CalculatorItemQueue 리팩토링
bar-d May 20, 2022
2612cac
add: OperatorError 및 Double extension 생성
bar-d May 20, 2022
819c209
refactor: Operator 열거형 에러처리
bar-d May 20, 2022
43ddcfe
refactor: Formula 및 FormulaTests 리팩토링
bar-d May 20, 2022
7c1188a
add: ExpressionParser TDD기반 구현 및 테스트코드 생성
bar-d May 20, 2022
e62b8c2
style: 여백 및 컨벤션 수정
bar-d May 20, 2022
5b1e771
style: 여백제거
bar-d May 20, 2022
4ce4647
refactor: 불필요한 라이브러리 제거
bar-d May 20, 2022
aa0ce9b
Merge branch 'ic_6_barrd' into step2
bar-d May 21, 2022
380efe3
refactor: 테스트 코드 리팩토링
bar-d May 21, 2022
ba58cf6
Merge remote-tracking branch 'refs/remotes/origin/step2'
bar-d May 21, 2022
805b769
refactor: info.plist 제거 및 타겟 멤버십 재설정
bar-d May 21, 2022
c94e67c
test: info.plist 제거 및 타겟 멤버십 재설정이 잘되었는지 확인하기 위한 공백 추가
bar-d May 21, 2022
d02ecdd
style: OperatorError내 케이스 네이밍 변경 및 String 파일 네이밍 변경
bar-d May 22, 2022
98ddc56
refactor: Double extension CalculateItem 프로토콜 채택
bar-d May 22, 2022
de398ee
style: QueueError내 case구문 네이밍 변경
bar-d May 22, 2022
394db33
style: Operator내 에러처리 변경
bar-d May 22, 2022
42fcfe2
refactor: ExpressionParser내 componentsByOperators함수 접근제한 변경
bar-d May 22, 2022
5f807db
style: Formular 구조체 내 개행처리 변경
bar-d May 22, 2022
c262dcf
refactor: Formula구조체 및 ExpressionParserTests 리팩토링
bar-d May 22, 2022
aa81a73
style: 테스트 코드 내 개행 처리 및 주석 재 배치
bar-d May 22, 2022
b8b8778
style: parseTests 내 불필요한 변수 제거 및 컨벤션 통일
bar-d May 22, 2022
c1d1a0c
refactor: componentsByOperatorsTests내 사용하지 않는 변수 제거
bar-d May 22, 2022
989610d
style: Formula 구조체 내 컨벤션 수정
bar-d May 22, 2022
19a8877
style: 컨벤션 수정 및 공백제거
bar-d May 22, 2022
f3cb41d
refactor: FormulaTests코드 리팩토링
bar-d May 22, 2022
cded86a
style: 코드 전반적인 컨벤션 수정
bar-d May 22, 2022
5a99bca
docs: README.md 파일 수정
bar-d May 22, 2022
af63b41
refactor: Formula구조체 내 while구문 리팩토링
bar-d May 22, 2022
f9d5f4c
style: 컨벤션 수정
bar-d May 22, 2022
ea8d728
refactor: Operator 열거형 내 divide 케이스 리팩토링
bar-d May 22, 2022
c012a17
refactor: OperatorError내 errorDescription수정
bar-d May 22, 2022
1ca638d
refactor: ExpressionParser내 throws 제거
bar-d May 22, 2022
3ed1c88
test: test코드 추가
bar-d May 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
749 changes: 717 additions & 32 deletions Calculator/Calculator.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,46 @@
ReferencedContainer = "container:Calculator.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D7A1C35D28364C17004767E3"
BuildableName = "FormulaTests.xctest"
BlueprintName = "FormulaTests"
ReferencedContainer = "container:Calculator.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D7A1C37E2836D65B004767E3"
BuildableName = "ExpressionParser.xctest"
BlueprintName = "ExpressionParser"
ReferencedContainer = "container:Calculator.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D7A1C3962837553A004767E3"
BuildableName = "componentsByOperatorsTests.xctest"
BlueprintName = "componentsByOperatorsTests"
ReferencedContainer = "container:Calculator.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D7A1C3B728375EB2004767E3"
BuildableName = "splitTests.xctest"
BlueprintName = "splitTests"
ReferencedContainer = "container:Calculator.xcodeproj">
</BuildableReference>
</TestableReference>
Comment on lines +41 to +80

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

각 테스트들을 별도의 타깃으로 만들어주셔서 사용한 의도가 궁금합니다🙋🏻
이렇다면 각 테스트할 실 코드의 모듈들의 타겟을 다 지정해주어야하는데 번거롭지 않을까요..?
이미 기존에 있던 CalculatorTests 타겟에서 각 파일로 생성함으로 구분지어줘도 충분할것 같다고 느껴졌어요🤔

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

별도의 파일을 만들어볼 생각을 안하고 각 테스트 별로 테스트 타겟을 만들어야 하는줄 알았습니다..!

</Testables>
</TestAction>
<LaunchAction
Expand Down
17 changes: 10 additions & 7 deletions Calculator/Calculator/Model/CalculatorItemQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
struct CalculatorItemQueue<T>: Queue, CalculateItem {
private(set) var enQueueStack: Array<T> = []
private(set) var deQueueStack: Array<T> = []
private(set) var joinedQueue: Array<T> = []

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요거 실제로 필요한 변수인가요~?🙋🏻
deQueue에서 이미 enQueue를 리버스하고 enQueue를 리무브해준다음
deQueue에 역순으로 잘 들어간걸 아래 deQueue 메서드를 Formula에서 while을 돌려주는걸로 이해됐는데
결국 아래 deQueue메서드에서 deQueueStack과 빈 어레이를 합쳐주는것 같아서 필요한가 궁금했습니다!
혹시 제가 놓친게 있다면 설명 부탁드립니다🙏🏻

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

joinedQueue라는 변수를 만들어준 이유는 한번 더 검증을 해보자는 의미에서 만들어준 변수입니다.
물론 로직상에서는 충분히 불필요한 변수로 보입니다만, 혹시라도 잘못된 값이 나올 수 있지 않을까라는 생각에 만들어줬는데,
제 로직에 확신이 있다면 불필요한 변수가 될까요

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 불필요한 변수일거라 생각듭니다..!
만약 확신이 없어 검증을 해주는 용도라면 불필요한 변수가 생기지 않도록 로직을 수정하는편이 맞을것 같다고 생각해요🙋🏻
말씀대로 로직에 확신이 있다면 제거되어도 될것 같아요.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그 로직에 확신을 갖기위해 테스트 코드를 짜는것이라 생각들고 확신을 갖기 위해 여러 테스트 케이스를 거칠거라고 생각들어서요!
그걸 다 통과하면 확신을 가져도 되지 않을까요?


public mutating func enQueue(_ input: T) {
mutating func enQueue(_ input: T) {
enQueueStack.append(input)
}

public mutating func deQueue() throws -> T? {
if enQueueStack.isEmpty {
throw QueueError.empty
}
else {
mutating func deQueue() throws -> T {
if deQueueStack.isEmpty {
deQueueStack = enQueueStack.reversed()
enQueueStack.removeAll()
}
return deQueueStack.popLast()
guard let deQueue = deQueueStack.popLast() else {
throw QueueError.unknown
}

joinedQueue = enQueueStack + deQueueStack
return deQueue
}

public mutating func clearAllStacks() {
Expand Down
26 changes: 26 additions & 0 deletions Calculator/Calculator/Model/Enum/Error/OperatorError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// OperatorError.swift
// Calculator
//
// Created by 김동용 on 2022/05/21.
//

import Foundation

enum OperatorError: LocalizedError {
case devideFail
case unknown
}

extension OperatorError {
var errorDescription: String {
switch self {
case .devideFail:
return NSLocalizedString("NaN",
comment: "devide error")
case .unknown:
return NSLocalizedString("unknown error",
comment: "unknown error")
}
}
}
34 changes: 34 additions & 0 deletions Calculator/Calculator/Model/Enum/Error/QueueError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// QueueError.swift
// Calculator
//
// Created by 김동용 on 2022/05/17.
//

import Foundation

enum QueueError: LocalizedError {
case empty
case unknown
case wrongOperators
case wrongOperands
}

extension QueueError {
var errorDescription: String {
switch self {
case .empty:
return NSLocalizedString("empty",
comment: "deQueue stack is empty")
case .unknown:
return NSLocalizedString("unknown error",
comment: "unknown error")
case .wrongOperators:
return NSLocalizedString("operators error",
comment: "operators got error")
case .wrongOperands:
return NSLocalizedString("operands error",
comment: "operands got error")
}
}
}
28 changes: 28 additions & 0 deletions Calculator/Calculator/Model/Enum/ExpressionParser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// ExpressionParser.swift
// Calculator
//
// Created by 김동용 on 2022/05/18.
//

enum ExpressionParser {
static func parse(from input: String) -> Formula {
var operandsQueue = CalculatorItemQueue<Double>()
var operatorQueue = CalculatorItemQueue<Operator>()

let operands = input.compactMap { Double(String($0)) }
let operators = componentsByOperators(from: input)
.filter{$0.count == 1}
.compactMap{ Operator(rawValue: Character($0)) }
Comment on lines +15 to +16

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요기 로직 설명 한번 해주실 수 있을까요?🙋🏻

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

연산자는 +, -, *, /이므로 카운트가 하나밖에 될 수가 없습니다. 하지만 숫자 같은 경우는 음수나 더블 형태가 나올 수 있기에 filter로 1차로 한 번 검증을 해주었고, 컨테이너 내 값들을 Character로 변환을 해주었을 때, 연산자가 아닌 것들은 nil값이 나오기에 compactMap을 통해 nil값을 제거해주었습니다.


operands.forEach { operandsQueue.enQueue($0) }
operators.forEach { operatorQueue.enQueue($0) }

return Formula(operands: operandsQueue,
operators: operatorQueue)
}

static private func componentsByOperators(from input: String) -> [String] {
return input.split(with: " ")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문자열을 공백 기준으로 쪼개주는데 그렇다면 추후 UI에서 버튼 클릭후 연산자 혹은 피연산자 입력 시 공백이 같이 무조건 들어가게 되는건가요?

@bar-d bar-d May 22, 2022 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

연산자버튼이 눌릴 때 연산자 앞뒤로 공백을 더해주면 된다고 생각했는데 다시 한번 생각해보겠습니다.

}
}
43 changes: 43 additions & 0 deletions Calculator/Calculator/Model/Enum/Operator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Operator.swift
// Calculator
//
// Created by 김동용 on 2022/05/18.
//

enum Operator: Character, CaseIterable {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CaseIterable은 어떤 용도로 쓰이는것이며 해당 프로젝트에서는 어떻게 쓰이게 되나요~?🙋🏻

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

caseIterable을 채택함으로써 열거형 케이스를 배열처럼 사용할 수 있는 것으로 알고 있습니다.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 프로젝트에서 넣어준 이유를 생각하면 caseIterable을 사용할려면 피연산자와 연산자를 나누는 로직을 변경해봐야 할 것 같습니다.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아니면 현재 상황에서는 쓰일 일이 없지만 연산자 버튼을 컬렉션 타입으로 만들어준다면 CaseIterable을 사용할 수 있을 것 같습니다.

case add = "+"
case subtract = "-"
case divide = "/"
case multiply = "*"

func calculate(lhs: Double, rhs: Double) throws -> Double {
switch self {
case .add:
return add(lhs: lhs, rhs: rhs)
case .subtract:
return subtract(lhs: lhs, rhs: rhs)
case .divide:
return try divide(lhs: lhs, rhs: rhs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

띄어쓰기가 뎁스가 한단계 더 된것 같습니다🙋🏻

case .multiply:
return multiply(lhs: lhs, rhs: rhs)
}
}

private func add(lhs: Double, rhs: Double) -> Double {
return lhs + rhs
}

private func subtract(lhs: Double, rhs: Double) -> Double {
return lhs - rhs
}

private func divide(lhs: Double, rhs: Double) throws -> Double {
guard rhs != 0 else { throw OperatorError.devideFail }
return lhs / rhs
}

private func multiply(lhs: Double, rhs: Double) -> Double {
return lhs * rhs
}
}
8 changes: 8 additions & 0 deletions Calculator/Calculator/Model/Extension/Double.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// Double.swift

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

얘도 기본 Double 타입의 extension이면 파일명의 컨벤션을 맞춰볼 수 있을것 같습니다🙋🏻

// Calculator
//
// Created by 김동용 on 2022/05/20.
//

extension Double: CalculateItem { }
12 changes: 12 additions & 0 deletions Calculator/Calculator/Model/Extension/String+extensions .swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// String.swift
// Calculator
//
// Created by 김동용 on 2022/05/20.
//

extension String {
func split(with target: Character) -> [String] {
return self.split(separator: target).map { String($0) }
}
}
35 changes: 35 additions & 0 deletions Calculator/Calculator/Model/Formula.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Formula.swift
// Calculator
//
// Created by 김동용 on 2022/05/18.
//

struct Formula {
var operands = CalculatorItemQueue<Double>()
var operators = CalculatorItemQueue<Operator>()

mutating func result() throws -> Double {
guard var lastResult = try? operands.deQueue() else {
throw QueueError.wrongOperands
}

while !operands.joinedQueue.isEmpty {
guard let inputNumber = try? operands.deQueue() else {
throw QueueError.wrongOperands
}

guard let inputOperator = try? operators.deQueue() else {
throw QueueError.wrongOperators
}

do {
lastResult = try inputOperator.calculate(lhs: lastResult,
rhs: inputNumber)
} catch {
OperatorError.devideFail

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음 에러가 여기서 catch를 선언해주는게 맞을까요~?🙋🏻

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw를 넣어주고 던져주었습니다.
현재 calculate함수를 호출하는 부분이 Formula 구조체 내이기 때문에
현재 로직에서는 여기서 catch를 해주는게 아닌가요?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아마 저 30번째 라인에서 워닝 메시지가 나올텐데 어떤건지 한번 보시면 어떻게 잘못쓰였는지 확인해보실 수 있을거 같아요..!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen Shot 2022-05-23 at 4 56 28 PM

Screen Shot 2022-05-23 at 4 57 52 PM
제가 catch를 해주고 catch를 해준 것에 대해 던져줄지 프린트를 할지 리턴값을 줄지 해당 결과를 사용하지 않아서 나온 워닝 메세지인 것 같습니다.
30번째 줄에 throw를 넣어주었다고 말씀드린거였는데 혹시 제가 오해하시게 말씀드린걸까요...?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아아 제가 코멘트를 잘못읽었습니다!
죄송합니다ㅠㅠ
네네 수정하신 방향이 맞다고 보입니다..!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 감사합니다..!

}
}
return lastResult
}
}
2 changes: 1 addition & 1 deletion Calculator/Calculator/Model/Protocol/Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ protocol Queue {
associatedtype T

mutating func enQueue(_ T: T)
mutating func deQueue() throws -> T?
mutating func deQueue() throws -> T
mutating func clearAllStacks()
}
64 changes: 0 additions & 64 deletions Calculator/Calculator/View/Info.plist

This file was deleted.

Loading