Examples by example

An “example” (or example method ) is a unary method that produces an example object .

In Glamorous Toolkit, examples serve as test methods and can contain assertions.

An example is always annotated with a <gtExample> pragma. For example, BrTextEditorSelecterExamples>>#select_moveOneRight_at_6 select_moveOneRight_at_6 <gtExample> <return: #BrEditor> | anEditor | anEditor := self editorOnMultilineText. anEditor editor moveCursorTo: 6. anEditor selecter moveOneToRight; select. self assert: anEditor selection equals: (BlCompositeSelection new select: 6 to: 7). self assert: anEditor editor selectedText asString equals: 'e'. self assert: anEditor cursors equals: (BrTextEditorCursor atAll: #(7)). ^ anEditor defines an example method that produces a text editor, moves a cursor, simulates a selection, and eventually asserts that it answers the selection.

An example can be based on one or more other examples. In the case above, the example relies on another example from BrTextEditorOperatorExamples>>#editorOnMultilineText editorOnMultilineText <gtExample> <return: #BrEditor> | anEditor anEditorElement | anEditorElement := self editorFor: self multilineText. anEditor := anEditorElement editor. self assert: anEditor cursors isEmpty. self assert: anEditor selection isEmpty. ^ anEditorElement . It does that by calling self editorOnMultilineText.