| 1 |
local lua_stat = require 'lua_stat' |
| 2 |
local filename_to_tokens = lua_stat._filename_to_tokens |
| 3 |
|
| 4 |
context("lua_stat - filename_to_tokens", function() |
| 5 |
local cases = { |
| 6 |
{ |
| 7 |
name = 'ASCII filename: numeric parts discarded', |
| 8 |
input = {'invoice_2024-03-15.pdf', '#f:', '#fe:', '#fp:'}, |
| 9 |
result = {'#f:invoice_2024-03-15.pdf', '#fe:pdf', '#fp:invoice'}, |
| 10 |
}, |
| 11 |
{ |
| 12 |
name = 'letter+digit boundary split', |
| 13 |
input = {'temp3617331711606037072.pdf', '#f:', '#fe:', '#fp:'}, |
| 14 |
result = {'#f:temp3617331711606037072.pdf', '#fe:pdf', '#fp:temp'}, |
| 15 |
}, |
| 16 |
{ |
| 17 |
name = 'CamelCase split', |
| 18 |
input = {'InvoiceReport.docx', '#f:', '#fe:', '#fp:'}, |
| 19 |
result = {'#f:InvoiceReport.docx', '#fe:docx', '#fp:invoice', '#fp:report'}, |
| 20 |
}, |
| 21 |
{ |
| 22 |
name = 'double extension: legitimate compound format', |
| 23 |
input = {'archive.tar.gz', '#f:', '#fe:', '#fp:'}, |
| 24 |
result = {'#f:archive.tar.gz', '#fe:gz', '#fe:tar.gz', '#fp:archive', '#fp:tar'}, |
| 25 |
}, |
| 26 |
{ |
| 27 |
name = 'double extension: malware disguise pattern', |
| 28 |
input = {'document.pdf.exe', '#f:', '#fe:', '#fp:'}, |
| 29 |
result = {'#f:document.pdf.exe', '#fe:exe', '#fe:pdf.exe', '#fp:document', '#fp:pdf'}, |
| 30 |
}, |
| 31 |
{ |
| 32 |
name = 'Cyrillic + CamelCase + single-char part discarded', |
| 33 |
input = {'Заполнение CodeStock в диадок.docx', '#f:', '#fe:', '#fp:'}, |
| 34 |
result = { |
| 35 |
'#f:Заполнение CodeStock в диадок.docx', |
| 36 |
'#fe:docx', |
| 37 |
'#fp:заполнение', |
| 38 |
'#fp:code', |
| 39 |
'#fp:stock', |
| 40 |
'#fp:диадок', |
| 41 |
}, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name = 'Cyrillic + digit suffix split', |
| 45 |
input = {'апр26.pdf', '#f:', '#fe:', '#fp:'}, |
| 46 |
result = {'#f:апр26.pdf', '#fe:pdf', '#fp:апр'}, |
| 47 |
}, |
| 48 |
{ |
| 49 |
name = 'no extension', |
| 50 |
input = {'Makefile', '#f:', '#fe:', '#fp:'}, |
| 51 |
result = {'#f:Makefile', '#fp:makefile'}, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name = 'single-char base part discarded', |
| 55 |
input = {'a.pdf', '#f:', '#fe:', '#fp:'}, |
| 56 |
result = {'#f:a.pdf', '#fe:pdf'}, |
| 57 |
}, |
| 58 |
{ |
| 59 |
name = 'numeric base parts discarded', |
| 60 |
input = {'DHL_Tracking_8473921.pdf', '#f:', '#fe:', '#fp:'}, |
| 61 |
result = {'#f:DHL_Tracking_8473921.pdf', '#fe:pdf', '#fp:dhl', '#fp:tracking'}, |
| 62 |
}, |
| 63 |
{ |
| 64 |
name = 'dot-prefixed filename (no extension extracted)', |
| 65 |
input = {'.gitignore', '#f:', '#fe:', '#fp:'}, |
| 66 |
result = {'#f:.gitignore', '#fp:gitignore'}, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name = 'original token case-preserved', |
| 70 |
input = {'Invoice.PDF', '#f:', '#fe:', '#fp:'}, |
| 71 |
result = {'#f:Invoice.PDF', '#fe:pdf', '#fp:invoice'}, |
| 72 |
}, |
| 73 |
{ |
| 74 |
name = 'empty prefixes (image path, backward compatibility)', |
| 75 |
input = {'IMG_20260328_184333.jpg', '', '', ''}, |
| 76 |
result = {'IMG_20260328_184333.jpg', 'jpg', 'img'}, |
| 77 |
}, |
| 78 |
} |
| 79 |
|
| 80 |
for _, c in ipairs(cases) do |
| 81 |
test(c.name, function() |
| 82 |
local tokens = filename_to_tokens(table.unpack(c.input)) |
| 83 |
assert_equal(#c.result, #tokens, |
| 84 |
string.format('token count mismatch: expected %d, got %d: {%s}', |
| 85 |
#c.result, #tokens, table.concat(tokens, ', '))) |
| 86 |
for i, expected in ipairs(c.result) do |
| 87 |
assert_equal(expected, tokens[i]) |
| 88 |
end |
| 89 |
end) |
| 90 |
end |
| 91 |
end) |