Example Object

**Problem:** page

How do you organize examples for testing and documentation?

**Forces:**

Concrete examples are need for many purposes, such as documentation, testing, and exploration. Examples can be complex to set up.Unit tests consume examples, but they are only accessible if a test fails.

**Solution:**

Wrap examples as (instance) methods that optionally evaluate some tests (assertions), and return the example instance. Each example may also use one or more examples as the initial setup for the new example.

**Steps:**

To start, you need a modified unit testing framework in which tests return the exercised fixture, namely, an example. In GT, you create an Example by defining a parameterless method that has a <gtExample> pragma, and returns an object. (A similar framework for Java is JExample.)

[…]

**Related patterns:**

Examples can be embedded in the notebook pages of a Project Diary. Examples can produce a Moldable Object for moldable development.

With Moldable Development we use examples rather than tests. Basically these are unit tests that return a live instance that you can interact with.

**Known Uses:**

(Consequences) Examples can be run just like classical unit tests. When an example fails, its dependent examples do not need to be run. When an example succeeds, it can be inspected, used as a moldable object to start coding, or embedded as a live example snippet within a notebook page to illustrate some point. (The example snippets above illustrate this.) When you are searching for usages of an API, not only do you find examples that illustrate the usage, but by running the example you obtain a live instance that you can explore.

~

In the paper proposed by Myers (1990), the author makes use of a new Visual Programming tool ‘Peridot’ (Myers, 1988, 1987) for creating graphical and interactive interfaces. This language makes use of the interesting concept of “programming by example”. A picture of the required interface needs to be drawn by the user. This picture is then generalized by the system to generate a parameterized procedure. The user has to provide some example values for each parameter to enable the system in displaying a solid instance of the user interface.