smalltalkpharo

Class Test generate in Pharo via source code


I have a problem with my pharo code. I would like to generate a test class via the source code and that this class is visible in the packages of the browser system. But I don't know why it doesn't appear among the packages. Here is some of the code I wrote:

buildTestClassDefinitionFrom: aClass

    ^ 'TestCase subclass: ', (self buildTestClassNameFrom: aClass) printString, '
    instanceVariableNames: ''''
    classVariableNames: ''''
    poolDictionaries: ''''
    package: ''',(self buildTestPackageNameFrom:aClass),''''

and

buildTestPackageNameFrom:aClass

    ^ aClass package name asString, '-Tests'

and

buildTestClassNameFrom: aClass
    
    ^ (aClass name asString,'Test') asSymbol

So if I take an example, by sending Car as a class, I would like to see CarTest appear in the packages of the system browser.


Solution

  • It looks like you are returning an instance of String, not a new Class. That is, you are returning the code that could generate a Class, but you are not executing the code. Try removing most of the quotes so that you are actually sending the subclass: message to TestCase.

    Try the following:

    testClassFor: aClass
    
        ^ TestCase subclass: (self buildTestClassNameFrom: aClass)
            instanceVariableNames: ''
            classVariableNames: ''
            poolDictionaries: ''
            package: (self buildTestPackageNameFrom: aClass)