Skip to content

Commit

Permalink
[policy] implement policy equality and tostring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed Feb 1, 2018
1 parent bc23f85 commit cd7f450
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
13 changes: 11 additions & 2 deletions gateway/src/apicast/policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ local PHASES = {

local setmetatable = setmetatable
local ipairs = ipairs
local format = string.format

local noop = function() end

local function __tostring(policy)
return format("Policy: %s (%s)", policy._NAME, policy._VERSION)
end

local function __eq(policy, other)
return policy._NAME == other._NAME and policy._VERSION == other._VERSION
end

--- Initialize new policy
-- Returns a new policy that you can extend however you want.
-- @tparam string name Name of the new policy.
Expand All @@ -31,7 +40,7 @@ function _M.new(name, version)
_NAME = name,
_VERSION = version or '0.0',
}
local mt = { __index = policy }
local mt = { __index = policy, __tostring = __tostring, __eq = __eq }

function policy.new()
return setmetatable({}, mt)
Expand All @@ -41,7 +50,7 @@ function _M.new(name, version)
policy[phase] = noop
end

return policy
return setmetatable(policy, { __tostring = __tostring, __eq = __eq })
end

function _M.phases()
Expand Down
50 changes: 50 additions & 0 deletions spec/policy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,56 @@ describe('policy', function()
end)
end)

describe('module equality', function()
it('equals when name and version are the same', function()
local p1 = policy.new('NAME', 'VERSION')
local p2 = policy.new('NAME', 'VERSION')

assert.are.equal(p1, p2)
assert.not_same(p1, p2)
end)

it('is not equal when names are different', function()
local p1 = policy.new('NAME', 'VERSION')
local p2 = policy.new('NAME2', 'VERSION')

assert.are.not_equal(p1, p2)
end)

it('is not equal when versions are different', function()
local p1 = policy.new('NAME', 'VERSION')
local p2 = policy.new('NAME', 'VERSION2')

assert.are.not_equal(p1, p2)
end)
end)

describe('module tostring', function()
it('shows name and version', function()
local p1 = policy.new('NAME', 'VERSION')

assert.equal('Policy: NAME (VERSION)', tostring(p1))
end)
end)

describe('instance equality', function()
local p1 = policy.new('NAME', 'VERSION')
local p2 = policy.new('NAME', 'VERSION')

it('equals when name and version are the same', function()
assert.are.equal(p1.new(), p1.new())
assert.are.equal(p1.new(), p2.new())
end)
end)

describe('instance tostring', function()
it('shows name and version', function()
local p1 = policy.new('NAME', 'VERSION').new()

assert.equal('Policy: NAME (VERSION)', tostring(p1))
end)
end)

describe('.phases', function()
it('returns the nginx phases where policies can run, sorted by order of execution', function()
local res = {}
Expand Down

0 comments on commit cd7f450

Please sign in to comment.