<commit_before>/*
 * Copyright 2012-2020 the original author or authors.
 *
 * 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 laika.config

import laika.ast.DocumentMetadata
import laika.config.Config.ConfigResult
import laika.time.PlatformDateFormat
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

/**
  * @author <NAME>
  */
class ConfigCodecSpec extends AnyWordSpec with Matchers {

  def decode[T: ConfigDecoder] (input: String): ConfigResult[T] =
    ConfigParser.parse(input).resolve().flatMap(_.get[T](Key.root))

  def decode[T: ConfigDecoder] (config: Config): ConfigResult[T] = config.get[T]("test")

  "The codec for DocumentMetadata" should {

    "decode an instance with all fields populated" in {
      val input =
        """{
          |  identifier = XX-33-FF-01
          |  authors = [ "<NAME>", "<NAME>" ]
          |  language = en
          |  date = "2002-10-10T12:00:00"
          |}
        """.stripMargin
      decode[DocumentMetadata](input) shouldBe Right(DocumentMetadata(
        Some("XX-33-FF-01"),
        Seq("<NAME>", "<NAME>"),
        Some("en"),
        Some(PlatformDateFormat.parse("2002-10-10T12:00:00").toOption.get)
      ))
    }

    "decode an instance with a single author" in {
      val input =
        """{
          |  identifier = XX-33-FF-01
          |  author = "<NAME>"
          |  language = en
          |  date = "2002-10-10T12:00:00"
          |}
        """.stripMargin
      decode[DocumentMetadata](input) shouldBe Right(DocumentMetadata(
        Some("XX-33-FF-01"),
        Seq("Dorothea West"),
        Some("en"),
        Some(PlatformDateFormat.parse("2002-10-10T12:00:00").toOption.get)
      ))
    }

    "round-trip encode and decode" in {
      val input = DocumentMetadata(
        Some("XX-33-FF-01"),
        Seq("Helen North", "Maria South"),
        Some("en"),
        Some(PlatformDateFormat.parse("2002-10-10T12:00:00").toOption.get)
      )
      val encoded = ConfigBuilder.empty.withValue("test", input).build
      decode[DocumentMetadata](encoded) shouldBe Right(DocumentMetadata(
        Some("XX-33-FF-01"),
        Seq("Helen North", "Maria South"),
        Some("en"),
        Some(PlatformDateFormat.parse("2002-10-10T12:00:00").toOption.get)
      ))
    }

    "fail with an invalid date" in {
      val input =
        """{
          |  identifier = XX-33-FF-01
          |  author = "<NAME>"
          |  language = en
          |  date = "2000-XX-01T00:00:00Z"
          |}
        """.stripMargin
      decode[DocumentMetadata](input) shouldBe Left(
        DecodingError("Invalid date format: Text '2000-XX-01T00:00:00Z' could not be parsed at index 5")
      )
    }

  }

}
<commit_msg>codec spec for new BookConfig - #141<commit_after>/*
 * Copyright 2012-2020 the original author or authors.
 *
 * 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 laika.config

import laika.ast.DocumentMetadata
import laika.ast.Path.Root
import laika.config.Config.ConfigResult
import laika.rewrite.nav.BookConfig
import laika.time.PlatformDateFormat
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

/**
  * @author <NAME>
  */
class ConfigCodecSpec extends AnyWordSpec with Matchers {

  def decode[T: ConfigDecoder] (input: String): ConfigResult[T] =
    ConfigParser.parse(input).resolve().flatMap(_.get[T](Key.root))

  def decode[T: ConfigDecoder] (config: Config): ConfigResult[T] = config.get[T]("test")

  "The codec for DocumentMetadata" should {

    "decode an instance with all fields populated" in {
      val input =
        """{
          |  identifier = XX-33-FF-01
          |  authors = [ "<NAME>", "<NAME>" ]
          |  language = en
          |  date = "2002-10-10T12:00:00"
          |}
        """.stripMargin
      decode[DocumentMetadata](input) shouldBe Right(DocumentMetadata(
        Some("XX-33-FF-01"),
        Seq("<NAME>", "<NAME>"),
        Some("en"),
        Some(PlatformDateFormat.parse("2002-10-10T12:00:00").toOption.get)
      ))
    }

    "decode an instance with a single author" in {
      val input =
        """{
          |  identifier = XX-33-FF-01
          |  author = "<NAME>"
          |  language = en
          |  date = "2002-10-10T12:00:00"
          |}
        """.stripMargin
      decode[DocumentMetadata](input) shouldBe Right(DocumentMetadata(
        Some("XX-33-FF-01"),
        Seq("Dorothea West"),
        Some("en"),
        Some(PlatformDateFormat.parse("2002-10-10T12:00:00").toOption.get)
      ))
    }

    "round-trip encode and decode" in {
      val input = DocumentMetadata(
        Some("XX-33-FF-01"),
        Seq("<NAME>", "<NAME>"),
        Some("en"),
        Some(PlatformDateFormat.parse("2002-10-10T12:00:00").toOption.get)
      )
      val encoded = ConfigBuilder.empty.withValue("test", input).build
      decode[DocumentMetadata](encoded) shouldBe Right(DocumentMetadata(
        Some("XX-33-FF-01"),
        Seq("<NAME>", "<NAME>"),
        Some("en"),
        Some(PlatformDateFormat.parse("2002-10-10T12:00:00").toOption.get)
      ))
    }

    "fail with an invalid date" in {
      val input =
        """{
          |  identifier = XX-33-FF-01
          |  author = "<NAME>"
          |  language = en
          |  date = "2000-XX-01T00:00:00Z"
          |}
        """.stripMargin
      decode[DocumentMetadata](input) shouldBe Left(
        DecodingError("Invalid date format: Text '2000-XX-01T00:00:00Z' could not be parsed at index 5")
      )
    }

  }

  "The codec for BookConfig" should {

    "decode an instance with all fields populated" in {
      val input =
        """{
          |  metadata {
          |    identifier = XX-33-FF-01
          |    authors = [ "Helen North", "<NAME>" ]
          |    language = en
          |    date = "2002-10-10T12:00:00"
          |  }
          |  navigationDepth = 3
          |  coverImage = cover.jpg
          |}
        """.stripMargin
      decode[BookConfig](input) shouldBe Right(BookConfig(
        DocumentMetadata(
          Some("XX-33-FF-01"),
          Seq("<NAME>", "<NAME>"),
          Some("en"),
          Some(PlatformDateFormat.parse("2002-10-10T12:00:00").toOption.get)
        ),
        Some(3),
        Some(Root / "cover.jpg")
      ))
    }

    "decode an instance with some fields populated" in {
      val input =
        """{
          |  metadata {
          |    identifier = XX-33-FF-01
          |  }
          |  navigationDepth = 3
          |}
        """.stripMargin
      decode[BookConfig](input) shouldBe Right(BookConfig(
        DocumentMetadata(
          Some("XX-33-FF-01")
        ),
        Some(3)
      ))
    }

    "round-trip encode and decode" in {
      val input = BookConfig(DocumentMetadata(Some("XX-33-FF-01")), Some(3), Some(Root / "cover.jpg"))
      val encoded = ConfigBuilder.empty.withValue("test", input).build
      decode[BookConfig](encoded) shouldBe Right(BookConfig(
        DocumentMetadata(
          Some("XX-33-FF-01")
        ),
        Some(3),
        Some(Root / "cover.jpg")
      ))
    }

  }

}

{'fraction_non_alphanumeric': 0.13434119960668633, 'fraction_numerical': 0.04412487708947886, 'mean_word_length': 2.3050365556458163, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1216616', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2428, 'n_words': 575}