fortrangoingonforty/fortsh / 9e63e2d

Browse files

add c-style for loop and keyword-outside-cmd-pos tests

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
9e63e2d32cf10433b30bf0fc0511d642d1c7abd2
Parents
43b7109
Tree
f3b2b1a

1 changed file

StatusFile+-
M tests/test_syntax_highlight.f90 36 4
tests/test_syntax_highlight.f90modified
@@ -49,6 +49,8 @@ program test_syntax_highlight
4949
   call test_complex_pipeline()
5050
   call test_keyword_resets_cmd_pos()
5151
   call test_case_esac()
52
+  call test_c_style_for_loop()
53
+  call test_keyword_outside_cmd_pos()
5254
 
5355
   write(*, '(a)') ''
5456
   write(*, '(a)') '=========================================='
@@ -431,10 +433,40 @@ contains
431433
     call tokenize_v2(input, 9, tokens, n)
432434
     call assert_eq('case_esac: count', 2, n)
433435
     call assert_eq('case_esac/case', HTOK_KEYWORD, tokens(1)%token_type)
434
-    ! esac is not in command position after case (case doesn't reset cmd pos)
435
-    ! but 'case' is a keyword. After case, in_cmd_pos goes to false.
436
-    ! esac is a word in non-cmd position — DEFAULT
437
-    call assert_eq('case_esac/esac', HTOK_DEFAULT, tokens(2)%token_type)
436
+    ! esac is always recognized as a keyword regardless of command position
437
+    call assert_eq('case_esac/esac', HTOK_KEYWORD, tokens(2)%token_type)
438
+  end subroutine
439
+
440
+  ! C-style for loop: for ((i=0; i<3; i++)) do echo $i; done
441
+  subroutine test_c_style_for_loop()
442
+    character(len=256) :: input
443
+    type(hl_token_t) :: tokens(MT)
444
+    integer :: n
445
+
446
+    input = 'for ((i=0; i<3; i++)) do echo $i; done'
447
+    call tokenize_v2(input, 38, tokens, n)
448
+    ! for = KEYWORD, ((...)) = NUMBER (arithmetic), do = KEYWORD,
449
+    ! echo = BUILTIN, $i = VARIABLE, ; = OPERATOR, done = KEYWORD
450
+    call assert_eq('cfor: for', HTOK_KEYWORD, tokens(1)%token_type)
451
+    call assert_eq('cfor: (())', HTOK_NUMBER, tokens(2)%token_type)
452
+    call assert_eq('cfor: do', HTOK_KEYWORD, tokens(3)%token_type)
453
+    call assert_eq('cfor: echo', HTOK_BUILTIN, tokens(4)%token_type)
454
+    call assert_eq('cfor: $i', HTOK_VARIABLE, tokens(5)%token_type)
455
+    call assert_eq('cfor: ;', HTOK_OPERATOR, tokens(6)%token_type)
456
+    call assert_eq('cfor: done', HTOK_KEYWORD, tokens(7)%token_type)
457
+  end subroutine
458
+
459
+  ! Keywords recognized even outside command position
460
+  subroutine test_keyword_outside_cmd_pos()
461
+    character(len=256) :: input
462
+    type(hl_token_t) :: tokens(MT)
463
+    integer :: n
464
+
465
+    ! 'echo done' — done after a builtin should still be keyword
466
+    input = 'echo done'
467
+    call tokenize_v2(input, 9, tokens, n)
468
+    call assert_eq('kw_nonpos: echo', HTOK_BUILTIN, tokens(1)%token_type)
469
+    call assert_eq('kw_nonpos: done', HTOK_KEYWORD, tokens(2)%token_type)
438470
   end subroutine
439471
 
440472
 end program test_syntax_highlight