Skip to content

Commit

Permalink
pepegar#111 Added methods to Status (pepegar#112)
Browse files Browse the repository at this point in the history
* pepegar#111 added methods to Status

* pepegar#111 added tests
  • Loading branch information
IIVat authored and pepegar committed Jul 2, 2018
1 parent 1a397b3 commit dcde7d5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
8 changes: 7 additions & 1 deletion core/shared/src/main/scala/hammock/Status.scala
Expand Up @@ -2,7 +2,13 @@ package hammock

import monocle.macros.Lenses

@Lenses case class Status(code: Int, text: String, description: String)
@Lenses case class Status(code: Int, text: String, description: String) {
def isInformational: Boolean = this.code / 100 == 1
def isSuccess: Boolean = this.code / 100 == 2
def isRedirection: Boolean = this.code / 100 == 3
def isClientError: Boolean = this.code / 100 == 4
def isServerError: Boolean = this.code / 100 == 5
}

object Status {
val Continue = Status(
Expand Down
92 changes: 92 additions & 0 deletions core/shared/src/test/scala/hammock/StatusSpec.scala
@@ -0,0 +1,92 @@
package hammock

import org.scalatest.{Matchers, WordSpec}

class StatusSpec extends WordSpec with Matchers {
val status: Int => Status = (code: Int) => Status(code, "", "")

"Status.isInformational" when {
"status code is 1xx" should {
"return true" in {
(100 to 102).foreach(code => assert(status(code).isInformational))
}
}

"status code is not 1xx" should {
"return false" in {
assert(!status(200).isInformational)
assert(!status(300).isInformational)
assert(!status(400).isInformational)
assert(!status(500).isInformational)
}
}
}

"Status.isSuccess" when {
"status code is 2xx" should {
"return true" in {
(200 to 208).foreach(code => assert(status(code).isSuccess))
}
}

"status code is not 2xx" should {
"return false" in {
assert(!status(100).isSuccess)
assert(!status(300).isSuccess)
assert(!status(400).isSuccess)
assert(!status(500).isSuccess)
}
}
}

"Status.isRedirection" when {
"status code is 3xx" should {
"return true" in {
(300 to 308).foreach(code => assert(status(code).isRedirection))
}
}

"status code is not 3xx" should {
"return false" in {
assert(!status(100).isRedirection)
assert(!status(200).isRedirection)
assert(!status(400).isRedirection)
assert(!status(500).isRedirection)
}
}
}

"Status.isClientError" when {
"status code is 4xx" should {
"return true" in {
(400 to 451).foreach(code => assert(status(code).isClientError))
}
}

"status code is not 4xx" should {
"return false" in {
assert(!status(100).isClientError)
assert(!status(200).isClientError)
assert(!status(300).isClientError)
assert(!status(500).isClientError)
}
}
}

"Status.isServerError" when {
"status code is 5xx" should {
"return true" in {
(500 to 599).foreach(code => assert(status(code).isServerError))
}
}

"status code is not 5xx" should {
"return false" in {
assert(!status(100).isServerError)
assert(!status(200).isServerError)
assert(!status(300).isServerError)
assert(!status(400).isServerError)
}
}
}
}

0 comments on commit dcde7d5

Please sign in to comment.