Refactor nullness tests

This commit is contained in:
Alexey Andreev 2017-01-02 13:28:35 +03:00
parent 9dc4b47253
commit 5fd95f21cb
9 changed files with 81 additions and 63 deletions

View File

@ -0,0 +1,75 @@
/*
* Copyright 2017 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.model;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.junit.Assert;
import org.teavm.model.text.ListingParseException;
import org.teavm.model.text.ListingParser;
public final class ListingParseUtils {
private ListingParseUtils() {
}
public static Program parseFromResource(String resourceName) {
ClassLoader classLoader = ListingParseUtils.class.getClassLoader();
try (InputStream input = classLoader.getResourceAsStream(resourceName);
InputStreamReader reader = new InputStreamReader(input, "UTF-8")) {
return new ListingParser().parse(reader);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ListingParseException e) {
Location location;
try (InputStream input = classLoader.getResourceAsStream(resourceName)) {
location = offsetToLocation(e.getIndex(), input);
} catch (IOException e2) {
throw new RuntimeException(e2);
}
Assert.fail("Parse error at [" + (location.row + 1) + "; " + (location.column + 1)
+ "]: " + e.getMessage());
return null;
}
}
private static Location offsetToLocation(int offset, InputStream input) throws IOException {
int row = 0;
int column = 0;
try (InputStreamReader reader = new InputStreamReader(input, "UTF-8")) {
for (int i = 0; i < offset; ++i) {
int c = reader.read();
if (c == '\n') {
row++;
column = 0;
} else {
column++;
}
}
}
return new Location(row, column);
}
static class Location {
int row;
int column;
Location(int row, int column) {
this.row = row;
this.column = column;
}
}
}

View File

@ -17,30 +17,25 @@ package org.teavm.model.analysis.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.carrotsearch.hppc.IntIntMap;
import com.carrotsearch.hppc.IntIntOpenHashMap;
import com.carrotsearch.hppc.ObjectByteMap;
import com.carrotsearch.hppc.ObjectByteOpenHashMap;
import com.carrotsearch.hppc.ObjectIntMap;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.teavm.model.ListingParseUtils;
import org.teavm.model.MethodDescriptor;
import org.teavm.model.Program;
import org.teavm.model.ValueType;
import org.teavm.model.Variable;
import org.teavm.model.analysis.NullnessInformation;
import org.teavm.model.text.ListingBuilder;
import org.teavm.model.text.ListingParseException;
import org.teavm.model.text.ListingParser;
import org.teavm.model.util.ProgramUtils;
public class NullnessAnalysisTest {
@ -66,11 +61,11 @@ public class NullnessAnalysisTest {
}
private void test() {
String baseName = "model/analysis/" + name.getMethodName();
String baseName = "model/analysis/nullness/" + name.getMethodName();
String originalResourceName = baseName + ".original.txt";
String extendedResourceName = baseName + ".extended.txt";
Program originalProgram = parseResource(originalResourceName);
Program extendedProgram = parseResource(extendedResourceName);
Program originalProgram = ListingParseUtils.parseFromResource(originalResourceName);
Program extendedProgram = ListingParseUtils.parseFromResource(extendedResourceName);
ListingBuilder listingBuilder = new ListingBuilder();
String listingBeforeExtension = listingBuilder.buildListing(originalProgram, "");
@ -99,18 +94,6 @@ public class NullnessAnalysisTest {
assertEquals(listingBeforeExtension, listingAfterDispose);
}
private Program parseResource(String name) {
ClassLoader classLoader = NullnessAnalysisTest.class.getClassLoader();
try (InputStream input = classLoader.getResourceAsStream(name);
Reader reader = new InputStreamReader(input, "UTF-8")) {
return new ListingParser().parse(reader);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ListingParseException e) {
throw new RuntimeException("at " + e.getIndex() + "", e);
}
}
private ObjectByteMap<String> extractExpectedNullness(String name) {
ClassLoader classLoader = NullnessAnalysisTest.class.getClassLoader();
try (InputStream input = classLoader.getResourceAsStream(name);

View File

@ -22,6 +22,7 @@ import java.io.InputStreamReader;
import org.junit.Assert;
import org.junit.Test;
import org.teavm.model.BasicBlock;
import org.teavm.model.ListingParseUtils;
import org.teavm.model.Program;
public class ParserTest {
@ -98,47 +99,6 @@ public class ParserTest {
}
private Program runTest(String name) throws IOException {
ClassLoader classLoader = ParserTest.class.getClassLoader();
String path = "model/text/" + name + ".txt";
try (InputStream input = classLoader.getResourceAsStream(path);
InputStreamReader reader = new InputStreamReader(input, "UTF-8")) {
return new ListingParser().parse(reader);
} catch (ListingParseException e) {
Location location;
try (InputStream input = classLoader.getResourceAsStream(path)) {
location = offsetToLocation(e.getIndex(), input);
}
Assert.fail("Parse error at [" + (location.row + 1) + "; " + (location.column + 1)
+ "]: " + e.getMessage());
return null;
}
}
private Location offsetToLocation(int offset, InputStream input) throws IOException {
int row = 0;
int column = 0;
try (InputStreamReader reader = new InputStreamReader(input, "UTF-8")) {
for (int i = 0; i < offset; ++i) {
int c = reader.read();
if (c == '\n') {
row++;
column = 0;
} else {
column++;
}
}
}
return new Location(row, column);
}
static class Location {
int row;
int column;
Location(int row, int column) {
this.row = row;
this.column = column;
}
return ListingParseUtils.parseFromResource("model/text/" + name + ".txt");
}
}