-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
147 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
53 changes: 0 additions & 53 deletions
53
weejson-jackson/test/scala/com/rallyhealth/weejson/v1/jackson/GenValue.scala
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
...jackson/test/scala/com/rallyhealth/weejson/v1/jackson/JsonGeneratorOutputStreamSpec.scala
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
weejson-jackson/test/scala/com/rallyhealth/weejson/v1/jackson/WeeJacksonSpec.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
weepickle-tests/src/test/scala/com/rallyhealth/weepickle/v1/MaxDepthVisitor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.rallyhealth.weepickle.v1 | ||
|
||
import com.rallyhealth.weepickle.v1.core.{ArrVisitor, NoOpVisitor, ObjArrVisitor, ObjVisitor, Visitor} | ||
|
||
/** | ||
* Returns the max depth encountered. | ||
* Useful for creating valid test inputs that can be sent to a parser that throws at high depth. | ||
*/ | ||
class MaxDepthVisitor extends NoOpVisitor[Int](0) { | ||
|
||
private[this] var maxDepth = 0 | ||
private[this] var currentDepth = 0 | ||
|
||
private def inc() = { | ||
currentDepth += 1 | ||
maxDepth = math.max(maxDepth, currentDepth) | ||
} | ||
|
||
private def dec() = { | ||
currentDepth -= 1 | ||
} | ||
|
||
private trait ObjArrBase { | ||
self: ObjArrVisitor[Any, Int] => | ||
|
||
override def subVisitor: Visitor[_, _] = { | ||
inc() | ||
MaxDepthVisitor.this.map(_ => maxDepth) | ||
} | ||
|
||
override def visitValue(v: Any): Unit = dec() | ||
|
||
override def visitEnd(): Int = maxDepth | ||
} | ||
|
||
override def visitObject(length: Int): ObjVisitor[Any, Int] = new ObjVisitor[Any, Int] with ObjArrBase { | ||
override def visitKey(): Visitor[_, _] = { | ||
inc() | ||
NoOpVisitor | ||
} | ||
|
||
override def visitKeyValue(v: Any): Unit = dec() | ||
} | ||
|
||
override def visitArray(length: Int): ArrVisitor[Any, Int] = new ArrVisitor[Any, Int] with ObjArrBase | ||
} |
68 changes: 68 additions & 0 deletions
68
weepickle-tests/src/test/scala/com/rallyhealth/weepickle/v1/ParserSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.rallyhealth.weepickle.v1 | ||
|
||
import com.rallyhealth.weejson.v1.jackson.{FromJson, ToJson, ToPrettyJson} | ||
import com.rallyhealth.weejson.v1.{BufferedValue, GenBufferedValue} | ||
import com.rallyhealth.weepickle.v1.core.FromInput | ||
import org.scalactic.TypeCheckedTripleEquals | ||
import org.scalatest.freespec.AnyFreeSpec | ||
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks | ||
|
||
import java.io.{ByteArrayInputStream, StringReader} | ||
import java.nio.file.Files | ||
import scala.language.{existentials, implicitConversions} | ||
|
||
abstract class ParserSpec(parse: Array[Byte] => FromInput, depthLimit: Int = 100) | ||
extends AnyFreeSpec | ||
with ScalaCheckPropertyChecks | ||
with GenBufferedValue | ||
with TypeCheckedTripleEquals { | ||
|
||
import com.rallyhealth.weejson.v1.BufferedValue._ | ||
import com.rallyhealth.weejson.v1.BufferedValueOps._ | ||
|
||
private implicit def toBytes(s: String): Array[Byte] = s.getBytes | ||
|
||
"roundtrip" in testJson() | ||
"deep arr" in testDepth(Arr(_)) | ||
"deep obj" in testDepth(b => Obj("k" -> b)) | ||
|
||
private def testJson(tweak: BufferedValue => BufferedValue = identity) = { | ||
forAll { (b: BufferedValue) => | ||
val value = tweak(b) | ||
val expected = value.transform(ToJson.string) | ||
|
||
def testInput(s: Array[Byte]) = assert(parse(s).transform(ToJson.string) === expected) | ||
|
||
testInput(expected) | ||
testInput(s"\n $expected \n ") | ||
testInput(value.transform(ToPrettyJson.bytes)) | ||
|
||
assert(parse(expected.getBytes()).transform(BufferedValue.Builder) === value) | ||
} | ||
} | ||
|
||
private def testDepth(f: BufferedValue => BufferedValue) = { | ||
testJson { in => | ||
val depth = in.transform(new MaxDepthVisitor) | ||
val numPads = math.max(depthLimit - depth, 0) | ||
(0 to numPads).foldLeft(in)((p, _) => f(p)) | ||
} | ||
} | ||
} | ||
|
||
class FromJsonBytesSpec extends ParserSpec(FromJson(_)) | ||
|
||
class FromJsonStringSpec extends ParserSpec(b => FromJson(new String(b))) | ||
|
||
class FromJsonInputStreamSpec extends ParserSpec(b => FromJson(new ByteArrayInputStream(b))) | ||
|
||
class FromJsonReaderSpec extends ParserSpec(b => FromJson(new StringReader(new String(b)))) | ||
|
||
class FromJsonPathSpec extends ParserSpec( | ||
b => FromJson(Files.write(Files.createTempFile("FromJsonPathSpec", ".json"), b)) | ||
) | ||
|
||
class FromJsonFileSpec extends ParserSpec( | ||
b => FromJson(Files.write(Files.createTempFile("FromJsonFileSpec", ".json"), b).toFile) | ||
) | ||
|