diff --git a/infer/tests/utils/InferStats.java b/infer/tests/utils/InferStats.java new file mode 100644 index 000000000..9a84821e5 --- /dev/null +++ b/infer/tests/utils/InferStats.java @@ -0,0 +1,84 @@ +/* +* Copyright (c) 2015 - present Facebook, Inc. +* All rights reserved. +* +* This source code is licensed under the BSD style license found in the +* LICENSE file in the root directory of this source tree. An additional grant +* of patent rights can be found in the PATENTS file in the same directory. +*/ + +package utils; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.Reader; + +@JsonIgnoreProperties(ignoreUnknown=true) +public class InferStats { + + private static class IntFields { + @JsonProperty(value = "files") + int numFiles; + + @JsonProperty(value = "lines") + int numLines; + } + + private static class FloatFields { + @JsonProperty(value = "reporting_time") + float reportingTime; + + @JsonProperty(value = "capture_time") + float captureTime; + + @JsonProperty(value = "analysis_time") + float analysisTime; + } + + @JsonProperty(value = "int") + private IntFields intFields; + + @JsonProperty(value = "float") + private FloatFields floatFields; + + private static InferStats parseInferStatsFromJson(Reader reader) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + InferStats user = mapper.readValue(reader, InferStats.class); + return user; + } + + public static InferStats loadInferStats(Class currentClass, String sourceDir) throws IOException { + BufferedReader reader = + new BufferedReader( + new InputStreamReader( + currentClass.getResourceAsStream(sourceDir + "stats.json"))); + return parseInferStatsFromJson(reader); + } + + public int getNumFiles() { + return intFields.numFiles; + } + + public int getNumLines() { + return intFields.numLines; + } + + public float getReportingTime() { + return floatFields.reportingTime; + } + + public float getCaptureTime() { + return floatFields.captureTime; + } + + public float getAnalysisTime() { + return floatFields.analysisTime; + } + +} diff --git a/infer/tests/utils/matchers/NumberOfFilesAnalyzed.java b/infer/tests/utils/matchers/NumberOfFilesAnalyzed.java new file mode 100644 index 000000000..1e4786651 --- /dev/null +++ b/infer/tests/utils/matchers/NumberOfFilesAnalyzed.java @@ -0,0 +1,52 @@ +/* +* Copyright (c) 2015 - present Facebook, Inc. +* All rights reserved. +* +* This source code is licensed under the BSD style license found in the +* LICENSE file in the root directory of this source tree. An additional grant +* of patent rights can be found in the PATENTS file in the same directory. +*/ + +package utils.matchers; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +import utils.InferError; +import utils.InferStats; + +public class NumberOfFilesAnalyzed extends BaseMatcher { + + private int expectedNumFiles; + private int actualNumFiles; + + public NumberOfFilesAnalyzed(int expectedNumFiles) { + this.expectedNumFiles = expectedNumFiles; + this.actualNumFiles = -1; + } + + @Override + public boolean matches(Object o) { + InferStats stats = (InferStats) o; + actualNumFiles = stats.getNumFiles(); + return actualNumFiles == expectedNumFiles; + } + + @Override + public void describeTo(Description description) { + description.appendText(expectedNumFiles + " files analyzed by Infer."); + } + + @Override + public void describeMismatch(Object item, Description description) { + InferStats stats = (InferStats) item; + description.appendText("found " + actualNumFiles + " files analyzed by Infer."); + } + + public static Matcher numberOfFilesAnalyzed( + int expectedNumFiles) { + return new NumberOfFilesAnalyzed(expectedNumFiles); + } + +}