Skip to content

Commit

Permalink
Update: Added Swift Unit Tests
Browse files Browse the repository at this point in the history
Signed-off-by: Son Nguyen <[email protected]>
  • Loading branch information
hoangsonww authored Oct 16, 2024
1 parent 2932d82 commit 299720f
Showing 1 changed file with 82 additions and 19 deletions.
101 changes: 82 additions & 19 deletions Game-2048Tests/Game_2048Tests.swift
Original file line number Diff line number Diff line change
@@ -1,35 +1,98 @@
//
// Game_2048Tests.swift
// Game-2048Tests
//
// Created by Dav Nguyen on 3/19/24.
//

import XCTest
@testable import Game_2048

final class GameViewModelTests: XCTestCase {

final class Game_2048Tests: XCTestCase {
var gameViewModel: GameViewModel!

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
// Initialize GameViewModel before each test
gameViewModel = GameViewModel()
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
// Clean up after each test
gameViewModel = nil
}

func testInitialGridSetup() throws {
// Test if grid is initialized correctly
let nonZeroTiles = gameViewModel.grid.flatMap { $0 }.filter { $0 != 0 }
XCTAssertEqual(nonZeroTiles.count, 2, "Grid should start with exactly 2 non-zero tiles.")
XCTAssertTrue(nonZeroTiles.allSatisfy { $0 == 2 || $0 == 4 }, "Initial tiles should be 2 or 4.")
}

func testSwipeLeftMergesCorrectly() throws {
// Set a custom grid where merging is expected when swiping left
gameViewModel.grid = [
[2, 2, 4, 0],
[0, 4, 4, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]

gameViewModel.swipe(direction: .left)

let expectedGrid = [
[4, 4, 0, 0],
[8, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]

XCTAssertEqual(gameViewModel.grid, expectedGrid, "Swiping left should correctly merge tiles.")
}

func testAddNewNumberAfterValidMove() throws {
// Set a grid with a valid move, swipe left, and ensure a new number is added
gameViewModel.grid = [
[2, 2, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]

let oldGrid = gameViewModel.grid
gameViewModel.swipe(direction: .left)

let nonZeroTilesOld = oldGrid.flatMap { $0 }.filter { $0 != 0 }.count
let nonZeroTilesNew = gameViewModel.grid.flatMap { $0 }.filter { $0 != 0 }.count

XCTAssertGreaterThan(nonZeroTilesNew, nonZeroTilesOld, "A new number should be added after a valid swipe.")
}

func testGameOverDetection() throws {
// Set a grid where no moves are left and verify game over
gameViewModel.grid = [
[2, 4, 2, 4],
[4, 2, 4, 2],
[2, 4, 2, 4],
[4, 2, 4, 2]
]

XCTAssertTrue(gameViewModel.isGameOver(), "The game should detect a game-over state when no moves are left.")
}

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.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
func testGameWinDetection() throws {
// Set a grid with the winning tile 2048 and verify the game detects a win
gameViewModel.grid = [
[2, 4, 2, 4],
[4, 2, 4, 2],
[2, 2048, 2, 4],
[4, 2, 4, 2]
]

gameViewModel.updateGameStatus()

XCTAssertTrue(gameViewModel.hasWon, "The game should detect a win when a 2048 tile appears.")
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
// Performance test case example
measure {
// Put the code you want to measure the time of here.
for _ in 0..<1000 {
gameViewModel.swipe(direction: .left)
}
}
}

}

0 comments on commit 299720f

Please sign in to comment.