aboutsummaryrefslogtreecommitdiffstats
path: root/test/busted/done.lua
blob: e6319044dfd4cd9a357237377c4b03d02efa3b7a (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
local M = {}

-- adds tokens to the current wait list, does not change order/unordered
M.wait = function(self, ...)
  local tlist = { ... }

  for _, token in ipairs(tlist) do
    if type(token) ~= 'string' then
      error('Wait tokens must be strings. Got '..type(token), 2)
    end
    table.insert(self.tokens, token)
  end
end

-- set list as unordered, adds tokens to current wait list
M.wait_unordered = function(self, ...)
  self.ordered = false
  self:wait(...)
end

-- set list as ordered, adds tokens to current wait list
M.wait_ordered = function(self, ...)
  self.ordered = true
  self:wait(...)
end

-- generates a message listing tokens received/open
M.tokenlist = function(self)
  local list

  if #self.tokens_done == 0 then
    list = 'No tokens received.'
  else
    list = 'Tokens received ('..tostring(#self.tokens_done)..')'
    local s = ': '

    for _,t in ipairs(self.tokens_done) do
      list = list .. s .. '\''..t..'\''
      s = ', '
    end

    list = list .. '.'
  end

  if #self.tokens == 0 then
    list = list .. ' No more tokens expected.'
  else
    list = list .. ' Tokens not received ('..tostring(#self.tokens)..')'
    local s = ': '

    for _, t in ipairs(self.tokens) do
      list = list .. s .. '\''..t..'\''
      s = ', '
    end

    list = list .. '.'
  end

  return list
end

-- marks a token as completed, checks for ordered/unordered, checks for completeness
M.done = function(self, ...) self:_done(...) end  -- extra wrapper for same error level constant as __call method
M._done = function(self, token)
  if token then
    if type(token) ~= 'string' then
      error('Wait tokens must be strings. Got '..type(token), 3)
    end

    if self.ordered then
      if self.tokens[1] == token then
        table.remove(self.tokens, 1)
        table.insert(self.tokens_done, token)
      else
        if self.tokens[1] then
          error(('Bad token, expected \'%s\' got \'%s\'. %s'):format(self.tokens[1], token, self:tokenlist()), 3)
        else
          error(('Bad token (no more tokens expected) got \'%s\'. %s'):format(token, self:tokenlist()), 3)
        end
      end
    else
      -- unordered
      for i, t in ipairs(self.tokens) do
        if t == token then
          table.remove(self.tokens, i)
          table.insert(self.tokens_done, token)
          token = nil
          break
        end
      end

      if token then
        error(('Unknown token \'%s\'. %s'):format(token, self:tokenlist()), 3)
      end
    end
  end
  if not next(self.tokens) then
    -- no more tokens, so we're really done...
    self.done_cb()
  end
end


-- wraps a done callback into a done-object supporting tokens to sign-off
M.new = function(done_callback)
  local obj = {
    tokens = {},
    tokens_done = {},
    done_cb = done_callback,
    ordered = true,  -- default for sign off of tokens
  }

  return setmetatable( obj, {
    __call = function(self, ...)
      self:_done(...)
    end,
    __index = M,
  })
end

return M