Skip to content

Commit

Permalink
Fix #138 add source code of parent Specifications in Vivid Reports.
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoathaydes committed Mar 25, 2018
1 parent 5d1b7b2 commit 84a7352
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ import org.spockframework.util.Nullable
@CompileStatic
class SpecSourceCode {

private final Map<String, FeatureSourceCode> features = [ : ]
private final Map<String, FeatureSourceCode> sourceCodeByFeatureName = [ : ]
private final LinkedHashSet<SpecSourceCode> parents = [ ]

void addParent( SpecSourceCode parent ) {
parents << parent
}

void startBlock( MethodNode feature, String label, @Nullable String text ) {
features.get( feature.name, new FeatureSourceCode() ).startBlock( label, text )
sourceCodeByFeatureName.get( feature.name, new FeatureSourceCode() ).startBlock( label, text )
}

void addStatement( MethodNode feature, String statement, int lineNumber ) {
def currentFeature = features[ feature.name ]
def currentFeature = sourceCodeByFeatureName[ feature.name ]
if ( currentFeature ) {
statement = removeIndent( statement )
currentFeature.addStatement( statement, lineNumber )
Expand All @@ -30,7 +35,21 @@ class SpecSourceCode {
}

List<BlockCode> getBlocks( String featureName ) {
features.get( featureName )?.blocks ?: [ ]
List<BlockCode> result = sourceCodeByFeatureName[ featureName ]?.blocks
if ( result == null ) {
log.debug( 'Unable to find code for feature "{}", will try in parent Specs: {}', featureName, parents )

Iterator<SpecSourceCode> parentIterator = parents.iterator()
while ( result == null ) {
if ( parentIterator.hasNext() ) {
def parent = parentIterator.next()
result = parent.getBlocks( featureName )
} else {
break
}
}
}
return result ?: [ ]
}

static String removeIndent( String code ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.athaydes.spockframework.report.vivid

import com.athaydes.spockframework.report.util.Utils
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.codehaus.groovy.ast.MethodNode
Expand All @@ -20,7 +21,7 @@ class SpecSourceCodeCollector {
final SourceLookup sourceLookup
final ModuleNode module

private final Map<String, SpecSourceCode> specSourceCodeByClassName = [ : ] as ConcurrentHashMap
private static final Map<String, SpecSourceCode> specSourceCodeByClassName = [ : ] as ConcurrentHashMap

@Nullable
private String className
Expand All @@ -33,6 +34,7 @@ class SpecSourceCodeCollector {
}

void setClassName( String className ) {
log.debug( "Collecting code for class {}", className )
this.@className = className
specSourceCodeByClassName[ className ] = new SpecSourceCode()
}
Expand All @@ -43,7 +45,17 @@ class SpecSourceCodeCollector {

@Nullable
SpecSourceCode getResultFor( String className ) {
specSourceCodeByClassName.remove( className )
def sourceCode = specSourceCodeByClassName[ className ]
if ( sourceCode ) {
def parentSpecs = Utils.getParentSpecNames( className )
for ( parentSpec in parentSpecs ) {
def parentCode = specSourceCodeByClassName[ parentSpec ]
if ( parentCode ) {
sourceCode.addParent( parentCode )
}
}
}
sourceCode
}

void add( Statement statement ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class SpecSourceCodeReader {
@Nullable
private SpecSourceCode specSourceCode

private final VividAstInspector inspector = new VividAstInspector()

void read( SpecData data ) {
try {
VividAstInspector inspector = new VividAstInspector()

File file = Utils.getSpecFile( testSourceRoots, data )
specSourceCode = inspector.load( file, Utils.getSpecClassName( data ) )
} catch ( Exception e ) {
Expand All @@ -26,6 +26,6 @@ class SpecSourceCodeReader {
}

List<BlockCode> getBlocks( FeatureInfo feature ) {
return specSourceCode?.getBlocks( feature.name ) ?: []
return specSourceCode?.getBlocks( feature.name ) ?: [ ]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.athaydes.spockframework.report.vivid

import com.athaydes.spockframework.report.vivid.VividAstInspector.AstSuccessfullyCaptured
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.codehaus.groovy.ast.ClassCodeVisitorSupport
Expand Down

0 comments on commit 84a7352

Please sign in to comment.