File: filter_loader.lua

package info (click to toggle)
lua-busted 2.0~rc12-1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 656 kB
  • ctags: 310
  • sloc: sh: 191; makefile: 6
file content (106 lines) | stat: -rw-r--r-- 3,453 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
return function()
  local function filter(busted, options)
    local getFullName = function(name)
      local parent = busted.context.get()
      local names = { name }

      while parent and (parent.name or parent.descriptor) and
            parent.descriptor ~= 'file' do
        table.insert(names, 1, parent.name or parent.descriptor)
        parent = busted.context.parent(parent)
      end

      return table.concat(names, ' ')
    end

    local hasTag = function(name, tag)
      local found = name:find('#' .. tag)
      return (found ~= nil)
    end

    local filterExcludeTags = function(name)
      for i, tag in pairs(options.excludeTags) do
        if hasTag(name, tag) then
          return nil, false
        end
      end
      return nil, true
    end

    local filterTags = function(name)
      local fullname = getFullName(name)
      for i, tag in pairs(options.tags) do
        if hasTag(fullname, tag) then
          return nil, true
        end
      end
      return nil, (#options.tags == 0)
    end

    local filterOutNames = function(name)
      for _, filter in pairs(options.filterOut) do
        if getFullName(name):find(filter) ~= nil then
          return nil, false
        end
      end
      return nil, true
    end

    local filterNames = function(name)
      for _, filter in pairs(options.filter) do
        if getFullName(name):find(filter) ~= nil then
          return nil, true
        end
      end
      return nil, (#options.filter == 0)
    end

    local printNameOnly = function(name, fn, trace)
      local fullname = getFullName(name)
      if trace and trace.what == 'Lua' then
        print(trace.short_src .. ':' .. trace.currentline .. ': ' .. fullname)
      else
        print(fullname)
      end
      return nil, false
    end

    local ignoreAll = function()
      return nil, false
    end

    local skipOnError = function()
      return nil, not busted.skipAll
    end

    local applyFilter = function(descriptors, name, fn)
      if options[name] and options[name] ~= '' then
        for _, descriptor in ipairs(descriptors) do
          busted.subscribe({ 'register', descriptor }, fn, { priority = 1 })
        end
      end
    end

    if options.list then
      busted.subscribe({ 'suite', 'start' }, ignoreAll, { priority = 1 })
      busted.subscribe({ 'suite', 'end' }, ignoreAll, { priority = 1 })
      applyFilter({ 'setup', 'teardown', 'before_each', 'after_each' }, 'list', ignoreAll)
      applyFilter({ 'lazy_setup', 'lazy_teardown' }, 'list', ignoreAll)
      applyFilter({ 'strict_setup', 'strict_teardown' }, 'list', ignoreAll)
      applyFilter({ 'it', 'pending' }, 'list', printNameOnly)
    end

    applyFilter({ 'lazy_setup', 'lazy_teardown' }, 'nokeepgoing', skipOnError)
    applyFilter({ 'strict_setup', 'strict_teardown' }, 'nokeepgoing', skipOnError)
    applyFilter({ 'setup', 'teardown', 'before_each', 'after_each' }, 'nokeepgoing', skipOnError)
    applyFilter({ 'file', 'describe', 'it', 'pending' }, 'nokeepgoing', skipOnError)

    -- The following filters are applied in reverse order
    applyFilter({ 'it', 'pending' }            , 'filter'     , filterNames      )
    applyFilter({ 'describe', 'it', 'pending' }, 'filterOut'  , filterOutNames   )
    applyFilter({ 'it', 'pending' }            , 'tags'       , filterTags       )
    applyFilter({ 'describe', 'it', 'pending' }, 'excludeTags', filterExcludeTags)
  end

  return filter
end