Skip to content

Commit

Permalink
Simplify ApplyChangeToNativeSourceFileMutator
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfs committed Mar 1, 2021
1 parent c0b7d26 commit 3bdaef7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package org.gradle.profiler.mutations;

import com.google.common.collect.ImmutableSet;

import org.apache.commons.io.FilenameUtils;
import org.gradle.profiler.BuildContext;

import java.io.File;
import java.util.stream.Stream;
import java.util.Locale;

public class ApplyChangeToNativeSourceFileMutator extends AbstractFileChangeMutator {

// Both lists taken from https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html
private static final ImmutableSet<String> nativeSourcecodeFileEndings = ImmutableSet.of(
"c", "cc", "cp", "cxx", "cpp", "CPP", "c++", "C"
"c", "cc", "cp", "cxx", "cpp", "c++"
);

private static final ImmutableSet<String> nativeHeaderFileEndings = ImmutableSet.of(
"h", "hh", "H", "hp", "hxx", "hpp", "HPP", "h++", "tcc"
"h", "hh", "hp", "hxx", "hpp", "h++", "tcc"
);

public ApplyChangeToNativeSourceFileMutator(File file) {
super(file);
String fileExtension = FilenameUtils.getExtension(sourceFile.getName());
boolean isSupportedExtension = Stream.concat(nativeSourcecodeFileEndings.stream(), nativeHeaderFileEndings.stream())
.anyMatch(fileExtension::equals);
String fileExtension = getSourceFileExtension();
boolean isSupportedExtension = nativeSourcecodeFileEndings.contains(fileExtension) || nativeHeaderFileEndings.contains(fileExtension);

if (isSupportedExtension) {
return;
Expand All @@ -34,8 +32,7 @@ public ApplyChangeToNativeSourceFileMutator(File file) {
@Override
protected void applyChangeTo(BuildContext context, StringBuilder text) {
int insertPos;
String fileExtension = FilenameUtils.getExtension(sourceFile.getName());
if (nativeSourcecodeFileEndings.contains(fileExtension)) {
if (nativeSourcecodeFileEndings.contains(getSourceFileExtension())) {
insertPos = text.length();
applyChangeAt(context, text, insertPos);
} else {
Expand All @@ -47,6 +44,10 @@ protected void applyChangeTo(BuildContext context, StringBuilder text) {
}
}

private String getSourceFileExtension() {
return FilenameUtils.getExtension(sourceFile.getName()).toLowerCase(Locale.US);
}

protected String getFieldName(BuildContext context) {
return "_m" + context.getUniqueBuildId();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.gradle.profiler.mutations

import spock.lang.Unroll

@Unroll
class ApplyChangeToNativeSourceFileMutatorTest extends AbstractMutatorTest {

def "adds and replaces method to end of c source file"() {
def sourceFile = tmpDir.newFile("Thing.c")
def "adds and replaces method to end of #extension source file"() {
def sourceFile = tmpDir.newFile("Thing.${extension}")
sourceFile.text = " "
def mutator = new ApplyChangeToNativeSourceFileMutator(sourceFile)

Expand All @@ -12,18 +15,9 @@ class ApplyChangeToNativeSourceFileMutatorTest extends AbstractMutatorTest {

then:
sourceFile.text == " \nint _m_276d92f3_16ac_4064_9a18_5f1dfd67992f_testScenario_3c4925d7_MEASURE_7 () { }"
}

def "adds and replaces method to end of cpp source file"() {
def sourceFile = tmpDir.newFile("Thing.cpp")
sourceFile.text = " "
def mutator = new ApplyChangeToNativeSourceFileMutator(sourceFile)

when:
mutator.beforeBuild(buildContext)

then:
sourceFile.text == " \nint _m_276d92f3_16ac_4064_9a18_5f1dfd67992f_testScenario_3c4925d7_MEASURE_7 () { }"
where:
extension << ["c", "C", "cpp", "CPP", "c++", "cxx"]
}

def "adds and replaces method to end of h source file"() {
Expand All @@ -37,29 +31,23 @@ class ApplyChangeToNativeSourceFileMutatorTest extends AbstractMutatorTest {

then:
sourceFile.text == "#ifndef ABC\n\nint _m_276d92f3_16ac_4064_9a18_5f1dfd67992f_testScenario_3c4925d7_MEASURE_7();\n#endif"

where:
extension << ["h", "H", "hpp", "HPP", "hxx"]
}

def "uses same name for method in C, CPP and H files"() {
def sourceFileC = tmpDir.newFile("Thing.c")
def "uses same name for method in CPP and H files"() {
def sourceFileCPP = tmpDir.newFile("Thing.cpp")
def sourceFileH = tmpDir.newFile("Thing.h")
sourceFileC.text = " "
sourceFileCPP.text = " "
sourceFileH.text = "#ifndef ABC\n\n#endif"

def mutatorC = new ApplyChangeToNativeSourceFileMutator(sourceFileC)
def mutatorCPP = new ApplyChangeToNativeSourceFileMutator(sourceFileCPP)
def mutatorC = new ApplyChangeToNativeSourceFileMutator(sourceFileCPP)
def mutatorH = new ApplyChangeToNativeSourceFileMutator(sourceFileH)

when:
mutatorC.beforeBuild(buildContext)

then:
sourceFileC.text == " \nint _m_276d92f3_16ac_4064_9a18_5f1dfd67992f_testScenario_3c4925d7_MEASURE_7 () { }"

when:
mutatorCPP.beforeBuild(buildContext)

then:
sourceFileCPP.text == " \nint _m_276d92f3_16ac_4064_9a18_5f1dfd67992f_testScenario_3c4925d7_MEASURE_7 () { }"

Expand Down

0 comments on commit 3bdaef7

Please sign in to comment.