summaryrefslogtreecommitdiff
path: root/lib/lua-repl/t/plugin-feature-tests.lua
blob: 4c74a4c1b99e407c14c029d3b39706ff8b36414c (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
122
123
124
125
126
127
128
129
130
131
132
-- vim:foldmethod=marker
local repl  = require 'repl'
local utils = require 'test-utils'
pcall(require, 'luarocks.loader')
require 'Test.More'

plan(19)

do -- basic tests {{{
  local clone = repl:clone()

  clone:loadplugin(function()
    features = 'foo'
  end)

  ok(clone:hasfeature 'foo')
  ok(not clone:hasfeature 'bar')
  ok(not clone:hasfeature 'baz')

  clone:loadplugin(function()
    features = { 'bar', 'baz' }
  end)

  ok(clone:hasfeature 'foo')
  ok(clone:hasfeature 'bar')
  ok(clone:hasfeature 'baz')
end -- }}}

do -- requirefeature {{{
  local clone = repl:clone()

  clone:loadplugin(function()
    features = 'foo'
  end)

  clone:requirefeature 'foo'

  local line_no
  local _, err = pcall(function()
    line_no = utils.next_line_number()
    clone:requirefeature 'bar'
  end)

  like(err, tostring(line_no) .. ': required feature "bar" not present')
end -- }}}

do -- conflicts {{{
  local clone = repl:clone()
  local line_no

  clone:loadplugin(function()
    features = 'foo'
  end)

  local _, err = pcall(function()
    line_no = utils.next_line_number()
    clone:loadplugin(function()
      features = 'foo'
    end)
  end)
  like(err, tostring(line_no) .. ': feature "foo" already present')

  -- XXX what about methods injected into the object?
end -- }}}

do -- clone:hasfeature {{{
  local child = repl:clone()

  child:loadplugin(function()
    features = 'foo'
  end)

  local grandchild = child:clone()

  ok(not repl:hasfeature 'foo')
  ok(child:hasfeature 'foo')
  ok(grandchild:hasfeature 'foo')

  child:loadplugin(function()
    features = 'bar'
  end)

  ok(not repl:hasfeature 'bar')
  ok(child:hasfeature 'bar')
  ok(not grandchild:hasfeature 'bar')
end -- }}}

do -- iffeature tests {{{
  local clone = repl:clone()
  local has_run

  clone:iffeature('foo', function()
    has_run = true
  end)

  ok(not has_run)

  clone:loadplugin(function()
    features = 'foo'
  end)

  ok(has_run)

  has_run = false

  clone:iffeature('foo', function()
    has_run = true
  end)

  ok(has_run)
end -- }}}

do -- iffeature multiple times {{{
  local clone = repl:clone()
  local has_run
  local has_run2

  clone:iffeature('foo', function()
    has_run = true
  end)

  clone:iffeature('foo', function()
    has_run2 = true
  end)

  clone:loadplugin(function()
    features = 'foo'
  end)

  ok(has_run)
  ok(has_run2)
end -- }}}