123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- program test_bash;
- {$mode objfpc}{$H+}
- uses
- SysUtils, Classes, syntax.highlighter, syntax.bash;
- procedure TestBashKeywords;
- const
- BashKeywords: array[0..19] of string = (
- 'if', 'then', 'else', 'fi', 'case', 'esac', 'for', 'do', 'done', 'while',
- 'function', 'return', 'break', 'continue', 'declare', 'local', 'export', 'set', 'test', 'eval'
- );
- var
- highlighter: TBashSyntaxHighlighter;
- tokens: TSyntaxTokenArray;
- i: Integer;
- begin
- WriteLn('Testing Bash Keywords:');
- WriteLn('=====================');
- highlighter := TBashSyntaxHighlighter.Create;
- try
- for i := 0 to High(BashKeywords) do begin
- tokens := highlighter.Execute(BashKeywords[i]);
- if (Length(tokens) = 1) and (tokens[0].Kind = shKeyword) then
- WriteLn(BashKeywords[i] + ': PASS - recognized as keyword')
- else
- WriteLn(BashKeywords[i] + ': FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
- end;
- finally
- highlighter.Free;
- end;
- WriteLn;
- end;
- procedure TestBashTokenTypes;
- var
- highlighter: TBashSyntaxHighlighter;
- tokens: TSyntaxTokenArray;
- begin
- WriteLn('Testing Bash Token Types:');
- WriteLn('========================');
- highlighter := TBashSyntaxHighlighter.Create;
- try
- // Test single-quoted string
- tokens := highlighter.Execute('''hello world''');
- if (Length(tokens) = 1) and (tokens[0].Kind = shStrings) then
- WriteLn('Single-quoted string: PASS')
- else
- WriteLn('Single-quoted string: FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
- // Test double-quoted string
- tokens := highlighter.Execute('"hello $USER"');
- if (Length(tokens) >= 1) and (tokens[0].Kind = shStrings) then
- WriteLn('Double-quoted string: PASS')
- else
- WriteLn('Double-quoted string: FAIL');
- // Test comment
- tokens := highlighter.Execute('# This is a comment');
- if (Length(tokens) = 1) and (tokens[0].Kind = shComment) then
- WriteLn('Comment: PASS')
- else
- WriteLn('Comment: FAIL');
- // Test variable
- tokens := highlighter.Execute('$USER');
- if (Length(tokens) = 1) and (tokens[0].Kind = shDefault) then
- WriteLn('Variable: PASS')
- else
- WriteLn('Variable: FAIL');
- // Test number
- tokens := highlighter.Execute('123');
- if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
- WriteLn('Number: PASS')
- else
- WriteLn('Number: FAIL');
- // Test operator
- tokens := highlighter.Execute('==');
- if (Length(tokens) = 1) and (tokens[0].Kind = shOperator) then
- WriteLn('Operator: PASS')
- else
- WriteLn('Operator: FAIL');
- // Test backquote command substitution
- tokens := highlighter.Execute('`date`');
- if (Length(tokens) = 1) and (tokens[0].Kind = shInterpolation) then
- WriteLn('Command substitution: PASS')
- else
- WriteLn('Command substitution: FAIL');
- finally
- highlighter.Free;
- end;
- WriteLn;
- end;
- procedure TestBashScript;
- var
- highlighter: TBashSyntaxHighlighter;
- tokens: TSyntaxTokenArray;
- i: Integer;
- script: string;
- begin
- WriteLn('Testing Complete Bash Script:');
- WriteLn('============================');
- script := '|';
- highlighter := TBashSyntaxHighlighter.Create;
- try
- tokens := highlighter.Execute(script);
- WriteLn('Script: ', script);
- WriteLn('Tokens (', Length(tokens), '):');
- for i := 0 to High(tokens) do begin
- WriteLn(' "', tokens[i].Text, '" - Kind: ', Ord(tokens[i].Kind),
- ' (', tokens[i].Kind, ') - Category: ', tokens[i].CategoriesAsString);
- end;
- finally
- highlighter.Free;
- end;
- WriteLn;
- end;
- procedure TestCategorySystem;
- var
- categories: TStringList;
- bashCategoryID: Integer;
- i: Integer;
- begin
- WriteLn('Testing Category System for Bash:');
- WriteLn('=================================');
- bashCategoryID := TSyntaxHighlighter.RegisterCategory('Bash');
- WriteLn('Bash category ID: ', bashCategoryID);
- categories := TStringList.Create;
- try
- TSyntaxHighlighter.GetRegisteredCategories(categories);
- WriteLn('All registered categories:');
- for i := 0 to categories.Count - 1 do
- WriteLn(' ', categories[i], ' = ', PtrInt(categories.Objects[i]));
- finally
- categories.Free;
- end;
- WriteLn;
- end;
- begin
- WriteLn('Bash Syntax Highlighter Test');
- WriteLn('=============================');
- WriteLn;
- TestCategorySystem;
- TestBashKeywords;
- TestBashTokenTypes;
- TestBashScript;
- WriteLn('Test completed. Press Enter to exit.');
- ReadLn;
- end.
|