From df0b3b78cf3d1e26b4f3e4b7970d6d52dedfa32d Mon Sep 17 00:00:00 2001 From: EdgSgroi Date: Mon, 13 Jul 2020 14:53:06 -0300 Subject: [PATCH 1/2] Add unit tests of BrainstormingViewModel --- Blink/Blink.xcodeproj/project.pbxproj | 4 ++ Blink/BlinkTests/BlinkTests.swift | 21 +-------- .../BrainstormingViewModelTests.swift | 44 +++++++++++++++++++ 3 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 Blink/BlinkTests/BrainstormingViewModelTests.swift diff --git a/Blink/Blink.xcodeproj/project.pbxproj b/Blink/Blink.xcodeproj/project.pbxproj index 952ecd6..04296dc 100644 --- a/Blink/Blink.xcodeproj/project.pbxproj +++ b/Blink/Blink.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ 4F5F131124B6659A00A7D9E7 /* BrainstormingViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F5F131024B6659A00A7D9E7 /* BrainstormingViewModel.swift */; }; 4F5F131324B78BCB00A7D9E7 /* VotingViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F5F131224B78BCB00A7D9E7 /* VotingViewModel.swift */; }; 4F5F131524B8DDB500A7D9E7 /* VotingViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F5F131424B8DDB500A7D9E7 /* VotingViewModelTests.swift */; }; + 4F5F131724BCCE9600A7D9E7 /* BrainstormingViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F5F131624BCCE9600A7D9E7 /* BrainstormingViewModelTests.swift */; }; A418231A24B39DDF0082962F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A418231924B39DDF0082962F /* AppDelegate.swift */; }; A418231C24B39DDF0082962F /* MenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A418231B24B39DDF0082962F /* MenuView.swift */; }; A418231E24B39DE10082962F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A418231D24B39DE10082962F /* Assets.xcassets */; }; @@ -74,6 +75,7 @@ 4F5F131024B6659A00A7D9E7 /* BrainstormingViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrainstormingViewModel.swift; sourceTree = ""; }; 4F5F131224B78BCB00A7D9E7 /* VotingViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VotingViewModel.swift; sourceTree = ""; }; 4F5F131424B8DDB500A7D9E7 /* VotingViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VotingViewModelTests.swift; sourceTree = ""; }; + 4F5F131624BCCE9600A7D9E7 /* BrainstormingViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrainstormingViewModelTests.swift; sourceTree = ""; }; A418231624B39DDF0082962F /* Blink.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Blink.app; sourceTree = BUILT_PRODUCTS_DIR; }; A418231924B39DDF0082962F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; A418231B24B39DDF0082962F /* MenuView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuView.swift; sourceTree = ""; }; @@ -359,6 +361,7 @@ A418232E24B39DE10082962F /* BlinkTests.swift */, A418233024B39DE10082962F /* Info.plist */, 4F5F131424B8DDB500A7D9E7 /* VotingViewModelTests.swift */, + 4F5F131624BCCE9600A7D9E7 /* BrainstormingViewModelTests.swift */, ); path = BlinkTests; sourceTree = ""; @@ -575,6 +578,7 @@ buildActionMask = 2147483647; files = ( A418232F24B39DE10082962F /* BlinkTests.swift in Sources */, + 4F5F131724BCCE9600A7D9E7 /* BrainstormingViewModelTests.swift in Sources */, 4F5F131524B8DDB500A7D9E7 /* VotingViewModelTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Blink/BlinkTests/BlinkTests.swift b/Blink/BlinkTests/BlinkTests.swift index 8aac0c4..1624be8 100644 --- a/Blink/BlinkTests/BlinkTests.swift +++ b/Blink/BlinkTests/BlinkTests.swift @@ -11,24 +11,5 @@ import XCTest class BlinkTests: XCTestCase { - override func setUpWithError() throws { - // Put setup code here. This method is called before the invocation of each test method in the class. - } - - override func tearDownWithError() throws { - // Put teardown code here. This method is called after the invocation of each test method in the class. - } - - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct results. - } - - func testPerformanceExample() throws { - // This is an example of a performance test case. - self.measure { - // Put the code you want to measure the time of here. - } - } - + } diff --git a/Blink/BlinkTests/BrainstormingViewModelTests.swift b/Blink/BlinkTests/BrainstormingViewModelTests.swift new file mode 100644 index 0000000..4b6ae02 --- /dev/null +++ b/Blink/BlinkTests/BrainstormingViewModelTests.swift @@ -0,0 +1,44 @@ +// +// BrainstormingViewModelTests.swift +// BlinkTests +// +// Created by Edgar Sgroi on 13/07/20. +// Copyright © 2020 Artur Carneiro. All rights reserved. +// + +import XCTest +@testable import Blink + +class BrainstormingViewModelTests: XCTestCase { + + func testConvertIdeasArrayInMatrix() { + let viewModel = BrainstormingViewModel() + let icecreams = ["Napolitano", "Milho", "Creme", "Chocolate", "Morango", "Limão", "Banana", "Açaí", "Chiclete"] + + XCTAssertFalse(icecreams.isEmpty) + + let resultIcecreams = viewModel.convertIdeasArrayInMatrix(ideas: icecreams) + XCTAssertFalse(resultIcecreams.isEmpty) + + let expectedResultIcecreams = [["Napolitano", "Milho", "Creme"], ["Chocolate", "Morango", "Limão"], ["Banana", "Açaí", "Chiclete"]] + for indexRow in 0.. Date: Mon, 13 Jul 2020 15:23:25 -0300 Subject: [PATCH 2/2] Fix the convert to matrix function The tests told us that the function that converts the String type array to a 2D String type matrix was not working due to going out of index. Now the function is fixed and the tests are validated. --- .../ViewModels/BrainstormingViewModel.swift | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Blink/Blink/Brainstorming/ViewModels/BrainstormingViewModel.swift b/Blink/Blink/Brainstorming/ViewModels/BrainstormingViewModel.swift index 6bc6e96..0d01ab3 100644 --- a/Blink/Blink/Brainstorming/ViewModels/BrainstormingViewModel.swift +++ b/Blink/Blink/Brainstorming/ViewModels/BrainstormingViewModel.swift @@ -36,16 +36,27 @@ class BrainstormingViewModel: NSObject, ObservableObject { /// This function is called with the following parameters: /// - Parameter ideas: The String array that contains the ideas sent through P2P connection. func convertIdeasArrayInMatrix(ideas: [String]) -> [[String]] { - var matrixIdeas: [[String]] = [[String]]() + var matrixIdeas: [[String]] = [] var colIndex: Int = 0 - var rowIndex: Int = 0 + var ideaArray: [String] = [] for idea in ideas { if colIndex == 3 { + matrixIdeas.append(ideaArray) + ideaArray = [] colIndex = 0 + + if ideaArray.isEmpty { + ideaArray.append(idea) + colIndex += 1 + } + + } else { + ideaArray.append(idea) + colIndex += 1 } - matrixIdeas[rowIndex].append(idea) - rowIndex += 1 - colIndex += 1 + } + if ideaArray.isEmpty == false { + matrixIdeas.append(ideaArray) } return matrixIdeas }