100% found this document useful (4 votes)
53 views

Immediate Download Test Driven Development in Ruby A Practical Introduction To TDD Using Problem and Solution Domain Analysis Paranj Ebooks 2024

Using

Uploaded by

kennehribati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
53 views

Immediate Download Test Driven Development in Ruby A Practical Introduction To TDD Using Problem and Solution Domain Analysis Paranj Ebooks 2024

Using

Uploaded by

kennehribati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Download the full version of the textbook now at textbookfull.

com

Test Driven Development in Ruby A Practical


Introduction to TDD Using Problem and
Solution Domain Analysis Paranj

https://textbookfull.com/product/test-driven-
development-in-ruby-a-practical-introduction-to-
tdd-using-problem-and-solution-domain-analysis-
paranj/

Explore and download more textbook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Test Driven Java Development Invoke TDD principles for end


to end application development 2nd Edition Alex Garcia

https://textbookfull.com/product/test-driven-java-development-invoke-
tdd-principles-for-end-to-end-application-development-2nd-edition-
alex-garcia/
textbookfull.com

Domain-Driven Laravel: Learn to Implement Domain-Driven


Design Using Laravel Jesse Griffin

https://textbookfull.com/product/domain-driven-laravel-learn-to-
implement-domain-driven-design-using-laravel-jesse-griffin/

textbookfull.com

iOS Test Driven Development by Tutorials First Edition


Learn Real World Test Driven Development Joshua Greene

https://textbookfull.com/product/ios-test-driven-development-by-
tutorials-first-edition-learn-real-world-test-driven-development-
joshua-greene/
textbookfull.com

Hydroelectric energy: renewable energy and the environment


1st Edition Bikash Pandey

https://textbookfull.com/product/hydroelectric-energy-renewable-
energy-and-the-environment-1st-edition-bikash-pandey/

textbookfull.com
Is Shakespeare any Good And Other Questions on How to
Evaluate Literature 1st Edition Richard Bradford

https://textbookfull.com/product/is-shakespeare-any-good-and-other-
questions-on-how-to-evaluate-literature-1st-edition-richard-bradford/

textbookfull.com

Intelligent Residential Buildings and the Behaviour of the


Occupants: State of the Art Pedro F. Pereira

https://textbookfull.com/product/intelligent-residential-buildings-
and-the-behaviour-of-the-occupants-state-of-the-art-pedro-f-pereira/

textbookfull.com

Lonely Planet Kuala Lumpur Melaka Penang Albiston

https://textbookfull.com/product/lonely-planet-kuala-lumpur-melaka-
penang-albiston/

textbookfull.com

Mathematical Theory of Bayesian Statistics First Edition


Watanabe

https://textbookfull.com/product/mathematical-theory-of-bayesian-
statistics-first-edition-watanabe/

textbookfull.com

Pediatric Allergy A Case Based Collection with MCQs Volume


1 Nima Rezaei

https://textbookfull.com/product/pediatric-allergy-a-case-based-
collection-with-mcqs-volume-1-nima-rezaei/

textbookfull.com
Running Wild LCR Elite Series 4 1st Edition Christy Reece

https://textbookfull.com/product/running-wild-lcr-elite-series-4-1st-
edition-christy-reece/

textbookfull.com
Test Driven
Development
in Ruby
A Practical Introduction to TDD Using
Problem and Solution Domain Analysis

Bala Paranj
Test Driven
Development in Ruby
A Practical Introduction to TDD Using
Problem and Solution Domain Analysis

Bala Paranj
Test Driven Development in Ruby: A Practical Introduction to TDD Using Problem and Solution
Domain Analysis
Bala Paranj
Atlanta, Georgia, USA
ISBN-13 (pbk): 978-1-4842-2637-7 ISBN-13 (electronic): 978-1-4842-2638-4
DOI 10.1007/978-1-4842-2638-4
Library of Congress Control Number: 2017934648
Copyright © 2017 by Bala Paranj
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material
contained herein.
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Technical Reviewer: Ronald Petty
Coordinating Editor: Mark Powers
Copy Editor: April Rondeau
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover image designed by Freepik.
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC
and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM
Finance Inc is a Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit http://www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web
page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book's product page, located at www.apress.com/9781484226377. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
This book is dedicated to the memory of my father.
Contents at a Glance

About the Author��������������������������������������������������������������������������������������������������� xiii


About the Technical Reviewer���������������������������������������������������������������������������������xv
Foreword���������������������������������������������������������������������������������������������������������������xvii
Acknowledgments��������������������������������������������������������������������������������������������������xix
Introduction������������������������������������������������������������������������������������������������������������xxi


■Chapter 1: The Basics�������������������������������������������������������������������������������������������� 1

■Chapter 2: Katas�������������������������������������������������������������������������������������������������� 57

■Chapter 3: Techniques in TDD���������������������������������������������������������������������������� 125

■Chapter 4: Importance of Test Cases����������������������������������������������������������������� 153

■Chapter 5: Character-to-Number Conversion���������������������������������������������������� 163

■Chapter 6: Conway’s Game of Life��������������������������������������������������������������������� 171

■Chapter 7: Gilded Rose�������������������������������������������������������������������������������������� 221

■Chapter 8: Dealing with Third-Party APIs���������������������������������������������������������� 263

■Chapter 9: Pair Ranking������������������������������������������������������������������������������������ 277

Index��������������������������������������������������������������������������������������������������������������������� 285

v
Contents

About the Author��������������������������������������������������������������������������������������������������� xiii


About the Technical Reviewer���������������������������������������������������������������������������������xv
Foreword���������������������������������������������������������������������������������������������������������������xvii
Acknowledgments��������������������������������������������������������������������������������������������������xix
Introduction������������������������������������������������������������������������������������������������������������xxi


■Chapter 1: The Basics�������������������������������������������������������������������������������������������� 1
Terminology���������������������������������������������������������������������������������������������������������������������� 1
Kata�������������������������������������������������������������������������������������������������������������������������������������������������������� 1
Coding Kata�������������������������������������������������������������������������������������������������������������������������������������������� 2
Domain��������������������������������������������������������������������������������������������������������������������������������������������������� 2
Problem Domain������������������������������������������������������������������������������������������������������������������������������������� 2
Solution Domain������������������������������������������������������������������������������������������������������������������������������������� 2
Domain Expert���������������������������������������������������������������������������������������������������������������������������������������� 2
Domain Knowledge��������������������������������������������������������������������������������������������������������������������������������� 3

Defining the Problem Domain and the Solution Domain�������������������������������������������������� 3


Learning TDD ������������������������������������������������������������������������������������������������������������������� 3
Learning by Coding Kata������������������������������������������������������������������������������������������������������������������������� 3
Learning Retrospective��������������������������������������������������������������������������������������������������������������������������� 4
Intent vs. Implementation������������������������������������������������������������������������������������������������ 4
Intent������������������������������������������������������������������������������������������������������������������������������������������������������ 4
Implementation��������������������������������������������������������������������������������������������������������������������������������������� 4
Intent and Implementation��������������������������������������������������������������������������������������������������������������������� 4
Separate Intent from Implementation����������������������������������������������������������������������������������������������������� 7

vii
■ Contents

Tests Are Executable Documentation��������������������������������������������������������������������������������������������������� 11


Shift in Mental State����������������������������������������������������������������������������������������������������������������������������� 11

Overview of TDD������������������������������������������������������������������������������������������������������������� 12
Test Driven Development���������������������������������������������������������������������������������������������������������������������� 12
The Five Steps of TDD��������������������������������������������������������������������������������������������������������������������������� 13
Separation of Intent from Implementation�������������������������������������������������������������������������������������������� 16
Ping Pong Pair Programming Technique���������������������������������������������������������������������������������������������� 16
Black Box Perspective�������������������������������������������������������������������������������������������������������������������������� 16
Shift in System and Mental States������������������������������������������������������������������������������������������������������� 17
Importance of Discipline����������������������������������������������������������������������������������������������������������������������� 18
Overcoming Difficulty �������������������������������������������������������������������������������������������������������������������������� 18

Problem-Solving Skills��������������������������������������������������������������������������������������������������� 18
Four Phases of Problem Solving����������������������������������������������������������������������������������������������������������� 19
Subskills of Test Driven Development�������������������������������������������������������������������������������������������������� 20
Alternative Representations����������������������������������������������������������������������������������������������������������������� 25
Divide and Conquer Strategy���������������������������������������������������������������������������������������������������������������� 26

Designing Test Cases����������������������������������������������������������������������������������������������������� 26


Sum a List of Numbers������������������������������������������������������������������������������������������������������������������������� 26
Sequence of Test Cases������������������������������������������������������������������������������������������������������������������������ 27

Assertion������������������������������������������������������������������������������������������������������������������������ 28
Test Driving Calculator��������������������������������������������������������������������������������������������������� 30
Canonical Test Structure����������������������������������������������������������������������������������������������������������������������� 30
Arrange, Act, Assert������������������������������������������������������������������������������������������������������������������������������ 30
Calculator��������������������������������������������������������������������������������������������������������������������������������������������� 31
Subtraction������������������������������������������������������������������������������������������������������������������������������������������� 34

Common Mistakes���������������������������������������������������������������������������������������������������������� 37
Common Mistakes in the TDD Cycle����������������������������������������������������������������������������������������������������� 37
Base Conversion����������������������������������������������������������������������������������������������������������������������������������� 38
Code Reflecting Test Data��������������������������������������������������������������������������������������������������������������������� 46
Multiple Assertions������������������������������������������������������������������������������������������������������������������������������� 47

viii
■ Contents

Forgetting to Test the Negative Case���������������������������������������������������������������������������������������������������� 50


Not Testing the Boundary Conditions���������������������������������������������������������������������������������������������������� 50
Not Updating the Tests�������������������������������������������������������������������������������������������������������������������������� 50
Implementation-Aware Tests���������������������������������������������������������������������������������������������������������������� 53

Summary������������������������������������������������������������������������������������������������������������������������ 54

■Chapter 2: Katas�������������������������������������������������������������������������������������������������� 57
Fibonacci Sequence������������������������������������������������������������������������������������������������������� 57
Problem Statement������������������������������������������������������������������������������������������������������������������������������� 58
Problem Domain Analysis��������������������������������������������������������������������������������������������������������������������� 58
Solution Domain Analysis��������������������������������������������������������������������������������������������������������������������� 59
Assumptions����������������������������������������������������������������������������������������������������������������������������������������� 62
Test-Driven Fibonacci��������������������������������������������������������������������������������������������������������������������������� 62

Fizz Buzz������������������������������������������������������������������������������������������������������������������������ 71
Problem Statement������������������������������������������������������������������������������������������������������������������������������� 71
Problem Domain Analysis��������������������������������������������������������������������������������������������������������������������� 71
Solution Domain Analysis��������������������������������������������������������������������������������������������������������������������� 72
Test-Driven FizzBuzz���������������������������������������������������������������������������������������������������������������������������� 72
No if-else Constraint����������������������������������������������������������������������������������������������������������������������������� 77
No if Constraint������������������������������������������������������������������������������������������������������������������������������������� 86
Implementation-Independent Tests������������������������������������������������������������������������������������������������������ 88
Crossing the System Boundary������������������������������������������������������������������������������������������������������������ 90
Mocking as a Design Technique����������������������������������������������������������������������������������������������������������� 90
Abstraction Levels in a System������������������������������������������������������������������������������������������������������������ 94
Testing Random Behavior��������������������������������������������������������������������������������������������������������������������� 95
Testing Time-Dependent Behavior�������������������������������������������������������������������������������������������������������� 96
Simulating User Input��������������������������������������������������������������������������������������������������������������������������� 97
Open Closed Principle������������������������������������������������������������������������������������������������������������������������� 100
Difference Reduction�������������������������������������������������������������������������������������������������������������������������� 102
Defect Localization����������������������������������������������������������������������������������������������������������������������������� 104

ix
■ Contents

Stack���������������������������������������������������������������������������������������������������������������������������� 105
Basic Stack����������������������������������������������������������������������������������������������������������������������������������������� 105
Test Precisely and Concretely������������������������������������������������������������������������������������������������������������� 107
Make Your Code Robust���������������������������������������������������������������������������������������������������������������������� 109

The Sieve of Eratosthenes�������������������������������������������������������������������������������������������� 112


Algorithm�������������������������������������������������������������������������������������������������������������������������������������������� 114

Prime Factors��������������������������������������������������������������������������������������������������������������� 119


Problem Statement����������������������������������������������������������������������������������������������������������������������������� 119
Problem Domain Analysis������������������������������������������������������������������������������������������������������������������� 119
Solution Domain Analysis������������������������������������������������������������������������������������������������������������������� 120

Summary���������������������������������������������������������������������������������������������������������������������� 124

■Chapter 3: Techniques in TDD���������������������������������������������������������������������������� 125
Obvious Implementation���������������������������������������������������������������������������������������������� 125
Fake It Till You Make It�������������������������������������������������������������������������������������������������� 127
Triangulation����������������������������������������������������������������������������������������������������������������� 129
Addition���������������������������������������������������������������������������������������������������������������������������������������������� 129
Sum a List of Numbers����������������������������������������������������������������������������������������������������������������������� 130

Solution Domain Analysis Redux���������������������������������������������������������������������������������� 135


Reduction��������������������������������������������������������������������������������������������������������������������� 136
Problem Statement����������������������������������������������������������������������������������������������������������������������������� 137
Steps to Solve the Problem���������������������������������������������������������������������������������������������������������������� 137
Test-Driven GCD���������������������������������������������������������������������������������������������������������������������������������� 139

Transformation Priority Premise����������������������������������������������������������������������������������� 142


Refactorings and Transformations������������������������������������������������������������������������������������������������������ 143
Transformation List����������������������������������������������������������������������������������������������������������������������������� 143
Counter����������������������������������������������������������������������������������������������������������������������������������������������� 144
Factorial���������������������������������������������������������������������������������������������������������������������������������������������� 149

Summary���������������������������������������������������������������������������������������������������������������������� 152

x
■ Contents


■Chapter 4: Importance of Test Cases����������������������������������������������������������������� 153
Problem Statement������������������������������������������������������������������������������������������������������ 153
Problem Domain Analysis��������������������������������������������������������������������������������������������� 153
Solution Domain Analysis��������������������������������������������������������������������������������������������� 153
Algorithm Description��������������������������������������������������������������������������������������������������� 156
Code����������������������������������������������������������������������������������������������������������������������������� 156
Summary���������������������������������������������������������������������������������������������������������������������� 162

■Chapter 5: Character-to-Number Conversion���������������������������������������������������� 163
Problem Statement������������������������������������������������������������������������������������������������������ 163
Discussion������������������������������������������������������������������������������������������������������������������������������������������ 163

Solution Domain Analysis��������������������������������������������������������������������������������������������� 163


Test Cases�������������������������������������������������������������������������������������������������������������������� 165
Summary���������������������������������������������������������������������������������������������������������������������� 169

■Chapter 6: Conway’s Game of Life��������������������������������������������������������������������� 171
Problem Statement������������������������������������������������������������������������������������������������������ 171
Problem Domain Analysis��������������������������������������������������������������������������������������������� 171
Assumptions��������������������������������������������������������������������������������������������������������������������������������������� 176

Solution Domain Analysis��������������������������������������������������������������������������������������������� 176


Moore Neighborhood�������������������������������������������������������������������������������������������������������������������������� 176

Test Cases�������������������������������������������������������������������������������������������������������������������� 177


Test-Driven Game of Life���������������������������������������������������������������������������������������������� 177
Application Statement������������������������������������������������������������������������������������������������������������������������ 209
Refactor Solution�������������������������������������������������������������������������������������������������������������������������������� 210

Code Review����������������������������������������������������������������������������������������������������������������� 214


Summary���������������������������������������������������������������������������������������������������������������������� 220

■Chapter 7: Gilded Rose�������������������������������������������������������������������������������������� 221
Form vs. Structure������������������������������������������������������������������������������������������������������� 221
Problem Statement������������������������������������������������������������������������������������������������������ 222

xi
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
■ Contents

Initial Setup������������������������������������������������������������������������������������������������������������������ 223


Form��������������������������������������������������������������������������������������������������������������������������������������������������� 225
Structure��������������������������������������������������������������������������������������������������������������������������������������������� 226
DeMorgan’s Laws������������������������������������������������������������������������������������������������������������������������������� 244
Making Temporal Dependency Explicit����������������������������������������������������������������������������������������������� 245
Improving the Structure���������������������������������������������������������������������������������������������������������������������� 248
Express Intent������������������������������������������������������������������������������������������������������������������������������������� 249
Tell-Don’t-Ask Principle���������������������������������������������������������������������������������������������������������������������� 252
Inheritance������������������������������������������������������������������������������������������������������������������������������������������ 255

Implementing the New Feature������������������������������������������������������������������������������������ 257


Express Domain Concepts������������������������������������������������������������������������������������������������������������������ 258

Retrospective��������������������������������������������������������������������������������������������������������������� 261
Summary���������������������������������������������������������������������������������������������������������������������� 262

■Chapter 8: Dealing with Third-Party APIs���������������������������������������������������������� 263
Subscription Plan��������������������������������������������������������������������������������������������������������� 263
Create a Plan�������������������������������������������������������������������������������������������������������������������������������������� 263
Delete a Plan�������������������������������������������������������������������������������������������������������������������������������������� 264

Stripe Customer����������������������������������������������������������������������������������������������������������� 264


Subscribe a Customer to a Plan����������������������������������������������������������������������������������� 265
Test-Driven Stripe Library�������������������������������������������������������������������������������������������� 266
Integration Tests��������������������������������������������������������������������������������������������������������������������������������� 266
Unit Tests�������������������������������������������������������������������������������������������������������������������������������������������� 268

Make Your Code Robust����������������������������������������������������������������������������������������������� 274


Summary���������������������������������������������������������������������������������������������������������������������� 276

■Chapter 9: Pair Ranking������������������������������������������������������������������������������������ 277
Problem Domain Analysis��������������������������������������������������������������������������������������������� 277
Solution������������������������������������������������������������������������������������������������������������������������ 278
Summary���������������������������������������������������������������������������������������������������������������������� 284

Index��������������������������������������������������������������������������������������������������������������������� 285

xii
About the Author

Bala Paranj has a master’s degree in electrical engineering from Wichita


State University. He has been working in the software industry since 1996.
He started his career as a technical support engineer and later became a
web developer. He most commonly uses Perl, Java, and Ruby. He has
consulted for companies in the United States, Australia, and Jamaica in
finance, telecommunications, and other domains.
He is the founder of the developer training company rubyplus.com.
He publishes podcasts and screencasts in Ruby. He has been organizing
Ruby, Rails and TDD related events since 2007. He also has developed
several open-source gems that were extracted from his side projects.
He lives in Los Altos Hills, California, with his girlfriend and his dog,
Chico. He enjoys hiking, tennis, and tango. You can buy the TDD in Ruby
course at https://rubyplus.com/sales/new?id=11
You can contact the author with any questions about his book at
feedback@rubyplus.com.

xiii
About the Technical Reviewer

Ronald Petty, M.B.A., M.S. is the founder of Minimum Distance LLC, a


management consulting firm based in San Francisco. He spends his time
helping technology-based startups do the right thing. He is also an
instructor at UC Berkeley Extension.

xv
Foreword

I have known Bala for more than a decade. In all this time, he has consistently been a leader in the Ruby
community in the San Francisco Bay Area. Bala has mentored many Ruby and Rails developers, including
me. He is very approachable and has a very appealing style of conversation.
Case in point, last week I was waiting for him at the Starbucks near Google’s HQ in Mountain View to
get his opinions on a project I am working on. While I waited, I met a fellow coffee enthusiast who, upon
finding out that I work for Intuit (the TurboTax company), asked me for some career advice. Ten minutes
later, Bala walked in. After a quick introduction to what we were talking about, Bala took the lead on the
mentoring. No more than a minute had passed before the protégé said, “Can I please record this?” Yes, Bala
is that impressive!
Bala thinks differently. He is a genius. In fact, when the above conversation was done, the protégé asked
us for our contact information. Whereas I proceeded to enter my name, number, and email into his phone,
Bala said, “Just go to my website and click on download vCard. Boom.”
Bala’s excellence in communicating rather abstract concepts clearly and concisely is very clear in the
way the lessons and examples are laid out in this book. He builds up the examples one step at a time so that
it is crystal clear what he intends you to learn. He uses various techniques to make sure the concepts stick in
your mind.
My two key takeaways from this book are as follows:
1.
Understanding the problem is the most important part of solving the problem.
2.
Test Driven Development does not magically result in a good design. There is
more to design than just reducing duplication in code.
This is an excellent book, and I highly recommend that you read it if you plan to create great software.

—John Varghese
Devops Evangelist at Intuit

xvii
Acknowledgments

My first thanks go to Steve Anglin, who approached me with the idea of writing a Ruby book. As a first-time
author, I was both petrified and excited at the same time. He was very understanding about my situation and
provided me the freedom to choose the topic and timing of the book.
I would like to thank Rhonda Jezek for pairing with me on most of the examples in this book, and Daniel
Pritchard for believing in me and encouraging me to write the book. I also would like to thank the members
of my Silicon Valley Ruby meetup, who provided valuable feedback about the early material in this book at
the bootcamps and tutorials. Thanks to Emily Bache for providing me with very candid technical feedback,
and Randy Coulman for his inspiring presentations and technical review of one of the chapters. I would also
like to thank Mayank Suri for challenging me and pushing me out of my comfort zone in order to accomplish
bigger goals in my career.
This book builds on top of the work of others like Kent Beck, Robert Martin, Eric Evans, Gerard
Meszaros, Michael Feathers, and others mentioned in the book. I cannot thank Ryan Davis enough for
building a simple and elegant testing framework with a gentle learning curve for beginners.
This book would not have been a reality without Steve Anglin, Mark Powers, and Matthew Moodie. I am
deeply indebted to the Apress team for recognizing my talent and providing me with a channel for my voice.
Last but not least, thanks to Ronald Petty for patiently reviewing my book and pointing out the mistakes.
This book is the result of my search for solutions to overcome the difficulties I faced when I started to
learn TDD. It also distills the design knowledge I have acquired over my career. I sincerely hope that this
book shows empathy for beginners and makes it easier for them to learn TDD.

xix
Introduction

Experience is simply the name we give our mistakes.


—Oscar Wilde

This book is the result of my struggle to learn Test Driven Development. I started documenting the
difficulties that I faced and what I did to overcome them. I started sharing my tips with the members of
my Silicon Valley Ruby meetup and on my blog. You will learn both from my mistakes and from other
developers in this book.

Mistakes are the portals of discovery.


—James Joyce

More importantly, if you document your mistakes and start analyzing them, you too will discover new tips
that you can share with others. Mastering TDD is a journey. I hope you enjoy the journey as much as I have
enjoyed writing this book. Good luck.

xxi
CHAPTER 1

The Basics

This chapter will discuss coding kata, basic terminology, overcoming difficulty in TDD, and how to improve
your TDD skills over time. We will look at the distinction between intent and implementation and its role in
coming up with elegant solutions. We will briefly discuss the basics of Test Driven Development (TDD) and
how problem-solving skills fit into TDD. We will also cover designing test cases, assertion, Canonical Test
Structure, and how to avoid common mistakes.

Terminology
This section will introduce you to the basic terminology required to grasp the material in this book. We will
look at basic terms such as kata, coding kata, domain, problem domain, and solution domain.

Kata
Kata is a Japanese word meaning form. In martial arts, it describes a choreographed pattern of movements
used to train yourself to the level of muscle memory. The focus is on making small improvements during
practice. Figure 1-1 shows a choreographed pattern of movements in martial arts.

Figure 1-1. A choreographed pattern of movements

© Bala Paranj 2017 1


B. Paranj, Test Driven Development in Ruby, DOI 10.1007/978-1-4842-2638-4_1
Chapter 1 ■ The Basics

Coding Kata
A coding kata is a short exercise that is thirty minutes to an hour long. It can be coded in many different
ways. It is likely that coding katas have many solutions. The focus is on learning when you work through
them. The goal is to practice in order to improve your skills, not to achieve perfection.

Domain
What comes to mind when you hear terms such as equity, debt, gross margin, and net income? How about
contour, contrast, opacity, and form? Here’s another example: parallel, ordinate, arc, and angle. A domain is
defined as a specific sphere of activity or knowledge. The first example corresponds to finance. The second
example corresponds to visual communication, and the third corresponds to geometry. Figure 1-2 shows
three different domains—finance, visual communication, and math—consisting of abstractions found in
those domains.

Figure 1-2. Examples of domains

Problem Domain
The problem domain refers to real-world things and concepts related to a problem.

Solution Domain
The solution domain refers to real-world things and concepts related to a solution.

Domain Expert
A domain expert is someone with special knowledge or skill in a particular domain. For instance, an
accountant is an expert in the accounting domain. The development of accounting software requires
knowledge in two different domains, namely accounting and software.

2
Chapter 1 ■ The Basics

Domain Knowledge
Domain knowledge is expertise in a particular problem domain. It is a critical ingredient in coming up with
abstractions that create elegant solutions.

Defining the Problem Domain and the Solution Domain


When you read a problem statement in a textual form, you will find concepts in the problem statement.
You can list the concepts you find and group them to come up with the problem domain name. Figure 1-3
illustrates the process of coming up with the problem domain name from a given problem statement.

Figure 1-3. Process of finding the domain name

Let’s now see an example of problem domain and solution domain. Let’s say you have leaking sink
problem in your kitchen. You search on the Internet for the term leaking sink. The phrase leaking sink is
found in the problem domain. Once you read about the solution to this problem, you learn about things
like: clevis screw, stopper rod, clevis, retaining nut and so on. These terms belong to the solution domain.
You watch a video and find out that you need to buy a retaining nut to fix the leak. You now start using this
term found in the solution domain, retaining nut, to find the nearest store carrying this item. So, the term:
retaining nut belongs to the solution domain.

Learning TDD
Learning by Coding Kata
Why coding Kata? Test Driven Development (TDD) is a difficult but learnable skill. So, in order to answer
this question, we need to look at why TDD is difficult.

TDD is not a testing technique. It’s an analysis technique, a design technique, really a
technique for all activities of development.
—Kent Beck, Test Driven Development by Example

3
Chapter 1 ■ The Basics

By using the small but precise nature of the coding kata to practice these skills separately, you can
move past this difficulty and hone your TDD skills. So, coding kata is the best way to learn TDD. How do you
practice a coding kata? You will work through a coding kata by following the five steps of TDD, which will be
discussed in an upcoming section.

Learning Retrospective
After you complete a coding kata using TDD, reflect on the TDD practice session. Ask yourself the following
questions:
• What went well during the TDD session?
• What went wrong during the TDD session?
• What can I do differently next time to improve?

The answers to these questions will tell you where you need to focus your efforts in your next practice
session so as to improve your skills. It’s a good idea to keep a journal that records the coding kata name and
the answers to these questions for each practice session.

Intent vs. Implementation


In this section, we will discuss the intent and implementation, or the specification and implementation.
We will look at examples to illustrate the differences between intent and implementation, why we need to
separate them, and how to separate them in the code.

Intent
The dictionary definition of intent is determined to do something. If you want to travel in your car, your intent
is to drive your car. You don’t reach into the transmission and pull levers to drive. You can drive without
knowing the details of the car engine. You use the visible parts of the car, such as the steering wheel, gas
pedal, brake, and gears, to drive. You can express your intent by using the public interface of the car. In
programming terms, the intent-revealing drive() method is used. The public interface would consist of
things you can do to a car, such as start, stop, drive, turn, and so on.

Implementation
The things under the hood of the car make up the implementation. Only your car mechanic knows about the
details of the car engine. You may be aware of the 3.0 liter V-6 engine, but you have no idea of how it works.
There could be methods that are internal to the class, such as burn_fuel(), that are not part of the public
interface of the car. This means the user does not directly invoke any private methods of the car.

Intent and Implementation


We will now see three examples of intent and implementation, the difference between them, and why we
need to separate them.

4
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Chapter 1 ■ The Basics

Music
Music is composed by music composers. Music composers organize and develop the music to create a
written score that can be interpreted by performers. Performers play the music using different musical
instruments by referring the sheet music. Sheet music is not music. Sheet music is a visual abstraction of
music. To be specific, it is a symbolic abstraction of music that can be read by anyone who knows how to
read music notation. The composers communicate with other musicians across space and time using music
notation. This is the intent, the what, the logical design. Figure 1-4 shows sheet music that communicates
with other musicians.

Figure 1-4. Sheet music is a symbolic abstraction of music

Playing the music with a musical instrument is the implementation, the how, or the physical design.
There are many physical designs for a given logical design. In this example, the same sheet music can be
used to play the music using different musical instruments. Figure 1-5 shows music being performed by a
musical instrument, achieved by interpreting the sheet music.

5
Chapter 1 ■ The Basics

Figure 1-5. Music is played using a musical instrument

Lyrics
A lyricist writes the words for a song by organizing sections into one of the agreed upon structures. This
provides the roadmap for the song. Let’s consider the lyrics for the song “Come Together.” John Lennon
wrote this song. This is the what. The how in this case are the performances by The Beatles, Aerosmith, and
Michael Jackson of the same song, “Come Together.”

Home Plan
An architect gets requirements from the client. A hand-drawn sketch consisting of bubbles, squares,
and rectangles captures the idea of where things are and how they interrelate. This diagram becomes
architecture. Figure 1-6 shows the blueprint of a house that captures the design decisions made by the
architect. This blueprint of a house is the what. It is a visual abstraction of the design. You can build many
houses using the same blueprint.

6
Chapter 1 ■ The Basics

Figure 1-6. Blueprint of a house

Table 1-1 summarizes the intent and implementation of some common, everyday things that you
encounter.

Table 1-1. Intent vs. Implementation

Intent Implementation
Drive a car Internal mechanism of a car that drives
Represent music The process of playing music with a musical instrument
Lyrics of a song The unique way of singing a song by a singer
Plan of a home Houses that conform to the same blueprint but look different

Separate Intent from Implementation


We need to separate the intent from implementation. Why? Because it allows us to change the
implementation without breaking the tests. The intent is the focus of the tests. The implementation is the
focus of the production code. As long as the behavior is the same, the implementation changes should not
break the tests.

7
Chapter 1 ■ The Basics

Sheep Example
So, how do we separate the intent from implementation? We can use Chris Stevenson’s TestDox (https://
en.wikipedia.org/wiki/TestDox). TestDox expresses the subject in the code as part of a sentence. Here is
an example:
• A sheep eats grass.
• A sheep bleats when frightened.
• A sheep produces delicious milk.
• A sheep moves away from sheep dogs.
In this example, the sheep is the subject. Its behavior is expressed in a sentence. This can be
automatically converted to specifications in code. Figure 1-7 shows the behavior of a sheep expressed as
specifications in code.

Figure 1-7. Specifications in ĉode

In this example, we are using the spec style of the Minitest testing framework to describe the behavior of
a sheep. Minitest ships with the Ruby language. When you think about a system from the outside, you focus
on the intent. In this case, what does the sheep do?

Test API Example


For a realistic example, examine Figure 1-8 from the RSpec documentation that shows developers how the
eq() method in RSpec (http://rspec.info) works.

8
Chapter 1 ■ The Basics

Figure 1-8. Specification that shows RSpec API usage example

The example is easy to read and shows how the eq() method can be used to compare two strings that
are equal and not equal. (Source: https://www.relishapp.com/rspec/rspec-expectations/docs/built-
in-matchers/equality-matchers)

Bowling Game Example


Let’s look at an example that uses a bowling game scoring program to illustrate how to discover an
intent-revealing interface.

it 'score is 10 for a strike - knocking down all ten pins' do


  game = BowlingGame.new
  10.times { game.roll(1) }

  assert_equal 10, game.score


end

9
Random documents with unrelated
content Scribd suggests to you:
A gleam of light fell across the threshold.
They could see a slipper lying there—​one that Maude had
dropped.
The burglars had probably perceived this, and thence argued that
people were afoot, for the light disappeared, and they could hear
whisperings outside the door.
The big bedroom, as it was called, was a square chamber, barely
furnished. The two bedsteads had been placed close to the window
on the left-hand side.
Round and about these beds the six besieged persons were
crouched or seated.
The moonlight poured in at the window in such a manner that
while the whole of the opposite side, except one corner, was as light
as day, the little nook by the beds was buried in impenetrable
darkness.
The one dark corner on the opposite side was formed by the
chimney, which jutted out some little way into the room.
They listened breathlessly for some moments, till they fancied
that they heard a board creak inside the room close to the door; and
at that moment, as if by magic, a voice issued from the corner of the
chimney.
“We are armed with loaded revolvers; if you come a step nearer
we fire!”
The lurid flash of a pistol flamed within the room, and they heard
a ball strike sharply against the wall.
Maude betrayed their hiding-place with a shriek, and fell fainting
in her brother John’s arms.
A loud report rang in their ears, and the room was filled with a
thick, sulphurous smoke.
By the light of the powder’s flame when the first shot was fired,
there was one who had seen the robber’s face—​a face, once seen,
not soon to be forgotten. The dark cavernous eyes of the “Badger”
had been distinctly visible to Jane Ryan, who gave a scream of
triumph and revenge.
It was but momentarily that she had caught sight of the
forbidding features of the miscreant; but it was enough for her
purpose.
She levelled her master’s gun at the supposed spot where the
robber was; and as she fired, something fell heavily upon the floor.
A shudder passed over them like a cold wind. They drew their
breath and heard the same whisperings outside the door.
John Ashbrook placed his sister behind himself and his brother.
There was an interval of silence; they began to hope that the
burglars had gone, when presently they perceived something on the
opposite wall.
They watched it with fascinated eyes. It was a small, dark
shadow, creeping towards them along the wall.
It was the shadow of a man’s hand.
Then they heard a harsh, rustling sound, as if something was
being dragged along the floor.
The robbers were taking away the dead body of their comrade.
They did not dare to move, for they knew the burglars were
armed to a far greater extent than they were, and exposure might
prove fatal.
Ten minutes passed thus; ten minutes of frightful suspense to
these farmers—​who were brave but not phlegmatic—​who now
fought men for the first time, and fought them in the dark.
They could not possibly tell how many there were of their
enemies. To fire the only three remaining charges they had would
have been an act of madness; they therefore thought it prudent to
keep these in reserve for the grand or final conflict.
But the worst was over, as far as the Oakfield housebreakers were
concerned.
Presently the eager tramp of men’s feet echoed from the road
before the farm, and a dozen rough voices were heard bawling to
each other.
The besieged party rushed to the window, and saw in the front of
the house one of the village constabulary force, who was
accompanied by a posse of strong-bodied youths of the immediate
neighbourhood. In addition to these there were shepherds armed
with crowbars, stablemen with their pitchforks, bird-keepers with
their rusty fowling-pieces, woodmen with their billhooks, and a tall
relation of Jane Ryan’s with a substantial kitchen poker.
The reports of the gun and pistol in the dead hour of the night
had aroused the whole neighbourhood.
As may be readily imagined, the strong reinforcement at once
dispelled all anxiety or doubt in the minds of the farmer’s household.
Three men were instantly mounted, and started off in the dark to
the three nearest railway stations. The rest were invited into the
kitchen to wait till daybreak.
There had been an unprecedented number of burglaries
committed at several houses in the neighbourhood within the space
of a few months—​hence it was that the rustic population were so
keenly alive when any signal of alarm was given.
To capture the robbers was the wish of everyone assembled at
Oakfield on that eventful night.
With the first streaks of dawn the party congregated in the yard,
and took counsel on the best means of pursuit.
“If they have been carrying a body with them they can’t be very
far off,” said Mr. Jamblin.
“They are lurking about somewhere hard by, I dare say,” said the
police-officer.
“Where’s Jarvis?” cried Will, the carter. “He’d be the boy to find
’em for us. He’d ketch ’em if they burrowed underground like a
rabbit.”
“Would he?” ejaculated the policeman. “He must be a clever
chap.”
“Aye, that he be,” returned another rustic.
“Have you got any more of his sort in this neighbourhood?” asked
the officer.
The rustics made no reply.
“Who is this Jarvis you were speaking of?” inquired John
Ashbrook.
“Jarvis, sir? Why, him as ’listed some years ago, and fought under
Lord Clyde in the Injies. Arter that they sent him to the other Injies,
where the red men be, and they’ve taught him a power of strange
tricks. He came here wi’ us, but he’s got lost since, or summat.”
“No, I baint lost, Joe,” said a tall young man, whose left cheek
was one great red scar, and whose face had been bronzed by no
English sun.
“Why, sure enough, it is Jarvis!” exclaimed Mr. Ashbrook. “Give us
your hand, lad. Sure enough I shouldn’t ha’ known ye, they’ve
knocked ye about so.”
“Aye, that they have, Master Ashbrook,” returned the soldier. “But
tell us, neighbour, what you can about this night’s business.”
“You shall know all I know,” answered the farmer; who thereupon
put the soldier in possession of all the facts with which the reader is
already acquainted.
When he had finished, the soldier said, “I’ll be bound for it that
the body of the dead or wounded man is not very far from here.”
“You think not?”
“Ah! that I do. We came up so soon that they’d have no time to
get far away with that load upon their backs; and most likely they’ve
been forced to hide it in a slovenly way.”
CHAPTER II.

CAPTURE OF THE BRISTOL BADGER—​MURDER


WILL OUT—​CHASE AFTER CHARLES PEACE—​
HIS MYSTERIOUS ESCAPE.

The sudden disappearance of Charles Peace and his two


companions upon the arrival of the villagers excited surprise in the
minds of all who had assembled at the farmhouse. The police officer
did not choose to commit himself by any expression of opinion. He
was not a man given to loquacity where silence was requisite. He did
not, however, attempt to deny the assertion made by soldier Jarvis—​
namely, that the robbers were not far off.
Enjoining the villagers to stay where they were and to carefully
avoid treading over more ground than was absolutely necessary, the
young soldier accompanied Mr. Ashbrook to the kitchen window,
where the entrance had been forced by removing the glass with a
diamond—​or “starring the glaze,” as it is termed in the burglar’s
phraseology—​and after this had been done panelling the shutter. It
was this last process that aroused Jane Ryan to a sense of danger.
Jarvis carefully examined the ground beneath the window, and
pointed to some footprints in the wet earth which led towards the
straw yard. In one place they were so plain that every nail in the
soles could be distinguished.
“They are the impressions of a strange foot—​that’s certain sure,”
observed Ashbrook.
“We are on the trail of one of them,” returned Jarvis. “I dare say
they thought they could do as they liked among yokels, but we’ve
got the trail and I mean to keep it.”
The speaker walked slowly across the yard, following the tracks
with his eye as a bloodhound would have followed them with his
nose.
“They’re in this barn, Master Ashbrook,” he said, stopping before
one of the doors. “No, they baint, though, they’re come out agen
and gone along the wall. But they’ve left their dead mate behind
’em. See how different their track is now; they tramples quite close
alongside of each other, while afore they carried the body from
shoulder to shoulder, and so were forced to walk one behind and a
little way apart.”
The villagers gave a murmur of astonishment.
“Ah, he knows how many blue beans make five,” said the carter,
as he took out the peg by which the folding doors were kept dosed.
“I don’t feel quite so sure about the footsteps,” remarked the
policeman; “they don’t appear to me to tally with the others.”
“If I’m mistaken, we shall have to try back,” answered Jarvis. “Of
course, it is just possible we are on a false scent. Ah! what is this?”
The speaker pointed significantly to some drops of blood upon the
straw in front of the barn. “What say you to that?”
“Blood, without a doubt,” observed the constable.
“That’s where they laid him down when they opened the barn
door.”
“Ah!—​dare say—​most likely.”
The villagers were open-mouthed with wonder. They, one and all,
voted the soldier a necromancer.
The doors were flung wide open, and they sprang over the rack
into the body of the barn. There had been some threshing done the
day before, and there was a vast heap of chaff just outside.
While they were gazing around, a low moan, as of one in pain,
fell upon their ears.
“Keep quiet, lads,” exclaimed Jarvis; “leave this matter to me and
the constable. Keep where you are. We can none of us tell what next
will happen.”
“Here’s footmarks on the chaff, and blood on it also,” said the
constable, who took a few steps further inside, whereupon his eyes
lighted on the prostrate figure of a man lying in the corner on a
heap of straw.
He flashed his bull’s-eye on the face of the wounded burglar, and
uttered an exclamation of surprise.
The Bristol Badger lay helpless, and bathed in blood.
Jane Ryan, who had followed the constable and Jarvis, gave a
slight scream.
“Don’t take on so, woman,” said the constable. “He’s only got his
deserts.”
Heedless of this observation, Jane went close to the wounded
burglar and peered into his face.
“Dost know who this here is? I’ll tell ye!” she exclaimed, in a voice
of concentrated rage; “he’s the murderer of my sweetheart. I should
ha’ known him out o’ ten thousand.”
There was a murmur of unmixed surprise at this observation.
“What beest thee saying, Jane?” said the farmer, scratching his
head. “Hast ever seen ’im afore?”
“Aye, sure enough I have, master. It was not for nothing that I
sat up this night. I knew summut was about to happen, but never
guessed it would turn out like this.”
Gregson endeavoured to rise to his feet, but the attempt was a
futile one; he was too weak from loss of blood.
“What has that false, wicked woman been saying?” he inquired of
the policeman.
“She accuses you of murder,” was the brief rejoinder.
“She’s mad. I never saw her before.”
“What’s to be done wi’ this man?” inquired the farmer of the
constable.
“He’s my prisoner, anyway,” answered the latter. “Best see and
have his wounds attended to, and then we will take him to the lock-
up. You charge him, I suppose?”
“Yes, with burglary.”
“Attempted burglary,” chimed in the cracksman.
“And I charge him with wilful murder!” exclaimed Jane Ryan.
Having said this, she folded her arms upon her breast and
relapsed into gloomy silence. There she stood, colossal as an
Amazon, in her sublime strength, beautiful as a Judith in her just
and fearful vengeance.
A hurdle was brought by some of the villagers, and upon this the
ill-fated Badger was placed; he was then carried into the farmhouse,
not, however, before the constable had taken the precaution to
handcuff him, for he was known to that astute officer as a ruffian of
no common order. He was, however, run to earth, having been, in a
manner of speaking, hunted down by a woman.
A doctor was sent for, who bandaged his wound, which, although
severe, was not likely to prove mortal—​certainly not unless some
unfavourable symptons set in.
While all this clatter had been going on, Charles Peace had
contrived to conceal himself in a neighbouring coppice, from which
he durst not emerge while the village folk were prowling about.
When Gregson was conveyed into the house the majority of the
villagers wheeled off; at the same time Jarvis, however, was still
No. 2. endeavouring to trace out the Badger’s companions. He came
too near to the coppice where Peace was concealed to be at all
pleasant to a gentleman of his retiring habits, so Peace was fain to
avail himself of a neighbouring hedge, on the other side of which he
crept along on all fours.
THE “BRISTOL BADGER” SHOT BY JANE RYAN.

Having gone some considerable distance by this means of


progressive, he imagined that he was out of sight, and betook
himself to the open field, across which he ran at the top of his
speed. His movements were however not unobserved by Jarvis.
The latter caught Mr. John Ashbrook by the leg. The farmer was
mounted on his bay mare, and said: “There goes one of them; ride
down the lane and intercept his flight, while I run across the field.
We shall have him yet.”
The farmer needed no second bidding. He rode at the hedge
which skirted the lane. With one stroke from the long corded whip,
and one cry from the rider’s lips, the gallant animal bounded over
the hedge like a flying deer.
“Wouldn’t ’a brushed a fly off the top twig,” exclaimed Ashbrook,
triumphantly. “Now, for my gentleman. Dall it, if this won’t turn an
eventful night, especially if I catch that rascal.”
While the farmer was riding down the lane, Jarvis and several
others were in hot pursuit of the fugitive.
Peace became aware, much to his discomfiture, that every
movement he made was plainly visible to his pursuers, and he
deeply regretted having taken to the open field.
He ran his hardest, and had the satisfaction of getting into the
lane before any of the pursuing party had even reached the field.
Ashbrook, as he was trotting down the lane, saw the fugitive
jump through a gap in the hedge. The farmer urged on his steed,
being now under the full impression that the capture of Peace was
reduced to a certainty.
In a brief space of time he came within a hundred yards of the
enemy.
“I’ve got him now!” exclaimed the farmer. “He’s mine as sure as
my name’s Jack Ashbrook.”
But there’s an old adage “that it is as well not to reckon your
chickens before they are hatched.”
Peace was in imminent danger, but he was an astute, cunning
rascal, who was up to every feint and dodge in all cases of
emergency. He, nevertheless, was fully impressed with the fact that
matters were growing serious—​much too serious to be pleasant. He
turned round and boldly faced the horseman.
Drawing a revolver from his pocket, he watched till Ashbrook
came within range of the shot, then he fired. At this time he could
not have been more than twenty paces from the horse and its rider.
A bullet was lodged in Mr. Ashbrook’s right shoulder. The wound
was not a very serious one certainly—​not enough to place the
farmer hors de combat, but the effects of the shot proved more
disastrous in another way.
The mare, who was a high-spirited animal, became restive from
the pistol’s flash. She reared, then stumbled, and threw her rider
heavily to the ground. Peace rushed forward and struck Ashbrook
two blows on the head, which produced insensibility.
He then made for the mare’s head. Turning her sharply round, he
led her some paces from the scene of action. He patted her on the
neck, and strove as best he could to overcome the effects of the
fright caused by the flash of his weapon. The mare became
comparatively quiet and tractable.
Peace jumped on her back, and rode off at headlong speed.
While all this had been taking place in the lane the mob of
persons in the field had increased considerably in numbers; but the
foremost of them were a long way from that part of the lane where
the short but decisive struggle had taken place.
Two other horses had been brought out from the stables at
Oakfield, but some time necessarily elapsed before they could be
saddled; and when Mr. Cheadle and Mr. Jamblin mounted them for
the purpose of giving chase, Peace was so far ahead that the
chances were remote of finding him.
He knew the bye-roads of the neighbourhood perfectly well, and
took very excellent care to choose a circuitous route. As he was
riding along he listened every now and then to ascertain if there
were any sounds of horses’ hoofs to be heard, but none were as yet
audible. He felicitated himself upon this fact, arguing therefrom that
his pursuers had gone another road.
“I shall give them the double; they are on the wrong scent,” he
ejaculated, in a tone of satisfaction; “but even when the worst
comes to the worst all that will be left for me will be to make a stout
fight of it.”
He had unlimited faith in his own power, skill, and address in
confronting and overcoming difficulties; and his confidence did not
desert him on this occasion.
Presently he came to three cross roads, and was hesitating which
to take—​calculating the while the chances of detection with his
accustomed coolness.
While thus engaged he descried a mounted patrol on a
formidable-looking horse, coming at a measured pace towards him.
To turn and fly was his first impulse, but upon second
consideration he thought it better to put a bold face on the matter.
The mounted policeman came forward, and regarded him with a
look of doubt and mistrust.
“Good morning, friend,” said Peace, in a cheery tone. “Better
weather than it was a few hours ago.”
“Yes,” returned the other. “Where might you be journeying to this
early hour?”
The speaker regarded him with a searching glance. This, however,
did not in any way discompose Peace, who, throughout his career,
plumed himself upon being able to throw dust in the eyes of the
constabulary.
“Ah, I’m sorry to say my errand, or rather the cause of it, is one
of a painful nature. A poor gentleman is at death’s door, and I have
been sent off for the doctor. In cases of this sort minutes are
precious. Let me see, yonder’s the nearest way to Hull, isn’t it?”
“Yes, the right-hand one. But who is in such extreme danger?”
“A farmer—​Mr. Ashbrook. Poor fellow, it is a chance if he recovers,
so they seem to think.”
“I know both the Mr. Ashbrooks perfectly well. Which one is it
that’s so bad; they were right enough yesterday?”
“Mr. John Ashbrook.”
“Umph! that’s strange. What’s the matter with Mr. John?”
“He was thrown—​horse reared and fell upon him. His injuries are
very serious.”
“I’m sorry to hear this, but—” and here the speaker regarded
Peace with a still more searching look, “it’s his horse you are riding.”
“Yes, that’s right enough; it is.”
“Then who are you going for?”
“Dr. Gardiner.”
While this conversation was taking place, Peace had so distorted
his features that recognition was almost impossible. He was an
adept at this. By constant practice he was enabled to throw out his
under jaw, lift up his eyebrows, and so alter the expression of his
features that he defied detection. This is now pretty generally
acknowledged.
“Well, I must not let anyone detain me very long in a case like
this,” he observed, carelessly. “So farewell for the present.”
The patrol made no reply. He did not, however, feel quite satisfied
that all was quite right; at the same time he did not consider it his
duty to offer any obstacle to Peace’s passage along the road, which
led directly to the town of Hull.
Peace trotted along till the patrol was lost to sight, then he pulled
the bridal rein of the mare, and turned her into a narrow lane which
ran at right angles with the road.
“That fellow suspects something,” he murmured; “and for two
pins he would have collared me there and then. The sooner I part
company with the mare the better it will be for both of us, I’m
thinking.”
He dismounted, opened a gate which was at the corner of a
meadow, and led the mare into the field; then he took off the saddle
and bridle, which he threw into a ditch, gave the animal a sharp
crack with his whip, shut the gate, and left her to herself. This done,
he proceeded merrily along on foot.
Messrs. Cheadle and Jamblin had meanwhile been riding to their
hearts’ content, but they did not catch the most distant glance of the
man of whom they were in search. No wonder, seeing that they had
lost all traces of the fugitive, and had been journeying in an
opposite, or very nearly in an opposite direction to the one taken by
Peace. They had, therefore, the gratification of riding many miles
upon a bootless errand.
They returned, vexed and dispirited, to Oakfield House, where
they found John Ashbrook in bed, with his sister and the village
surgeon in close attendance upon him.
The latter had extracted the bullet, and strapped up the head of
the sufferer, who was, he said, doing as well as could be expected.
Certainly there was no immediate danger.
The farmer had an unimpaired constitution, and, although sadly
bruised and knocked about, would in all probability soon get the
better of his wounds.
Peace, when he came to the end of the lane, turned into a road,
where stood a small beerhouse, of a primitive character, with a good
dry skittle-ground at the back.
He knocked several times at the side door of this establishment,
but received no answer to his repeated summonses. It was evident
that all were asleep within.
He called the landlord by name, with no better result. While thus
engaged, a man came forward from the opposite side of the road,
and said—
“Why, what’s up now, Charley? Want to get in?”
Peace turned round in some alarm, but was a little reassured
upon finding the speaker was a friend of his.
“Hang it! I’m as tired as a dog, and wanted an hour or two’s rest,”
said Peace.
“Tired! where have you been to?”
“Playing the fiddle to a party some miles away from here. They
could not accommodate me with a shake-down, so I’ve had to
trudge it.”
“Come along wi’ me, my lad,” said the good-natured groom. “You
shall have an hour or two’s rest in my little crib over the stable.”
Peace gladly availed himself of his friend’s offer.
A hue and cry would be raised throughout the neighbourhood of
the attempted burglary at Oakfield House and the surrounding
districts, and Peace, young as he was at this time—​he had only just
turned twenty—​was fully impressed with the necessity of using
caution.
No one would dream of his being in the groom’s sleeping
apartment. The latter informed him that he had to take the carriage
up to London, and that he should not return from the metropolis for
several days.
“But that aint of no kind o’ consequence,” said the groom. “You
can sleep away to your heart’s content, only when you do leave
mind and lock the door. You can give the key to the stable-boy.”
“I’m sure I do not know how to sufficiently thank you, Jim,”
observed Peace in his blandest tone and manner.
There’s no call for thanks, lad. You’ve done me a good turn afore
now, and one good turn deserves another.”
Peace was conducted by his companion into the small sleeping
chamber.
“There you are,” said the latter, pointing to the bed. “In less than
half an hour I shall be at the station; make yourself comfortable. We
shan’t meet again for some days, that’s quite certain, and so good-
bye for the present.”
“Good-bye, Jim, and many thanks.”
Then, as the man was about to pass out, Peace said, quietly—
“Oh, by the way, there is no occasion for you to say you have
seen me, or, indeed, I’ve been here. It’s a little private matter I’ve
been about. You understand.”
“A nod’s as good as a wink to a blind horse,” returned the other,
with a chuckle. “No one will know anything from me, Charlie.”
After the departure of his friend, Peace was too disturbed in his
mind to sleep. He watched from the little window of his dormitory
the carriage and pair, driven towards the railway station by his
friend, the groom.
When the vehicle was lost to sight, he walked towards the door,
took the key out of the lock, and fastened the door on the inside. In
a few minutes after this he stretched himself on the bed, and sank
into a deep sleep—​the village clock had struck eleven before he
awoke.
He now began to consider his course of action; he felt perfectly
secure from observation in his present quarters. No one would for a
moment imagine that he was safely ensconced in one of the
apartments of the stables adjoining a gentleman’s house.
He thought it best to watch and wait; it would not do to be too
precipitate; in the dusk of the evening he might creep out and get
clear off.
He found in the groom’s bedroom some bread and cold meat,
which served him for a meal, and he prepared himself to pass the
lonely hours as best he could. The day wore on tediously enough,
but the longest day must have an end. And when the grey mists of
evening began to encircle the objects seen before so distinctly from
the window, Charles Peace prepared to take his departure.
He disguised himself in so complete a manner as to almost defy
detection. He made himself up as a hawker. He took the precaution
to always carry a hawker’s licence, made out in a fictitious name; the
licence itself, however, was genuine enough.
He heard, as he descended the creaking stairs, the boy whistling
in the stable. Agreeably to the directions he had received, he handed
the key to the lad, at the same time dropping a shilling in his hand.
The lad stared with astonishment, which was not unmixed with
alarm.
A few words from Peace soon reassured him.
“But ye’ve been ’nation quiet all the day though,” said the lad,
with a broad grin.
“People generally are quiet when they are asleep, my lad,” was
the ready rejoinder.
“Ugh! ’spose so.”
Peace did not want to have further parley. His purpose was
served, and he therefore proceeded on his journey.
CHAPTER III.

COMMITTAL OF GREGSON—​JANE TELLS A


TERRIBLE TALE—​BROXWELL GAOL.

The most celebrated cracksman of his day, Ned Gregson, alias the
Bristol Badger, was certainly the least fortunate of the three ruffians
who contrived to effect an entrance into Oakfield House. He was run
to earth. After he had been carried on the hurdle into the farmhouse
the village surgeon made a superficial examination of his wound,
which was of a fearful nature; the whole of the charge from the gun
fired by Jane Ryan had entered the burglar’s chest, and the loss of
blood was enormous. The only wonder was, that Gregson had not
been killed outright; but he was not the sort of man to be so easily
disposed of. As far as physical strength was concerned he was a
perfect giant; this he had proved on many occasions.
He was more than double the age of Peace, with three times his
strength. Nevertheless, as far as the guilty and lawless lives of the
two men were concerned, there was not much difference between
them; they were both criminals of the worst type, their whole career
being one of profligacy and crime.
Gregson was taken away to the lock-up in charge of the
constabulary, who procured an ambulance from the hospital. The
divisional surgeon was sent for; every care was taken of the
prisoner; and all that skill and attention could do to preserve so
valuable a life as the burglar’s was, as is usual in such cases, not
wanting.
When sufficiently recovered Gregson was examined before the
stipendiary magistrates. The facts deposed to were plain enough,
and the prisoner was committed for trial upon two distinct charges—​
namely, murder and burglary.
Mr. John Ashbrook had by this time sufficiently regained his
strength to leave his room and look after his farming stock, but he
was not as yet up to his usual form.
“This extraordinary charge of murder,” said the farmer to Jane,
one afternoon, as he reclined upon the sofa in the front parlour, “it
seems just like a romance. Strange that you should have recognised
the ruffian by the pistol’s flash on that eventful night.”
“I should have known him out of ten thousand. His face was as
familiar to me as if I had seen him but yesterday.”
“Tell us all about it, Jane.”
“Well, master, it’s a sad and sorrowful tale, which I have kept
locked up in my own breast for ever so long, but it is but right you
should know all about it.”
“Right lass, right you are; go on. What made you imagine that the
house was likely to be attacked? You asked me to load the two other
guns.”
“I did, because I felt assured that danger was at hand.”
“Why so?”
“I had a dream—​twice I dreamt the same thing—​and then I went
over to Mother Crowther and consulted her. She can read the future
—​being—​being a wise woman.”
“She is a wise woman indeed if she can do that,” remarked the
farmer, with a smile; “what did she say?”
“She consulted a book, cast my nativity, and told me that in less
than three days I should see here or hereabouts the murderer of
James Hopgood.”
“And who might he be?”
“He is dead now; he was my sweetheart,” answered Jane,
hanging down her head.
“Oh, your sweetheart—​eh?”
“Yes, before I came here I lived at Squire Gordon’s. A kinder
master never lived. James Hopgood was a carpenter by trade; he
had been doing some work for the squire—​building some outhouses,
and while the work was going on he slept in the house.”
“How long ago was this?”
“Aye, it must be nearly six years.”
“You’ve been here over four.”
“That’s true. Indeed, it must be more than six years. I cannot say
to a certainty; but they’ve got the date—​the pleece have.”
“No matter, that’s quite near enough—​six years or a little more.
What happened then?”
“I will describe all to you, just as it occurred. James Hopgood was
in the kitchen; he and Mary, my fellow-servant, were having supper
together. I was in the back kitchen, when all of a sudden we heard a
scuffle in the passage, and my master cried, ‘Murder!’ James rushed
past me, and flew up the kitchen stairs. Then we heard a heavy fall
in the passage; this was followed by some low moans. I went up to
see what was the matter, and found my master stretched on the
floor of the passage, with blood flowing from a wound in his left
temple. I endeavoured to raise him, but was unable to do so. He
was a stout, heavy man, and I had not strength enough to lift him.”
“Was he killed?”
“No—​oh, dear, no; he recovered afterwards. But the worst
remains to be told. Oh, master, these be tears that are a flowin’ from
my eyes. I can see it all now, as if it occurred but yesterday.”
“Yes, your master, the squire, you found him senseless. There’s
no hurry, girl, take your time—​don’t flurry yourself.”
“While I was looking at my poor master, I caught sight of James
Hopgood and the burglar—​him as I shot down in the big bedroom.
James had closed with the ruffian, who, as far as I could judge, was
striving to shake James off; but he was not able to do this so easily;
they wrestled like two serpents. I felt sick and faint; but,
notwithstanding, I had sufficient strength left to hasten to young
Hopgood’s assistance. I saw the flash of an open knife in the pale
moonlight, saw the gleaming of the desperate wretch’s eyes, and in
another moment the knife was buried up to the hilt in James’s
breast. He fell with a deep groan, and never stirred hand or foot
afterwards.
“I rushed forward, and caught his murderer by the handkerchief
which encircled his throat. After this I lost all consciousness. When I
came to I found myself on the wet grass of the lawn—​the ruffian’s
handkerchief was firmly grasped in my right hand.”
“Why, Jane, my girl, this is indeed a horrible story, and have you
kept this all to yourself for these last six years?”
“Indeed I have; but, waking or sleeping, one burning thought has
been in my brain. It is this—​to avenge the death of my dear and
true-hearted James.”
The farmer was bewildered—​partly dazed by the fearful tale he
had been listening to. He turned his eyes towards his sister, who had
crept into the room to listen to the appalling narrative.
“Did you know of this?” inquired Ashbrook.
“I knew a shocking affair of some sort took place at Squire
Gordon’s when Jane was there, but I never knew till now its precise
nature. I understood that some young man was murdered—​that is
all. How and by whom I was never told.”
“And was the man never discovered? An attempt was made to
find him, I s’pose?” asked the farmer of his servant.
“Government offered a reward of a hundred pounds; a description
of the man was printed on handbills, which were sent, so they said,
to every police-station.”
“With what result?”
“With none, except the arrest of a poor harmless fellow, who
never set foot in the squire’s house, and who had no more to do
with the crime than you or I have.”
“And the handkerchief?”
“That I have kept. The knife also with which the murder was
committed was picked up on the lawn; that, too, I have preserved.
They are both now in the possession of the pleece. Ah! we shall
bring it home to the deep-dyed villain. I felt certain that, sooner or
later, he would be caught, the murderin’ thief.”
“What became of the squire?”
“He left England for good, and settled in Brittany. He has a
daughter who is married there.”
“Is he still alive?”
“I believe so. I never heard of his death—​oh! I’m pretty sure he’s
alive.”
“Do you think he could identify the man?”
“He told me after his recovery that he saw his features distinctly,
and that he would be able to swear to him. It appeared that
Gregson was making his escape from the house with the things he
had stolen, when he was suddenly and unexpectedly confronted by
the squire, who had come over the fields, crossed the lawn, and
entered by the back door of his residence.”
“We’ve all of us had a narrow escape,” said Maud Ashbrook, “and
it will be a warning to us for the future.”
“I’m glad Jane shot the fellow down,” observed the farmer. “She’s
a true-hearted, brave girl—​not, mind ye, but it would ha’ bin better
for him to have fallen by the hands of one of us men.”
“No, master, no,” cried Jane, in a deprecating tone. “I am the
most deeply injured, sick and sore of heart—​I who have sworn to
devote the remainder of my life to discover the slayer of James
Hopgood—​I was the most fitting person to hunt him down. It has
been done, and he will not escape now.”
Jane had given her evidence before the stipendiary magistrates in
the clearest and most lucid manner. She swore positively to the
prisoner Gregson, whose features she declared had not changed
since she saw them so distinctly on that fatal night. Her fellow-
servant also identified the prisoner, whom she saw, so she averred,
through the back-parlour window at the time Jane had hold of him
by the handkerchief.
He was also recognised by several of the police as a well-known
burglar, who had been convicted several times.
Gregson, who was about as hardened a ruffian as it was possible
to conceive, knew and felt that his game was up; nevertheless he
clung to the hope, as most criminals do of his class, that he might
escape the last dread sentence of the law—​perhaps his life might be
spared.
He was taken to Broxwell Gaol; his custodians conducted him
through the lodge, then he passed through a square with a green
plot of grass in the middle, encircled by a gravel walk.
It was like a college quadrangle.
Gregson looked at the grass and the turnkeys who came out to
meet him. He was conducted up a flight of stone steps, and one of
the turnkeys who had joined him and the constabulary who had him
under their charge tapped at a thick oak door, which was covered
with iron nails and secured with a gigantic lock.
They were admitted immediately into a little room, which was
almost entirely filled by a clerks’ desk and stool.
Upon this stool was seated an old man, with a pair of iron-rimmed
spectacles on his nose, making entries in an account-book.
The turnkey who had opened the door to them now closed it with
an ominous sound.
The key clanked loudly in the lock.
The Bristol Badger was in prison.
The turnkey unlocked another door and disappeared. In a few
moments he returned, dismissed the constable, and ordered the
prisoner to follow him.
They entered a snow-white corridor, which was lined with iron
doors, and above with galleries, also of iron, bright and polished.
Gregson was placed in a cell, for some time in the company of a
single turnkey, who stood by him, rigid and voiceless as a statue,
watchful as a lynx.
The “cracksman” assumed an air of dejection, and kept his eyes
fixed upon the ground.
He had only partially recovered from his wound. From this a vast
number of shots had been extracted; but several more, it was
thought, still remained in the flesh.
The burning pain in his chest had not entirely left him, although it
was not nearly so insupportable as at first.
Presently the door of the cell opened, and a gentleman in plain
clothes came in. He had a ruddy complexion, with a brown
moustache and beard.
Gregson recognised him immediately. He was the governor.
The recognition was mutual.
“So,” he said, “you have come here again?”
“They’ve brought me here,” muttered the cracksman.
“Precisely. Of course you know the usual forms prescribed by the
authorities. We must put you through the ordeal of a warm bath.
“Turn on the tap, Wilson,” said the governor; and in a few
minutes the bath was filled with hot water.
They took off his handcuffs, and then stood by him as he
undressed.
“Do you know, governor, that I’ve been wounded—​well-nigh to
death? It’s too bad to put a cove in hot water in my state.”
“Wounded, eh? We’ll send for the doctor.”
The prison surgeon was brought into the room.
He glanced at the wound, which still presented an angry
appearance.
“The bath won’t hurt you. There is no necessity for you to
immerse your chest or shoulders in the water. In my opinion you will
be better for it.”
“All right,” returned Gregson; “you shall have your way. I’m not
one to make things disagreeable.”
And forthwith he jumped in.
The governor paced the corridor for the next ten minutes or so
under pretence of superintending the arrangements of the prisoners’
dinners, which ascended from the kitchen on a great tray by means
of mysterious machinery.
On his return he called another turnkey, and ordered him to have
the prisoner’s clothes brushed and cleaned.
“You have got plenty of money,” he said to Gregson. “You are
suffering from a severe wound. We don’t wish to deal harshly with
you.”
“I’m much obliged, I’m sure,” returned the Badger.
The governor took no notice of this last observation, which, to say
the truth, was half conciliatory and half sarcastic.
“We will therefore allow you to wear your own clothes, and to
procure your own meals from an eating-house if you prefer it.”
“Yes, I do prefer it, if it makes no difference.”
“So be it, then. You will see by the printed copy of the rules which
is hung up in every cell that you are not allowed more than one pint
of wine or one quart of malt liquor daily, and that, if you undertake
to board yourself, you must do so altogether. Besides this you will be
allowed books to read and paper to write upon, and other little
comforts, under my supervision, as I have no desire to treat you
with unnecessary severity during the brief period that will elapse
while you are awaiting your trial. I hope you will conduct yourself in
a proper and becoming manner.”
The cracksman nodded, and seemed by his demeanour to
appreciate the lenity which the governor displayed.
“You are very good, sir, I’m sure,” he muttered. “I wish all
gentlemen in your position were equally kind and merciful.”
The governor bowed in a dignified manner, and then left the cell.
The turnkey returned with Gregson’s clothes, and stood by him as he
dressed. He was then conducted to Cell No. 15.
There they showed him how to ring the bell, how to pull the slide
from the grating when he wanted fresh air, and how to manage the
water taps and the bed furniture.
They also informed him that when he wanted anything from the
town there was a prison servant attached to the establishment
whose office it was to run errands for the prisoners who were
waiting for trial.
The turnkeys made these explanations with a courteous accent,
for turnkeys have a sort of veneration for great criminals.
And Ned Gregson, in this respect, was a man of mark.
The prison officials went out of the cell backwards, as if they were
retiring from a royal presence; they locked the door with an
ostentatious noise that they might thereby strike a wholesome awe
into the mind of their prisoner.
Gregson sat himself down upon the wooden stool in his cell
without moving.
The bitterness of his thoughts it would not be so easy to describe.
He remembered with harrowing distinctness the most remote
incident of the night upon which the ill-fated James Hopgood fell
beneath the fatal blow.
“And that cursed woman!” ejaculated the Badger. “Who would
have dreamt of her being an inmate of the farmhouse? And the oily-
tongued Peace, he has got clear off, I’ll dare be sworn; and the
chances are that he is now playing that old fiddle of his—​whilst I—​I
—”
Here he uttered an impious oath, and then lapsed into silence
again.
He sat for two hours a prey to the most agonising thoughts.
At the expiration of that time he uttered curses loud and deep. He
ran frantically round his narrow cell.
One of the turnkeys opened the door, and told him that he must
make less noise. There was a punishment for making an outcry of
that nature, and he pointed to one of the printed rules.
The “cracksman” answered with a howl of rage, and squatted
abjectly on his stone floor.
The turnkey, who was pretty well used to scenes of this nature,
and who, therefore, made due allowance, repeated his warning and
shut the door.
Soon after this the prison servant brought a wooden tray in.
There were two dishes, each surrounded by a pewter cover. One
contained three slices of roast mutton, floating in lukewarm gravy;
the other contained four good-sized potatoes.
Gregson, who was still on the floor, looked at them supinely.
“Governor! thought you would like a little dinner,” said the man
kindly; and he propped up a slab which was hanging from the wall,
placed the tray on it, reached down a salt dish from a shelf in the
corner, where it had grown dusty, in company with a bible and two
hymn books.
“Will you take beer or wine?”
“I want wine,” said the Badger, sulkily.
“Very good, I will bring you a pint; it’s against the rules to have
any more.”

You might also like