mirror of
https://github.com/lightbend/config.git
synced 2025-02-22 09:10:32 +08:00
add a test setup where files that should be parsed to same syntax tree are compared
This commit is contained in:
parent
986e2a24cb
commit
ff5bf095c7
@ -1,5 +1,9 @@
|
|||||||
package com.typesafe.config.impl;
|
package com.typesafe.config.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
@ -44,6 +48,26 @@ final class Parser {
|
|||||||
return parse(flavor, origin, new StringReader(input));
|
return parse(flavor, origin, new StringReader(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static AbstractConfigValue parse(File f) {
|
||||||
|
ConfigOrigin origin = new SimpleConfigOrigin(f.getPath());
|
||||||
|
SyntaxFlavor flavor = null;
|
||||||
|
if (f.getName().endsWith(".json"))
|
||||||
|
flavor = SyntaxFlavor.JSON;
|
||||||
|
else if (f.getName().endsWith(".conf"))
|
||||||
|
flavor = SyntaxFlavor.HOCON;
|
||||||
|
else
|
||||||
|
throw new ConfigException.IO(origin, "Unknown filename extension");
|
||||||
|
AbstractConfigValue result = null;
|
||||||
|
try {
|
||||||
|
InputStream stream = new BufferedInputStream(new FileInputStream(f));
|
||||||
|
result = parse(flavor, origin, stream);
|
||||||
|
stream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ConfigException.IO(origin, "failed to read file", e);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
static private final class ParseContext {
|
static private final class ParseContext {
|
||||||
private int lineNumber;
|
private int lineNumber;
|
||||||
private SyntaxFlavor flavor;
|
private SyntaxFlavor flavor;
|
||||||
|
1
src/test/resources/equiv01/no-whitespace.json
Normal file
1
src/test/resources/equiv01/no-whitespace.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"ints":{"fortyTwo":42,"fortyTwoAgain":42},"floats":{"fortyTwoPointOne":42.1,"fortyTwoPointOneAgain":42.1},"strings":{"abcd":"abcd","abcdAgain":"abcd","a":"a","b":"b","c":"c","d":"d"},"arrays":{"empty":[],"1":[1],"12":[1,2],"123":[1,2,3]},"booleans":{"true":true,"trueAgain":true,"false":false,"falseAgain":false},"nulls":{"null":null,"nullAgain":null}}
|
39
src/test/resources/equiv01/original.json
Normal file
39
src/test/resources/equiv01/original.json
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
{
|
||||||
|
"ints" : {
|
||||||
|
"fortyTwo" : 42,
|
||||||
|
"fortyTwoAgain" : 42
|
||||||
|
},
|
||||||
|
|
||||||
|
"floats" : {
|
||||||
|
"fortyTwoPointOne" : 42.1,
|
||||||
|
"fortyTwoPointOneAgain" : 42.1
|
||||||
|
},
|
||||||
|
|
||||||
|
"strings" : {
|
||||||
|
"abcd" : "abcd",
|
||||||
|
"abcdAgain" : "abcd",
|
||||||
|
"a" : "a",
|
||||||
|
"b" : "b",
|
||||||
|
"c" : "c",
|
||||||
|
"d" : "d"
|
||||||
|
},
|
||||||
|
|
||||||
|
"arrays" : {
|
||||||
|
"empty" : [],
|
||||||
|
"1" : [ 1 ],
|
||||||
|
"12" : [1, 2],
|
||||||
|
"123" : [1, 2, 3]
|
||||||
|
},
|
||||||
|
|
||||||
|
"booleans" : {
|
||||||
|
"true" : true,
|
||||||
|
"trueAgain" : true,
|
||||||
|
"false" : false,
|
||||||
|
"falseAgain" : false
|
||||||
|
},
|
||||||
|
|
||||||
|
"nulls" : {
|
||||||
|
"null" : null,
|
||||||
|
"nullAgain" : null
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
package com.typesafe.config.impl
|
||||||
|
|
||||||
|
import org.junit.Assert._
|
||||||
|
import org.junit._
|
||||||
|
import net.liftweb.{ json => lift }
|
||||||
|
import java.io.Reader
|
||||||
|
import java.io.StringReader
|
||||||
|
import com.typesafe.config._
|
||||||
|
import java.util.HashMap
|
||||||
|
import java.io.File
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.junit.runners.AllTests
|
||||||
|
|
||||||
|
class EquivalentsTest extends TestUtils {
|
||||||
|
|
||||||
|
private def equivDirs() = {
|
||||||
|
val resourceDir = new File("src/test/resources")
|
||||||
|
if (!resourceDir.exists())
|
||||||
|
throw new RuntimeException("This test can only be run from the project's root directory")
|
||||||
|
val rawEquivs = resourceDir.listFiles()
|
||||||
|
val equivs = rawEquivs.filter({ f => f.getName().startsWith("equiv") })
|
||||||
|
equivs
|
||||||
|
}
|
||||||
|
|
||||||
|
private def filesForEquiv(equiv: File) = {
|
||||||
|
val rawFiles = equiv.listFiles()
|
||||||
|
val files = rawFiles.filter({ f => f.getName().endsWith(".json") })
|
||||||
|
files
|
||||||
|
}
|
||||||
|
|
||||||
|
// would like each "equivNN" directory to be a suite and each file in the dir
|
||||||
|
// to be a test, but not sure how to convince junit to do that.
|
||||||
|
@Test
|
||||||
|
def testEquivalents() {
|
||||||
|
var dirCount = 0
|
||||||
|
var fileCount = 0
|
||||||
|
for (equiv <- equivDirs()) {
|
||||||
|
dirCount += 1
|
||||||
|
|
||||||
|
val files = filesForEquiv(equiv)
|
||||||
|
val (originals, others) = files.partition({ f => f.getName().startsWith("original.") })
|
||||||
|
if (originals.isEmpty)
|
||||||
|
throw new RuntimeException("Need a file named 'original' in " + equiv.getPath())
|
||||||
|
if (originals.size > 1)
|
||||||
|
throw new RuntimeException("Multiple 'original' files in " + equiv.getPath() + ": " + originals)
|
||||||
|
val original = Parser.parse(originals(0))
|
||||||
|
|
||||||
|
for (testFile <- others) {
|
||||||
|
fileCount += 1
|
||||||
|
val value = Parser.parse(testFile)
|
||||||
|
describeFailure(testFile.getPath()) {
|
||||||
|
assertEquals(original, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is a little "checksum" to be sure we really tested what we were expecting.
|
||||||
|
// it breaks every time you add a file, so you have to update it.
|
||||||
|
assertEquals(1, dirCount)
|
||||||
|
assertEquals(1, fileCount)
|
||||||
|
}
|
||||||
|
}
|
@ -23,7 +23,10 @@ class JsonTest extends TestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def tokenize(s: String): java.util.Iterator[Token] = {
|
def tokenize(s: String): java.util.Iterator[Token] = {
|
||||||
tokenize(new StringReader(s))
|
val reader = new StringReader(s)
|
||||||
|
val result = tokenize(reader)
|
||||||
|
// reader.close() // can't close until the iterator is traversed, so this tokenize() flavor is inherently broken
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
def tokenizeAsList(s: String) = {
|
def tokenizeAsList(s: String) = {
|
||||||
|
Loading…
Reference in New Issue
Block a user