Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context.mergeFilters #889

Merged
merged 5 commits into from
Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/base/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,14 @@
--

function context.mergeFilters(ctx, src)
for k,v in pairs(src.terms) do
ctx.terms[k] = v
if src.terms ~= nil then
for k, v in pairs(src.terms) do
ctx.terms[k] = table.joinunique(ctx.terms[k] or {}, src.terms[k])

if #ctx.terms[k] == 1 then
ctx.terms[k] = ctx.terms[k][1]
end
end
end
end

Expand Down
30 changes: 30 additions & 0 deletions src/base/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,33 @@
end
return result
end

--
-- Adds the values from one array to the end of another without duplicates and
-- returns the result.
--
function table.joinunique(...)

local arg = {...}
local elems = { }
local result = { }

local function insertunique(elem)
if not elems[elem] then
table.insert(result, elem)
elems[elem] = true
end
end

for _,t in ipairs(arg) do

if type(t) == "table" then
table.foreachi(t, insertunique)
else
insertunique(t)
end

end

return result
end
38 changes: 38 additions & 0 deletions tests/base/test_context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,41 @@
-- detoken in extended context should result in value set in that environ.
test.isequal("text", ext.targetname)
end

--
-- mergeFilters should behave as expected for tags
--

function suite.mergeFilters()

ctx = { terms = { tags = { "ctxtags" } } }
src = { terms = { tags = { "srctags" } } }

context.mergeFilters(ctx, src)

result = { terms = { tags = { "ctxtags", "srctags" } } }

test.isequal(result, ctx)
end

function suite.mergeFilters_keeptype()

ctx = { terms = { kind = "ConsoleApp" } }
src = { terms = { kind = "ConsoleApp" } }

context.mergeFilters(ctx, src)

test.isequal("string", type(ctx.terms.kind))
end

function suite.mergeFilters_createtable()

ctx = { terms = { tags = "ctxtags" } }
src = { terms = { tags = "srctags" } }

context.mergeFilters(ctx, src)

result = { terms = { tags = { "ctxtags", "srctags" } } }

test.isequal(result, ctx)
end