% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
  ignorenonframetext,
]{beamer}
\usepackage{pgfpages}
\setbeamertemplate{caption}[numbered]
\setbeamertemplate{caption label separator}{: }
\setbeamercolor{caption name}{fg=normal text.fg}
\beamertemplatenavigationsymbolsempty
% Prevent slide breaks in the middle of a paragraph
\widowpenalties 1 10000
\raggedbottom
\setbeamertemplate{part page}{
  \centering
  \begin{beamercolorbox}[sep=16pt,center]{part title}
    \usebeamerfont{part title}\insertpart\par
  \end{beamercolorbox}
}
\setbeamertemplate{section page}{
  \centering
  \begin{beamercolorbox}[sep=12pt,center]{part title}
    \usebeamerfont{section title}\insertsection\par
  \end{beamercolorbox}
}
\setbeamertemplate{subsection page}{
  \centering
  \begin{beamercolorbox}[sep=8pt,center]{part title}
    \usebeamerfont{subsection title}\insertsubsection\par
  \end{beamercolorbox}
}
\AtBeginPart{
  \frame{\partpage}
}
\AtBeginSection{
  \ifbibliography
  \else
    \frame{\sectionpage}
  \fi
}
\AtBeginSubsection{
  \frame{\subsectionpage}
}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  pdftitle={Testing (or how I learned to stop worrying and love specification)},
  pdfauthor={David Nutter},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\newif\ifbibliography
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\usetheme[nosnake=true, nogradient=true]{BioSS}

\title{Testing (or how I learned to stop worrying and love
specification)}
\author{David Nutter}
\date{May 28th 2020}

\begin{document}
\frame{\titlepage}

\begin{frame}{Why test}
\protect\hypertarget{why-test}{}
So far, we've been testing to confirm things:

\begin{itemize}
\tightlist
\item
  Functionality (the code does what I expect)
\item
  Regressions (the change I made has not broken anything)
\item
  Bugfixes (the code no longer exhibits a bug)
\end{itemize}

There is another reason
\end{frame}

\begin{frame}{Specification}
\protect\hypertarget{specification}{}
A specification defines

\begin{itemize}
\tightlist
\item
  What your software should do
\item
  How it should do it (if you want to get really thorough)
\end{itemize}

. . . Usually writing this is \textbf{BORING!}

Even more so than writing unit tests!

\pause

And the tools are \textbf{HORRIBLE!}

Let me show you just how bad it can be\ldots{}
\end{frame}

\begin{frame}{Flashback}
\protect\hypertarget{flashback}{}
To my undergrad years - behold:

\begin{figure}
\centering
\includegraphics{zed-birthday-book-sample.png}
\caption{(Spivey, J.M. and Abrial, J.R., 1992. The Z notation. Hemel
Hempstead: Prentice Hall.)}
\end{figure}
\end{frame}

\begin{frame}{Wossat then?}
\protect\hypertarget{wossat-then}{}
That's Z notation.

\begin{itemize}
\tightlist
\item
  Clear,
\item
  precise,
\item
  provable
\item
  next to no help when writing a program, (or talking to a collaborator
  about your program).
\end{itemize}

Two main problems:

\begin{enumerate}
\tightlist
\item
  It's as hard, or harder, to write a good spec as a good program
  (validation)
\item
  Checking your program conforms to the spec is hard (mapping, automated
  verification)
\end{enumerate}

Also, ``Fork my Z specification on github''\ldots{}

\pause

\ldots said no-one ever!
\end{frame}

\begin{frame}{A closer look}
\protect\hypertarget{a-closer-look}{}
\includegraphics{zed-birthday-book-sample.png}

\pause

What this spec describes is slightly tricksy to do in R due to mutable
state.
\end{frame}

\begin{frame}{But\ldots I code by HACKING AWAY}
\protect\hypertarget{buti-code-by-hacking-away}{}
..not writing a load of rubbish first!

And you can. You can also get a starter set of unit tests ``for free''

And how?

\pause

The answer is to write your tests \emph{first}

Madness!?!!!

\pause

No!

This is \textbf{Test Driven Development} (TDD) and will force you to
think about two things:

\begin{enumerate}
\tightlist
\item
  What your code should be doing
\item
  Making your code easy to use

  \begin{itemize}
  \tightlist
  \item
    (rather than easy to write\ldots)
  \end{itemize}
\end{enumerate}
\end{frame}

\begin{frame}[fragile]{Back to the birthday book}
\protect\hypertarget{back-to-the-birthday-book}{}
What should it do? Let's write a test:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{BirthDayBook =}\StringTok{ }\NormalTok{... ? ...}
\NormalTok{add\_birthday =}\StringTok{ }\NormalTok{... ? ...}

\KeywordTok{test\_that}\NormalTok{(}\StringTok{"addbirthday"}\NormalTok{, \{}
    \KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{length}\NormalTok{(BirthDayBook), }\DecValTok{0}\NormalTok{)}
    \KeywordTok{add\_birthday}\NormalTok{(BirthDayBook, }\StringTok{"David"}\NormalTok{, }\StringTok{"1980{-}01{-}04"}\NormalTok{)}
    \KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{length}\NormalTok{(BirthDayBook), }\DecValTok{1}\NormalTok{)}
    \KeywordTok{expect\_true}\NormalTok{( }\StringTok{"David"} \OperatorTok{\%in\%}\StringTok{ }\KeywordTok{names}\NormalTok{(BirthDayBook) )}
    \KeywordTok{add\_birthday}\NormalTok{(BirthDayBook, }\StringTok{"David"}\NormalTok{, }\StringTok{"1980{-}06{-}04"}\NormalTok{)}
    \KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{length}\NormalTok{(BirthDayBook), }\DecValTok{1}\NormalTok{)}
\NormalTok{\})}
\end{Highlighting}
\end{Shaded}

The test specifies some of the properties that any add\_birthday
function must possess, just like the formal specification.

\emph{You can read it, if you know R}

\emph{And we can run it, directly}
\end{frame}

\begin{frame}[fragile]{But does it pass?}
\protect\hypertarget{but-does-it-pass}{}
Nope!

We have \emph{implementation decisions} to make. But we already know:

\begin{itemize}
\tightlist
\item
  some of the \emph{what} (arguments expected by add\_birthday)
\item
  some (not all) success/fail conditions
\end{itemize}

So a good start. Further decisions:

\begin{enumerate}
\tightlist
\item
  \emph{What} is BirthDayBook itself?
\item
  \emph{How} should add\_birthday do its thing
\end{enumerate}

\pause

For 1, an environment is mutable. That'll do:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{BirthDayBook=}\KeywordTok{new.env}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\end{frame}

\begin{frame}[fragile]{Implementing}
\protect\hypertarget{implementing}{}
And for 2, knowing what BirthDayBook is we can now implement the
add\_birthday function:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{add\_birthday =}\StringTok{ }\ControlFlowTok{function}\NormalTok{(book, name, date) \{}
    \ControlFlowTok{if}\NormalTok{ (}\OperatorTok{!}\StringTok{ }\NormalTok{name }\OperatorTok{\%in\%}\StringTok{ }\KeywordTok{names}\NormalTok{(book)) \{}
\NormalTok{        book[[name]]=}\KeywordTok{as.Date}\NormalTok{(date)}
\NormalTok{    \}}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}

\pause

Later we could add more sanity checks and so on. Knock yourself out, but
a key idea in TDD is to write the simplest code possible to satisfy the
test

\pause

Adding more functionality requires more tests
\end{frame}

\begin{frame}[fragile]{Finally, verify}
\protect\hypertarget{finally-verify}{}
Then we can run the test again:

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{test\_that}\NormalTok{(}\StringTok{"addbirthday"}\NormalTok{, \{}
    \KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{length}\NormalTok{(BirthDayBook), }\DecValTok{0}\NormalTok{)}
    \KeywordTok{add\_birthday}\NormalTok{(BirthDayBook, }\StringTok{"David"}\NormalTok{, }\StringTok{"1980{-}01{-}04"}\NormalTok{)}
    \KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{length}\NormalTok{(BirthDayBook), }\DecValTok{1}\NormalTok{)}
    \KeywordTok{expect\_true}\NormalTok{( }\StringTok{"David"} \OperatorTok{\%in\%}\StringTok{ }\KeywordTok{names}\NormalTok{(BirthDayBook) )}
    \KeywordTok{add\_birthday}\NormalTok{(BirthDayBook, }\StringTok{"David"}\NormalTok{, }\StringTok{"1980{-}06{-}04"}\NormalTok{)}
    \KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{length}\NormalTok{(BirthDayBook), }\DecValTok{1}\NormalTok{)}
    \KeywordTok{expect\_equal}\NormalTok{(BirthDayBook[[}\StringTok{"David"}\NormalTok{]], }
                 \KeywordTok{as.Date}\NormalTok{(}\StringTok{"1980{-}01{-}04"}\NormalTok{))}
\NormalTok{\})}
\end{Highlighting}
\end{Shaded}

And it passes!

(We could also do this with R.oo, closure-as-object, or in a more R-like
way in the first place (state-free))
\end{frame}

\begin{frame}{Things to think about when writing spec tests}
\protect\hypertarget{things-to-think-about-when-writing-spec-tests}{}
Similar to when writing tests generally. How lucky!

\begin{enumerate}
\item
  ``Typical'' use cases (functionality check). We started with this, and
  you should too
\item
  Invalid input.

  \begin{itemize}
  \tightlist
  \item
    Example: My routine expects a positive int, what should happen when
    I put in a negative float? Or NA, NULL, the contents of your sock
    drawer \ldots{}
  \item
    A stop() error, warn() or some special value returned?
  \item
    Decide \emph{before} you write code with subtle bugs!
  \end{itemize}
\end{enumerate}
\end{frame}

\begin{frame}
\begin{enumerate}
\setcounter{enumi}{2}
\tightlist
\item
  Zero, one, many.

  \begin{itemize}
  \tightlist
  \item
    Basically what should happen when you give certain routines no
    input, a single input item or lots of input
  \item
    R has some ``features'' which make this important
  \end{itemize}
\item
  Boundary conditions

  \begin{itemize}
  \tightlist
  \item
    If there's an expected change in behaviour for certain input values,
    test each side of the change point
  \item
    Guards against off-by-one errors and suchlike
  \end{itemize}
\end{enumerate}
\end{frame}

\begin{frame}
Finally, some other considerations

\begin{itemize}
\tightlist
\item
  concentrate your spec/testing effort on the ``core'' parts of your
  code that will be used heavily
\item
  Your data input code would likely benefit from thorough testing
\item
  But it's probably not worth writing loads of spec for one-off plotting
  routines and so forth.
\item
  Though a little might help clarify your thoughts
\end{itemize}
\end{frame}

\begin{frame}{And when to start implementing?}
\protect\hypertarget{and-when-to-start-implementing}{}
No hard and fast rules:

\begin{itemize}
\tightlist
\item
  When you feel you have enough spec to build a more or less correct
  implementation (of part of your software)
\item
  When you get bored of specifying.
\end{itemize}

Iterative development is vastly better than building a vast software
castle on shaky foundations
\end{frame}

\begin{frame}{Learnings}
\protect\hypertarget{learnings}{}
Hopefully I've shown you how test-driven development can help

\begin{itemize}
\tightlist
\item
  Clarify your thoughts at an early stage in development
\item
  Avoid tedious/useless specification tasks
\item
  Give you a head-start on the process of software testing
\item
  Catch a number of annoying programming errors
\end{itemize}

Obviously, I've only scratched the surface. Some resources

\begin{itemize}
\tightlist
\item
  The ``bible'' for this technique is ``Test Driven Development by
  Example'' (Beck, 2003). Not R-based.
\item
  Online \emph{\href{http://agiledata.org/essays/tdd.html}{Introduction
  to Test Driven Development (TDD)}}
\item
  Online
  \emph{\href{http://rstudio-pubs-static.s3.amazonaws.com/278724_4d8935a2955c49d9934e2113c737e70e.html}{Test
  Driven Development in R}}
\end{itemize}

A small amount of supplementary material follows this slide
\end{frame}

\begin{frame}[fragile]{Zero, one, many tests}
\protect\hypertarget{zero-one-many-tests}{}
R sometimes tries to be helpful. Helpful like a toddler.

I once wrote a function that returned different column(s) from a
data.frame according to the input.

The caller always expected a data.frame though.

Contrived example, starting with the zero case:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{foo=mtcars[,}\KeywordTok{c}\NormalTok{()]}
\KeywordTok{expect\_true}\NormalTok{(}\KeywordTok{is}\NormalTok{(foo,}\StringTok{"data.frame"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

Good?

\pause

Looks good. The ``one'' case:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{bar=mtcars[,}\KeywordTok{c}\NormalTok{(}\StringTok{"cyl"}\NormalTok{)]}
\KeywordTok{expect\_true}\NormalTok{(}\KeywordTok{is}\NormalTok{(bar,}\StringTok{"data.frame"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

Problem ????

\pause

Oh dear, the type is numeric!
\end{frame}

\begin{frame}[fragile]
The ``many'' case is OK:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{baz=mtcars[,}\KeywordTok{c}\NormalTok{(}\StringTok{"cyl"}\NormalTok{,}\StringTok{"disp"}\NormalTok{,}\StringTok{"hp"}\NormalTok{)]}
\KeywordTok{expect\_true}\NormalTok{(}\KeywordTok{is}\NormalTok{(baz,}\StringTok{"data.frame"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\pause

So let's fix our ``one'' case:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{bar=mtcars[,}\KeywordTok{c}\NormalTok{(}\StringTok{"cyl"}\NormalTok{), drop=}\OtherTok{FALSE}\NormalTok{]}
\KeywordTok{expect\_true}\NormalTok{(}\KeywordTok{is}\NormalTok{(bar,}\StringTok{"data.frame"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

That's better!
\end{frame}

\begin{frame}[fragile]{Boundary conditions}
\protect\hypertarget{boundary-conditions}{}
Say we have some data:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{dat =}\StringTok{ }\KeywordTok{sample}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{,}\DecValTok{20}\NormalTok{, }\OtherTok{TRUE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

We want to replace values `1-5 with ``Bad'', 6-8 with ``OK'' and
``9-10'' with ``Good''. We want a function called boundaries to do this:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{dat2 =}\StringTok{ }\KeywordTok{sapply}\NormalTok{(dat,boundaries)}
\end{Highlighting}
\end{Shaded}

Let's test the boundaries:

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{boundaries}\NormalTok{(}\DecValTok{1}\NormalTok{),}\StringTok{"Bad"}\NormalTok{)}
\KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{boundaries}\NormalTok{(}\DecValTok{5}\NormalTok{),}\StringTok{"Bad"}\NormalTok{)}
\KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{boundaries}\NormalTok{(}\DecValTok{6}\NormalTok{),}\StringTok{"OK"}\NormalTok{)}
\KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{boundaries}\NormalTok{(}\DecValTok{8}\NormalTok{),}\StringTok{"OK"}\NormalTok{)}
\KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{boundaries}\NormalTok{(}\DecValTok{9}\NormalTok{),}\StringTok{"Good"}\NormalTok{)}
\KeywordTok{expect\_equal}\NormalTok{(}\KeywordTok{boundaries}\NormalTok{(}\DecValTok{10}\NormalTok{),}\StringTok{"Good"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Done?
\end{frame}

\begin{frame}[fragile]
Not quite. We also need:

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{expect\_error}\NormalTok{(}\KeywordTok{boundaries}\NormalTok{(}\DecValTok{0}\NormalTok{))}
\KeywordTok{expect\_error}\NormalTok{(}\KeywordTok{boundaries}\NormalTok{(}\DecValTok{11}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

And a function:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{boundaries=}\ControlFlowTok{function}\NormalTok{(x) \{}
    \ControlFlowTok{if}\NormalTok{ (x}\OperatorTok{\textless{}}\DecValTok{5}\NormalTok{) }\KeywordTok{return}\NormalTok{(}\StringTok{"Bad"}\NormalTok{)}
    \ControlFlowTok{if}\NormalTok{ (x}\OperatorTok{\textgreater{}}\DecValTok{5} \OperatorTok{\&\&}\StringTok{ }\NormalTok{x}\OperatorTok{\textless{}}\DecValTok{9}\NormalTok{) }\KeywordTok{return}\NormalTok{(}\StringTok{"OK"}\NormalTok{)}
    \ControlFlowTok{if}\NormalTok{ (x}\OperatorTok{\textgreater{}=}\DecValTok{9}\NormalTok{) }\KeywordTok{return}\NormalTok{(}\StringTok{"Good"}\NormalTok{) }
    \KeywordTok{stop}\NormalTok{(}\KeywordTok{paste}\NormalTok{(}\StringTok{"Value "}\NormalTok{,x,}\StringTok{"out of range"}\NormalTok{)) }
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}

Will it pass?

\pause

Nope. Not one, but THREE bugs that will be caught by our tests.

And at least one that won't.

\pause

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{boundaries}\NormalTok{(}\OtherTok{NULL}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Deary me. One bug per line!
\end{frame}

\begin{frame}[fragile]{Closure-as-object implementation of BirthDayBook}
\protect\hypertarget{closure-as-object-implementation-of-birthdaybook}{}
Like this:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{BirthDayBook2\textless{}{-}}\ControlFlowTok{function}\NormalTok{() \{}
\NormalTok{    birthday\textless{}{-}}\KeywordTok{list}\NormalTok{()}
    \KeywordTok{list}\NormalTok{(}\DataTypeTok{add\_birthday=}\ControlFlowTok{function}\NormalTok{(name,date) \{}
        \ControlFlowTok{if}\NormalTok{ (}\OperatorTok{!}\StringTok{ }\NormalTok{name }\OperatorTok{\%in\%}\StringTok{ }\KeywordTok{names}\NormalTok{(birthday)) \{}
\NormalTok{            birthday[[name]]\textless{}\textless{}{-}}\KeywordTok{as.Date}\NormalTok{(date)}
\NormalTok{        \}}
\NormalTok{    \},}
    \DataTypeTok{known=}\ControlFlowTok{function}\NormalTok{(name) \{}
\NormalTok{        name }\OperatorTok{\%in\%}\StringTok{ }\KeywordTok{names}\NormalTok{(birthday)}
\NormalTok{    \},}
    \DataTypeTok{get\_birthday=}\ControlFlowTok{function}\NormalTok{(name) \{}
\NormalTok{        birthday[[name]]}
\NormalTok{    \})}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
\end{frame}

\begin{frame}[fragile]
We use it like this:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{book =}\StringTok{ }\KeywordTok{BirthDayBook2}\NormalTok{()}
\NormalTok{book}\OperatorTok{$}\KeywordTok{add\_birthday}\NormalTok{(}\StringTok{"David"}\NormalTok{,}\StringTok{"1980{-}10{-}02"}\NormalTok{)}

\KeywordTok{expect\_equal}\NormalTok{(book}\OperatorTok{$}\KeywordTok{get\_birthday}\NormalTok{(}\StringTok{"David"}\NormalTok{), }
             \KeywordTok{as.Date}\NormalTok{(}\StringTok{"1980{-}10{-}02"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\end{frame}

\end{document}
