refactoring_custom/SmallSense__CustomTestCaseSetUpCodeGenerator.st
changeset 833 297eb38e4eee
parent 832 59c248fc74f0
child 943 43d408a5e517
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/refactoring_custom/SmallSense__CustomTestCaseSetUpCodeGenerator.st	Sun Jun 14 09:46:51 2015 +0100
@@ -0,0 +1,180 @@
+"
+A custom code generation and refactoring support for Smalltalk/X
+Copyright (C) 2013-2015 Jakub Nesveda
+Copyright (C) 2013-now  Jan Vrany
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+"
+"{ Package: 'stx:goodies/smallsense/refactoring_custom' }"
+
+"{ NameSpace: SmallSense }"
+
+CustomCodeGenerator subclass:#CustomTestCaseSetUpCodeGenerator
+	instanceVariableNames:'samePackageAsTestedClass'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Interface-Refactoring-Custom-Generators'
+!
+
+!CustomTestCaseSetUpCodeGenerator class methodsFor:'documentation'!
+
+copyright
+"
+A custom code generation and refactoring support for Smalltalk/X
+Copyright (C) 2013-2015 Jakub Nesveda
+Copyright (C) 2013-now  Jan Vrany
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+"
+! !
+
+!CustomTestCaseSetUpCodeGenerator class methodsFor:'accessing-presentation'!
+
+description
+    ^ 'Generates initial #setUp for test cases'
+
+    "Modified: / 16-09-2014 / 11:24:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+group
+    "Returns a collection strings describing a group to which
+     receiver belongs. A groups may be nested hence the array of
+     strings. For example for subgroup 'Accessors' in group 'Generators'
+     this method should return #('Generators' 'Accessors')."
+
+    "/ By default return an empty array which means the item will appear
+    "/ in top-level group.
+    ^ #('Testing')
+
+    "Created: / 05-08-2014 / 14:14:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+label
+    ^ 'TestCase setUp method'
+
+    "Modified: / 05-08-2014 / 14:13:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!CustomTestCaseSetUpCodeGenerator class methodsFor:'queries'!
+
+availableInContext:aCustomContext
+    ^ aCustomContext selectedClasses anySatisfy: [:cls | cls isMetaclass not and:[ cls inheritsFrom: Smalltalk::TestCase ] ]
+
+    "Modified: / 12-06-2015 / 20:46:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+availableInPerspective:aCustomPerspective
+    ^ aCustomPerspective isClassPerspective
+
+    "Modified: / 05-08-2014 / 13:49:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!CustomTestCaseSetUpCodeGenerator methodsFor:'accessing'!
+
+samePackageAsTestedClass
+    "Returns true when we should assign TestCase class 
+    to the same package as tested class."
+
+    ^ samePackageAsTestedClass
+
+    "Created: / 15-11-2014 / 11:54:37 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+!
+
+samePackageAsTestedClass: aBoolean
+    "see samePackageAsTestedClass"
+
+    samePackageAsTestedClass := aBoolean
+
+    "Created: / 15-11-2014 / 11:56:36 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+! !
+
+!CustomTestCaseSetUpCodeGenerator methodsFor:'accessing - defaults'!
+
+defaultSamePackageAsTestedClass
+    "default value for samePackageAsTestedClass"
+
+    ^ true
+
+    "Created: / 15-11-2014 / 12:21:40 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+! !
+
+!CustomTestCaseSetUpCodeGenerator methodsFor:'executing'!
+
+buildForClass: class
+    | source category superHasSetup current package |
+
+    current := class.
+    superHasSetup := false.
+    [ superHasSetup not and:[ current isNil not ] ] whileTrue:[
+        superHasSetup := current includesSelector: #setUp.
+        superHasSetup ifFalse:[
+            current := current superclass.
+        ].
+    ].
+    superHasSetup ifTrue:[ 
+        source := 'setUp
+    super setUp.
+
+    "Add your own code here..."
+'.
+        category := (current compiledMethodAt: #setUp) category.
+    ] ifFalse:[ 
+        source := 'setUp
+    "/ super setUp.
+
+    "Add your own code here..."
+'.
+        category := (Smalltalk::TestCase compiledMethodAt: #setUp) category.
+    ].
+
+    package := PackageId noProjectID.
+    samePackageAsTestedClass ? self defaultSamePackageAsTestedClass ifTrue: [ 
+        package := class package
+    ].
+
+    model createMethod
+        class: class;
+        source: source;
+        category: category;
+        package: package;
+        compile.
+
+    "Created: / 05-08-2014 / 14:17:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 31-01-2015 / 18:32:53 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+    "Modified: / 12-06-2015 / 20:46:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+buildInContext:aCustomContext
+    aCustomContext selectedClasses do:[:cls |  
+        (cls isMetaclass not and:[ cls inheritsFrom: Smalltalk::TestCase ]) ifTrue:[ 
+            self buildForClass: cls.
+        ].
+    ]
+
+    "Modified: / 12-06-2015 / 20:46:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+