Coder Social home page Coder Social logo

scala-expect's Introduction

Scala Expect license

Scaladoc Maven Central

Build Status Codacy Badge Codacy Badge BCH compliance

A Scala implementation of a very small subset of the widely known TCL expect.

Scala Expect comes with three different flavors: core, fluent and dsl.

Install

libraryDependencies += "work.martins.simon" %% "scala-expect" % "6.0.0"

Core

Advantages

  • Closer to metal / basis for the other flavors.
  • Immutable and therefore thread-safe.
  • Most errors will be caught at compile time (eg. you won't be able to use a SendWithRegex inside a StringWhen).

Disadvantages

  • Verbose syntax.
  • Can't cleanly add expect blocks/whens/actions based on a condition.

Example

import work.martins.simon.core._
import scala.concurrent.ExecutionContext.Implicits.global

val e = new Expect("bc -i", defaultValue = 5)(
  ExpectBlock(
    StringWhen("For details type `warranty'.")(
      Sendln("1 + 2")
    )
  ),
  ExpectBlock(
    RegexWhen("""\n(\d+)\n""".r)(
      SendlnWithRegex { m =>
        val previousAnswer = m.group(1)
        println(s"Got $previousAnswer")
        s"$previousAnswer + 3"
      }
    )
  ),
  ExpectBlock(
    RegexWhen("""\n(\d+)\n""".r)(
      ReturningWithRegex(_.group(1).toInt)
    )
  )
)
e.run() //Returns 6 inside a Future[Int]

Fluent

Advantages

  • Less verbose syntax:
    • StringWhen, RegexWhen, etc is just when.
    • Returning, ReturningWithRegex, etc is just returning.
    • Less commas and parenthesis.
  • Most errors will be caught at compile time.
  • Easy to add expect blocks/whens/actions based on a condition.
  • Easy to refactor the creation of expects.
  • Can be called from Java easily.

Disadvantages

  • Some overhead since the fluent expect is just a builder for a core expect.
  • Mutable - the fluent expect has to maintain a state of the objects that have been built.
  • IDE's will easily mess the custom indentation.

Example

import work.martins.simon.fluent._
import scala.concurrent.ExecutionContext.Implicits.global

val e = new Expect("bc -i", defaultValue = 5) {
  expect
    .when("For details type `warranty'.")
      .sendln("1 + 2")
  expect
    .when("""\n(\d+)\n""".r)
      .sendln { m =>
        val previousAnswer = m.group(1)
        println(s"Got $previousAnswer")
        s"$previousAnswer + 3"
      }
  //This is a shortcut. It works just like the previous expect block.
  expect("""\n(\d+)\n""".r)
    .returning(_.group(1).toInt)
}
e.run() //Returns 6 inside a Future[Int]

DSL

Advantages

  • Code will be indented in blocks so IDE's won't mess the indentation.
  • Syntax more close to the TCL expect.
  • Easy to add expect blocks/whens/actions based on a condition.
  • Easy to refactor the creation of expects.

Disadvantages

  • Most errors will only be caught at runtime as opposed to compile time.
  • More overhead than the fluent expect since it's just a wrapper arround fluent expect.
  • Mutable - it uses a fluent expect as the backing expect and a mutable stack to keep track of the current context.

Example

import work.martins.simon.dsl._
import scala.concurrent.ExecutionContext.Implicits.global

val e = new Expect("bc -i", defaultValue = 5) {
  expect {
    when("For details type `warranty'.") {
      sendln("1 + 2")
    }
  }
  expect {
    when("""\n(\d+)\n""".r) {
      sendln { m =>
        val previousAnswer = m.group(1)
        println(s"Got $previousAnswer")
        s"$previousAnswer + 3"
      }
    }
  }
  //This is a shortcut. It works just like the previous expect block.
  expect("""\n(\d+)\n""".r) {
    returning(_.group(1).toInt)
  }
}
e.run() //Returns 6 inside a Future[Int]

License

Scala Expect is open source and available under the MIT license.

scala-expect's People

Contributors

lasering avatar magicknot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

magicknot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.