Skip to content

Commit 968ebab

Browse files
committed
Add test for BlogParser and Can parse a String
1 parent 0daeabe commit 968ebab

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed
Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
package dotty.tools.scaladoc
2-
package site
1+
package dotty.tools.scaladoc.site
32

43
import com.fasterxml.jackson.databind.ObjectMapper
54
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
65
import com.fasterxml.jackson.databind.DeserializationFeature
76
import java.io.File
8-
import scala.beans._
7+
import scala.beans.{BooleanBeanProperty, BeanProperty}
8+
import scala.util.Try
99

1010
case class BlogConfig(
11-
@BeanProperty var input: String,
12-
@BeanProperty var output: String,
13-
@BooleanBeanProperty var hidden: Boolean
11+
@BeanProperty input: String,
12+
@BeanProperty output: String,
13+
@BooleanBeanProperty hidden: Boolean
1414
):
15-
def this() = this(null, null, false)
15+
def this() = this(null, null, false)
1616

1717
object BlogParser:
18-
def readYml(root: File): BlogConfig =
19-
val ymlFile = root.toPath
20-
.resolve("blog.yml")
21-
.toFile
18+
def readYml(content: File | String): BlogConfig =
19+
val mapper = ObjectMapper(YAMLFactory())
20+
.findAndRegisterModules()
2221

23-
if ymlFile.exists then
24-
val mapper = new ObjectMapper(new YAMLFactory())
25-
mapper.findAndRegisterModules();
26-
27-
val blogConfig: BlogConfig = mapper.readValue(ymlFile, classOf[BlogConfig])
28-
blogConfig
29-
else new BlogConfig
22+
content match
23+
case f: File =>
24+
val ymlFile = f.toPath.resolve("blog.yml").toFile
25+
if ymlFile.exists then mapper.readValue(ymlFile, classOf[BlogConfig]) else new BlogConfig
26+
case s: String => Try(mapper.readValue(s, classOf[BlogConfig])).getOrElse(new BlogConfig)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package dotty.tools.scaladoc
2+
package site
3+
4+
import org.junit.Test
5+
import org.junit.Assert._
6+
7+
class BlogParserTest:
8+
9+
private val blogConfig =
10+
"""input: blog
11+
|output: blog
12+
|hidden: false
13+
|""".stripMargin
14+
15+
@Test
16+
def loadBlog(): Unit = assertEquals(
17+
BlogConfig("blog", "blog", false),
18+
BlogParser.readYml(blogConfig)
19+
)

0 commit comments

Comments
 (0)