8000 Add tests to `Parser.parseDocuments` by lucaviolanti · Pull Request #342 · circe/circe-yaml · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add tests to Parser.parseDocuments #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 131 additions & 2 deletions circe-yaml/src/test/scala/io/circe/yaml/ParserTests.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package io.circe.yaml

import io.circe.Json
import io.circe.syntax._
import java.io.StringReader
import org.scalatest.EitherValues
import org.scalatest.flatspec.AnyFlatSpec
import io.circe.syntax._
import org.scalatest.matchers.should.Matchers

class ParserTests extends AnyFlatSpec with Matchers with EitherValues {
// the laws should do a pretty good job of surfacing errors; these are mainly to ensure test coverage

"Parser" should "fail on invalid tagged numbers" in {
"Parser.parse" should "fail on invalid tagged numbers" in {
assert(parser.parse("!!int 12foo").isLeft)
}

Expand Down Expand Up @@ -122,4 +123,132 @@ class ParserTests extends AnyFlatSpec with Matchers with EitherValues {
.isLeft
)
}

"Parser.parseDocuments" should "fail on invalid tagged numbers" in {
val result = parser.parseDocuments(new StringReader("!!int 12foo")).toList
assert(result.size == 1)
assert(result.head.isLeft)
}

it should "fail to parse complex keys" in {
val result = parser
.parseDocuments(new StringReader("""
|? - foo
| - bar
|: 1""".stripMargin))
.toList
assert(result.size == 1)
assert(result.head.isLeft)
}

it should "fail to parse invalid YAML" in {
/*
See comments below that document the actual behaviour
*/
// val result = Either.catchNonFatal(parser.parseDocuments(new StringReader("""foo: - bar""")).toList)
// assert(result.isLeft)
// assert(result.swap.value.isInstanceOf[org.yaml.snakeyaml.scanner.ScannerException])
val result = parser.parseDocuments(new StringReader("""foo: - bar""")).toList
assert(result.size == 1)
assert(result.head.isLeft)
assert(result.head.isInstanceOf[Either[io.circe.ParsingFailure, Json]])
}

it should "parse yes as true" in {
val result = parser.parseDocuments(new StringReader("""foo: yes""")).toList
assert(result.size == 1)
assert(result.head.isRight)
}

it should "parse hexadecimal" in {
val result = parser.parseDocuments(new StringReader("""[0xFF, 0xff, 0xab_cd]""")).toList
assert(result.size == 1)
assert(result.head.contains(Seq(0xff, 0xff, 0xabcd).asJson))
}

it should "parse decimal with underscore breaks" in {
val result = parser.parseDocuments(new StringReader("""foo: 1_000_000""")).toList
assert(result.size == 1)
assert(result.head.contains(Map("foo" -> 1000000).asJson))
}

it should "parse empty string as false" in {
/*
See comments below that document the actual behaviour
*/
val result = parser.parseDocuments(new StringReader("")).toList
// assert(result.isEmpty)
assert(result.size == 1)
assert(result.head.right.value == Json.False)
}

it should "parse blank string as false" in {
/*
See comments below that document the actual behaviour
*/
val result = parser.parseDocuments(new StringReader(" ")).toList
// assert(result.isEmpty)
assert(result.size == 1)
assert(result.head.right.value == Json.False)
}

it should "parse aliases" in {
val result = parser
.parseDocuments(
new StringReader(
"""
| aliases:
| - &alias1
| foo:
| bar
| baz:
| - *alias1
| - *alias1
|""".stripMargin
)
)
.toList
assert(result.size == 1)
assert(result.head.isRight)
}

it should "fail to parse too many aliases" in {
/*
See comments below that document the actual behaviour
*/
// val result = Either.catchNonFatal(
// Parser(maxAliasesForCollections = 1)
// .parseDocuments(
// new StringReader(
// """
// | aliases:
// | - &alias1
// | foo:
// | bar
// | baz:
// | - *alias1
// | - *alias1
// |""".stripMargin
// )
// )
// .toList
// )
// assert(result.isLeft)
// assert(result.swap.value.isInstanceOf[org.yaml.snakeyaml.error.YAMLException])
val result =
Parser(maxAliasesForCollections = 1)
.parseDocuments(new StringReader(
"""
| aliases:
| - &alias1
| foo:
| bar
| baz:
| - *alias1
| - *alias1
|""".stripMargin
)).toList
assert(result.isEmpty)
assert(result.head.isLeft)
}
}
0