LuaUnit for Mudlet

Share your scripts and packages with other Mudlet users.
Post Reply
User avatar
demonnic
Posts: 884
Joined: Sat Dec 05, 2009 3:19 pm

LuaUnit for Mudlet

Post by demonnic »

I hacked up LuaUnit 3.3 to work with Mudlet. Largely involved replacing print and io.write calls with cechos and injecting a bit of color to the output. Oh and removing all the os.exit calls so it didn't close Mudlet.

This allows you to run unit tests for Mudlet scripts inside of Mudlet, without having to install luarocks etc. I have attached an mpackage as well as the luaunit.lua file for those who really wanna do the require() themselves =)

Either install the mpackage, or place the luaunit.lua file somewhere in the package path (like the profile directory) and require() it as per usual lua standards. If you're not sure what those are you may be easier off just installing the mpackage.

LuaUnit documentation

If you install the mpackage it will require luaunit as the global 'lum', giving access to 'lum.assertEquals' etc where the documentation shows 'lu.assertEquals' (lum for Lua Unit Mudlet, of course)

The testing output looks a little something like this

successful run:
Selection_008.png
Selection_008.png (3.5 KiB) Viewed 3269 times
Unsuccessful run:
Selection_009.png

And this is the code I have in the 'ft' alias which produced the successful screenshot. Test names could maybe be better, but it shows some simple usage. I experimented with making the tests a local table but then they did not seem to get picked up by the test scan. I didn't spend a whole ton of time on it though.
Code: [show] | [select all] lua
if not lum then return end

TestFText = {}

function TestFText:test_ftext_width_20_centered()
  local expected = "     some text      "
  local actual = demonnic:fText("some text", {width = 20})
  lum.assertEquals(actual, expected)
  lum.assertEquals(string.len(actual), 20)
end

function TestFText:test_ftext_width_20_left()
  local expected = "some text           "
  local actual = demonnic:fText("some text", {width = 20, alignment = "left"})
  lum.assertEquals(actual, expected)
  lum.assertEquals(string.len(actual), 20)
end

function TestFText:test_ftext_width_20_right()
  local expected = "           some text"
  local actual = demonnic:fText("some text", {width = 20, alignment = "right"})
  lum.assertEquals(actual, expected)
  lum.assertEquals(string.len(actual), 20)
end

function TestFText:test_ftext_wrap_width_10_centered()
  local str = "This is a test of the emergency broadcast system"
  local actual = demonnic:fText(str, {width = 10, alignment = "centered"})
  for _,line in ipairs(actual:split("\n")) do
    lum.assertEquals(line:len(), 10)
  end
end

function TestFText:test_ftext_left_align_nonspace_spacer()
  local str = "some text"
  local expected = "some text =========="
  local actual = demonnic:fText(str, {width = 20, alignment = "left", spacer = "="})
  lum.assertEquals(actual, expected)
end

function TestFText:test_ftext_left_align_nonspace_spacer_nogap()
  local str = "some text"
  local expected = "some text==========="
  local actual = demonnic:fText(str, {width = 20, alignment = "left", spacer = "=", nogap = true})
  lum.assertEquals(actual, expected)
end

function TestFText:test_ftext_right_align_nonspace_spacer()
  local str = "some text"
  local expected = "========== some text"
  local actual = demonnic:fText(str, {width = 20, alignment = "right", spacer = "="})
  lum.assertEquals(actual, expected)
end

function TestFText:test_ftext_right_align_nonspace_spacer_nogap()
  local str = "some text"
  local expected = "===========some text"
  local actual = demonnic:fText(str, {width = 20, alignment = "right", spacer = "=", nogap = true})
  lum.assertEquals(actual, expected)
end

function TestFText:test_ftext_center_align_nonspace_spacer()
  local expected = "==== some text ====="
  local actual = demonnic:fText("some text", {width = 20, spacer = "="})
  lum.assertEquals(actual, expected)
  lum.assertEquals(string.len(actual), 20)
end

function TestFText:test_ftext_center_align_nonspace_spacer_nogap()
  local expected = "=====some text======"
  local actual = demonnic:fText("some text", {width = 20, spacer = "=", nogap = true})
  lum.assertEquals(actual, expected)
  lum.assertEquals(string.len(actual), 20)
end

function TestFText:test_ftext_center_align_nonspace_spacer_pipe_cap()
  local expected = "===| some text |===="
  local actual = demonnic:fText("some text", {width = 20, spacer = "=", cap = "|"})
  lum.assertEquals(actual, expected)
  lum.assertEquals(string.len(actual), 20)
end

function TestFText:test_ftext_center_align_nonspace_spacer_pipe_cap_inside()
  local expected = "|=== some text ====|"
  local actual = demonnic:fText("some text", {width = 20, spacer = "=", cap = "|", inside = true})
  lum.assertEquals(actual, expected)
  lum.assertEquals(string.len(actual), 20)
end

function TestFText:test_ftext_center_align_nonspace_spacer_lbracket_cap_inside()
  local expected = "[=== some text ====["
  local actual = demonnic:fText("some text", {width = 20, spacer = "=", cap = "[", inside = true})
  lum.assertEquals(actual, expected)
  lum.assertEquals(string.len(actual), 20)
end

function TestFText:test_ftext_center_align_nonspace_spacer_lbracket_cap_inside_mirrored()
  local expected = "[=== some text ====]"
  local actual = demonnic:fText("some text", {width = 20, spacer = "=", cap = "[", inside = true, mirror = true})
  lum.assertEquals(actual, expected)
  lum.assertEquals(string.len(actual), 20)
end

lum.LuaUnit.run('--pattern', 'FText')

License information, from original author. The github for the project is at https://github.com/bluebird75/luaunit
Code: [show] | [select all] lua
--[=[
This software is distributed under the BSD License.

Copyright (c) 2005-2018, Philippe Fremy <phil at freehackers dot org>

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
© 2020 GitHub, Inc.
--]=]
Attachments
luaunit.mpackage
(26.51 KiB) Downloaded 285 times
luaunit.lua
(108.89 KiB) Downloaded 295 times

User avatar
demonnic
Posts: 884
Joined: Sat Dec 05, 2009 3:19 pm

Re: LuaUnit for Mudlet

Post by demonnic »

I made a stream in which I tried to explain why you'd wanna use this: https://www.youtube.com/watch?v=16_aKITBU9g

If I get a bunch of questions I'll try to do a follow up video.

Post Reply