summaryrefslogtreecommitdiff
path: root/Data/Libraries/Penlight/spec/utils-deprecate_spec.lua
blob: 1a6e3526075b0e1a21379bee4751d647eab067fb (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
local utils = require("pl.utils")

describe("pl.utils", function ()

  local old_fn, last_msg, last_trace

  before_each(function()
    old_fn = function() end
    last_msg = nil
    last_trace = nil
    utils.set_deprecation_func(function(msg, trace)
      last_msg = msg
      last_trace = trace
    end)
  end)


  after_each(function()
    utils.deprecation_warning = old_fn
  end)



  describe("set_deprecation_func", function ()

    it("accepts nil as callback", function()
      assert.has.no.error(function()
        utils.set_deprecation_func()
      end)
    end)


    it("accepts function as callback", function()
      assert.has.no.error(function()
        utils.set_deprecation_func(function() end)
      end)
    end)


    it("fails on non-functions", function()
      assert.has.error(function()
        utils.set_deprecation_func("not a function")
      end, "argument 1 expected a 'function', got a 'string'")
    end)

  end)



  describe("raise_deprecation", function ()

    it("requires the opts table", function()
      assert.has.error(function() utils.raise_deprecation(nil) end,
                       "argument 1 expected a 'table', got a 'nil'")
    end)


    it("requires the opts.message field", function()
      assert.has.error(function() utils.raise_deprecation({}) end,
                       "field 'message' of the options table must be a string")
    end)


    it("should output the message", function ()
      utils.raise_deprecation {
        message = "hello world"
      }
      assert.equal("hello world", last_msg)
    end)


    it("should output the deprecated version", function ()
      utils.raise_deprecation {
        message = "hello world",
        deprecated_after = "2.0.0",
      }
      assert.equal("hello world (deprecated after 2.0.0)", last_msg)
    end)


    it("should output the removal version", function ()
      utils.raise_deprecation {
        message = "hello world",
        version_removed = "3.0.0",
      }
      assert.equal("hello world (scheduled for removal in 3.0.0)", last_msg)
    end)


    it("should output the deprecated and removal versions", function ()
      utils.raise_deprecation {
        message = "hello world",
        deprecated_after = "2.0.0",
        version_removed = "3.0.0",
      }
      assert.equal("hello world (deprecated after 2.0.0, scheduled for removal in 3.0.0)", last_msg)
    end)


    it("should output the application/module name", function ()
      utils.raise_deprecation {
        source = "MyApp 1.2.3",
        message = "hello world",
        deprecated_after = "2.0.0",
        version_removed = "3.0.0",
      }
      assert.equal("[MyApp 1.2.3] hello world (deprecated after 2.0.0, scheduled for removal in 3.0.0)", last_msg)
    end)


    it("should add a stracktrace", function ()
      local function my_function_name()
        utils.raise_deprecation {
          source = "MyApp 1.2.3",
          message = "hello world",
          deprecated_after = "2.0.0",
          version_removed = "3.0.0",
        }
      end
      my_function_name()

      assert.Not.match("raise_deprecation", last_trace)
      assert.match("my_function_name", last_trace)
    end)

  end)

end)