Example snippet

An example is like a test that returns an object. Beside the utility for testing, examples can also be used for documentation purposes through an exmaple snippet.

Consider the example defined in BlGridLayoutExamples>>#exampleCellSpacingHorizontal1 exampleCellSpacingHorizontal1 <gtExample> <return: #BlElement> | parent childA childB | childA := self childElementMatchingParent. childB := self childElementMatchingParent. parent := self parentWithGridHorizontalLayout. parent constraints horizontal exact: 400. parent constraints vertical exact: 200. parent layout cellSpacing: 20. parent layout horizontal. parent addChildren: {childA. childB}. parent forceLayout. self assert: childA extent equals: 170 @ 160. self assert: childA position equals: 20 @ 20. self assert: childB extent equals: 170 @ 160. self assert: childB position equals: 210 @ 20. self assert: parent extent equals: 400 @ 200. self assert: parent position equals: 0 @ 0. ^ parent . If we point an example snippet to that method, we get this:

exampleCellSpacingHorizontal1
	<gtExample>
	<return: #BlElement>
	| parent childA childB |
	childA := self childElementMatchingParent.
	childB := self childElementMatchingParent.

	parent := self parentWithGridHorizontalLayout.
	parent constraints horizontal exact: 400.
	parent constraints vertical exact: 200.
	parent layout cellSpacing: 20.
	parent layout horizontal.

	parent
		addChildren: {childA.
				childB}.
	parent forceLayout.

	self assert: childA extent equals: 170 @ 160.
	self assert: childA position equals: 20 @ 20.

	self assert: childB extent equals: 170 @ 160.
	self assert: childB position equals: 210 @ 20.

	self assert: parent extent equals: 400 @ 200.
	self assert: parent position equals: 0 @ 0.
	^ parent
    

The example snippet can be parameterized in several ways:

previewExpanded: when it's true the example is executed automatically and the returned object is shown in an inspector.

noCode: When noCode is true, only the inspector is shown.

previewShow: Specify a message that will be executed on the result of the returned object. The message should define a view. For example, in our case, we use #gtLiveFor:.

previewHeight: Specifies the height of the preview as a number.