summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/basic/noder.lua24
-rw-r--r--test/catch.lua58
2 files changed, 69 insertions, 13 deletions
diff --git a/test/basic/noder.lua b/test/basic/noder.lua
index bc572a4c..4c0a1620 100644
--- a/test/basic/noder.lua
+++ b/test/basic/noder.lua
@@ -2,6 +2,7 @@ local noder = require 'core.noder'
local files = require 'files'
local util = require 'utility'
local guide = require 'parser.guide'
+local catch = require 'catch'
local function getSource(pos)
local ast = files.getState('')
@@ -30,12 +31,9 @@ local CARE = {}
local function TEST(script)
return function (expect)
files.removeAll()
- local start = script:find('<?', 1, true)
- local finish = script:find('?>', 1, true)
- local pos = (start + finish) // 2 + 1
- local newScript = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ')
+ local newScript, catched = catch(script, '?')
files.setText('', newScript)
- local source = getSource(pos)
+ local source = getSource(catched[1][1])
assert(source)
local result = {
id = noder.getID(source),
@@ -53,21 +51,21 @@ CARE['id'] = true
TEST [[
local <?x?>
]] {
- id = 'l:9',
+ id = 'l:6',
}
TEST [[
local x
print(<?x?>)
]] {
- id = 'l:7',
+ id = 'l:6',
}
TEST [[
local x
<?x?> = 1
]] {
- id = 'l:7',
+ id = 'l:6',
}
TEST [[
@@ -86,14 +84,14 @@ TEST [[
local x
print(x.y.<?z?>)
]] {
- id = 'l:7|.y|.z',
+ id = 'l:6|.y|.z',
}
TEST [[
local x
function x:<?f?>() end
]] {
- id = 'l:7|.f',
+ id = 'l:6|.f',
}
TEST [[
@@ -113,7 +111,7 @@ TEST [[
<?x?> = 1,
}
]] {
- id = 't:1|.x',
+ id = 't:0|.x',
}
TEST [[
@@ -134,12 +132,12 @@ TEST [[
::<?label?>::
goto label
]] {
- id = 'l:5',
+ id = 'l:2',
}
TEST [[
::label::
goto <?label?>
]] {
- id = 'l:3',
+ id = 'l:2',
}
diff --git a/test/catch.lua b/test/catch.lua
new file mode 100644
index 00000000..910c368f
--- /dev/null
+++ b/test/catch.lua
@@ -0,0 +1,58 @@
+
+local function getLine(offset, lns)
+ for i = 0, #lns do
+ if offset >= lns[i]
+ and offset < lns[i+1] then
+ return i
+ end
+ end
+end
+
+local function getPosition(offset, lns)
+ for i = 0, #lns do
+ if offset >= lns[i]
+ and offset < lns[i+1] then
+ return 10000 * i + offset - lns[i]
+ end
+ end
+end
+
+---@param script string
+---@param sep string
+return function (script, sep)
+ local pattern = ('()<%%%s.-%%%s>()'):format(sep, sep)
+ local lns = {}
+ lns[0] = 0
+ for pos in script:gmatch '()\n' do
+ lns[#lns+1] = pos
+ end
+ lns[#lns+1] = math.maxinteger
+ local codes = {}
+ local pos = 1
+ ---@type integer[]
+ local list = {}
+ local cuted = 0
+ local lastLine = 0
+ for a, b in script:gmatch(pattern) do
+ codes[#codes+1] = script:sub(pos, a - 1)
+ codes[#codes+1] = script:sub(a + 2, b - 3)
+ pos = b
+ local line1 = getLine(a + 1, lns)
+ if line1 ~= lastLine then
+ cuted = 0
+ lastLine = line1
+ end
+ cuted = cuted + 2
+ local left = getPosition(a + 1, lns) - cuted
+ local line2 = getLine(b - 3, lns)
+ if line2 ~= lastLine then
+ cuted = 0
+ lastLine = line2
+ end
+ local right = getPosition(b - 3, lns) - cuted
+ cuted = cuted + 2
+ list[#list+1] = { left, right }
+ end
+ codes[#codes+1] = script:sub(pos)
+ return table.concat(codes), list
+end