Skip to content

IBlockListener per block, not BlockKind / precise block logging #2182

@ptomaszek

Description

@ptomaszek

Is your feature request related to a problem?

Yes - precise logging of particular block's text is not possible.

Describe the solution you'd like

Example code:

Repo with working example: https://github.com/ptomaszek/spock-IBlockListener
./mvnw test

Spock test:

    def "test"() {
        given: "given 1"
        log.info("given 1 custom log ")

        and: "given 2"
        log.info("given 2 custom log ")

        expect: "expect"
        log.info("expect custom log ")
    }

Listener and extension setup:

class IBlockLoggingExtension implements IGlobalExtension {

    @Override
    void visitSpec(SpecInfo spec) {
        spec.addListener(new BlockLoggingRunListener())
    }

    class BlockLoggingRunListener extends AbstractRunListener {

        @Override
        void beforeFeature(FeatureInfo feature) {
            feature.addBlockListener(new BlockLoggingListener())
        }
    }

    class BlockLoggingListener implements IBlockListener {

        @Override
        <S extends Specification> void blockEntered(S specificationInstance, BlockInfo blockInfo) {
            def logger = LoggerFactory.getLogger(specificationInstance.class)
            logger.info("{}", blockInfo.getTexts().join("; "))
        }
    }
}

What happens:

[main] INFO org.example.IBlockListenerSpec - given 1; given 2
[main] INFO org.example.IBlockListenerSpec - given 1 custom log 
[main] INFO org.example.IBlockListenerSpec - given 2 custom log 
[main] INFO org.example.IBlockListenerSpec - expect
[main] INFO org.example.IBlockListenerSpec - expect custom log 

What I would like to happen:

[main] INFO org.example.IBlockListenerSpec - given 1
[main] INFO org.example.IBlockListenerSpec - given 1 custom log 
[main] INFO org.example.IBlockListenerSpec - given 2
[main] INFO org.example.IBlockListenerSpec - given 2 custom log 
[main] INFO org.example.IBlockListenerSpec - expect
[main] INFO org.example.IBlockListenerSpec - expect custom log 

High level proposal

Extend IBlockListener with blockPartEntered and blockPartExited methods. An implementaion similar to the following should be possible:

        @Override
        <S extends Specification> void blockPartEntered(S specificationInstance, BlockPartInfo blockPartInfo) {
            def logger = LoggerFactory.getLogger(specificationInstance.class)
            logger.info("{}", blockPartInfo.getText())
        }

Describe alternatives you've considered

I am aware of the _ workaround (def _(def message) {...}) mentioned in couple of places, but I believe this is just a workaround, not the proper solution.
If Spock distinguishes blocks, then in should support proper block listener, not only block-kind listener.

Additional context

By looking at historical PRs and issues, I believe this request is valid to be considered for precise logging and possibly other custom aspects.

References:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions