[Infer][tests] Adding parser for stats.json, matcher for checking number of files analyzed by stats.json
parent
7640b01096
commit
daac9c7af7
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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<InferStats> {
|
||||
|
||||
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 <T> Matcher<InferStats> numberOfFilesAnalyzed(
|
||||
int expectedNumFiles) {
|
||||
return new NumberOfFilesAnalyzed(expectedNumFiles);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue