Skip to content

Commit

Permalink
Add unit testing framework (#230)
Browse files Browse the repository at this point in the history
* Add unit tests for server files

* opt copied luaunit library out of stylua checking

* verify ci fails on test failure

* Remove intentionally broken test
  • Loading branch information
charliefoxtwo authored Sep 21, 2023
1 parent c65232a commit ef74f4a
Show file tree
Hide file tree
Showing 12 changed files with 3,713 additions and 2 deletions.
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

0 comments on commit ef74f4a

Please sign in to comment.