2
0

demo_bash.pp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. program test_bash;
  2. {$mode objfpc}{$H+}
  3. uses
  4. SysUtils, Classes, syntax.highlighter, syntax.bash;
  5. procedure TestBashKeywords;
  6. const
  7. BashKeywords: array[0..19] of string = (
  8. 'if', 'then', 'else', 'fi', 'case', 'esac', 'for', 'do', 'done', 'while',
  9. 'function', 'return', 'break', 'continue', 'declare', 'local', 'export', 'set', 'test', 'eval'
  10. );
  11. var
  12. highlighter: TBashSyntaxHighlighter;
  13. tokens: TSyntaxTokenArray;
  14. i: Integer;
  15. begin
  16. WriteLn('Testing Bash Keywords:');
  17. WriteLn('=====================');
  18. highlighter := TBashSyntaxHighlighter.Create;
  19. try
  20. for i := 0 to High(BashKeywords) do begin
  21. tokens := highlighter.Execute(BashKeywords[i]);
  22. if (Length(tokens) = 1) and (tokens[0].Kind = shKeyword) then
  23. WriteLn(BashKeywords[i] + ': PASS - recognized as keyword')
  24. else
  25. WriteLn(BashKeywords[i] + ': FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  26. end;
  27. finally
  28. highlighter.Free;
  29. end;
  30. WriteLn;
  31. end;
  32. procedure TestBashTokenTypes;
  33. var
  34. highlighter: TBashSyntaxHighlighter;
  35. tokens: TSyntaxTokenArray;
  36. begin
  37. WriteLn('Testing Bash Token Types:');
  38. WriteLn('========================');
  39. highlighter := TBashSyntaxHighlighter.Create;
  40. try
  41. // Test single-quoted string
  42. tokens := highlighter.Execute('''hello world''');
  43. if (Length(tokens) = 1) and (tokens[0].Kind = shStrings) then
  44. WriteLn('Single-quoted string: PASS')
  45. else
  46. WriteLn('Single-quoted string: FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  47. // Test double-quoted string
  48. tokens := highlighter.Execute('"hello $USER"');
  49. if (Length(tokens) >= 1) and (tokens[0].Kind = shStrings) then
  50. WriteLn('Double-quoted string: PASS')
  51. else
  52. WriteLn('Double-quoted string: FAIL');
  53. // Test comment
  54. tokens := highlighter.Execute('# This is a comment');
  55. if (Length(tokens) = 1) and (tokens[0].Kind = shComment) then
  56. WriteLn('Comment: PASS')
  57. else
  58. WriteLn('Comment: FAIL');
  59. // Test variable
  60. tokens := highlighter.Execute('$USER');
  61. if (Length(tokens) = 1) and (tokens[0].Kind = shDefault) then
  62. WriteLn('Variable: PASS')
  63. else
  64. WriteLn('Variable: FAIL');
  65. // Test number
  66. tokens := highlighter.Execute('123');
  67. if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
  68. WriteLn('Number: PASS')
  69. else
  70. WriteLn('Number: FAIL');
  71. // Test operator
  72. tokens := highlighter.Execute('==');
  73. if (Length(tokens) = 1) and (tokens[0].Kind = shOperator) then
  74. WriteLn('Operator: PASS')
  75. else
  76. WriteLn('Operator: FAIL');
  77. // Test backquote command substitution
  78. tokens := highlighter.Execute('`date`');
  79. if (Length(tokens) = 1) and (tokens[0].Kind = shInterpolation) then
  80. WriteLn('Command substitution: PASS')
  81. else
  82. WriteLn('Command substitution: FAIL');
  83. finally
  84. highlighter.Free;
  85. end;
  86. WriteLn;
  87. end;
  88. procedure TestBashScript;
  89. var
  90. highlighter: TBashSyntaxHighlighter;
  91. tokens: TSyntaxTokenArray;
  92. i: Integer;
  93. script: string;
  94. begin
  95. WriteLn('Testing Complete Bash Script:');
  96. WriteLn('============================');
  97. script := '|';
  98. highlighter := TBashSyntaxHighlighter.Create;
  99. try
  100. tokens := highlighter.Execute(script);
  101. WriteLn('Script: ', script);
  102. WriteLn('Tokens (', Length(tokens), '):');
  103. for i := 0 to High(tokens) do begin
  104. WriteLn(' "', tokens[i].Text, '" - Kind: ', Ord(tokens[i].Kind),
  105. ' (', tokens[i].Kind, ') - Category: ', tokens[i].CategoriesAsString);
  106. end;
  107. finally
  108. highlighter.Free;
  109. end;
  110. WriteLn;
  111. end;
  112. procedure TestCategorySystem;
  113. var
  114. categories: TStringList;
  115. bashCategoryID: Integer;
  116. i: Integer;
  117. begin
  118. WriteLn('Testing Category System for Bash:');
  119. WriteLn('=================================');
  120. bashCategoryID := TSyntaxHighlighter.RegisterCategory('Bash');
  121. WriteLn('Bash category ID: ', bashCategoryID);
  122. categories := TStringList.Create;
  123. try
  124. TSyntaxHighlighter.GetRegisteredCategories(categories);
  125. WriteLn('All registered categories:');
  126. for i := 0 to categories.Count - 1 do
  127. WriteLn(' ', categories[i], ' = ', PtrInt(categories.Objects[i]));
  128. finally
  129. categories.Free;
  130. end;
  131. WriteLn;
  132. end;
  133. begin
  134. WriteLn('Bash Syntax Highlighter Test');
  135. WriteLn('=============================');
  136. WriteLn;
  137. TestCategorySystem;
  138. TestBashKeywords;
  139. TestBashTokenTypes;
  140. TestBashScript;
  141. WriteLn('Test completed. Press Enter to exit.');
  142. ReadLn;
  143. end.