TDD & Debugger-Driven Development

Smalltalk is where TDD was born, and the live environment makes the cycle extremely tight.

26.1 Classic TDD cycle

Red: write a small failing test.

Green: write the simplest code to make it pass.

Refactor: clean up with the safety net of tests.

Smalltalk is where TDD was born, and the live environment makes the cycle extremely tight.

26.2 Coding in the debugger

Smalltalk lets you write code while the program is running:

Write a test that uses a class and methods that don't exist yet:

When accepting, Pharo warns that MyStack is unknown and offers to define the class — accept and create it.

Run the test. The debugger opens on MyStack(Object)>>doesNotUnderstand: #push:.

Click Create: choose the class (MyStack) and protocol. The debugger generates a method stub and shows it.

Write the method body in the debugger (add a slot when prompted), accept, and press Proceed. Execution continues with your new method.

Repeat for pop and size until the test passes.

You discover the API *from the client's perspective*, with real objects in front of you — this is debugger-driven development (also called "coding in the debugger").

TestCase << #StackTest slots: {}; package: 'DS-Tests'.

StackTest >> testPushPop
    | stack |
    stack := MyStack new.
    stack push: 1; push: 2.
    self assert: stack pop equals: 2.
    self assert: stack size equals: 1

26.3 Breakpoints and halts

self halt always stops; self haltOnce stops only the first time; self haltIf: [ amount > 1000 ] is conditional; Transcript crShow: 'reached' gives printf-style tracing. In Pharo's browser you can also right-click a method or AST node to add breakpoints, conditional breakpoints, watchpoints (log variable values), and object-centric breakpoints (break only when a specific object receives a message) — no source modification needed.

26.4 Debugger power moves

Restart a frame after changing code (no need to rerun the scenario), return an entered value from a frame, evaluate expressions in any frame's context, Step Through into blocks, Run to here, copy the stack for bug reports, and script the debugger with Sindarin.

26.5 Exploratory programming

Combine Playground, Inspector, and Debugger: create objects, inspect them, send messages, add methods from the inspector, and promote Playground snippets into tests. Glamorous Toolkit takes this further with *moldable development*: custom inspector views and live notebooks for every domain object.