aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/check.yml16
-rw-r--r--Makefile5
-rwxr-xr-xparser_test.lua125
3 files changed, 142 insertions, 4 deletions
diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml
index fb5ddf2..d33d6af 100644
--- a/.github/workflows/check.yml
+++ b/.github/workflows/check.yml
@@ -15,12 +15,8 @@ on:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
- # This workflow contains a single job called "build"
check:
- # The type of runner that the job will run on
runs-on: ubuntu-latest
-
- # Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
@@ -37,3 +33,15 @@ jobs:
- name: static analysis
run: make check-luacheck
+ test:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v2
+
+ - name: install dependencies
+ run: |
+ sudo apt-get -y install luarocks
+ sudo luarocks install lunatest
+
+ - name: run test
+ run: make test
diff --git a/Makefile b/Makefile
index 116c2a0..cdf9647 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,8 @@
LUA_FILES := $(shell find -name "*.lua" -not -path "./json.lua")
+TEST_FILES := $(shell find -name "*_test.lua")
+
# bash's process substitution is used for check-format
SHELL := /bin/bash
@@ -15,3 +17,6 @@ check-format:
format:
lua-format -i $(LUA_FILES)
+
+test:
+ for tf in $(TEST_FILES); do $$tf; done
diff --git a/parser_test.lua b/parser_test.lua
new file mode 100755
index 0000000..e607611
--- /dev/null
+++ b/parser_test.lua
@@ -0,0 +1,125 @@
+#!/usr/bin/env lua
+
+local parser = require('parser')
+
+local function build_msg(body)
+ return 'Content-Length: ' .. tostring(string.len(body)) .. '\r\n\r\n' .. body
+end
+
+local lunatest = require('lunatest')
+
+function test_complete_msg() -- luacheck: ignore 111
+ local msg = build_msg('foo')
+ local p = parser.Parser()
+ local err = p:add(msg)
+ lunatest.assert_nil(err)
+
+ local msgs = p:get_msgs()
+ lunatest.assert_table(msgs)
+ lunatest.assert_len(1, msgs)
+ lunatest.assert_equal(msgs[1], 'foo')
+end
+
+function test_two_complete_msgs() -- luacheck: ignore 111
+ local p = parser.Parser()
+ local data = build_msg('foo')
+ local err = p:add(data)
+ lunatest.assert_nil(err)
+
+ local msgs = p:get_msgs()
+ lunatest.assert_table(msgs)
+ lunatest.assert_len(1, msgs)
+ lunatest.assert_equal(msgs[1], 'foo')
+
+ data = build_msg('bar')
+ err = p:add(data)
+ lunatest.assert_nil(err)
+ msgs = p:get_msgs()
+ lunatest.assert_table(msgs)
+ lunatest.assert_len(1, msgs)
+ lunatest.assert_equal(msgs[1], 'bar')
+end
+
+function test_two_complete_msgs_at_once() -- luacheck: ignore 111
+ local data = build_msg('foo') .. build_msg('bar')
+ local p = parser.Parser()
+ local err = p:add(data)
+ lunatest.assert_nil(err)
+
+ local msgs = p:get_msgs()
+ lunatest.assert_table(msgs)
+ lunatest.assert_len(2, msgs)
+ lunatest.assert_equal(msgs[1], 'foo')
+ lunatest.assert_equal(msgs[2], 'bar')
+end
+
+function test_split_msg() -- luacheck: ignore 111
+ local msg = build_msg('foo')
+ local part1 = msg:sub(1, -3)
+ local part2 = msg:sub(-2)
+ local p = parser.Parser()
+ local err = p:add(part1)
+ lunatest.assert_nil(err)
+
+ err = p:add(part2)
+ lunatest.assert_nil(err)
+
+ local msgs = p:get_msgs()
+ lunatest.assert_table(msgs)
+ lunatest.assert_len(1, msgs)
+ lunatest.assert_equal(msgs[1], 'foo')
+end
+
+function test_complete_and_split_msg() -- luacheck: ignore 111
+ local msg = build_msg('foo') .. build_msg('bar')
+ local part1 = msg:sub(1, -3)
+ local part2 = msg:sub(-2)
+ local p = parser.Parser()
+ local err = p:add(part1)
+ lunatest.assert_nil(err)
+
+ err = p:add(part2)
+ lunatest.assert_nil(err)
+
+ local msgs = p:get_msgs()
+ lunatest.assert_table(msgs)
+ lunatest.assert_len(2, msgs)
+ lunatest.assert_equal(msgs[1], 'foo')
+ lunatest.assert_equal(msgs[2], 'bar')
+end
+
+function test_split_hdr() -- luacheck: ignore 111
+ local msg = build_msg('foo')
+ local part1 = msg:sub(1, 3)
+ local part2 = msg:sub(4)
+ local p = parser.Parser()
+ local err = p:add(part1)
+ lunatest.assert_nil(err)
+
+ err = p:add(part2)
+ lunatest.assert_nil(err)
+
+ local msgs = p:get_msgs()
+ lunatest.assert_table(msgs)
+ lunatest.assert_len(1, msgs)
+ lunatest.assert_equal(msgs[1], 'foo')
+end
+
+function test_split_hdr_body_sep() -- luacheck: ignore 111
+ local msg = build_msg('foo')
+ local part1 = msg:sub(1, 19)
+ local part2 = msg:sub(20)
+ local p = parser.Parser()
+ local err = p:add(part1)
+ lunatest.assert_nil(err)
+
+ err = p:add(part2)
+ lunatest.assert_nil(err)
+
+ local msgs = p:get_msgs()
+ lunatest.assert_table(msgs)
+ lunatest.assert_len(1, msgs)
+ lunatest.assert_equal(msgs[1], 'foo')
+end
+
+lunatest.run()