usydusgduy123 / Outlaster-script

simple little script made with a little ChatGPT ;)
0 stars 0 forks source link

Write a test for the `is player friendly` feature #2

Closed jaylong255 closed 2 months ago

jaylong255 commented 2 months ago

Certainly! I'll create a simple test for one of the features in your script, specifically the isPlayerFriended function, and then provide a GitHub Actions workflow to run this test.

First, let's create a test file for the isPlayerFriended function:

-- File: test_player_friendship.lua

local function mockPlayer(userId)
    return {
        UserId = userId,
        IsFriendsWith = function(self, otherUserId)
            -- Mock implementation: players are friends if their IDs are both even or both odd
            return self.UserId % 2 == otherUserId % 2
        end
    }
end

local function isPlayerFriended(player, otherPlayer)
    local success, result = pcall(function()
        return player:IsFriendsWith(otherPlayer.UserId)
    end)
    return success and result
end

-- Test cases
local tests = {
    function()
        local player1 = mockPlayer(2)
        local player2 = mockPlayer(4)
        assert(isPlayerFriended(player1, player2), "Test 1 failed: Even-numbered players should be friends")
    end,
    function()
        local player1 = mockPlayer(1)
        local player2 = mockPlayer(3)
        assert(isPlayerFriended(player1, player2), "Test 2 failed: Odd-numbered players should be friends")
    end,
    function()
        local player1 = mockPlayer(1)
        local player2 = mockPlayer(2)
        assert(not isPlayerFriended(player1, player2), "Test 3 failed: Odd and even numbered players should not be friends")
    end
}

-- Run tests
for i, test in ipairs(tests) do
    local success, error = pcall(test)
    if success then
        print("Test " .. i .. " passed")
    else
        print("Test " .. i .. " failed: " .. error)
    end
end

Now, let's create a GitHub Actions workflow to run this test:

# File: .github/workflows/lua-tests.yml

name: Lua Tests

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Setup Lua
      uses: leafo/gh-actions-lua@v8
      with:
        luaVersion: "5.3"

    - name: Run Tests
      run: lua test_player_friendship.lua

This workflow does the following:

  1. It triggers on pushes to the main branch and on pull requests to the main branch.
  2. It sets up an Ubuntu environment.
  3. It checks out your repository.
  4. It sets up Lua 5.3 using the leafo/gh-actions-lua action.
  5. It runs your test file using the lua command.

To use this:

  1. Save the test file as test_player_friendship.lua in the root of your repository.
  2. Create a .github/workflows directory in your repository if it doesn't exist.
  3. Save the workflow file as lua-tests.yml in the .github/workflows directory.
  4. Commit and push these files to your repository.

Now, every time you push to the main branch or create a pull request, GitHub Actions will automatically run your tests.

Note that this is a basic setup and might need adjustments based on your specific needs:

Would you like me to explain any part of this setup in more detail?