Skip to content

Commit

Permalink
avoid calling Util.mkString with associative array (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
czenker committed Dec 31, 2017
1 parent c79ba8f commit 1ab9ca4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/lively_epsilon/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ Util = {
end,

mkString = function(table, separator, lastSeparator)
if not Util.isNumericTable(table) then
error("The given table needs to have numerical indices.", 2)
end

local string = ""

local lastIndex = 0
Expand All @@ -90,7 +94,6 @@ Util = {
string = string .. value
isFirst = false
else
local nextKey, nextValue = next(table)
if k == lastIndex and lastSeparator ~= nil then
-- if last element
string = string .. lastSeparator .. value
Expand Down
6 changes: 4 additions & 2 deletions test/util_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,12 @@ insulate("Util", function()
assert.equal(Util.mkString(table, ", ", " and "), "one, two and three")
end)

it("should return a string for three values when using an indexed table", function()
it("should fail when using an associative table", function()
local table = { a = "one", c = "two", b = "three" }

assert.equal(Util.mkString(table, ", ", " and "), "one, two and three")
assert.has_error(function()
Util.mkString(table, ", ", " and ")
end)
end)
end)

Expand Down

0 comments on commit 1ab9ca4

Please sign in to comment.