Debian Codegraph @

tests/code/testcases/castletestutils.pas

raw
1 {
2 Copyright 2023-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Useful utilities for tests. }
17 unit CastleTestUtils;
18
19 interface
20
21 type
22 TSavedLocale = record
23 OldDecimalSeparator: Char;
24 end;
25
26 { Change global settings to pretend current locale has
27 DecimalSeparator = ','.
28
29 This happens on some systems and locales, e.g. Polish Windows or German Windows.
30 It causes bugs, as on most locales and systems DecimalSeparator = '.',
31 which is also what most code expects because it is what most file formats assume.
32 See https://castle-engine.io/coding_conventions#float_string_dot
33
34 This routine allows to test this situation, on any system/locale. }
35 function FakeLocaleDecimalSeparatorComma: TSavedLocale;
36
37 { Restore global settings changed by FakeLocaleDecimalSeparatorComma. }
38 procedure RestoreLocaleDecimalSeparatorComma(const Saved: TSavedLocale);
39
40 { Does given String match a regular expression.
41
42 When compiled with FPC, this uses FPC RegExpr unit with TRegExpr class.
43 When compiled with Delphi, this uses Delphi RegularExpressions unit with TRegEx record.
44
45 @bold(The FPC and Delphi implementations are not guaranteed to be perfectly compatible.)
46 The FPC implementation has also some important fixes between FPC 3.2.0 and 3.2.2
47 (see our auto-tests in TTestCastleStringUtils.TestRegexpMatches).
48 Using this routine is only safe for the subset of regular expressions that are compatible
49 between FPC and Delphi implementations.
50
51 Simple things, like +, *, ranges like [0-9] and [\d] are compatible,
52 so in many practical cases this is acceptable.
53 But in general, if your application needs to support both FPC and Delphi,
54 be sure to double-test that the regular expressions you use
55 are interpreted the same by both FPC and Delphi.
56
57 It is possible we will use some consistent regular expression library in the future
58 (e.g. FPC RegExpr should be compatible with Delphi too) to avoid this issue.
59 Fow now, regexps are not very important for CGE code (or typical games using CGE),
60 so this isn't a critical issue. }
61 function StringMatchesRegexp(const S, RegexpPattern: String): Boolean;
62
63 implementation
64
65 uses SysUtils, {$ifdef FPC} Regexpr {$else} RegularExpressions, Character {$endif};
66
67 function FakeLocaleDecimalSeparatorComma: TSavedLocale;
68 begin
69 Result.OldDecimalSeparator :=
70 {$ifdef FPC}DefaultFormatSettings{$else}FormatSettings{$endif}.DecimalSeparator;
71 {$ifdef FPC}DefaultFormatSettings{$else}FormatSettings{$endif}.DecimalSeparator := ',';
72 end;
73
74 procedure RestoreLocaleDecimalSeparatorComma(const Saved: TSavedLocale);
75 begin
76 {$ifdef FPC}DefaultFormatSettings{$else}FormatSettings{$endif}.DecimalSeparator :=
77 Saved.OldDecimalSeparator;
78 end;
79
80 function StringMatchesRegexp(const S, RegexpPattern: String): Boolean;
81 {$ifdef FPC}
82 (*
83 var
84 R: TRegExpr;
85 begin
86 R := TRegExpr.Create;
87 try
88 R.Expression := RegexpPattern;
89 Result := R.Exec(S);
90 finally FreeAndNil(R) end;
91 *)
92 // Simpler:
93 begin
94 Result := ExecRegExpr(RegexpPattern, S);
95 {$else}
96 begin
97 Result := TRegEx.IsMatch(S, RegexpPattern);
98 {$endif}
99 end;
100
101 end.