Skip to content

Commit

Permalink
Support undefined-property exception
Browse files Browse the repository at this point in the history
  • Loading branch information
melvic-ybanez committed Nov 11, 2023
1 parent bcf90a1 commit 151aa3f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ object Assertions {
case IndexOutOfBounds(_, _) => Keys.Errors.IndexOutOfBounds
case InvalidIndex(_, _) => Keys.Errors.InvalidIndex
case InvalidArgument(_, _, _) => Keys.Errors.InvalidArgument
case UndefinedProperty(_) => Keys.Errors.UndefinedProperty
case UndefinedProperty(_, _) => Keys.Errors.UndefinedProperty
case UndefinedKey(_, _) => Keys.Errors.UndefinedKey
case ModuleNotFound(_, _) => Keys.Errors.ModuleNotFound
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ object Exceptions {
.defineWith(NotCallable.name, DException(NotCallable, _))
.defineWith(IncorrectArity.name, DException(IncorrectArity, _))
.defineWith(DoesNotHaveProperties.name, DException(DoesNotHaveProperties, _))
.defineWith(UndefinedProperty.name, DException(UndefinedProperty, _))

private def raise(env: Env): Callable = Callable.withLineNo(1, env) { line =>
def invalidArgument(got: Value): Result[Value] =
Expand All @@ -40,6 +41,7 @@ object Exceptions {
case NotCallable.name => fail(RuntimeError.notCallable)
case IncorrectArity.name => fail(RuntimeError.incorrectArity)
case DoesNotHaveProperties.name => fail(RuntimeError.doesNotHaveProperties)
case UndefinedProperty.name => fail(RuntimeError.undefinedProperty)
}
case arg :: _ => invalidArgument(arg)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ object DException {
case object NotCallable extends Kind
case object IncorrectArity extends Kind
case object DoesNotHaveProperties extends Kind
case object UndefinedProperty extends Kind

def apply(kind: Kind, env: Env): DException =
new DException(kind, env)
Expand Down
9 changes: 6 additions & 3 deletions src/main/scala/com/melvic/dry/result/Failure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ object Failure {
final case class NotCallable(token: Token, message: String) extends RuntimeError
final case class IncorrectArity(token: Token, message: String) extends RuntimeError
final case class DoesNotHaveProperties(token: Token, message: String) extends RuntimeError
final case class UndefinedProperty(token: Token) extends RuntimeError
final case class UndefinedProperty(token: Token, message: String) extends RuntimeError
final case class UndefinedKey(key: Expr, token: Token) extends RuntimeError
final case class CanNotApplyIndexOperator(obj: Expr, token: Token) extends RuntimeError
final case class IndexOutOfBounds(index: Int, line: Int) extends RuntimeError
Expand Down Expand Up @@ -117,8 +117,11 @@ object Failure {
def doesNotHaveProperties(obj: Expr, token: Token): RuntimeError =
doesNotHaveProperties(token, show"$obj does not have properties or fields.")

def undefinedProperty(token: Token, message: String): RuntimeError =
UndefinedProperty(token, message)

def undefinedProperty(token: Token): RuntimeError =
UndefinedProperty(token)
undefinedProperty(token, show"Undefined property: $token")

def undefinedKey(key: Expr, token: Token): RuntimeError =
UndefinedKey(key, token)
Expand Down Expand Up @@ -147,7 +150,7 @@ object Failure {
case NotCallable(token, message) => errorMsg(token, message)
case IncorrectArity(token, message) => errorMsg(token, message)
case DoesNotHaveProperties(token, message) => errorMsg(token, message)
case UndefinedProperty(token) => errorMsg(token, show"Undefined property: $token")
case UndefinedProperty(token, message) => errorMsg(token, message)
case UndefinedKey(key, token) => errorMsg(token, show"Undefined key: $key")
case CanNotApplyIndexOperator(obj, token) =>
errorMsg(token, show"Can not apply [] operator to $obj")
Expand Down
4 changes: 4 additions & 0 deletions src/test/scala/com/melvic/dry/tests/ExceptionsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class ExceptionsSpec extends AnyFlatSpec with should.Matchers with ScalaCheckPro
checkException("DoesNotHaveProperties")(RuntimeError.doesNotHaveProperties)
}

"Raising a UndefinedProperty exception" should "return a runtime error" in {
checkException("UndefinedProperty")(RuntimeError.undefinedProperty)
}

def checkException(exception: String)(error: (Token, String) => RuntimeError): Unit = {
val errorMsg = "No money for you!"

Expand Down

0 comments on commit 151aa3f

Please sign in to comment.