Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Professional Scala

You're reading from   Professional Scala Combine object-oriented and functional programming to build high-performance applications

Arrow left icon
Product type Paperback
Published in Jul 2018
Publisher
ISBN-13 9781789533835
Length 186 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Ruslan Shevchenko Ruslan Shevchenko
Author Profile Icon Ruslan Shevchenko
Ruslan Shevchenko
Mads Hartmann Mads Hartmann
Author Profile Icon Mads Hartmann
Mads Hartmann
Arrow right icon
View More author details
Toc

Writing a Small DSL


In this section, we'll reimplement some FlatSpec ScalaTest DSLs in order to see how to implement DSLs in Scala.

First, we'll have a look at a simple way to model test cases in Scala using case classes. Then, we'll have a look at how to create a little DSL for creating those test cases.

Modeling Test Cases

Before we can create a DSL, we need to have something to create a DSL for. In our case, we want to create a DSL for specifying tests, so let's have a look at how we could model tests using case classes in Scala:

sealed trait TestResult
case class TestFailure(message: String) extends TestResult
case object TestSuccess extends TestResult

We'll create an algebraic data type that represents the result of running a test case. The result can either be a failure that contains a message regarding the failure, or a TestSuccess that simply indicates that the test passed:

case class TestCase(description: TestDescription, run: () => TestResult)
case class TestDescription(name...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £16.99/month. Cancel anytime