Skip to content

Commit

Permalink
Group included and excluded patterns into a single list, then use the…
Browse files Browse the repository at this point in the history
… inclusion/exclusion capabilities of patterFilter()
  • Loading branch information
bluebird75 committed Nov 27, 2016
1 parent 95dd309 commit a3f34f3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
22 changes: 11 additions & 11 deletions luaunit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ end
M.private.strMatch = strMatch

local function patternFilter(patterns, expr, nil_result)
-- Check if any of `patterns` is contained in `expr`. If so, return `true`.
-- Return `false` if none of the patterns are contained in expr.
-- Run `expr` through the inclusion and exclusion rules defined in patterns
-- and return true if expr shall be included, false for excluded.
-- Inclusion pattern are defined as normal patterns, exclusions
-- patterns start with `!` and are followed by a normal pattern
-- If patterns is `nil` (= unset) or empty, return default value passed in `nil_result`.

if patterns ~= nil and #patterns > 0 then
Expand Down Expand Up @@ -2133,10 +2135,11 @@ end
end
return
elseif state == SET_EXCLUDE then
if result['exclude'] then
table.insert( result['exclude'], cmdArg )
local notArg = '!'..cmdArg
if result['pattern'] then
table.insert( result['pattern'], notArg )
else
result['exclude'] = { cmdArg }
result['pattern'] = { notArg }
end
return
end
Expand Down Expand Up @@ -2305,7 +2308,6 @@ end
startDate = os.date(os.getenv('LUAUNIT_DATEFMT')),
startIsodate = os.date('%Y-%m-%dT%H:%M:%S'),
patternIncludeFilter = self.patternIncludeFilter,
patternExcludeFilter = self.patternExcludeFilter,
tests = {},
failures = {},
errors = {},
Expand Down Expand Up @@ -2609,12 +2611,11 @@ end
return result
end

function M.LuaUnit.applyPatternFilter( patternIncFilter, patternExcFilter, listOfNameAndInst )
function M.LuaUnit.applyPatternFilter( patternIncFilter, listOfNameAndInst )
local included, excluded = {}, {}
for i, v in ipairs( listOfNameAndInst ) do
-- local name, instance = v[1], v[2]
if patternFilter( patternIncFilter, v[1], true ) and
not patternFilter( patternExcFilter, v[1], false ) then
if patternFilter( patternIncFilter, v[1], true ) then
table.insert( included, v )
else
table.insert( excluded, v )
Expand All @@ -2636,7 +2637,7 @@ end
randomizeTable( expandedList )
end
local filteredList, filteredOutList = self.applyPatternFilter(
self.patternIncludeFilter, self.patternExcludeFilter, expandedList )
self.patternIncludeFilter, expandedList )

self:startSuite( #filteredList, #filteredOutList )

Expand Down Expand Up @@ -2756,7 +2757,6 @@ end

self.exeCount = options.exeCount
self.patternIncludeFilter = options.pattern
self.patternExcludeFilter = options.exclude
self.randomize = options.randomize

if options.output then
Expand Down
28 changes: 17 additions & 11 deletions test/test_luaunit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,10 @@ TestLuaUnitUtilities = { __class__ = 'TestLuaUnitUtilities' }
lu.assertEquals( lu.LuaUnit.parseCmdLine( { '-p', 'toto' } ), { pattern={'toto'} } )
lu.assertEquals( lu.LuaUnit.parseCmdLine( { '-p', 'titi', '-p', 'toto' } ), { pattern={'titi', 'toto'} } )
lu.assertErrorMsgContains( 'Missing argument after -p', lu.LuaUnit.parseCmdLine, { '-p', } )
lu.assertEquals( lu.LuaUnit.parseCmdLine( { '--exclude', 'toto' } ), { exclude={'toto'} } )
lu.assertEquals( lu.LuaUnit.parseCmdLine( { '-x', 'toto' } ), { exclude={'toto'} } )
lu.assertEquals( lu.LuaUnit.parseCmdLine( { '-x', 'titi', '-x', 'toto' } ), { exclude={'titi', 'toto'} } )
lu.assertEquals( lu.LuaUnit.parseCmdLine( { '--exclude', 'toto' } ), { pattern={'!toto'} } )
lu.assertEquals( lu.LuaUnit.parseCmdLine( { '-x', 'toto' } ), { pattern={'!toto'} } )
lu.assertEquals( lu.LuaUnit.parseCmdLine( { '-x', 'titi', '-x', 'toto' } ), { pattern={'!titi', '!toto'} } )
lu.assertEquals( lu.LuaUnit.parseCmdLine( { '-x', 'titi', '-p', 'foo', '-x', 'toto' } ), { pattern={'!titi', 'foo', '!toto'} } )
lu.assertErrorMsgContains( 'Missing argument after -x', lu.LuaUnit.parseCmdLine, { '-x', } )

-- count
Expand Down Expand Up @@ -638,39 +639,44 @@ TestLuaUnitUtilities = { __class__ = 'TestLuaUnitUtilities' }
}

-- default action: include everything
local included, excluded = lu.LuaUnit.applyPatternFilter( nil, nil, testset )
local included, excluded = lu.LuaUnit.applyPatternFilter( nil, testset )
lu.assertEquals( #included, 8 )
lu.assertEquals( excluded, {} )

-- single exclude pattern (= select anything not matching "bar")
included, excluded = lu.LuaUnit.applyPatternFilter( nil, {'bar'}, testset )
included, excluded = lu.LuaUnit.applyPatternFilter( {'!bar'}, testset )
lu.assertEquals( included, {testset[1], testset[3], testset[5]} )
lu.assertEquals( #excluded, 5 )

-- single include pattern
included, excluded = lu.LuaUnit.applyPatternFilter( {'t.t.'}, nil, testset )
included, excluded = lu.LuaUnit.applyPatternFilter( {'t.t.'}, testset )
lu.assertEquals( #included, 6 )
lu.assertEquals( excluded, {testset[7], testset[8]} )

-- single include and exclude patterns
included, excluded = lu.LuaUnit.applyPatternFilter( {'foo'}, {'test'}, testset )
included, excluded = lu.LuaUnit.applyPatternFilter( {'foo', '!test'}, testset )
lu.assertEquals( included, {testset[1], testset[3], testset[5], testset[7]} )
lu.assertEquals( #excluded, 4 )

-- multiple (specific) includes
included, excluded = lu.LuaUnit.applyPatternFilter( {'toto', 'titi'}, nil, testset )
included, excluded = lu.LuaUnit.applyPatternFilter( {'toto', 'titi'}, testset )
lu.assertEquals( included, {testset[1], testset[2], testset[3], testset[4]} )
lu.assertEquals( #excluded, 4 )

-- multiple excludes
included, excluded = lu.LuaUnit.applyPatternFilter( nil, {'tata', '%.bar'}, testset )
included, excluded = lu.LuaUnit.applyPatternFilter( {'!tata', '!%.bar'}, testset )
lu.assertEquals( included, {testset[1], testset[3], testset[8]} )
lu.assertEquals( #excluded, 5 )

-- combined test
included, excluded = lu.LuaUnit.applyPatternFilter( {'t[oai]', 'bar$', 'test'}, {'%.b', 'titi'}, testset )
included, excluded = lu.LuaUnit.applyPatternFilter( {'t[oai]', 'bar$', 'test', '!%.b', '!titi'}, testset )
lu.assertEquals( included, {testset[1], testset[5], testset[8]} )
lu.assertEquals( #excluded, 5 )

--[[ Combining positive and negative filters ]]--
included, excluded = lu.LuaUnit.applyPatternFilter( {'foo', 'bar', '!t.t.', '%.bar'}, testset )
lu.assertEquals( included, {testset[2], testset[4], testset[6], testset[7], testset[8]} )
lu.assertEquals( #excluded, 3 )
end

function TestLuaUnitUtilities:test_strMatch()
Expand Down Expand Up @@ -2672,7 +2678,7 @@ TestLuaUnitExecution = { __class__ = 'TestLuaUnitExecution' }
lu.assertEquals( executedTests[7], "MyTestToto2:test1" )
lu.assertEquals( #executedTests, 7)

runner:runSuite('-x', 'Toto2', '-p', 'Toto.' )
runner:runSuite('-p', 'Toto.', '-x', 'Toto2' )
lu.assertEquals( runner.result.testCount, 5) -- MyTestToto2 excluded
end

Expand Down

0 comments on commit a3f34f3

Please sign in to comment.