lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Sep 21, 2012 at 3:09 AM, mitch abaza <mitch.abaza@gmail.com> wrote:
> I come from a heavy TDD/OOP background and I'm finding it challenging to
> apply the same testing discipline to Lua.  Here's the problem domain I'm
> trying to tackle:
>
> I have a bunch of "rules" (ie: functions) and I want to apply some of those
> rules to certain customer data. I have a configuration system that says,
> "When applying rules to data from customer X, use this set of rules."
>
> I've come up with three modules:
>
> filterRulesLibrary == contains a list of available rules (functions)
>
> configSystem == determines which rules should be applied for a given
> customer
>
> filterRuleProvider = module that uses the two above modules to return a
> table of functions (rules) to the client application
>
> Sample Code
>
> ==========================================================
> --5.2 module style
> local filterRuleProvider ={}
>
> filterRuleProvider = require("filterRules")
> configSystem =require("configSystem ")
>
> filterRuleProvider.assembleRules = function(facilityId)
>
>     local filters = {}
>
>     if ( configSystem .shouldFilterEmptyMessages(facilityId)) then
>         table.insert(filters,filterRuleProvider.filterEmptyMessages)
>     end
>
>     return filters
>   end
>
> return filterRuleProvider
> ==========================================================
>
> I'm told you can't do reference equality on functions.  If true, how would I
> test that the returned table contains a "reference" to the
> filterRuleProvider.filterEmptyMessages function?

As Michal says, reference equality with functions does work. However
it looks like it would be more natural to use tables directly in the
config, either with functions or with names, so you just take a config
which looks like

config = {
  shouldFilterEMptyMessages = true,
  ...
}

then have a map that takes the rule names to a list of functions that
need to be tested, then just iterate through to make a list of
functions to test.

map = {
  shouldFilterEmptyMessages = {filterEmptyMessages, ...}
}

It is usually more idiomatic to use tables for config type stuff in
Lua, rather than functions, as that is the data structure.

Justin