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

Fix a bug with strings as default value for a property. #857

Merged
merged 1 commit into from
Nov 26, 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 @@ -212,6 +212,8 @@ FmxMBRingEnvironment >> slotNamed: slotName cardinality: cardinality type: type

{ #category : #initialization }
FmxMBRingEnvironment >> slotNamed: slotName defaultValue: aValue [

aValue ifNil: [ ^ self slotNamed: slotName ].
^ (RGUnknownSlot named: slotName asSymbol) expression: ('FMProperty defaultValue: {1}' format: { aValue })
^ (RGUnknownSlot named: slotName asSymbol) expression:
('FMProperty defaultValue: {1}' format: { aValue printString })
]
4 changes: 2 additions & 2 deletions src/Famix-MetamodelBuilder-Core/FmxMBTypedProperty.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ FmxMBTypedProperty >> generateGetterIn: aClassOrTrait [
pragmaFormat := self defaultValue ifNil: [ '<FMProperty: #{1} type: #{2}>' ]
ifNotNil: [ '<FMProperty: #{1} type: #{2} defaultValue: {3}>' ].
propertyDefinition := pragmaFormat
format: { self name. self propertyType. self defaultValue }.
format: { self name. self propertyType. self defaultValue printString}.

commentDefinition := self comment
ifNotEmpty: [ '<FMComment: {1}>' format: { self comment printString } ].
Expand All @@ -58,7 +58,7 @@ FmxMBTypedProperty >> generateGetterIn: aClassOrTrait [
ifNotEmpty: [ s tab; nextPutAll: commentDefinition; cr].
s tab; nextPutAll: '^ '; nextPutAll: self name.
self defaultValue ifNotNil: [
s nextPutAll: (' ifNil: [ {1} := {2} ]' format: { self name. self defaultValue }) ] ].
s nextPutAll: (' ifNil: [ {1} := {2} ]' format: { self name. self defaultValue printString }) ] ].

self builder environment compile: methodSource in: aClassOrTrait classified: 'accessing'.

Expand Down
20 changes: 18 additions & 2 deletions src/Famix-Test6-Entities/FamixTest6Bacon.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

| Name | Type | Default value | Comment |
|---|
| `brand` | `String` | '' | |
| `eggs` | `Number` | 12 | |
| `isFood` | `Boolean` | true | |

Expand All @@ -12,8 +13,9 @@ Class {
#name : #FamixTest6Bacon,
#superclass : #FamixTest6Entity,
#instVars : [
'#isFood => FMProperty defaultValue: true',
'#eggs => FMProperty defaultValue: 12'
'#brand => FMProperty defaultValue: \'\'',
'#eggs => FMProperty defaultValue: 12',
'#isFood => FMProperty defaultValue: true'
],
#category : #'Famix-Test6-Entities-Entities'
}
Expand All @@ -27,6 +29,20 @@ FamixTest6Bacon class >> annotation [
^ self
]

{ #category : #accessing }
FamixTest6Bacon >> brand [

<FMProperty: #brand type: #String defaultValue: ''>
<generated>
^ brand ifNil: [ brand := '' ]
]

{ #category : #accessing }
FamixTest6Bacon >> brand: anObject [
<generated>
brand := anObject
]

{ #category : #accessing }
FamixTest6Bacon >> eggs [

Expand Down
35 changes: 26 additions & 9 deletions src/Famix-Test6-Tests/FamixTest6Test.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,37 @@ Class {
#category : #'Famix-Test6-Tests'
}

{ #category : #running }
FamixTest6Test >> testDefaultValueEmptyString [

| bacon brandProperty newBrand |
bacon := FamixTest6Bacon new.
brandProperty := bacon mooseDescription properties detect: [ :each |
each name = #brand ].

self assert: bacon brand equals: ''.
self assert: brandProperty defaultValue equals: ''.

newBrand := 'WonderfulBacon'.
bacon brand: newBrand.
self assert: bacon brand equals: newBrand.
self assert: brandProperty defaultValue equals: ''
]

{ #category : #running }
FamixTest6Test >> testDefaultValueOnCreation [

| bacon foodProperty |

| bacon brandProperty |
bacon := FamixTest6Bacon new.
foodProperty := (bacon mooseDescription properties select: [ :each | each name = #isFood ]) at: 1.

self assert: bacon isFood equals: true.
self assert: foodProperty defaultValue equals: true.

brandProperty := (bacon mooseDescription properties select: [ :each |
each name = #isFood ]) at: 1.

self assert: bacon isFood.
self assert: brandProperty defaultValue.

bacon isFood: false.
self assert: bacon isFood equals: false.
self assert: foodProperty defaultValue equals: true.
self deny: bacon isFood.
self assert: brandProperty defaultValue
]

{ #category : #running }
Expand Down
3 changes: 2 additions & 1 deletion src/Famix-TestGenerators/FamixTest6Generator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ FamixTest6Generator >> defineProperties [
super defineProperties.
bacon property: #isFood type: #Boolean defaultValue: true.
bacon property: #eggs type: #Number defaultValue: 12.
spam property: #isSomething type: #Boolean defaultValue: false
spam property: #isSomething type: #Boolean defaultValue: false.
bacon property: #brand type: #String defaultValue: ''
]
Loading