1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
{
Test with:
./runtests --format=plain --suite=TTestCodetoolsH2Pas
./runtests --format=plain --suite=TestCTH2PMergeHeaderFiles
./runtests --format=plain --suite=TestCTH2PReplaceMacros
./runtests --format=plain --suite=TestCTH2PConvertSimpleVars
./runtests --format=plain --suite=TestCTH2PConvertEnumsTypes
./runtests --format=plain --suite=TestCTH2PConvertConst
./runtests --format=plain --suite=TestCTH2PConvertSimpleTypedefs
./runtests --format=plain --suite=TestCTH2PConvertSimpleStructs
}
unit TestCTH2Pas;
{$mode objfpc}{$H+}
{$DEFINE VerboseTestCTH2Pas}
interface
uses
Classes, SysUtils, fpcunit, testregistry, FileProcs,
CodeToolManager, BasicCodeTools, CodeCache, CCodeParserTool, H2PasTool;
type
{ TTestCodetoolsH2Pas }
TTestCodetoolsH2Pas = class(TTestCase)
protected
procedure Test(Title, CHeaderSrc, ExpectedPasSrc: string);
procedure TestIntf(Title, CHeaderSrc, ExpectedPasSrc: string);
published
procedure TestCTH2PMergeHeaderFiles;
procedure TestCTH2PReplaceMacros;
procedure TestCTH2PConvertSimpleVars;
procedure TestCTH2PConvertEnumsTypes;
procedure TestCTH2PConvertConst;
procedure TestCTH2PConvertSimpleTypedefs;
procedure TestCTH2PConvertSimpleStructs;
end;
implementation
{ TTestCodetoolsH2Pas }
procedure TTestCodetoolsH2Pas.Test(Title, CHeaderSrc, ExpectedPasSrc: string);
var
Tool: TH2PasTool;
Header1: TCodeBuffer;
PasCode: TCodeBuffer;
begin
Tool:=TH2PasTool.Create;
Header1:=nil;
PasCode:=nil;
try
Header1:=CodeToolBoss.CreateFile('header1.h');
PasCode:=CodeToolBoss.CreateFile('header1.pas');
Header1.Source:=CHeaderSrc;
Tool.Convert(Header1,PasCode);
if CompareTextIgnoringSpace(ExpectedPasSrc,PasCode.Source,true)<>0 then begin
// failed
debugln(['TTestCodetoolsH2Pas.Test C Source="',CHeaderSrc,'"']);
debugln(['TTestCodetoolsH2Pas.Test Expected pas="',ExpectedPasSrc,'"']);
debugln(['TTestCodetoolsH2Pas.Test Found pas="',PasCode.Source,'"']);
Tool.WriteH2PNodeReport;
Tool.WriteH2PDirectivesNodeReport;
AssertEquals(Title,ExpectedPasSrc,PasCode.Source);
end else begin
//debugln(['TTestCodetoolsH2Pas.Test Found pas="',PasCode.Source,'"']);
AssertEquals(Title,true,true);
end;
finally
if Header1<>nil then Header1.IsDeleted:=true;
if PasCode<>nil then PasCode.IsDeleted:=true;
Tool.Free;
end;
end;
procedure TTestCodetoolsH2Pas.TestIntf(Title, CHeaderSrc, ExpectedPasSrc: string
);
var
UsesCTypes: String;
EmpytImplementation: String;
begin
UsesCTypes:='uses ctypes;'+LineEnding;
EmpytImplementation:=LineEnding+'implementation'+LineEnding+'end.';
Test(Title,CHeaderSrc,UsesCTypes+ExpectedPasSrc+EmpytImplementation);
end;
procedure TTestCodetoolsH2Pas.TestCTH2PMergeHeaderFiles;
var
Header1, Header2: TCodeBuffer;
Merger: TCHeaderFileMerger;
Filenames: TStringList;
Src: String;
Header1Start: LongInt;
Header2Start: LongInt;
Header1End: LongInt;
begin
Header1:=CodeToolBoss.CreateFile('header1.h');
Header2:=CodeToolBoss.CreateFile('header2.h');
Header1.Source:='// header1.h'+LineEnding
+'#include header2.h'+LineEnding
+'// end of header1.h';
Header2.Source:='// header2.h';
Merger:=TCHeaderFileMerger.Create;
Filenames:=TStringList.Create;
try
Filenames.Add('header1.h');
Filenames.Add('header2.h');
Merger.Merge(Filenames,CodeToolBoss.SourceCache,[]);
Src:=Merger.CombinedSource.Source;
Header1Start:=System.Pos('// header1.h',Src);
Header2Start:=System.Pos('// header2.h',Src);
Header1End:=System.Pos('// end of header1.h',Src);
//debugln(['TTestCodetoolsH2Pas.TestCTH2PMergeHeaderFiles ',Src]);
AssertEquals('start comment header1.h',1,Header1Start);
AssertEquals('start comment header1.h in front of header2.h',true,Header1Start<Header2Start);
AssertEquals('start comment header2.h in front of end of header1.h',true,Header2Start<Header1End);
finally
Merger.Free;
Filenames.Free;
end;
end;
procedure TTestCodetoolsH2Pas.TestCTH2PReplaceMacros;
var
Header1: TCodeBuffer;
Buffers: TFPList;
Merger: TCHeaderFileMerger;
procedure Check(Msg, Src, ExpectedSrc: string);
var
NewSrc: String;
begin
Header1.Source:=Src;
Merger.Merge(Buffers,[]);
NewSrc:=Trim(Merger.CombinedSource.Source);
AssertEquals(Msg,dbgstr(ExpectedSrc),dbgstr(NewSrc));
end;
begin
Header1:=CodeToolBoss.CreateFile('header1.h');
Merger:=TCHeaderFileMerger.Create;
Buffers:=TFPList.Create;
try
Buffers.Add(Header1);
// undefine
Merger.Macros['remove1']:='';
Check('undefine remove1','remove1','');
// define
Merger.Macros['macro1']:='newvalue';
Check('define macro1','macro1','newvalue');
// define macro function
Merger.Macros['macrofunc1()']:='newvalue';
Check('define macrofunc1','macrofunc1(param)','newvalue');
// do not replace #define
Merger.Macros['macro1']:='newvalue';
Check('do not replace macros in #define','#define macro1','#define macro1');
Check('do not replace macros in #undef','#undef macro1','#undef macro1');
Check('do not replace macros in #ifdef','#ifdef macro1','#ifdef macro1');
Check('do not replace macros in #ifndef','#ifndef macro1','#ifndef macro1');
finally
Buffers.Free;
Merger.Free;
end;
end;
procedure TTestCodetoolsH2Pas.TestCTH2PConvertSimpleVars;
begin
TestIntf('convert int i;', 'int i;', 'var i: cint; cvar; external;');
TestIntf('convert #define c 1', '#define c 1', 'const c=1;');
TestIntf('convert int y = 7;', 'int y = 7;', 'var y:cint;cvar;external;');
TestIntf('convert short signed int ssi_octal = 0123;',
'short signed int ssi_octal = 0123;',
'var ssi_octal:csshort;cvar;external;');
TestIntf('convert unsigned short unsigned_short;',
'unsigned short unsigned_short;',
'var unsigned_short:cushort;cvar;external;');
TestIntf('convert unsigned long long unsigned_long_long;',
'unsigned long long unsigned_long_long;',
'var unsigned_long_long:culonglong;cvar;external;');
end;
procedure TTestCodetoolsH2Pas.TestCTH2PConvertEnumsTypes;
begin
TestIntf('convert anonymous enum{ENUM1};',
'enum{ENUM1};',
'type enumENUM1 = (ENUM1);');
TestIntf('convert one named enum color{red};',
'enum color{red};',
'type color = (red);');
TestIntf('convert named enum with id: color{red=1};',
'enum color{red=1};',
'type color = (red=1);');
TestIntf('convert multi enums: color{red,green,blue};',
'enum color{red,green,blue};',
'type color = (red,green,blue);');
end;
procedure TTestCodetoolsH2Pas.TestCTH2PConvertConst;
begin
// const char a; // A constant character
TestIntf('convert const char a;',
'const char a;',
'var a: cchar;cvar;external;');
//char const b; // A constant character (the same)
TestIntf('convert char const b;',
'char const b;',
'var b: cchar;cvar;external;');
//char *const c; // A constant pointer to a character
TestIntf('convert char *const c;',
'char *const c;',
'var c: pcchar;cvar;external;');
//const char *const d; // A constant pointer to a constant character
TestIntf('convert char *const d;',
'char *const d;',
'var d: pcchar;cvar;external;');
//const char *e; // A pointer to a constant character. The pointer may be modified.
TestIntf('convert const char *e;',
'const char *e;',
'var e: pcchar;cvar;external;');
end;
procedure TTestCodetoolsH2Pas.TestCTH2PConvertSimpleTypedefs;
begin
TestIntf('convert typedef unsigned short sa_family_t;',
'typedef unsigned short sa_family_t;',
'type sa_family_t = cushort;');
end;
procedure TTestCodetoolsH2Pas.TestCTH2PConvertSimpleStructs;
begin
(*TestIntf('convert struct SwsContext;',
'struct SwsContext;',
'type SwsContext = record end;');
TestIntf('convert struct hidp_connlist_req {uint32_t cnum;};',
'struct hidp_connlist_req {'+LineEnding
+' uint32_t cnum;'+LineEnding
+'};',
'type hidp_connlist_req = record cnum:cuint32; end;');*)
// nested struct
TestIntf('convert struct hidp_connlist_req {uint32_t cnum;struct hidp_conninfo ci;};',
'struct hidp_connlist_req {'+LineEnding
+' uint32_t cnum;'+LineEnding
+' struct hidp_conninfo ci;'+LineEnding
+'};',
'type'+LineEnding
+' hidp_connlist_req = record'+LineEnding
+' cnum:cuint32;'+LineEnding
+' ci:hidp_conninfo;'+LineEnding
+' end;'+LineEnding
+' hidp_conninfo = record'+LineEnding
+' end;');
// nested pointer of struct
(*TestIntf('convert struct hidp_connlist_req {uint32_t cnum;struct hidp_conninfo *ci;};',
'struct hidp_connlist_req {'+LineEnding
+' uint32_t cnum;'+LineEnding
+' struct hidp_conninfo *ci;'+LineEnding
+'};',
'type'+LineEnding
+' hidp_connlist_req = record'+LineEnding
+' cnum:cuint32;'+LineEnding
+' ci:Phidp_conninfo;'+LineEnding
+' end;'+LineEnding
+' hidp_conninfo = record'+LineEnding
+' end;'); *)
end;
initialization
RegisterTest(TTestCodetoolsH2Pas);
end.
|