Repository navigation
Expand file tree
/
Copy pathtest_lua_string_patterns.py
More file actions
483 lines (389 loc) · 13.8 KB
/
Copy pathtest_lua_string_patterns.py
File metadata and controls
483 lines (389 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
from pathlib import Path
from unittest.mock import MagicMock
from codebase_rag.graph_updater import GraphUpdater
from codebase_rag.parser_loader import load_parsers
def test_string_pattern_matching(temp_repo: Path, mock_ingestor: MagicMock) -> None:
"""Test user-defined string pattern processing functions."""
project = temp_repo / "lua_patterns"
project.mkdir()
(project / "patterns.lua").write_text("""
local pattern_utils = {}
function pattern_utils.match_pattern(text, pattern)
-- Match pattern in text
return string.match(text, pattern)
end
function pattern_utils.match_all_pattern(text, pattern)
-- Match all occurrences of pattern
local matches = {}
for match in string.gmatch(text, pattern) do
table.insert(matches, match)
end
return matches
end
function pattern_utils.find_pattern_position(text, pattern, start_pos)
-- Find pattern position in text
return string.find(text, pattern, start_pos or 1)
end
function pattern_utils.replace_pattern(text, pattern, replacement)
-- Replace pattern in text
return string.gsub(text, pattern, replacement)
end
function pattern_utils.convert_to_number(text)
-- Convert text to number
return tonumber(text)
end
function pattern_utils.add_to_table(table, item)
-- Add item to table
table.insert(table, item)
return table
end
function validate_email(email)
local pattern = "[%w%.%-_]+@[%w%.%-]+%.%w+"
local match = pattern_utils.match_pattern(email, pattern)
return match ~= nil
end
function extract_numbers(text)
local number_strings = pattern_utils.match_all_pattern(text, "%d+")
local numbers = {}
for _, num_str in ipairs(number_strings) do
local num = pattern_utils.convert_to_number(num_str)
pattern_utils.add_to_table(numbers, num)
end
return numbers
end
function find_word_positions(text, word)
local positions = {}
local start = 1
while true do
local pos = pattern_utils.find_pattern_position(text, word, start)
if not pos then break end
pattern_utils.add_to_table(positions, pos)
start = pos + 1
end
return positions
end
-- Use our pattern utilities
local phone = "123-456-7890"
local area_code = pattern_utils.match_pattern(phone, "(%d%d%d)")
local formatted = pattern_utils.replace_pattern(phone, "%-", ".")
-- Test our functions
local email_valid = validate_email("[email protected]")
local numbers = extract_numbers("I have 10 cats and 5 dogs")
local positions = find_word_positions("hello world hello", "hello")
return pattern_utils
""")
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor, repo_path=project, parsers=parsers, queries=queries
)
updater.run()
# Verify DEFINES relationships for user-defined pattern functions
defines_rels = [
c
for c in mock_ingestor.ensure_relationship_batch.call_args_list
if c.args[1] == "DEFINES"
]
# Verify CALLS relationships for user-defined function calls
calls_rels = [
c
for c in mock_ingestor.ensure_relationship_batch.call_args_list
if c.args[1] == "CALLS"
]
assert len(defines_rels) >= 9, (
f"Expected at least 9 DEFINES relationships, got {len(defines_rels)}"
)
assert len(calls_rels) >= 6, (
f"Expected at least 6 CALLS relationships, got {len(calls_rels)}"
)
def test_string_manipulation_functions(
temp_repo: Path, mock_ingestor: MagicMock
) -> None:
"""Test user-defined string manipulation wrapper functions."""
project = temp_repo / "lua_string_manip"
project.mkdir()
(project / "string_manip.lua").write_text("""
local string_manip = {}
function string_manip.trim_whitespace(input)
-- Trim leading and trailing whitespace
return string.match(input, "^%s*(.-)%s*$")
end
function string_manip.reverse_string(input)
-- Reverse string
return string.reverse(input)
end
function string_manip.repeat_string(str, count)
-- Repeat string multiple times
return string.rep(str, count)
end
function string_manip.get_string_bytes(input, start_pos, end_pos)
-- Get byte values of string characters
return {string.byte(input, start_pos or 1, end_pos or -1)}
end
function string_manip.create_string_from_bytes(...)
-- Create string from byte values
return string.char(...)
end
function string_manip.get_string_length(input)
-- Get string length
return string.len(input)
end
function string_manip.get_substring(input, start_pos, end_pos)
-- Get substring
return string.sub(input, start_pos, end_pos)
end
function string_manip.replace_with_pattern(text, pattern, replacement)
-- Replace pattern in text
return string.gsub(text, pattern, replacement)
end
function string_manip.to_uppercase(text)
-- Convert to uppercase
return string.upper(text)
end
function string_manip.to_lowercase(text)
-- Convert to lowercase
return string.lower(text)
end
function string_manip.format_string(format, ...)
-- Format string with values
return string.format(format, ...)
end
function text_processing(input)
local trimmed = string_manip.trim_whitespace(input)
local reversed = string_manip.reverse_string(input)
local repeated = string_manip.repeat_string("*", 10)
local bytes = string_manip.get_string_bytes(input, 1, -1)
local chars = string_manip.create_string_from_bytes(65, 66, 67)
local length = string_manip.get_string_length(input)
local substring = string_manip.get_substring(input, 2, -2)
return {
trimmed = trimmed,
reversed = reversed,
repeated = repeated,
bytes = bytes,
chars = chars,
length = length,
substring = substring
}
end
function replace_patterns(text)
local no_digits = string_manip.replace_with_pattern(text, "%d", "X")
local no_spaces = string_manip.replace_with_pattern(text, "%s+", "_")
local capitalized = string_manip.replace_with_pattern(text, "(%w)(%w*)", function(first, rest)
return string_manip.to_uppercase(first) .. string_manip.to_lowercase(rest)
end)
return capitalized
end
-- Use our string manipulation functions
local test_input = " hello world "
local processed = text_processing(test_input)
local replaced = replace_patterns("hello 123 world")
local formatted_text = string_manip.format_string("Value: %d, Percent: %.2f%%", 42, 87.5)
return string_manip
""")
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor, repo_path=project, parsers=parsers, queries=queries
)
updater.run()
# Verify DEFINES relationships for user-defined string functions
defines_rels = [
c
for c in mock_ingestor.ensure_relationship_batch.call_args_list
if c.args[1] == "DEFINES"
]
# Verify CALLS relationships for user-defined function calls
calls_rels = [
c
for c in mock_ingestor.ensure_relationship_batch.call_args_list
if c.args[1] == "CALLS"
]
assert len(defines_rels) >= 13, (
f"Expected at least 13 DEFINES relationships, got {len(defines_rels)}"
)
assert len(calls_rels) >= 10, (
f"Expected at least 10 CALLS relationships, got {len(calls_rels)}"
)
def test_complex_pattern_operations(temp_repo: Path, mock_ingestor: MagicMock) -> None:
"""Test user-defined complex pattern processing functions."""
project = temp_repo / "lua_complex_patterns"
project.mkdir()
(project / "complex_patterns.lua").write_text("""
local complex_patterns = {}
function complex_patterns.extract_with_pattern(text, pattern)
-- Extract text using pattern
return string.match(text, pattern)
end
function complex_patterns.extract_all_with_pattern(text, pattern)
-- Extract all matches with pattern
local matches = {}
for match in string.gmatch(text, pattern) do
table.insert(matches, match)
end
return matches
end
function complex_patterns.escape_special_chars(input, char_pattern, replacement)
-- Escape special characters in input
return string.gsub(input, char_pattern, replacement)
end
function complex_patterns.add_to_collection(collection, item)
-- Add item to collection
table.insert(collection, item)
return collection
end
function complex_patterns.trim_field(field)
-- Trim whitespace from field
return string.match(field, "^%s*(.-)%s*$")
end
function parse_log_entry(log_line)
local timestamp_pattern = "(%d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d)"
local level_pattern = "%[(%w+)%]"
local message_pattern = ":%s*(.+)$"
local timestamp = complex_patterns.extract_with_pattern(log_line, timestamp_pattern)
local level = complex_patterns.extract_with_pattern(log_line, level_pattern)
local message = complex_patterns.extract_with_pattern(log_line, message_pattern)
return {
timestamp = timestamp,
level = level,
message = message
}
end
function extract_urls(text)
local url_pattern = "https?://[%w%.%-_/%%?=&]+"
return complex_patterns.extract_all_with_pattern(text, url_pattern)
end
function sanitize_input(input)
local escaped = complex_patterns.escape_special_chars(
input,
"([%^%$%(%)%%%.%[%]%*%+%-%?])",
"%%%1"
)
local safe = complex_patterns.escape_special_chars(escaped, "%c", "")
return safe
end
function parse_csv_line(csv_line)
local fields = complex_patterns.extract_all_with_pattern(csv_line, "([^,]+)")
local trimmed_fields = {}
for _, field in ipairs(fields) do
local trimmed = complex_patterns.trim_field(field)
complex_patterns.add_to_collection(trimmed_fields, trimmed)
end
return trimmed_fields
end
-- Use our complex pattern functions
local log_data = parse_log_entry("2023-01-01 10:00:00 [INFO]: Application started")
local urls = extract_urls("Visit https://example.com and http://test.org")
local safe_input = sanitize_input("user[input]")
local csv_fields = parse_csv_line("name, age, city")
return complex_patterns
""")
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor, repo_path=project, parsers=parsers, queries=queries
)
updater.run()
# Verify DEFINES relationships for user-defined complex pattern functions
defines_rels = [
c
for c in mock_ingestor.ensure_relationship_batch.call_args_list
if c.args[1] == "DEFINES"
]
# Verify CALLS relationships for user-defined function calls
calls_rels = [
c
for c in mock_ingestor.ensure_relationship_batch.call_args_list
if c.args[1] == "CALLS"
]
assert len(defines_rels) >= 9, (
f"Expected at least 9 DEFINES relationships, got {len(defines_rels)}"
)
assert len(calls_rels) >= 6, (
f"Expected at least 6 CALLS relationships, got {len(calls_rels)}"
)
def test_unicode_and_encoding(temp_repo: Path, mock_ingestor: MagicMock) -> None:
"""Test user-defined Unicode processing wrapper functions."""
project = temp_repo / "lua_unicode"
project.mkdir()
(project / "unicode.lua").write_text("""
local unicode_utils = {}
local utf8 = require("utf8")
function unicode_utils.get_utf8_length(text)
-- Get UTF-8 character count
return utf8.len(text)
end
function unicode_utils.get_codepoint_at(text, position)
-- Get codepoint at position
return utf8.codepoint(text, position)
end
function unicode_utils.create_char_from_codepoint(codepoint)
-- Create character from codepoint
return utf8.char(codepoint)
end
function unicode_utils.iterate_codepoints(text, processor_func)
-- Iterate over codepoints in text
for pos, code in utf8.codes(text) do
processor_func(pos, code)
end
end
function unicode_utils.get_byte_offset(text, char_position)
-- Get byte offset for character position
return utf8.offset(text, char_position)
end
function unicode_utils.format_codepoint(pos, code)
-- Format codepoint for display
return string.format("Position %d: U+%04X", pos, code)
end
function unicode_utils.print_codepoint_info(pos, code)
-- Print codepoint information
local formatted = unicode_utils.format_codepoint(pos, code)
print(formatted)
end
function handle_unicode(text)
local char_count = unicode_utils.get_utf8_length(text)
local codepoint = unicode_utils.get_codepoint_at(text, 1)
local char = unicode_utils.create_char_from_codepoint(0x1F4A9)
unicode_utils.iterate_codepoints(text, unicode_utils.print_codepoint_info)
local offset = unicode_utils.get_byte_offset(text, 5)
return {
length = char_count,
first_code = codepoint,
emoji = char,
fifth_offset = offset
}
end
-- Use our unicode utilities
local result = handle_unicode("Hello 🌍")
local valid_utf8 = unicode_utils.get_utf8_length("Hello 🌍")
return unicode_utils
""")
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor, repo_path=project, parsers=parsers, queries=queries
)
updater.run()
# Verify DEFINES relationships for user-defined unicode functions
defines_rels = [
c
for c in mock_ingestor.ensure_relationship_batch.call_args_list
if c.args[1] == "DEFINES"
]
# Verify CALLS relationships for user-defined function calls
calls_rels = [
c
for c in mock_ingestor.ensure_relationship_batch.call_args_list
if c.args[1] == "CALLS"
]
# Verify IMPORTS relationships (for utf8 module)
imports_rels = [
c
for c in mock_ingestor.ensure_relationship_batch.call_args_list
if c.args[1] == "IMPORTS"
]
assert len(defines_rels) >= 7, (
f"Expected at least 7 DEFINES relationships, got {len(defines_rels)}"
)
assert len(calls_rels) >= 5, (
f"Expected at least 5 CALLS relationships, got {len(calls_rels)}"
)
assert len(imports_rels) >= 1, (
f"Expected at least 1 IMPORTS relationship, got {len(imports_rels)}"
)