diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2021-08-16 14:04:39 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2021-08-16 14:09:26 +0200 |
| commit | 29b50c08a7e8524797549354a2e5063974c0e1b2 (patch) | |
| tree | 26afb1bdd77762d67b3803c8068eab5f9cbdf5a5 | |
| parent | 483ed22f686010cb9fe6f8ec4512263dd8d95706 (diff) | |
| download | vis-lspc-29b50c08a7e8524797549354a2e5063974c0e1b2.tar.gz vis-lspc-29b50c08a7e8524797549354a2e5063974c0e1b2.zip | |
add tests for the LSP message parser
| -rw-r--r-- | .github/workflows/check.yml | 16 | ||||
| -rw-r--r-- | Makefile | 5 | ||||
| -rwxr-xr-x | parser_test.lua | 125 |
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 @@ -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() |
