Skip to content

Commit

Permalink
Merge pull request #30 from ba-st/compatible-protocol
Browse files Browse the repository at this point in the history
Changed CollectionFormatter to accept strings as separators.
  • Loading branch information
mtabacman committed Sep 10, 2018
2 parents 5e69de1 + cea2bb0 commit af68ae3
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 114 deletions.
22 changes: 13 additions & 9 deletions source/Buoy-Collections-Tests/CollectionFormatterTest.class.st
Expand Up @@ -11,24 +11,25 @@ Class {
CollectionFormatterTest >> testFormat [

| formatter |
formatter := CollectionFormatter separatingWith: $,.
formatter := CollectionFormatter separatingWith: ', '.
self assert: (formatter format: #(1 2 3)) equals: '1, 2, 3'
]

{ #category : #tests }
CollectionFormatterTest >> testFormatEmptyCollection [

| formatter |
formatter := CollectionFormatter separatingWith: $, andLastWith: $y.
formatter := CollectionFormatter separatingWith: ', ' andLastWith: ' and '.
self assert: (formatter format: #()) equals: String new
]

{ #category : #tests }
CollectionFormatterTest >> testFormatOn [

| formatter stream |

stream := WriteStream on: String new.
formatter := CollectionFormatter separatingWith: $,.
formatter := CollectionFormatter separatingWith: ', '.
formatter format: #(1 2 3) on: stream.
self assert: stream contents equals: '1, 2, 3'
]
Expand All @@ -37,16 +38,18 @@ CollectionFormatterTest >> testFormatOn [
CollectionFormatterTest >> testFormatOneElementCollection [

| formatter |
formatter := CollectionFormatter separatingWith: $, andLastWith: $y.

formatter := CollectionFormatter separatingWith: ', ' andLastWith: ' and '.
self assert: (formatter format: #(1)) equals: '1'
]

{ #category : #tests }
CollectionFormatterTest >> testFormatTwoElementCollectionWithLastSeparator [

| formatter |
formatter := CollectionFormatter separatingWith: $, andLastWith: $y.
self assert: (formatter format: #(1 2)) equals: '1 y 2'

formatter := CollectionFormatter separatingWith: ', ' andLastWith: ' and '.
self assert: (formatter format: #(1 2)) equals: '1 and 2'
]

{ #category : #tests }
Expand All @@ -57,13 +60,14 @@ CollectionFormatterTest >> testFormatWithElementFormater [
formatter := CollectionFormatter
separatingWith: $,
applyingToEach: [ :element | '/<1p>/' expandMacrosWith: element ].
self assert: (formatter format: #(1 2 3)) equals: '/1/, /2/, /3/'
self assert: (formatter format: #(1 2 3)) equals: '/1/,/2/,/3/'
]

{ #category : #tests }
CollectionFormatterTest >> testFormatWithLastSeparator [

| formatter |
formatter := CollectionFormatter separatingWith: $, andLastWith: $y.
self assert: (formatter format: #(1 2 3)) equals: '1, 2 y 3'

formatter := CollectionFormatter separatingWith: ', ' andLastWith: ' and '.
self assert: (formatter format: #(1 2 3)) equals: '1, 2 and 3'
]
33 changes: 11 additions & 22 deletions source/Buoy-Collections/CollectionFormatter.class.st
Expand Up @@ -10,9 +10,9 @@ Class {
#instVars : [
'separator',
'elementFormatter',
'lastSeparator'
'lastSeparatorOptional'
],
#category : 'Buoy-Collections'
#category : #'Buoy-Collections'
}

{ #category : #'instance creation' }
Expand Down Expand Up @@ -45,8 +45,8 @@ CollectionFormatter class >> separatingWith: aSeparator andLastWith: aLastSepara
{ #category : #private }
CollectionFormatter class >> separatingWith: aSeparator andOptionallyLastWith: anOptionalSeparator applyingToEach: anElementFormatter [

^ self new
initializeSeparatingEachWith: aSeparator
^ super new
initializeSeparatingEachWith: aSeparator asString
andOptionallyLastWith: anOptionalSeparator
applyingToEach: anElementFormatter
]
Expand Down Expand Up @@ -79,35 +79,24 @@ CollectionFormatter >> format: aCollection on: aStream [
index := index + 1.
aStream nextPutAll: (elementFormatter value: element) ]
separatedBy: [ index = size
ifTrue: [ lastSeparator
ifTrue: [ lastSeparatorOptional
withContentDo: [ :content | self put: content on: aStream ]
ifUnused: [ self putSeparatorOn: aStream ] ]
ifFalse: [ self putSeparatorOn: aStream ] ].
^aStream contents
ifUnused: [ self put: separator on: aStream ] ]
ifFalse: [ self put: separator on: aStream ] ].

^ aStream contents
]

{ #category : #initialization }
CollectionFormatter >> initializeSeparatingEachWith: aSeparator andOptionallyLastWith: anOptional applyingToEach: aBlock [

separator := aSeparator.
lastSeparator := anOptional.
lastSeparatorOptional := anOptional.
elementFormatter := aBlock
]

{ #category : #formatting }
CollectionFormatter >> put: aSeparator on: aStream [

^ aStream
nextPut: Character space;
nextPut: aSeparator;
nextPut: Character space
]

{ #category : #formatting }
CollectionFormatter >> putSeparatorOn: aStream [

^ aStream
nextPut: separator;
nextPut: Character space
^ aStream nextPutAll: aSeparator
]

0 comments on commit af68ae3

Please sign in to comment.