|
| 1 | +/* |
| 2 | + * Sonar Delphi Plugin |
| 3 | + * Copyright (C) 2024 Integrated Application Development |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 3 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 |
| 18 | + */ |
| 19 | +package au.com.integradev.delphi.checks; |
| 20 | + |
| 21 | +import au.com.integradev.delphi.builders.DelphiTestUnitBuilder; |
| 22 | +import au.com.integradev.delphi.checks.verifier.CheckVerifier; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +class MissingRaiseCheckTest { |
| 26 | + private DelphiTestUnitBuilder sysUtils() { |
| 27 | + return new DelphiTestUnitBuilder() |
| 28 | + .unitName("System.SysUtils") |
| 29 | + .appendDecl("type") |
| 30 | + .appendDecl(" Exception = class(TObject)") |
| 31 | + .appendDecl(" public") |
| 32 | + .appendDecl(" constructor Create(Text: string);") |
| 33 | + .appendDecl(" end;"); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void testDiscardedBaseExceptionShouldAddIssue() { |
| 38 | + CheckVerifier.newVerifier() |
| 39 | + .withCheck(new MissingRaiseCheck()) |
| 40 | + .withStandardLibraryUnit(sysUtils()) |
| 41 | + .onFile( |
| 42 | + new DelphiTestUnitBuilder() |
| 43 | + .appendImpl("uses System.SysUtils;") |
| 44 | + .appendImpl("initialization") |
| 45 | + .appendImpl(" Exception.Create('Error!'); // Noncompliant")) |
| 46 | + .verifyIssues(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testDiscardedDescendantExceptionShouldAddIssue() { |
| 51 | + CheckVerifier.newVerifier() |
| 52 | + .withCheck(new MissingRaiseCheck()) |
| 53 | + .withStandardLibraryUnit(sysUtils()) |
| 54 | + .onFile( |
| 55 | + new DelphiTestUnitBuilder() |
| 56 | + .appendDecl("uses System.SysUtils;") |
| 57 | + .appendDecl("type") |
| 58 | + .appendDecl(" ECalculatorError = class(Exception)") |
| 59 | + .appendDecl(" public") |
| 60 | + .appendDecl(" constructor Create(A: Integer; B: Integer);") |
| 61 | + .appendDecl(" end;") |
| 62 | + .appendImpl("initialization") |
| 63 | + .appendImpl(" ECalculatorError.Create(1, 2); // Noncompliant")) |
| 64 | + .verifyIssues(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testDiscardedNonExceptionShouldNotAddIssue() { |
| 69 | + CheckVerifier.newVerifier() |
| 70 | + .withCheck(new MissingRaiseCheck()) |
| 71 | + .withStandardLibraryUnit(sysUtils()) |
| 72 | + .onFile( |
| 73 | + new DelphiTestUnitBuilder() |
| 74 | + .appendDecl("uses System.SysUtils;") |
| 75 | + .appendDecl("type") |
| 76 | + .appendDecl(" EConfusinglyNamedNormalObject = class(TObject)") |
| 77 | + .appendDecl(" public") |
| 78 | + .appendDecl(" constructor Create(A: Integer; B: Integer);") |
| 79 | + .appendDecl(" end;") |
| 80 | + .appendImpl("initialization") |
| 81 | + .appendImpl(" EConfusinglyNamedNormalObject.Create(1, 2);")) |
| 82 | + .verifyNoIssues(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testRaisedBaseExceptionShouldNotAddIssue() { |
| 87 | + CheckVerifier.newVerifier() |
| 88 | + .withCheck(new MissingRaiseCheck()) |
| 89 | + .withStandardLibraryUnit(sysUtils()) |
| 90 | + .onFile( |
| 91 | + new DelphiTestUnitBuilder() |
| 92 | + .appendImpl("uses System.SysUtils;") |
| 93 | + .appendImpl("initialization") |
| 94 | + .appendImpl(" raise Exception.Create('Error!');")) |
| 95 | + .verifyNoIssues(); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testDiscardedInvocationShouldNotAddIssue() { |
| 100 | + CheckVerifier.newVerifier() |
| 101 | + .withCheck(new MissingRaiseCheck()) |
| 102 | + .withStandardLibraryUnit(sysUtils()) |
| 103 | + .onFile( |
| 104 | + new DelphiTestUnitBuilder() |
| 105 | + .appendImpl("uses System.SysUtils;") |
| 106 | + .appendImpl("function Foo: Exception;") |
| 107 | + .appendImpl("begin") |
| 108 | + .appendImpl("end;") |
| 109 | + .appendImpl("initialization") |
| 110 | + .appendImpl(" Foo;")) |
| 111 | + .verifyNoIssues(); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void testDiscardedNonConstructorInvocationShouldNotAddIssue() { |
| 116 | + CheckVerifier.newVerifier() |
| 117 | + .withCheck(new MissingRaiseCheck()) |
| 118 | + .withStandardLibraryUnit(sysUtils()) |
| 119 | + .onFile( |
| 120 | + new DelphiTestUnitBuilder() |
| 121 | + .appendDecl("uses System.SysUtils;") |
| 122 | + .appendDecl("type") |
| 123 | + .appendDecl(" ECalculatorError = class(Exception)") |
| 124 | + .appendDecl(" public") |
| 125 | + .appendDecl(" class function Add(A: Integer; B: Integer);") |
| 126 | + .appendDecl(" end;") |
| 127 | + .appendImpl("initialization") |
| 128 | + .appendImpl(" ECalculatorError.Add(1, 2);")) |
| 129 | + .verifyNoIssues(); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testRaisedDescendantExceptionShouldNotAddIssue() { |
| 134 | + CheckVerifier.newVerifier() |
| 135 | + .withCheck(new MissingRaiseCheck()) |
| 136 | + .withStandardLibraryUnit(sysUtils()) |
| 137 | + .onFile( |
| 138 | + new DelphiTestUnitBuilder() |
| 139 | + .appendDecl("uses System.SysUtils;") |
| 140 | + .appendDecl("type") |
| 141 | + .appendDecl(" ECalculatorError = class(Exception)") |
| 142 | + .appendDecl(" public") |
| 143 | + .appendDecl(" constructor Create(A: Integer; B: Integer);") |
| 144 | + .appendDecl(" end;") |
| 145 | + .appendImpl("initialization") |
| 146 | + .appendImpl(" raise ECalculatorError.Create(1, 2);")) |
| 147 | + .verifyNoIssues(); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void testAssignedBaseExceptionShouldNotAddIssue() { |
| 152 | + CheckVerifier.newVerifier() |
| 153 | + .withCheck(new MissingRaiseCheck()) |
| 154 | + .withStandardLibraryUnit(sysUtils()) |
| 155 | + .onFile( |
| 156 | + new DelphiTestUnitBuilder() |
| 157 | + .appendImpl("uses System.SysUtils;") |
| 158 | + .appendImpl("var MyError: ECalculatorError;") |
| 159 | + .appendImpl("initialization") |
| 160 | + .appendImpl(" MyError := Exception.Create('Error!');")) |
| 161 | + .verifyNoIssues(); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void testAssignedDescendantExceptionShouldNotAddIssue() { |
| 166 | + CheckVerifier.newVerifier() |
| 167 | + .withCheck(new MissingRaiseCheck()) |
| 168 | + .withStandardLibraryUnit(sysUtils()) |
| 169 | + .onFile( |
| 170 | + new DelphiTestUnitBuilder() |
| 171 | + .appendDecl("uses System.SysUtils;") |
| 172 | + .appendDecl("type") |
| 173 | + .appendDecl(" ECalculatorError = class(Exception)") |
| 174 | + .appendDecl(" public") |
| 175 | + .appendDecl(" constructor Create(A: Integer; B: Integer);") |
| 176 | + .appendDecl(" end;") |
| 177 | + .appendImpl("var MyError: ECalculatorError;") |
| 178 | + .appendImpl("initialization") |
| 179 | + .appendImpl(" MyError := ECalculatorError.Create(1, 2);")) |
| 180 | + .verifyNoIssues(); |
| 181 | + } |
| 182 | +} |
0 commit comments