Skip to content

Commit dba9ddf

Browse files
authored
Merge pull request #120 from AntoninGoslin/fixing_allPropertiesDo_duplications_v2
Fixing allPropertiesDo duplications v2
2 parents 3b2d156 + 2411b00 commit dba9ddf

3 files changed

Lines changed: 87 additions & 14 deletions

File tree

src/Fame-Core/FM3Class.class.st

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,6 @@ FM3Class >> accept: aVisitor [
5959
^ aVisitor visitClass: self
6060
]
6161

62-
{ #category : 'enumerating' }
63-
FM3Class >> allPropertiesDo: block [
64-
properties do: block.
65-
self superclass ifNotNil: [ :class | class allPropertiesDo: block ].
66-
self traits do: [ :trait | trait allPropertiesDo: block ]
67-
]
68-
6962
{ #category : 'accessing' }
7063
FM3Class >> allSubclasses [
7164
| all |
@@ -111,6 +104,18 @@ FM3Class >> createInstance [
111104
ifNotNil: [ implementingClass new ]
112105
]
113106

107+
{ #category : 'accessing-query' }
108+
FM3Class >> fillPropertiesDictionary: nameDict [
109+
"this method is used to avoid code duplication when filling dictionnary"
110+
111+
super fillPropertiesDictionary: nameDict.
112+
113+
"adding superclasses"
114+
self superclass ifNotNil: [ :class |
115+
class allProperties do: [ :each |
116+
nameDict at: each name ifAbsentPut: [ each ] ] ]
117+
]
118+
114119
{ #category : 'testing' }
115120
FM3Class >> hasPackage [
116121
^ package isNotNil

src/Fame-Core/FM3Type.class.st

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Class {
44
#instVars : [
55
'package',
66
'properties',
7-
'traits'
7+
'traits',
8+
'cachedAllProperties'
89
],
910
#category : 'Fame-Core-Model',
1011
#package : 'Fame-Core',
@@ -46,19 +47,21 @@ FM3Type >> allPrimitiveProperties [
4647

4748
{ #category : 'accessing-query' }
4849
FM3Type >> allProperties [
50+
4951
<FMProperty: #allProperties type: 'FM3.Property'>
5052
<multivalued>
5153
<derived>
52-
| nameDict |
53-
nameDict := Dictionary new: 60. "estimated initial size."
54-
self allPropertiesDo: [ :each | nameDict at: each name ifAbsentPut: [ each ] ].
55-
^ nameDict values asArray
54+
^ cachedAllProperties ifNil: [
55+
| nameDict |
56+
nameDict := Dictionary new: 60. "estimated initial size."
57+
self fillPropertiesDictionary: nameDict.
58+
cachedAllProperties := nameDict values asArray ]
5659
]
5760

5861
{ #category : 'enumerating' }
5962
FM3Type >> allPropertiesDo: block [
60-
self properties do: block.
61-
self traits do: [ :trait | trait allPropertiesDo: block ]
63+
64+
self allProperties do: block
6265
]
6366

6467
{ #category : 'accessing-query' }
@@ -75,6 +78,18 @@ FM3Type >> classUsers [
7578
^ { self }
7679
]
7780

81+
{ #category : 'as yet unclassified' }
82+
FM3Type >> fillPropertiesDictionary: nameDict [
83+
"this method is used to avoid code duplication when filling dictionnary"
84+
85+
self properties do: [ :each |
86+
nameDict at: each name ifAbsentPut: [ each ] ].
87+
88+
self traits do: [ :trait |
89+
trait allPropertiesDo: [ :each |
90+
nameDict at: each name ifAbsentPut: [ each ] ] ]
91+
]
92+
7893
{ #category : 'initialization' }
7994
FM3Type >> initialize [
8095
super initialize.

src/Fame-Tests/FM3ClassTest.class.st

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,59 @@ FM3ClassTest >> testAllProperties [
2424
self denyEmpty: element allProperties
2525
]
2626

27+
{ #category : 'tests' }
28+
FM3ClassTest >> testAllPropertiesDoGiveSameElementsAsAllProperties [
29+
30+
| superclass class listWithPropDo listWithoutPropDo |
31+
superclass := self actualClass named: 'Superclass'.
32+
class := self actualClass named: 'Class'.
33+
class superclass: superclass.
34+
35+
superclass properties: {
36+
(FM3Property named: #isDead).
37+
(FM3Property named: #numberOfLinesOfCode) }.
38+
class properties: {
39+
(FM3Property named: #isDead).
40+
(FM3Property named: #cc) }.
41+
42+
"collecting with allPropertiesDo:"
43+
listWithPropDo := OrderedCollection new.
44+
class allPropertiesDo: [ :prop | listWithPropDo add: prop name ].
45+
46+
"collecting with allProperties"
47+
listWithoutPropDo := class allProperties collect: [ :prop |
48+
prop name ].
49+
50+
"the two lists should be equals"
51+
self
52+
assert: listWithPropDo asArray sorted equals: listWithoutPropDo asArray sorted
53+
]
54+
55+
{ #category : 'tests' }
56+
FM3ClassTest >> testAllPropertiesHasNoDuplication [
57+
58+
| superclass class numberOfProperties expectedNumberOfProperties |
59+
superclass := self actualClass named: 'Superclass'.
60+
class := self actualClass named: 'Class'.
61+
class superclass: superclass.
62+
63+
superclass properties: {
64+
(FM3Property named: #isDead).
65+
(FM3Property named: #numberOfLinesOfCode) }.
66+
class properties: {
67+
(FM3Property named: #isDead).
68+
(FM3Property named: #cc) }.
69+
70+
numberOfProperties := 0.
71+
class allPropertiesDo: [ :each |
72+
numberOfProperties := numberOfProperties + 1 ].
73+
74+
expectedNumberOfProperties := class allProperties size.
75+
76+
self assert: numberOfProperties equals: expectedNumberOfProperties.
77+
self assert: numberOfProperties equals: 3
78+
]
79+
2780
{ #category : 'tests' }
2881
FM3ClassTest >> testAllPropertiesMoreThanProperties [
2982
element := metaMetamodel elementNamed: 'FM3.Class'.

0 commit comments

Comments
 (0)