Fortran · 3307 bytes Raw Blame History
1 program test_accept_line
2 use fgof_lineedit, only : &
3 accept_line, &
4 add_history_entry, &
5 completion_count, &
6 default_prompt, &
7 history_count, &
8 history_next, &
9 history_previous, &
10 init_lineedit, &
11 lineedit_state, &
12 refresh_completion_menu, &
13 set_buffer
14 implicit none
15
16 type(lineedit_state) :: editor
17 character(len=:), allocatable :: line
18
19 call init_lineedit(editor, default_prompt("> "))
20
21 call set_buffer(editor, "build", 3)
22 call accept_line(editor, line)
23 if (line /= "build") error stop "accept_line should return the current buffer contents"
24 if (editor%buffer /= "") error stop "accept_line should clear the buffer for the next prompt"
25 if (editor%cursor /= 1) error stop "accept_line should reset the cursor to the first insertion column"
26 if (history_count(editor) /= 1) error stop "accept_line should store non-empty lines in history by default"
27 if (.not. editor%active) error stop "accept_line should not deactivate the editor"
28
29 call accept_line(editor, line)
30 if (line /= "") error stop "accept_line should return an empty line when the buffer is empty"
31 if (history_count(editor) /= 1) error stop "accept_line should not store empty lines by default"
32
33 call accept_line(editor, line, store_history=.true.)
34 if (line /= "") error stop "accept_line should still return the empty line when history storage is forced"
35 if (history_count(editor) /= 2) error stop "accept_line should store blank lines when store_history=.true."
36
37 call set_buffer(editor, "scratch", 8)
38 call accept_line(editor, line, store_history=.false.)
39 if (line /= "scratch") error stop "accept_line should still return the buffer when history storage is disabled"
40 if (history_count(editor) /= 2) error stop "accept_line should honor store_history=.false."
41
42 call add_history_entry(editor, "echo one")
43 call add_history_entry(editor, "echo two")
44 if (.not. history_previous(editor)) error stop "history_previous should recall the newest entry before accept_line"
45 call refresh_completion_menu(editor, provide_completions)
46 if (completion_count(editor) /= 1) error stop "completion menu should be visible before accept_line clears it"
47
48 call accept_line(editor, line)
49 if (line /= "echo two") error stop "accept_line should accept recalled history lines"
50 if (completion_count(editor) /= 0) error stop "accept_line should clear transient completion state"
51 if (editor%completion_visible) error stop "accept_line should hide the completion menu"
52 if (history_next(editor)) error stop "accept_line should clear transient history-navigation state"
53 if (history_count(editor) /= 5) error stop "accept_line should add accepted recalled lines to history by default"
54
55 contains
56
57 subroutine provide_completions(editor, span, items)
58 use fgof_lineedit, only : completion_item, completion_span
59 type(lineedit_state), intent(in) :: editor
60 type(completion_span), intent(in) :: span
61 type(completion_item), allocatable, intent(out) :: items(:)
62
63 if (editor%buffer /= "echo two") error stop "provider should see the recalled history line"
64 if (span%prefix /= "two") error stop "provider should see the active word prefix"
65
66 allocate(items(1))
67 items(1)%text = "echo"
68 end subroutine provide_completions
69
70 end program test_accept_line
71