Skip to content

Commit

Permalink
Added instance variable getter (#134)
Browse files Browse the repository at this point in the history
* Added instance variable getter

* Added tests for invalid instance variable setting and getting
  • Loading branch information
mtabacman authored Feb 20, 2025
1 parent dcd3de1 commit cd2be3c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .project
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
'srcDirectory' : 'source'
'srcDirectory' : 'source',
'tags': [ 'Buenos Aires Smalltalk' ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ LanguagePlatform >> includesGlobalNamed: aSymbol [
^ self subclassResponsibility
]

{ #category : 'reflection' }
LanguagePlatform >> instanceVariableNamed: name on: object [

^ self subclassResponsibility
]

{ #category : 'message digest' }
LanguagePlatform >> messageDigest: string [
"Returns a message digest for the given string, the hashing algorithm can be platform specific.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ GemStone64Platform >> includesGlobalNamed: aSymbol [
notNil
]

{ #category : 'reflection' }
GemStone64Platform >> instanceVariableNamed: name on: object [

| index |
index := (object class allInstVarNames collect: #asSymbol)
indexOf: name asSymbol
ifAbsent: [ self error: ('<1s> not found in <2p>' expandMacrosWith: name asString with: object) ].
^ object instVarAt: index

]

{ #category : 'message digest' }
GemStone64Platform >> messageDigest: string [

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ PharoPlatform >> includesGlobalNamed: aSymbol [
^ Smalltalk globals includesKey: aSymbol
]

{ #category : 'reflection' }
PharoPlatform >> instanceVariableNamed: name on: object [

^ object instVarNamed: name
]

{ #category : 'message digest' }
PharoPlatform >> messageDigest: string [

Expand Down
27 changes: 27 additions & 0 deletions source/Buoy-Metaprogramming-Tests/LanguagePlatformTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ LanguagePlatformTest >> testForkNamedAt [
self assert: wasEvaluated
]

{ #category : 'tests' }
LanguagePlatformTest >> testGetInvalidInstanceVariable [

self
should: [ LanguagePlatform current instanceVariableNamed: 'variableForFailing' on: self ]
raise: Error
]

{ #category : 'tests' }
LanguagePlatformTest >> testGlobalNamedIfAbsent [

Expand Down Expand Up @@ -107,6 +115,17 @@ LanguagePlatformTest >> testIncludesGlobalNamed [
#AnImplausibleGlobalName)
]

{ #category : 'tests' }
LanguagePlatformTest >> testInstanceVariableNamedOn [

| value |
LanguagePlatform current atInstanceVariableNamed: 'variableForTesting' on: self put: 8.

value := LanguagePlatform current instanceVariableNamed: 'variableForTesting' on: self.

self assert: value equals: 8
]

{ #category : 'tests' }
LanguagePlatformTest >> testMessageDigest [

Expand Down Expand Up @@ -193,3 +212,11 @@ LanguagePlatformTest >> testRemoveGlobalNamedIfAbsent [
ifAbsent: [ wasFound := false ].
self deny: wasFound
]

{ #category : 'tests' }
LanguagePlatformTest >> testSetInvalidInstanceVariable [

self
should: [ LanguagePlatform current atInstanceVariableNamed: 'variableForFailing' on: self put: 8 ]
raise: Error
]

0 comments on commit cd2be3c

Please sign in to comment.