From 4d80b27c9fd45be89c5b7585dedff18c83761c21 Mon Sep 17 00:00:00 2001 From: HJEHA Date: Wed, 16 Feb 2022 21:53:57 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20JuiceMaker=20make(with:=20)=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Unit test 통과 - FruitStoreError Error 프로토콜 제거 --- .../JuiceMaker.xcodeproj/project.pbxproj | 6 +- JuiceMaker/JuiceMaker/Model/FruitStore.swift | 2 +- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 14 +++++ .../JuiceMaker/Model/JuiceRecipes.swift | 16 ++--- .../JuiceMakerTests/JuiceMakerTests.swift | 60 +++++++++++++++++++ 5 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 JuiceMaker/JuiceMakerTests/JuiceMakerTests.swift diff --git a/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj b/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj index 886a26610..d43b353c4 100644 --- a/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj +++ b/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj @@ -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 */; }; @@ -31,6 +32,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 08A08DD827BD25930018CFFE /* JuiceMakerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JuiceMakerTests.swift; sourceTree = ""; }; 73B6685127BBE7CE005CF6E3 /* FruitType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitType.swift; sourceTree = ""; }; 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 = ""; }; @@ -69,6 +71,7 @@ isa = PBXGroup; children = ( 73B6685927BBFEE3005CF6E3 /* FruitStoreTests.swift */, + 08A08DD827BD25930018CFFE /* JuiceMakerTests.swift */, ); path = JuiceMakerTests; sourceTree = ""; @@ -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 = ""; @@ -234,6 +237,7 @@ buildActionMask = 2147483647; files = ( 73B6686127BC026A005CF6E3 /* FruitStoreTests.swift in Sources */, + 08A08DD927BD25930018CFFE /* JuiceMakerTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index 0a925cf1e..c5191734e 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -6,7 +6,7 @@ import Foundation -enum FruitStoreError: Error { +enum FruitStoreError { case notEnoughFruits } diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 862a94f62..b6e0ca7c3 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -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 { + let result = fruitStore.use(of: recipe.items) + switch result { + case .success(): + return .success(Void()) + case .failure(let error): + return .failure(error) + } + } } diff --git a/JuiceMaker/JuiceMaker/Model/JuiceRecipes.swift b/JuiceMaker/JuiceMaker/Model/JuiceRecipes.swift index b770a34ae..44d8db90c 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceRecipes.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceRecipes.swift @@ -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 ] diff --git a/JuiceMaker/JuiceMakerTests/JuiceMakerTests.swift b/JuiceMaker/JuiceMakerTests/JuiceMakerTests.swift new file mode 100644 index 000000000..71cd066d3 --- /dev/null +++ b/JuiceMaker/JuiceMakerTests/JuiceMakerTests.swift @@ -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() + } + } +}