Skip to content

Commit

Permalink
feat: JuiceMaker make(with: ) 생성 #1
Browse files Browse the repository at this point in the history
- Unit test 통과
- FruitStoreError Error 프로토콜 제거
  • Loading branch information
HJEHA committed Feb 16, 2022
1 parent 19577db commit 4d80b27
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 10 deletions.
6 changes: 5 additions & 1 deletion JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
08A08DD927BD25930018CFFE /* JuiceMakerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A08DD827BD25930018CFFE /* JuiceMakerTests.swift */; };
73B6685227BBE7CE005CF6E3 /* FruitType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73B6685127BBE7CE005CF6E3 /* FruitType.swift */; };
73B6686127BC026A005CF6E3 /* FruitStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73B6685927BBFEE3005CF6E3 /* FruitStoreTests.swift */; };
73B6686327BD18D3005CF6E3 /* JuiceRecipes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73B6686227BD18D3005CF6E3 /* JuiceRecipes.swift */; };
Expand All @@ -31,6 +32,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
08A08DD827BD25930018CFFE /* JuiceMakerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JuiceMakerTests.swift; sourceTree = "<group>"; };
73B6685127BBE7CE005CF6E3 /* FruitType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitType.swift; sourceTree = "<group>"; };
73B6685727BBFEE3005CF6E3 /* JuiceMakerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = JuiceMakerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
73B6685927BBFEE3005CF6E3 /* FruitStoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitStoreTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -69,6 +71,7 @@
isa = PBXGroup;
children = (
73B6685927BBFEE3005CF6E3 /* FruitStoreTests.swift */,
08A08DD827BD25930018CFFE /* JuiceMakerTests.swift */,
);
path = JuiceMakerTests;
sourceTree = "<group>";
Expand All @@ -87,9 +90,9 @@
isa = PBXGroup;
children = (
C73DAF4B255D0D0400020D38 /* JuiceMaker.swift */,
73B6686227BD18D3005CF6E3 /* JuiceRecipes.swift */,
C71CD66A266C7ACB0038B9CB /* FruitStore.swift */,
73B6685127BBE7CE005CF6E3 /* FruitType.swift */,
73B6686227BD18D3005CF6E3 /* JuiceRecipes.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -234,6 +237,7 @@
buildActionMask = 2147483647;
files = (
73B6686127BC026A005CF6E3 /* FruitStoreTests.swift in Sources */,
08A08DD927BD25930018CFFE /* JuiceMakerTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion JuiceMaker/JuiceMaker/Model/FruitStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Foundation

enum FruitStoreError: Error {
enum FruitStoreError {
case notEnoughFruits
}

Expand Down
14 changes: 14 additions & 0 deletions JuiceMaker/JuiceMaker/Model/JuiceMaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,19 @@ import Foundation

// 쥬스 메이커 타입
struct JuiceMaker {
private var fruitStore: FruitStore

init(fruitStore: FruitStore = FruitStore()) {
self.fruitStore = fruitStore
}

mutating func make(with recipe: RecipeProtocol) -> Result<Void, Error> {
let result = fruitStore.use(of: recipe.items)
switch result {
case .success():
return .success(Void())
case .failure(let error):
return .failure(error)
}
}
}
16 changes: 8 additions & 8 deletions JuiceMaker/JuiceMaker/Model/JuiceRecipes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,48 @@
import Foundation

protocol RecipeProtocol {
var recipe: [FruitType: Int] { get }
var items: [FruitType: Int] { get }
}

struct StrawberryJuice: RecipeProtocol {
var recipe: [FruitType: Int] = [
var items: [FruitType: Int] = [
.strawberry: 16
]
}

struct BananaJuice: RecipeProtocol {
var recipe: [FruitType: Int] = [
var items: [FruitType: Int] = [
.banana: 2
]
}

struct PineappleJuice: RecipeProtocol {
var recipe: [FruitType: Int] = [
var items: [FruitType: Int] = [
.pineapple: 2
]
}

struct KiwiJuice: RecipeProtocol {
var recipe: [FruitType: Int] = [
var items: [FruitType: Int] = [
.kiwi: 3
]
}

struct MangoJuice: RecipeProtocol {
var recipe: [FruitType: Int] = [
var items: [FruitType: Int] = [
.mango: 3
]
}

struct StrawberryBananaJuice: RecipeProtocol {
var recipe: [FruitType: Int] = [
var items: [FruitType: Int] = [
.strawberry: 10,
.banana: 1
]
}

struct MangoKiwiJuice: RecipeProtocol {
var recipe: [FruitType: Int] = [
var items: [FruitType: Int] = [
.mango: 2,
.kiwi: 1
]
Expand Down
60 changes: 60 additions & 0 deletions JuiceMaker/JuiceMakerTests/JuiceMakerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// JuiceMakerTests.swift
// JuiceMakerTests
//
// Created by 황제하 on 2022/02/16.
//

import XCTest
@testable import JuiceMaker

private extension XCTestCase {
func XCTSuccess() {
XCTAssert(true)
}
}

struct MockJuice: RecipeProtocol {
var items: [FruitType : Int]
}

class JuiceMakerTests: XCTestCase {

// MARK: - Test codes

func test_인벤토리보다_적은과일쥬스를_만들었을때_성공을_반환하는가() {
// given
let fruitStore = FruitStore(initialFruitCount: 10)
var juiceMaker = JuiceMaker(fruitStore: fruitStore)
let input = MockJuice(items: [.banana: 5])

// when
let writeResult = juiceMaker.make(with: input)

// then
switch writeResult {
case .success():
XCTSuccess()
case .failure(let error):
XCTFail(error.localizedDescription)
}
}

func test_인벤토리보다_많은과일쥬스를_만들었을때_실패을_반환하는가() {
// given
let fruitStore = FruitStore(initialFruitCount: 10)
var juiceMaker = JuiceMaker(fruitStore: fruitStore)
let input = MockJuice(items: [.banana: 15])

// when
let writeResult = juiceMaker.make(with: input)

// then
switch writeResult {
case .success():
XCTFail()
case .failure(_):
XCTSuccess()
}
}
}

0 comments on commit 4d80b27

Please sign in to comment.