Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move sensors to own library #1099

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class BooleanEqualityComparisonCheckTest {
@Test
public void detected() throws UnsupportedEncodingException, IOException {
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/BooleanEqualityComparisonCheck.cc", ".");
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, new BooleanEqualityComparisonCheck());
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), new BooleanEqualityComparisonCheck());
checkMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(12).withMessage("Remove the unnecessary boolean comparison to simplify this expression.")
.next().atLine(13)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void test() throws UnsupportedEncodingException, IOException {
check.setMaximumClassComplexityThreshold(5);

CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/ClassComplexity.cc", ".");
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(9).withMessage("Class has a complexity of 10 which is greater than 5 authorized.")
.noMore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CollapsibleIfCandidateCheckTest {
public void detected() throws UnsupportedEncodingException, IOException {
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/CollapsibleIfCandidateCheck.cc", ".");

SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, new CollapsibleIfCandidateCheck());
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), new CollapsibleIfCandidateCheck());
checkMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(16).withMessage("Merge this if statement with the enclosing one.")
.next().atLine(49)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void test() throws UnsupportedEncodingException, IOException {

CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/commentRegularExpression.cc", ".");

SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(3).withMessage("Avoid TODO")
.next().atLine(5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CommentedCodeCheckTest {
@Test
public void test() throws UnsupportedEncodingException, IOException {
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/commentedCode.cc", ".");
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, new CommentedCodeCheck());
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), new CommentedCodeCheck());

checkMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(10).withMessage("Remove this commented out code.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import org.mockito.Mockito;
import static org.mockito.Mockito.when;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.cxx.CxxLanguage;

/**
*
Expand Down Expand Up @@ -53,5 +56,16 @@ public static CxxFileTester CreateCxxFileTester(String fileName, String basePath

return tester;
}


public static CxxLanguage mockCxxLanguage() {
CxxLanguage language = Mockito.mock(CxxLanguage.class);
when(language.getKey()).thenReturn("c++");
when(language.getName()).thenReturn("c++");
when(language.getPropertiesKey()).thenReturn("cxx");
when(language.IsRecoveryEnabled()).thenReturn(true);
when(language.getFileSuffixes())
.thenReturn(new String [] { ".cpp", ".hpp", ".h", ".cxx", ".c", ".cc", ".hxx", ".hh" });

return language;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void check() throws UnsupportedEncodingException, IOException {
check.setMax(1);

CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/functions.cc", ".");
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);

CheckMessagesVerifier.verify(file.getCheckMessages())
.next().noMore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class FileEncodingCheckTest {
@Test
public void testAsciiFileAsciiEncoding() throws UnsupportedEncodingException, IOException {
Charset charset = Charset.forName("US-ASCII");
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
CxxConfiguration cxxConfig = new CxxConfiguration(charset, CxxFileTesterHelper.mockCxxLanguage());
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/TabCharacter.cc", ".", "US-ASCII");
SourceFile file = CxxAstScanner.scanSingleFileConfig(tester.cxxFile, cxxConfig, tester.sensorContext, check);
SourceFile file = CxxAstScanner.scanSingleFileConfig(CxxFileTesterHelper.mockCxxLanguage(), tester.cxxFile, cxxConfig, tester.sensorContext, check);

CheckMessagesVerifier.verify(file.getCheckMessages())
.noMore();
Expand All @@ -47,9 +47,9 @@ public void testAsciiFileAsciiEncoding() throws UnsupportedEncodingException, IO
@Test
public void testAsciiFileUtf8Encoding() throws UnsupportedEncodingException, IOException {
Charset charset = Charset.forName("UTF-8");
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
CxxConfiguration cxxConfig = new CxxConfiguration(charset, CxxFileTesterHelper.mockCxxLanguage());
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/TabCharacter.cc", ".", "UTF-8");
SourceFile file = CxxAstScanner.scanSingleFileConfig(tester.cxxFile, cxxConfig, tester.sensorContext, check);
SourceFile file = CxxAstScanner.scanSingleFileConfig(CxxFileTesterHelper.mockCxxLanguage(), tester.cxxFile, cxxConfig, tester.sensorContext, check);

CheckMessagesVerifier.verify(file.getCheckMessages())
.noMore();
Expand All @@ -58,19 +58,19 @@ public void testAsciiFileUtf8Encoding() throws UnsupportedEncodingException, IOE
@Test
public void testUnicodeFileUtf16Encoding() throws UnsupportedEncodingException, IOException {
Charset charset = Charset.forName("UTF-16");
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
CxxConfiguration cxxConfig = new CxxConfiguration(charset, CxxFileTesterHelper.mockCxxLanguage());
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/Unicode.cc", ".", "UTF-16");
SourceFile file = CxxAstScanner.scanSingleFileConfig(tester.cxxFile, cxxConfig, tester.sensorContext, check);
SourceFile file = CxxAstScanner.scanSingleFileConfig(CxxFileTesterHelper.mockCxxLanguage(), tester.cxxFile, cxxConfig, tester.sensorContext, check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.noMore();
}

@Test
public void testUnicodeFileAsciiEncoding() throws IOException {
Charset charset = Charset.forName("US-ASCII");
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
CxxConfiguration cxxConfig = new CxxConfiguration(charset, CxxFileTesterHelper.mockCxxLanguage());
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/Unicode.cc", ".", "US-ASCII");
SourceFile file = CxxAstScanner.scanSingleFileConfig(tester.cxxFile, cxxConfig, tester.sensorContext, check);
SourceFile file = CxxAstScanner.scanSingleFileConfig(CxxFileTesterHelper.mockCxxLanguage(), tester.cxxFile, cxxConfig, tester.sensorContext, check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.next().withMessage("Not all characters of the file can be encoded with the predefined charset " + charset.name() + ".")
.noMore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,64 +40,64 @@ public void test() throws UnsupportedEncodingException, IOException {
check.headerFormat = "// copyright 2005";

CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/FileHeaderCheck/Class1.cc", ".");
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);

CheckMessagesVerifier.verify(file.getCheckMessages())
.noMore();

check = new FileHeaderCheck();
check.headerFormat = "// copyright 20\\d\\d";

file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(null);

check = new FileHeaderCheck();
check.headerFormat = "// copyright 2005";

tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/FileHeaderCheck/Class2.cc", ".");
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(null).withMessage("Add or update the header of this file.");

check = new FileHeaderCheck();
check.headerFormat = "// copyright 2012";
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.noMore();

check = new FileHeaderCheck();
check.headerFormat = "// copyright 2012\n// foo";

file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.noMore();

check = new FileHeaderCheck();
check.headerFormat = "// copyright 2012\r\n// foo";

file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.noMore();

check = new FileHeaderCheck();
check.headerFormat = "// copyright 2012\r// foo";

file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.noMore();

check = new FileHeaderCheck();
check.headerFormat = "// copyright 2012\r\r// foo";

file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(null);

check = new FileHeaderCheck();
check.headerFormat = "// copyright 2012\n// foo\n\n\n\n\n\n\n\n\n\ngfoo";

file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(null);

Expand All @@ -106,7 +106,7 @@ public void test() throws UnsupportedEncodingException, IOException {


tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/FileHeaderCheck/Class3.cc", ".");
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.noMore();
}
Expand All @@ -118,50 +118,50 @@ public void regex() throws UnsupportedEncodingException, IOException {
check.isRegularExpression = true;

CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/FileHeaderCheck/Regex1.cc", ".");
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages()).next().atLine(null).withMessage("Add or update the header of this file.");
// Check that the regular expression is compiled once
check = new FileHeaderCheck();

file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages()).next().atLine(null).withMessage("Add or update the header of this file.");

check = new FileHeaderCheck();
check.headerFormat = "// copyright \\d{4}\\n// mycompany";
check.isRegularExpression = true;

tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/FileHeaderCheck/Regex2.cc", ".");
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages()).next().atLine(null).withMessage("Add or update the header of this file.");

check = new FileHeaderCheck();
check.headerFormat = "// copyright \\d{4}\\r?\\n// mycompany";
check.isRegularExpression = true;

tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/FileHeaderCheck/Regex3.cc", ".");
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages()).noMore();

check = new FileHeaderCheck();
check.headerFormat = "// copyright \\d{4}\\n// mycompany";
check.isRegularExpression = true;

tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/FileHeaderCheck/Regex4.cc", ".");
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages()).next().atLine(null).withMessage("Add or update the header of this file.");

check = new FileHeaderCheck();
check.headerFormat = "^(?=.*?\\bCopyright\\b)(?=.*?\\bVendor\\b)(?=.*?\\d{4}(-\\d{4})?).*$";
check.isRegularExpression = true;

tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/FileHeaderCheck/Regex5.cc", ".");
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages()).noMore();

check = new FileHeaderCheck();

tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/FileHeaderCheck/Regex6.cc", ".");
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, CxxFileTesterHelper.mockCxxLanguage(), check);
CheckMessagesVerifier.verify(file.getCheckMessages()).next().atLine(null).withMessage("Add or update the header of this file.");
}

Expand Down
Loading