Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tv.codely.scala_intro_examples.lesson_09_oop.exercise_solutions.joel_coll_solution

object Email {
def apply(emailStr: String): Email = {
if ("""(?=[^\s]+)(?=(\w+)@([\w\.]+\.[\w]+))""".r.findFirstIn(emailStr).isEmpty)
throw new IllegalArgumentException

val temp = emailStr.split('@')
Email(temp(0), temp(1))
}
}

final case class Email(local: String, domain: String) {
val email: String = local + "@" + domain
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tv.codely.scala_intro_examples.lesson_09_oop.exercise_solutions.joel_coll_solution

import org.scalatest.{Matchers, WordSpec}

final class EmailSpec extends WordSpec with Matchers {
private val localEmail = "local"
private val domainEmail = "domain.com"
private val badDomainEmail = "domaincom"
private val email = localEmail + '@' + domainEmail
private val badEmail = localEmail + '@' + badDomainEmail

"Email" should {
"return an instance when pass local and domain params" in {
val emailInstance = Email(local = localEmail, domain = domainEmail)
emailInstance.email shouldBe email
}
"return an instance when pass and email" in {
val emailInstance = Email(emailStr = email)
emailInstance.email shouldBe email
}
"throw an exception when pass a bad email" in {
intercept[IllegalArgumentException] {
Email(emailStr = badEmail)
}
}
}
}