Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit testing framework #230

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Run unit tests

on: [push]

jobs:
test:
runs-on: ubuntu-latest

name: Run unit tests
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up lua
uses: leafo/gh-actions-lua@v10
with:
luaVersion: "5.1.5"

- name: test
run: |
lua ./Scripts/DCS-BIOS/test/TestSuite.lua
2 changes: 2 additions & 0 deletions .styluaignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@

# opt-in test code
!/Scripts/DCS-BIOS/test
# opt-out copy-pasted test library code
/Scripts/DCS-BIOS/test/ext
4 changes: 3 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"recommendations": [
"johnnymorganz.stylua"
"johnnymorganz.stylua",
"tomblind.local-lua-debugger-vscode",
"sumneko.lua"
]
}
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run unit tests",
"type": "lua-local",
"request": "launch",
"program": {
"lua": "lua5.1",
"file": "./Scripts/DCS-BIOS/test/TestSuite.lua"
}
}
]
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"[lua]": {
"editor.defaultFormatter": "JohnnyMorganz.stylua"
},
"stylua.targetReleaseVersion": "v0.17.0"
"stylua.targetReleaseVersion": "v0.17.0",
"Lua.runtime.version": "Lua 5.1"
}
64 changes: 64 additions & 0 deletions Scripts/DCS-BIOS/test/ServerTest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local MockSocket = require("MockSocket")
local ReadableConnection = require("ReadableConnection")
local TCPConnection = require("TCPConnection")
local TCPServer = require("TCPServer")
local UDPListener = require("UDPListener")
local UDPSender = require("UDPSender")
local UDPServer = require("UDPServer")

local lu = require("luaunit")

-- Unit testing starts
--- @class TestServer
TestServer = {}

function TestServer:testCreateReadableConnection()
local listener = ReadableConnection:new("*", 0, MockSocket:new(), function() end)
listener:appendToBuffer("foo")
end

function TestServer:testCreateUDPListener()
local linesProcessed = {}
local line1 = "line 1"
local line2 = "line 2"
local listener = UDPListener:new("*", 0, MockSocket:new(), function(val)
table.insert(linesProcessed, val)
end)
listener:init()
listener:appendToBuffer(line1 .. "\n" .. line2 .. "\n") -- add two lines of data to the buffer
listener:receive() -- should process buffer line-by-line
lu.assertEquals(linesProcessed[1], line1)
lu.assertEquals(linesProcessed[2], line2)
listener:close()
end

function TestServer:testCreateUDPSender()
local sender = UDPSender:new("*", 0, MockSocket:new())
sender:init()
sender:send("foo")
sender:close()
end

function TestServer:testCreateUDPServer()
local server = UDPServer:new("*", 0, "*", 0, MockSocket:new(), function() end)
server:init()
server:step()
server:send("foo")
server:close()
end

function TestServer:testCreateTCPConnection()
local s = MockSocket:new()
local connection = TCPConnection:new(s.bind("", 0), MockSocket:new(), function() end)
connection:send("foo")
connection:receive()
connection:close()
end

function TestServer:testCreateTCPServer()
local server = TCPServer:new("*", 0, MockSocket:new(), function() end)
server:init()
server:step()
server:send("foo")
server:close()
end
20 changes: 20 additions & 0 deletions Scripts/DCS-BIOS/test/TestSuite.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package.path = "./Scripts/DCS-BIOS/test/?.lua;" .. package.path
package.path = "./Scripts/DCS-BIOS/test/ext/?.lua;" .. package.path
package.path = "./Scripts/DCS-BIOS/test/io/?.lua;" .. package.path
package.path = "./Scripts/DCS-BIOS/?.lua;" .. package.path
package.path = "./Scripts/DCS-BIOS/lib/?.lua;" .. package.path
package.path = "./Scripts/DCS-BIOS/lib/io/?.lua;" .. package.path
package.path = "./Scripts/DCS-BIOS/lib/modules/?.lua;" .. package.path
package.path = "./Scripts/DCS-BIOS/lib/modules/aircraft_modules/?.lua;" .. package.path
package.path = "./Scripts/DCS-BIOS/lib/modules/common_modules/?.lua;" .. package.path
package.path = "./Scripts/DCS-BIOS/lib/modules/documentation/?.lua;" .. package.path
package.path = "./Scripts/DCS-BIOS/lib/modules/memory_map/?.lua;" .. package.path

-- global functions that haven't been refactored yet
BIOS = {}
function BIOS.log(str) end -- noop

require("ServerTest") -- unit tests for tcp/udp server code

local lu = require("luaunit")
os.exit(lu.LuaUnit:run())
Loading