Skip to content

Commit

Permalink
feat(ec547): add Tests for DisabledDarkModeCheck Rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Guittonneau committed Oct 2, 2024
1 parent 08c5a71 commit 9445865
Show file tree
Hide file tree
Showing 6 changed files with 1,361 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pbxproj-lang/src/test/java/io/ecocode/ios/pbxproj/TestHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications
* Copyright © 2023 green-code-initiative (https://www.ecocode.io/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.ecocode.ios.pbxproj;

import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorContextTester;

import java.io.File;
import java.nio.file.Paths;

public class TestHelper {

private static final String TEST_ROOT = "src/test/resources";
private static final int LINE_COUNT = 1000;

public static SensorContextTester testFile(String relativePath) {
SensorContextTester context = SensorContextTester.create(new File(TEST_ROOT));
DefaultInputFile testFile = new TestInputFileBuilder("", relativePath)
.setType(InputFile.Type.MAIN)
.setLines(LINE_COUNT)
.setOriginalLineEndOffsets(new int[LINE_COUNT])
.setOriginalLineStartOffsets(new int[LINE_COUNT])
.setModuleBaseDir(Paths.get(TEST_ROOT))
.setLanguage(PbxprojLanguage.KEY).build();
context.fileSystem().add(testFile);

return context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications
* Copyright © 2023 green-code-initiative (https://www.ecocode.io/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.ecocode.ios.pbxproj.checks;

import io.ecocode.ios.antlr.ParseTreeAnalyzer;
import io.ecocode.ios.checks.DefaultRuleLoader;
import io.ecocode.ios.checks.RuleLoader;
import io.ecocode.ios.pbxproj.EcoCodePbxprojVisitor;
import io.ecocode.ios.pbxproj.PbxprojLanguage;
import io.ecocode.ios.pbxproj.PbxprojRuleCheck;
import io.ecocode.ios.pbxproj.antlr.PbxprojAntlrContext;
import io.ecocode.ios.pbxproj.TestHelper;

import org.reflections.Reflections;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.internal.SensorContextTester;

public class CheckTestHelper {


public static SensorContextTester analyzeTestFile(String relativePath) {
SensorContextTester context = TestHelper.testFile(relativePath);

final PbxprojAntlrContext antlrContext = new PbxprojAntlrContext();
RuleLoader ruleLoader = new DefaultRuleLoader(PbxprojRuleCheck.class, new Reflections("io.ecocode.ios.pbxproj.checks"));
new ParseTreeAnalyzer(PbxprojLanguage.KEY, InputFile.Type.MAIN, antlrContext, context)
.analyze(new EcoCodePbxprojVisitor(ruleLoader));

return context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications
* Copyright © 2023 green-code-initiative (https://www.ecocode.io/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package io.ecocode.ios.pbxproj.checks.sobriety;

import org.junit.Test;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.Issue;
import static org.junit.Assert.assertEquals;

import java.util.List;

import io.ecocode.ios.pbxproj.checks.CheckTestHelper;

public class DisabledDarkModeCheckTest {
private static final String TEST_CASE_PBXPROJ_UIUSERINTERFACESTYLE_LIGHT = "checks/sobriety/DisabledDarkModeCheck_light.pbxproj";
private static final String TEST_CASE_PBXPROJ_UIUSERINTERFACESTYLE_AUTOMATIC = "checks/sobriety/DisabledDarkModeCheckliant_automatic.pbxproj";
private static final String TEST_CASE_PBXPROJ_UIUSERINTERFACESTYLE_NO_KEY = "checks/sobriety/DisabledDarkModeCheckliant_no_key.pbxproj";

@Test
public void disabledDarkModeCheckTest_forcedLightMode_shouldTrigger(){
SensorContextTester context = CheckTestHelper.analyzeTestFile(TEST_CASE_PBXPROJ_UIUSERINTERFACESTYLE_LIGHT);
List<Issue> issues = context.allIssues().stream().toList();
assertEquals(2, issues.size());

Issue firstIssue = issues.get(0);
assertEquals("EC547", firstIssue.ruleKey().rule());
assertEquals("ecoCode-pbxproj", firstIssue.ruleKey().repository());
assertEquals(334, firstIssue.primaryLocation().textRange().start().line());

Issue secondIssue = issues.get(1);
assertEquals("EC547", secondIssue.ruleKey().rule());
assertEquals("ecoCode-pbxproj", secondIssue.ruleKey().repository());
assertEquals(364, secondIssue.primaryLocation().textRange().start().line());
}

@Test
public void disabledDarkModeCheckTest_automaticMode_shouldNotTrigger(){
SensorContextTester context = CheckTestHelper.analyzeTestFile(TEST_CASE_PBXPROJ_UIUSERINTERFACESTYLE_AUTOMATIC);
List<Issue> issues = context.allIssues().stream().toList();
assertEquals(0, issues.size());
}

@Test
public void disabledDarkModeCheckTest_noKey_shouldNotTrigger(){
SensorContextTester context = CheckTestHelper.analyzeTestFile(TEST_CASE_PBXPROJ_UIUSERINTERFACESTYLE_NO_KEY);
List<Issue> issues = context.allIssues().stream().toList();
assertEquals(0, issues.size());
}
}
Loading

0 comments on commit 9445865

Please sign in to comment.