]> source.dussan.org Git - sonarqube.git/commitdiff
Add more tests
authorDavid Gageot <david@gageot.net>
Fri, 5 Oct 2012 11:56:08 +0000 (13:56 +0200)
committerDavid Gageot <david@gageot.net>
Fri, 5 Oct 2012 11:56:25 +0000 (13:56 +0200)
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CoverageDecoratorTest.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ItCoverageDecoratorTest.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/OverallCoverageDecoratorTest.java

index 0a9a0a9ddb4171128ec347ec269b5a4fa29405dd..16efef23eb36dc60441323496c88f26396a1e3be 100644 (file)
@@ -105,6 +105,19 @@ public class CoverageDecoratorTest {
     verify(context, never()).saveMeasure(eq(CoreMetrics.COVERAGE), anyDouble());
   }
 
+  @Test
+  public void should_count_covered_elements_for_new_code() {
+    Measure newLines = measureWithVariation(1, 100.0);
+    Measure newUncoveredConditions = measureWithVariation(1, 10.0);
+    Measure newUncoveredLines = measureWithVariation(1, 5.0);
+    Measure newConditions = measureWithVariation(1, 1.0);
+    DecoratorContext context = mockNewContext(newLines, newUncoveredConditions, newUncoveredLines, newConditions);
+
+    long count = decorator.countCoveredElementsForNewCode(context, 1);
+
+    assertThat(count).isEqualTo(86).isEqualTo(100 + 1 - 10 - 5);
+  }
+
   private static DecoratorContext mockContext(int lines, int uncoveredLines, int conditions, int uncoveredConditions) {
     DecoratorContext context = mock(DecoratorContext.class);
     when(context.getMeasure(CoreMetrics.LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.LINES_TO_COVER, (double) lines));
@@ -113,4 +126,19 @@ public class CoverageDecoratorTest {
     when(context.getMeasure(CoreMetrics.UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.UNCOVERED_CONDITIONS, (double) uncoveredConditions));
     return context;
   }
+
+  private static DecoratorContext mockNewContext(Measure newLines, Measure newUncoveredConditions, Measure newUncoveredLines, Measure newConditions) {
+    DecoratorContext context = mock(DecoratorContext.class);
+    when(context.getMeasure(CoreMetrics.NEW_LINES_TO_COVER)).thenReturn(newLines);
+    when(context.getMeasure(CoreMetrics.NEW_UNCOVERED_LINES)).thenReturn(newUncoveredLines);
+    when(context.getMeasure(CoreMetrics.NEW_UNCOVERED_CONDITIONS)).thenReturn(newUncoveredConditions);
+    when(context.getMeasure(CoreMetrics.NEW_CONDITIONS_TO_COVER)).thenReturn(newConditions);
+    return context;
+  }
+
+  private static Measure measureWithVariation(int period, double variation) {
+    Measure measure = mock(Measure.class);
+    when(measure.getVariation(period)).thenReturn(variation);
+    return measure;
+  }
 }
index 1fc2089a376bf135a1970bc6eb4258580de6297b..b8e3afb2162b174aeecc2507154e66ae9e655649 100644 (file)
@@ -105,6 +105,19 @@ public class ItCoverageDecoratorTest {
     verify(context, never()).saveMeasure(eq(CoreMetrics.IT_COVERAGE), anyDouble());
   }
 
+  @Test
+  public void should_count_covered_elements_for_new_code() {
+    Measure newLines = measureWithVariation(1, 100.0);
+    Measure newUncoveredConditions = measureWithVariation(1, 10.0);
+    Measure newUncoveredLines = measureWithVariation(1, 5.0);
+    Measure newConditions = measureWithVariation(1, 1.0);
+    DecoratorContext context = mockNewContext(newLines, newUncoveredConditions, newUncoveredLines, newConditions);
+
+    long count = decorator.countCoveredElementsForNewCode(context, 1);
+
+    assertThat(count).isEqualTo(86).isEqualTo(100 + 1 - 10 - 5);
+  }
+
   private static DecoratorContext mockContext(int lines, int uncoveredLines, int conditions, int uncoveredConditions) {
     DecoratorContext context = mock(DecoratorContext.class);
     when(context.getMeasure(CoreMetrics.IT_LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.IT_LINES_TO_COVER, (double) lines));
@@ -113,4 +126,19 @@ public class ItCoverageDecoratorTest {
     when(context.getMeasure(CoreMetrics.IT_UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.IT_UNCOVERED_CONDITIONS, (double) uncoveredConditions));
     return context;
   }
+
+  private static DecoratorContext mockNewContext(Measure newLines, Measure newUncoveredConditions, Measure newUncoveredLines, Measure newConditions) {
+    DecoratorContext context = mock(DecoratorContext.class);
+    when(context.getMeasure(CoreMetrics.NEW_IT_LINES_TO_COVER)).thenReturn(newLines);
+    when(context.getMeasure(CoreMetrics.NEW_IT_UNCOVERED_LINES)).thenReturn(newUncoveredLines);
+    when(context.getMeasure(CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS)).thenReturn(newUncoveredConditions);
+    when(context.getMeasure(CoreMetrics.NEW_IT_CONDITIONS_TO_COVER)).thenReturn(newConditions);
+    return context;
+  }
+
+  private static Measure measureWithVariation(int period, double variation) {
+    Measure measure = mock(Measure.class);
+    when(measure.getVariation(period)).thenReturn(variation);
+    return measure;
+  }
 }
index 7ec96574cee0fa2bd447c835e52fa28436d6e9d3..546f9b8e3127e7e7714295ecb8da749f024142fa 100644 (file)
@@ -105,6 +105,19 @@ public class OverallCoverageDecoratorTest {
     verify(context, never()).saveMeasure(eq(CoreMetrics.OVERALL_COVERAGE), anyDouble());
   }
 
+  @Test
+  public void should_count_covered_elements_for_new_code() {
+    Measure newLines = measureWithVariation(1, 100.0);
+    Measure newUncoveredConditions = measureWithVariation(1, 10.0);
+    Measure newUncoveredLines = measureWithVariation(1, 5.0);
+    Measure newConditions = measureWithVariation(1, 1.0);
+    DecoratorContext context = mockNewContext(newLines, newUncoveredConditions, newUncoveredLines, newConditions);
+
+    long count = decorator.countCoveredElementsForNewCode(context, 1);
+
+    assertThat(count).isEqualTo(86).isEqualTo(100 + 1 - 10 - 5);
+  }
+
   private static DecoratorContext mockContext(int lines, int uncoveredLines, int conditions, int uncoveredConditions) {
     DecoratorContext context = mock(DecoratorContext.class);
     when(context.getMeasure(CoreMetrics.OVERALL_LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.OVERALL_LINES_TO_COVER, (double) lines));
@@ -113,4 +126,19 @@ public class OverallCoverageDecoratorTest {
     when(context.getMeasure(CoreMetrics.OVERALL_UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.OVERALL_UNCOVERED_CONDITIONS, (double) uncoveredConditions));
     return context;
   }
+
+  private static DecoratorContext mockNewContext(Measure newLines, Measure newUncoveredConditions, Measure newUncoveredLines, Measure newConditions) {
+    DecoratorContext context = mock(DecoratorContext.class);
+    when(context.getMeasure(CoreMetrics.NEW_OVERALL_LINES_TO_COVER)).thenReturn(newLines);
+    when(context.getMeasure(CoreMetrics.NEW_OVERALL_UNCOVERED_LINES)).thenReturn(newUncoveredLines);
+    when(context.getMeasure(CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS)).thenReturn(newUncoveredConditions);
+    when(context.getMeasure(CoreMetrics.NEW_OVERALL_CONDITIONS_TO_COVER)).thenReturn(newConditions);
+    return context;
+  }
+
+  private static Measure measureWithVariation(int period, double variation) {
+    Measure measure = mock(Measure.class);
+    when(measure.getVariation(period)).thenReturn(variation);
+    return measure;
+  }
 }