diff --git a/src/Spec2-Core/SpTableColumn.class.st b/src/Spec2-Core/SpTableColumn.class.st index c8965431..4449f51b 100644 --- a/src/Spec2-Core/SpTableColumn.class.st +++ b/src/Spec2-Core/SpTableColumn.class.st @@ -12,7 +12,8 @@ Class { 'evaluation', 'expandable', 'sortFunction', - 'width' + 'width', + 'formattingBlock' ], #category : 'Spec2-Core-Widgets-Table', #package : 'Spec2-Core', @@ -92,6 +93,18 @@ SpTableColumn >> beNotExpandable [ expandable := false ] +{ #category : 'private' } +SpTableColumn >> evaluateObject: anObject [ + + "Apply the evaluation to the object of the table row. + If no evaluation is set, evaluate to the object itself. + + IMPORTANT: Sorting happens on the results of evaluation and not formatting" + + self evaluation ifNil: [ ^ anObject ]. + ^ self evaluation cull: anObject +] + { #category : 'api' } SpTableColumn >> evaluated: aBlock [ "Define how the column will evaluate the element of the table/tree model @@ -111,6 +124,22 @@ SpTableColumn >> evaluation [ ^ evaluation ] +{ #category : 'private' } +SpTableColumn >> formatObject: anObject [ + "Returns the string representation of the argument to display in the table cell" + + formattingBlock ifNil: [ ^ anObject ]. + ^ formattingBlock value: anObject +] + +{ #category : 'api' } +SpTableColumn >> formatted: aBlock [ + "Defines how each element is formatted to be shown. + By default items will be shown using #asString" + + formattingBlock := aBlock +] + { #category : 'testing' } SpTableColumn >> hasFixedWidth [ @@ -161,7 +190,7 @@ SpTableColumn >> isSortable [ { #category : 'private' } SpTableColumn >> readObject: anObject [ - ^ self evaluation cull: anObject + ^ self formatObject: (self evaluateObject: anObject) ] { #category : 'api' } diff --git a/src/Spec2-Tests/SpStringTableColumnTest.class.st b/src/Spec2-Tests/SpStringTableColumnTest.class.st index 0f4096fa..d3e7f40f 100644 --- a/src/Spec2-Tests/SpStringTableColumnTest.class.st +++ b/src/Spec2-Tests/SpStringTableColumnTest.class.st @@ -9,6 +9,34 @@ Class { #tag : 'Core-Widgets' } +{ #category : 'tests' } +SpStringTableColumnTest >> testDisplayObjectRespectsFormatting [ + + | column dateToShow | + column := SpStringTableColumn new. + column formatted: [ :e | e displayString ]. + + dateToShow := Date today. + + self + assert: (column readObject: dateToShow) + equals: dateToShow displayString +] + +{ #category : 'tests' } +SpStringTableColumnTest >> testDisplayObjectWithoutFormattingReturnsTheObjectItself [ + + | column dateToShow | + column := SpStringTableColumn new. + + dateToShow := Date today. + + "It's up to the table to stringify the object later" + self + assert: (column readObject: dateToShow) + equals: dateToShow +] + { #category : 'tests' } SpStringTableColumnTest >> testIsSortable [ |widget|