Skip to content

Commit e8b06ed

Browse files
committed
Add custom test input ianding1#12
1 parent 05a3fdb commit e8b06ed

File tree

2 files changed

+94
-30
lines changed

2 files changed

+94
-30
lines changed

autoload/leetcode.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -312,24 +312,16 @@ def _check_result(submission_id):
312312
return result
313313

314314

315-
def test_solution(slug, filetype, code=None):
315+
def test_solution(problem_id, title, slug, filetype, code, test_input):
316316
assert is_login()
317-
problem = get_problem(slug)
318-
if not problem:
319-
return None
320-
321-
if not problem['testable']:
322-
_echoerr('the problem is not testable, please submit directly')
323-
return None
324317

325-
if code is None:
326-
code = '\n'.join(vim.current.buffer)
318+
code = _remove_description(code)
327319

328320
headers = _make_headers()
329321
headers['Referer'] = LC_PROBLEM.format(slug=slug)
330-
body = {'data_input': problem['testcase'],
322+
body = {'data_input': test_input,
331323
'lang': filetype,
332-
'question_id': str(problem['id']),
324+
'question_id': str(problem_id),
333325
'test_mode': False,
334326
'typed_code': code}
335327
url = LC_TEST.format(slug=slug)
@@ -345,25 +337,27 @@ def test_solution(slug, filetype, code=None):
345337

346338
actual = _check_result(res.json()['interpret_id'])
347339
expected = _check_result(res.json()['interpret_expected_id'])
348-
actual['testcase'] = problem['testcase'].split('\n')
340+
actual['testcase'] = test_input.split('\n')
349341
actual['expected_answer'] = expected['answer']
350-
actual['title'] = problem['title']
342+
actual['title'] = title
351343
return actual
352344

353345

354-
def test_solution_async(slug, filetype, code=None):
346+
def test_solution_async(problem_id, title, slug, filetype, code, test_input):
355347
assert is_login()
356348
global task_input, task_name
357349
if task_running:
358350
_echoerr('there is other task running: ' + task_name)
359351
return False
360352

361-
if code is None:
362-
code = '\n'.join(vim.current.buffer)
353+
log.info('code %s', code)
354+
363355
code = _remove_description(code)
364356

357+
log.info('code removed %s', code)
358+
365359
task_name = 'test_solution'
366-
task_input = [slug, filetype, code]
360+
task_input = [problem_id, title, slug, filetype, code, test_input]
367361
task_trigger.release()
368362
return True
369363

@@ -591,7 +585,7 @@ def get_problems_of_topic(topic_slug):
591585

592586
if not topic_tag:
593587
return {'topic_name': topic_slug, 'problems': []}
594-
588+
595589
if topic_tag['frequencies']:
596590
id_to_frequency_map = json.loads(topic_tag['frequencies'])
597591
else:
@@ -684,7 +678,7 @@ def process_problem(p):
684678
'ac_rate': stats['totalAcceptedRaw'] / stats['totalSubmissionRaw'],
685679
'level': p['difficulty'],
686680
'favor': False,
687-
'frequencies': id_to_frequency_map.get(p['questionId'],
681+
'frequencies': id_to_frequency_map.get(p['questionId'],
688682
EMPTY_FREQUENCIES)[4:]}
689683

690684
return {
@@ -704,11 +698,9 @@ def _thread_main():
704698
log.info('task thread input: name="%s" input="%s"', task_name, task_input)
705699
try:
706700
if task_name == 'test_solution':
707-
slug, file_type, code = task_input
708-
task_output = test_solution(slug, file_type, code)
701+
task_output = test_solution(*task_input)
709702
elif task_name == 'submit_solution':
710-
slug, file_type, code = task_input
711-
task_output = submit_solution(slug, file_type, code)
703+
task_output = submit_solution(*task_input)
712704
except BaseException as e:
713705
task_err = str(e)
714706
log.info('task thread output: name="%s" output="%s" error="%s"', task_name, task_output,

autoload/leetcode.vim

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -649,18 +649,90 @@ function! leetcode#TestSolution() abort
649649
let slug = split(file_name, '\.')[0]
650650
let filetype = s:GuessFileType()
651651

652+
if exists('b:leetcode_problem')
653+
let problem = b:leetcode_problem
654+
else
655+
let problem = py3eval(printf('leetcode.get_problem("%s")', slug))
656+
let b:leetcode_problem = problem
657+
endif
658+
659+
if !problem['testable']
660+
echomsg 'the problem is not testable'
661+
return
662+
endif
663+
664+
let code = join(getline('1', '$'), "\n")
665+
666+
call s:AskTestInputAndRunTest(problem, filetype, code)
667+
endfunction
668+
669+
let s:saved_test_input = {}
670+
671+
function! s:AskTestInputAndRunTest(problem, filetype, code) abort
672+
execute 'rightbelow new ' . tempname()
673+
setlocal noswapfile
674+
setlocal nobackup
675+
setlocal bufhidden=delete
676+
setlocal nospell
677+
setlocal nobuflisted
678+
679+
syn match TestInputComment /\v^#.*/
680+
hi! link TestInputComment Comment
681+
682+
let slug = a:problem['slug']
683+
684+
if has_key(s:saved_test_input, slug)
685+
let default_test_input = s:saved_test_input[slug]
686+
else
687+
let default_test_input = ['# Test Input'] +
688+
\ split(a:problem['testcase'], '\n', 1) +
689+
\ ['', '# Delete or comment out all lines to abort']
690+
let s:saved_test_input[slug] = default_test_input
691+
endif
692+
693+
call append('$', default_test_input)
694+
695+
silent! normal! ggdd
696+
697+
let s:leetcode_problem = a:problem
698+
let s:leetcode_code = a:code
699+
let s:leetcode_filetype = a:filetype
700+
701+
autocmd BufUnload <buffer> call <SID>RunTest()
702+
endfunction
703+
704+
let s:comment_pattern = '\v(^#.*)|(^\s*$)'
705+
706+
function! s:RunTest() abort
707+
let problem = s:leetcode_problem
708+
let code = s:leetcode_code
709+
let filetype = s:leetcode_filetype
710+
711+
let test_input = getline('1', '$')
712+
let s:saved_test_input[problem['slug']] = test_input
713+
let test_input = filter(copy(test_input), 'v:val !~# s:comment_pattern')
714+
let test_input = join(test_input, "\n")
715+
716+
if test_input == ''
717+
echo 'Abort testing because the test input is empty'
718+
return
719+
endif
720+
721+
let args = {'problem_id': problem['id'],
722+
\ 'title': problem['title'],
723+
\ 'slug': problem['slug'],
724+
\ 'filetype': filetype,
725+
\ 'code': code,
726+
\ 'test_input': test_input}
727+
652728
if has('timers')
653-
let expr = printf('leetcode.test_solution_async("%s", "%s")',
654-
\ slug, filetype)
655-
let ok = py3eval(expr)
729+
let ok = py3eval('leetcode.test_solution_async(**vim.eval("args"))')
656730
if ok
657731
call timer_start(200, function('s:CheckRunCodeTask'),
658732
\ {'repeat': -1})
659733
endif
660734
else
661-
let expr = printf('leetcode.test_solution("%s", "%s")',
662-
\ slug, filetype)
663-
let result = py3eval(expr)
735+
let result = py3eval('leetcode.test_solution(**vim.eval("args"))')
664736
if type(result) != v:t_dict
665737
return
666738
endif

0 commit comments

Comments
 (0)