Skip to content

Commit d4eb22a

Browse files
committed
Extracted common argument-formatting code
1 parent 73cba6e commit d4eb22a

File tree

7 files changed

+66
-18
lines changed

7 files changed

+66
-18
lines changed

rockspecs/mach-git-0.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ build = {
2222
['mach.unexpected_args_error'] = 'mach/unexpected_args_error.lua',
2323
['mach.out_of_order_call_error'] = 'mach/out_of_order_call_error.lua',
2424
['mach.call_status_message'] = 'mach/call_status_message.lua',
25+
['mach.format_arguments'] = 'mach/format_arguments.lua',
2526
}
2627
}

src/mach/CompletedCall.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local completed_call = {}
2+
completed_call.__index = completed_call
3+
4+
completed_call.__tostring = function(self)
5+
local arg_strings = {}
6+
for _, arg in ipairs(self._args) do
7+
table.insert(arg_strings, tostring(arg))
8+
end
9+
10+
return self._name .. '(' .. table.concat(arg_strings, ', ') .. ')'
11+
end
12+
13+
local function create(name, args)
14+
local o = {
15+
_name = name,
16+
_args = args
17+
}
18+
19+
setmetatable(o, completed_call)
20+
21+
return o
22+
end
23+
24+
return create

src/mach/call_status_message.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
return function(completed_calls, incomplete_calls)
2+
local incomplete_call_strings = {}
3+
for _, incomplete_call in ipairs(incomplete_calls) do
4+
table.insert(incomplete_call_strings, tostring(incomplete_call))
5+
end
6+
7+
local completed_call_strings = {}
8+
for _, completed_call in ipairs(completed_calls) do
9+
table.insert(completed_call_strings, tostring(completed_call))
10+
end
11+
12+
local message = ''
13+
14+
if #completed_calls > 0 then
15+
message = message ..
16+
'\ncompleted calls:' ..
17+
'\n\t' .. table.concat(completed_call_strings, '\n\t')
18+
end
19+
20+
if #incomplete_calls > 0 then
21+
message = message ..
22+
'\nincomplete calls:' ..
23+
'\n\t' .. table.concat(incomplete_call_strings, '\n\t')
24+
end
25+
26+
return message
27+
end

src/mach/format_arguments.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
return function(args)
2+
local arg_strings = {}
3+
for _, arg in ipairs(args) do
4+
table.insert(arg_strings, tostring(arg))
5+
end
6+
7+
return '(' .. table.concat(arg_strings, ', ') .. ')'
8+
end

src/mach/out_of_order_call_error.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
local call_status_message = require 'mach.call_status_message'
2+
local format_arguments = require 'mach.format_arguments'
23

34
return function(name, args, completed_calls, incomplete_calls, level)
4-
local arg_strings = {}
5-
for _, arg in ipairs(args) do
6-
table.insert(arg_strings, tostring(arg))
7-
end
8-
95
local error_message =
10-
'out of order function call ' .. name .. '(' .. table.concat(arg_strings, ', ') .. ')' ..
6+
'out of order function call ' .. name .. format_arguments(args) ..
117
call_status_message(completed_calls, incomplete_calls)
128

139
error(error_message, level + 1)

src/mach/unexpected_args_error.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
local call_status_message = require 'mach.call_status_message'
2+
local format_arguments = require 'mach.format_arguments'
23

34
return function(name, args, completed_calls, incomplete_calls, level)
4-
local arg_strings = {}
5-
for _, arg in ipairs(args) do
6-
table.insert(arg_strings, tostring(arg))
7-
end
8-
95
local error_message =
10-
'unexpected arguments (' .. table.concat(arg_strings) .. ') provided to function ' .. name ..
6+
'unexpected arguments ' .. format_arguments(args) .. ' provided to function ' .. name ..
117
call_status_message(completed_calls, incomplete_calls)
128

139
error(error_message, level + 1)

src/mach/unexpected_call_error.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
local call_status_message = require 'mach.call_status_message'
2+
local format_arguments = require 'mach.format_arguments'
23

34
return function(name, args, completed_calls, incomplete_calls, level)
4-
local arg_strings = {}
5-
for _, arg in ipairs(args) do
6-
table.insert(arg_strings, tostring(arg))
7-
end
8-
95
local message =
10-
'unexpected function call ' .. name .. '(' .. table.concat(arg_strings, ', ') .. ')' ..
6+
'unexpected function call ' .. name .. format_arguments(args) ..
117
call_status_message(completed_calls, incomplete_calls)
128

139
error(message, level + 1)

0 commit comments

Comments
 (0)