Showing posts with label Lua Modules. Show all posts
Showing posts with label Lua Modules. Show all posts

Saturday, 26 November 2016

Installing Lua LGI on macOS

I wanted to see how easy it would be to build some very simple GUI apps in Lua. As I already have GTK+3 installed on my machine, I thought it would be a good idea to try to use it from Lua. LGI is gobject-introspection based dynamic Lua binding to GObject based libraries including GTK+3. It can be installed via Luarocks.

A simple attempt at installing LGI results in an error as Luarocks can't find the libffi library that LGI requires. It is a similar problem to the one I encountered with installing the Ruby GTK+3 Gem.  Applying a similar solution to the one kindly supplied by the Ruby Gnome 2 team did the trick.

This is the command that worked:


  sudo PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig luarocks-jit install lgi

Notes:

I installed GTK+3 using Homebrew
I am using LuaJIT 2.0.4
I am using luarocks-jit 2.3.0


Sunday, 28 August 2016

Busted or LuaUnit?

Having decided to learn Lua and testing Lua at the same time, I need to decide on a testing framework. There seem to be quite a large number available. I was guided to look at Busted and LuaUnit. Both seem active, professional and have good documentation. They have quite different approaches and it is hard to chose between them without any experience of them. 

I have decided to use them both whilst learning Lua. That decision will, no doubt, slow my learning a little but it should give a greater depth to what I learn.

To get started quickly, a took a function from a Prag Prog Magazine  article "a-functional-introduction-to-lua" and created a module to put it in and wrote both Busted and LuaUnit tests for it.

Here is the module and function:

    local learnLua = {}

    function learnLua.namesOf (things)
      local names = {}
        for index, thing in pairs(things) do
    names[index] = thing.name
  end
      return names
    end

    return learnLua

Here's the Busted test:

    require 'busted.runner'()
    local ll = require 'learnLua'

    describe("Flames with Lua", function()
      describe("namesOf", function()
        it("extract string names", function()
          local cats = {
            { name = "meg", breed = "persian" },
      { name = "mog", breed = "siamese" }
          }
          assert.equal("meg", ll.namesOf(cats)[1])
          assert.equal("mog", ll.namesOf(cats)[2])
          assert.equal(2, # ll.namesOf(cats))
        end)
      end)
    end)

Here's the LuaUnit test:

    luaunit = require 'luaunit'
    ll = require 'learnLua'

    local cats = {
      { name = "meg", breed = "persian" },
      { name = "mog", breed = "siamese" }
    }

    function testNameExtract() 
      local cats = {
        { name = "meg", breed = "persian" },
  { name = "mog", breed = "siamese" }
      }
      local names = ll.namesOf(cats)
      luaunit.assertEquals(names[1], "meg")
      luaunit.assertEquals(names[2], "mog")
      luaunit.assertEquals(# names, 2)
   end

  os.exit( luaunit.LuaUnit.run() )

Both tests pass:
    $ luajit learn-unit.lua            
    . 
    Ran 1 tests in 0.000 seconds, 1 success, 0 failures    OK

    $ luajit learn-busted.lua    
    1 success / 0 failures / 0 errors / 0 pending : 0.000617 seconds

Not a lot to chose between them when testing such a simple function. I need to work up to something more complex to really see the differences between them.

I'd be happy to learn about improvements in style or form that I could make in writing Lua, Busted tests and LuaUnit tests.

There is some early feedback on Busted and LuaUnit in Test Doubles and you can read my conclusion in Busted or LuaUnit? Answered