Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model to source code integration #19

Merged
merged 10 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ Class {
BaselineOfBlocSerialization >> baseline: spec [

<baseline>
spec for: #common do: [

"dependencies"
spec for: #common do: [ "dependencies"
spec
baseline: 'Ston'
with: [ spec repository: 'github://svenvc/ston/repository' ].

spec
baseline: 'StashSerialization'
with: [ spec repository: 'github://Nyan11/Stash/src' ].

spec
baseline: 'Bloc'
with: [ spec repository: 'github://pharo-graphics/Bloc:master/src' ].

"project packages"
spec package: 'Bloc-Serialization-STON'.
spec
package: 'Bloc-Serialization'
with: [ spec requires: #( 'Bloc' 'Bloc-Serialization-STON' 'Ston' ) ].
spec package: 'Bloc-Serialization' with: [
spec requires: #( 'Bloc' 'Bloc-Serialization-STON'
'Ston' 'StashSerialization' ) ].
spec
package: 'Bloc-Serialization-Tests'
with: [ spec requires: #( 'Bloc-Serialization' ) ].

]
with: [ spec requires: #( 'Bloc-Serialization' ) ] ]
]
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ BlElementSerializationTests >> testSerializeThenMaterialize [
element := BlElement new.
newElement := element serializeThenMaterialize.

self assert: element class equals: newElement class.
self assert: element class equals: newElement class
]

{ #category : #tests }
BlElementSerializationTests >> testShouldSerializedChildren [

| element |
element := BlElement new.
self assert: element shouldSerializedChildren.
self assert: element shouldSerializedChildren
]
26 changes: 26 additions & 0 deletions src/Bloc-Serialization-Tests/BlFakeSerializer.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Class {
#name : #BlFakeSerializer,
#superclass : #Object,
#classInstVars : [
'counter'
],
#category : #'Bloc-Serialization-Tests-Core'
}

{ #category : #initialization }
BlFakeSerializer class >> materialize: anObject [

counter := counter + 1
]

{ #category : #initialization }
BlFakeSerializer class >> materializeCount [

^ counter
]

{ #category : #initialization }
BlFakeSerializer class >> resetForTest [

counter := 0.
]
15 changes: 14 additions & 1 deletion src/Bloc-Serialization-Tests/BlSerializerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ Class {
#category : #'Bloc-Serialization-Tests-Core'
}

{ #category : #tests }
BlSerializerTest >> testMaterialize [

| error blElement |
error := nil.

BlFakeSerializer resetForTest.
self assert: BlFakeSerializer materializeCount equals: 0.
[ blElement := BlSerializer materialize: '"BlFakeSerializer"' ] onErrorDo: [ :e |
error := e ].
self assert: BlFakeSerializer materializeCount equals: 1.
self assert: error equals: nil.
]

{ #category : #tests }
BlSerializerTest >> testMaterializeEmptyString [

Expand Down Expand Up @@ -59,7 +73,6 @@ BlSerializerTest >> testSerializeBlElementCollection [
error := nil. result := nil.
[ result := BlSerializer serialize: OrderedCollection new ] onErrorDo: [ :e | error := e ].
self deny: (error isKindOf: BlocSerializationError). "this test is ok because of a empty string is an empty collection"
self assert: result equals: 'OrderedCollection [ ]'.

error := nil. result := nil.
oc := OrderedCollection new.
Expand Down
31 changes: 26 additions & 5 deletions src/Bloc-Serialization-Tests/BlocSerializationTests.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,39 @@ BlocSerializationTests >> serializeThenMaterialize: aBlElement [
^ BlSerializer materialize: (BlSerializer serialize: aBlElement)
]

{ #category : #utilities }
BlocSerializationTests >> serializeThenMaterialize: aBlElement withSerializer: aBlSerializer [

^ aBlSerializer materialize: (aBlSerializer serialize: aBlElement)
]

{ #category : #'as yet unclassified' }
BlocSerializationTests >> serializersToTest [

^ TBlSerializer users
]

{ #category : #utilities }
BlocSerializationTests >> test: aBlElement on: aTestBloc [
"This method take a blElement, execute a Bloc with assertions inside (from test implementation) for this blElement. The blElement is serialized and rebuild, the new blElement rebuild is re-tested by the Bloc with assertions to confirm the good serialization process "

| newBlElement |

"Execute the test bloc with the BlElement before serialization"
aTestBloc value: aBlElement.

self serializersToTest do: [ :each |
"Serialize the blElement for building a new blElement which should be with same properties"
newBlElement := self serializeThenMaterialize: aBlElement withSerializer: each.

"Re-execute the test bloc with the new BlElement after materialization to confirm that the new object have same properties than the previous"
aTestBloc value: newBlElement ]


"Serialize the blElement for building a new blElement which should be with same properties"
newBlElement := self serializeThenMaterialize: aBlElement.


]

"Re-execute the test bloc with the new BlElement after materialization to confirm that the new object have same properties than the previous"
aTestBloc value: newBlElement
{ #category : #utilities }
BlocSerializationTests >> testNotEmptyTesting [
self assert: self serializersToTest isNotEmpty
]
7 changes: 7 additions & 0 deletions src/Bloc-Serialization/Array.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Extension { #name : #Array }

{ #category : #'*Bloc-Serialization' }
Array >> materializeAsBlElement [

^ self
]
64 changes: 32 additions & 32 deletions src/Bloc-Serialization/BlSerializer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,57 @@ Serailizer class for Bloc
Class {
#name : #BlSerializer,
#superclass : #Object,
#traits : 'TBlSerializer',
#classTraits : 'TBlSerializer classTrait',
#classInstVars : [
'serializer'
],
#category : #'Bloc-Serialization-Core'
}

{ #category : #serialization }
BlSerializer class >> materialize: aString [
"Materialize a String into a BlElement and return it. Default materializer is STON."
| blElement |
aString isString not ifTrue:[ BlocMaterializationError signal: 'Cannot materialize a no String object into BlElement' ].
"Try to materialize and catch an error into a BlocMaterialization error"
[ blElement := STON fromString: aString ] onErrorDo: [ :e | BlocMaterializationError signal: 'Cannot support Bloc materialization of this String (', e asString, ')' ].
blElement ifNil:[ BlocMaterializationError signal: 'Bloc materialization result is nil' ].

^ blElement
| serializerClassName serializerClass |
aString ifEmpty: [ ^ BlocMaterializationError new signal ].
aString first = $" ifFalse: [
^ BlStonSerializer materialize: aString ].
serializerClassName := aString lines first allButFirst allButLast.
serializerClass := Smalltalk environment classNamed:
serializerClassName.
^ serializerClass materialize: ('' join: aString lines allButFirst)
]

{ #category : #initialization }
BlSerializer class >> reset [

<script>
serializer := nil
]

{ #category : #serialization }
BlSerializer class >> serialize: aBlElementOrABlElementsCollection [
"Serialize a BlElement or a list of BlElements into a String using a serializer. Default serializer is STON."

| string |
aBlElementOrABlElementsCollection isCollection
ifTrue: [ self verifyCollection: aBlElementOrABlElementsCollection ]
ifFalse: [ self verifyElement: aBlElementOrABlElementsCollection ].

"Try to serialize and catch an error into a BlocSerialization error"
[ string := STON toStringPretty: aBlElementOrABlElementsCollection ] onErrorDo: [ :e | BlocSerializationError signal: 'Cannot support serialization of this BlElement (', e asString, ')' ].

^ string
^ self serialize: aBlElementOrABlElementsCollection with: self serializer
]

{ #category : #'as yet unclassified' }
BlSerializer class >> verifyCollection: aCollection [

"Check if the collection is supported : can contains some BlElements"
aCollection isString ifTrue:[
BlocSerializationError signal: 'Cannot serialize a String'
].

aCollection isDictionary ifTrue:[
BlocSerializationError signal: 'Cannot serialize a Dictionary'
].
{ #category : #serialization }
BlSerializer class >> serialize: aBlElementOrABlElementsCollection with: aBlSerializer [
"Serialize a BlElement or a list of BlElements into a String using a serializer. Default serializer is STON."

aCollection do: [ :each | self verifyElement: each ]
| string |
string := aBlSerializer serialize:
aBlElementOrABlElementsCollection.
^ '"<1s>"<r><2s>' expandMacrosWith: aBlSerializer name with: string
]

{ #category : #'as yet unclassified' }
BlSerializer class >> verifyElement: aBlElement [
{ #category : #accessing }
BlSerializer class >> serializer [

(aBlElement isKindOf: BlElement) ifFalse: [
BlocSerializationError signal:
'Cannot serialize an objet which is not from BlElement class hierarchy' ]
serializer ifNil: [ serializer := BlStashSerializer ].
^ serializer
]

{ #category : #'see class side' }
Expand Down
26 changes: 26 additions & 0 deletions src/Bloc-Serialization/BlStashSerializer.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"
Serailizer class for Bloc
"
Class {
#name : #BlStashSerializer,
#superclass : #Object,
#traits : 'TBlSerializer',
#classTraits : 'TBlSerializer classTrait',
#category : #'Bloc-Serialization-Core'
}

{ #category : #'as yet unclassified' }
BlStashSerializer class >> materializeImplementation: anObject [

^ Stash new materialize: anObject
]

{ #category : #'as yet unclassified' }
BlStashSerializer class >> serializeImplementation: anObject [

^ Stash new serialize: anObject
]

{ #category : #'see class side' }
BlStashSerializer >> seeClassSide [
]
26 changes: 26 additions & 0 deletions src/Bloc-Serialization/BlStonSerializer.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"
Serailizer class for Bloc
"
Class {
#name : #BlStonSerializer,
#superclass : #Object,
#traits : 'TBlSerializer',
#classTraits : 'TBlSerializer classTrait',
#category : #'Bloc-Serialization-Core'
}

{ #category : #'as yet unclassified' }
BlStonSerializer class >> materializeImplementation: anObject [

^ STON fromString: anObject
]

{ #category : #'as yet unclassified' }
BlStonSerializer class >> serializeImplementation: anObject [

^ STON toStringPretty: anObject
]

{ #category : #'see class side' }
BlStonSerializer >> seeClassSide [
]
54 changes: 54 additions & 0 deletions src/Bloc-Serialization/TBlSerializer.trait.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Trait {
#name : #TBlSerializer,
#category : #'Bloc-Serialization'
}

{ #category : #serialization }
TBlSerializer classSide >> materialize: aString [
"Materialize a String into a BlElement and return it. Default materializer is STON."
| blElement |
aString isString not ifTrue:[ BlocMaterializationError signal: 'Cannot materialize a no String object into BlElement' ].
"Try to materialize and catch an error into a BlocMaterialization error"
[ blElement := STON fromString: aString ] onErrorDo: [ :e | BlocMaterializationError signal: 'Cannot support Bloc materialization of this String (', e asString, ')' ].
blElement ifNil:[ BlocMaterializationError signal: 'Bloc materialization result is nil' ].

^ blElement
]

{ #category : #serialization }
TBlSerializer classSide >> serialize: aBlElementOrABlElementsCollection [
"Serialize a BlElement or a list of BlElements into a String using a serializer. Default serializer is STON."

| string |
aBlElementOrABlElementsCollection isCollection
ifTrue: [ self verifyCollection: aBlElementOrABlElementsCollection ]
ifFalse: [ self verifyElement: aBlElementOrABlElementsCollection ].

"Try to serialize and catch an error into a BlocSerialization error"
[ string := STON toStringPretty: aBlElementOrABlElementsCollection ] onErrorDo: [ :e | BlocSerializationError signal: 'Cannot support serialization of this BlElement (', e asString, ')' ].

^ string
]

{ #category : #'as yet unclassified' }
TBlSerializer classSide >> verifyCollection: aCollection [

"Check if the collection is supported : can contains some BlElements"
aCollection isString ifTrue:[
BlocSerializationError signal: 'Cannot serialize a String'
].

aCollection isDictionary ifTrue:[
BlocSerializationError signal: 'Cannot serialize a Dictionary'
].

aCollection do: [ :each | self verifyElement: each ]
]

{ #category : #'as yet unclassified' }
TBlSerializer classSide >> verifyElement: aBlElement [

(aBlElement isKindOf: BlElement) ifFalse: [
BlocSerializationError signal:
'Cannot serialize an objet which is not from BlElement class hierarchy' ]
]
Loading