[GOODIE] Namespaces-gk

Alexandre Bergel bergel at iam.unibe.ch
Thu Apr 15 19:30:42 UTC 2004


Goran, 

please, some tests!
I have created some. Did I found a feature or a bug?
The tests are quite simple but apparently there is something I did not grab.

Cheers,
Alexandre

On Thu, Apr 15, 2004 at 03:13:15PM +0200, goran.krampe at bluefish.se wrote:
> Hi people!
> 
> Yiha, here comes a first WORKING implementation of "the little proposal"
> on Namespaces! It is fresh out of the debugger, with wrinkles and all.
> Play around with it and discuss it. Tested in latest 3.7beta, not tested
> in *anything else*.
> 
> And you say - Ok, but haven't followed the thread, what the heck is this
> crap?
> 
> It allows you to create classes with what I call "qualified names" like
> this:
> 
> 	MyNameSpace::MyClass
> 
> That is pretty much it. :) It is only one level deep, the namespaces
> have no inter-relationships (no inheritance or anything) and there are
> NO IMPORTS.
> 
> You just create your classes with qualified names and then code just
> like before - using *either* the qualified names or the unqualified -
> the tools will help you when there is doubt.
> 
> And there is nothing stopping you from installing this and still just go
> on like before using unqualified names!
> 
> IMHO it is pretty damn hard to make it any simpler than this.
> 
> regards, Göran
> 
> from preamble:
> 
> "Change Set:		Namespaces-gk
> Date:			15 April 2004
> Author:			Göran Krampe
> 
> This is the first working implementation of the 'little namespace
> proposal' I have posted about. It contains a bunch of base modifications
> and two new classes in category Namespaces. They have class comments. :)
> 
> The code can be made a bit more succinct - it has gone through a few
> hoops along the way.
> 
> Play with it, check out the feel. I have not yet implemented shyness
> btw.
> 
> AFAIK this is the simplest and most backwards compatible way of
> introducing namespaces.
> 
> Known issues:
> - Namespaces are automatically created for you but not removed when
> empty. Just use Smalltalk removeKey: #MySpace
> 
> - I have of course not gone through all the tools, but the browser seems
> to work as it should including rename/delete/copy class."!


> 


-- 
_,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:
Alexandre Bergel  http://www.iam.unibe.ch/~bergel
^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.
-------------- next part --------------
'From Squeak3.7beta of ''1 April 2004'' [latest update: #5878] on 15 April 2004 at 9:28:57 pm'!
"Change Set:		NamespaceTest
Date:			15 April 2004
Author:			Alexandre Bergel

Namespace Tests"!

Object subclass: #MyGreatTest2::MyGreatClass2
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Blah blah'!
Object subclass: #MyGreatTest::MyGreatClass
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Blah blah'!
TestCase subclass: #NamespaceTest
	instanceVariableNames: 'classCreated namespacesCreated'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Namespaces'!

!MyGreatTest::MyGreatClass methodsFor: 'as yet unclassified' stamp: 'ab 4/15/2004 21:28'!
bar ^ MyGreatClass! !

!MyGreatTest::MyGreatClass methodsFor: 'as yet unclassified' stamp: 'ab 4/15/2004 21:28'!
baz ^ MyGreatClass2! !

!MyGreatTest::MyGreatClass methodsFor: 'as yet unclassified' stamp: 'ab 4/15/2004 21:28'!
foo ^ Object! !


!NamespaceTest methodsFor: 'utility' stamp: 'ab 4/15/2004 20:58'!
classCreated
	classCreated ifNil: [ classCreated := OrderedCollection new].
	^ classCreated! !

!NamespaceTest methodsFor: 'utility' stamp: 'ab 4/15/2004 21:01'!
createClassNamed: aClassName
	^ self createClassNamed: aClassName namespaceNamed: #Smalltalk! !

!NamespaceTest methodsFor: 'utility' stamp: 'ab 4/15/2004 21:01'!
createClassNamed: aClassName namespaceNamed: aNamespaceNamed

	^ self createClassNamed: aClassName superclassNamed: #Object namespaceNamed: aNamespaceNamed! !

!NamespaceTest methodsFor: 'utility' stamp: 'ab 4/15/2004 21:17'!
createClassNamed: aClassName superclassNamed: aSuperclassName namespaceNamed: aNamespaceNamed

	| superclassRef cls |
	superclassRef := Smalltalk at: aSuperclassName.
	cls := superclassRef subclass: (aNamespaceNamed, '::', aClassName) asSymbol
		instanceVariableNames: ''
		classVariableNames: ''
		poolDictionaries: ''
		category: 'Blah blah'.
	self classCreated add: cls.
	(aNamespaceNamed ~~ #System) ifTrue: [self namespacesCreated add: aNamespaceNamed].
	^ cls! !

!NamespaceTest methodsFor: 'utility' stamp: 'ab 4/15/2004 21:12'!
namespacesCreated
	namespacesCreated ifNil: [ namespacesCreated := OrderedCollection new].
	^ namespacesCreated! !

!NamespaceTest methodsFor: 'utility' stamp: 'ab 4/15/2004 21:11'!
removeNameSpaceIfExists: aNamespaceName
	Smalltalk removeKey: aNamespaceName ifAbsent: []! !

!NamespaceTest methodsFor: 'Running' stamp: 'ab 4/15/2004 20:59'!
setUp
	classCreated := nil! !

!NamespaceTest methodsFor: 'Running' stamp: 'ab 4/15/2004 21:13'!
tearDown
	self classCreated do: [:cls| cls removeFromSystem].
	self namespacesCreated do: [:namespaceName| self removeNameSpaceIfExists: namespaceName]! !

!NamespaceTest methodsFor: 'testing' stamp: 'ab 4/15/2004 21:24'!
testClassCreation
	| nb |
	Smalltalk garbageCollect.
	self removeNameSpaceIfExists: #MyGreatTest.
	nb := Namespace allSpaces size.

	self createClassNamed: #MyGreatClass namespaceNamed: #MyGreatTest.
		
	self assert: Namespace allSpaces size = (nb + 1).

! !

!NamespaceTest methodsFor: 'testing' stamp: 'ab 4/15/2004 21:28'!
testClassCreation2
	| cls1 ns1 ns2 |
	cls1 := self createClassNamed: #MyGreatClass namespaceNamed: #MyGreatTest.
	self createClassNamed: #MyGreatClass2 namespaceNamed: #MyGreatTest2.
	
	"There should be a method on Namespace class"
	self assert: (Smalltalk includesKey: #MyGreatTest).
	self assert: (Smalltalk includesKey: #MyGreatTest2).

	ns1 := Namespace named: #MyGreatTest.
	ns2 := Namespace named: #MyGreatTest2.
		
	self assert: (Smalltalk includesKey: #MyGreatClass) not.
	self assert: (cls1 name == #MyGreatTest::MyGreatClass).

	cls1 compile: 'foo ^ Object'.
	cls1 compile: 'bar ^ MyGreatClass'.
	cls1 compile: 'baz ^ MyGreatClass2'.

	self assert: (cls1 >> #foo) getSourceFromFile = 'foo ^ Object'.
	self assert: (cls1 >> #bar) getSourceFromFile = 'bar ^ MyGreatClass'.
	self assert: (cls1 >> #baz) getSourceFromFile = 'baz ^ MyGreatClass2'.

	self assert: (cls1 new foo == (Smalltalk at: #Object)).
	self assert: (cls1 new bar == (ns1 at: #MyGreatClass)).
	self assert: (cls1 new baz == (ns2 at: #MyGreatClass2)).! !

NamespaceTest removeSelector: #tearsDown!
Smalltalk removeClassNamed: #AnObsoleteMyGreatTest::MyGreatClass!


More information about the Squeak-dev mailing list