Skip to content

Commit

Permalink
Merge pull request #181 from TNG/scenario-sections
Browse files Browse the repository at this point in the history
Add possibility to add sections to scenarios
  • Loading branch information
janschaefer committed Dec 29, 2015
2 parents 2ee38f8 + b0db198 commit e35f74b
Show file tree
Hide file tree
Showing 31 changed files with 464 additions and 112 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# v0.11.0
## New Features

### Sections

Scenarios can have sections now. This allows you to structure larger scenarios into several parts with a title. [PR#181](https://github.com/TNG/JGiven/pull/181)

Example:
```
section("This is a section title");
given().something();
when().something();
section("This is another section title");
when().something_else();
then().something();
```

# v0.10.1

## New Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public THEN then() {
return getScenario().then();
}

/**
* Adds a new section to the scenario
* <h1>EXPERIMENTAL FEATURE</h1>
* This is an experimental feature. It might change in the future.
* If you have any feedback regarding this feature, please let us know
* by creating an issue at https://github.com/TNG/JGiven/issues
* @param sectionTitle the title of the section
* @since 0.11.0
*/
public void section( String sectionTitle ) {
getScenario().section( sectionTitle );
}

public void wireSteps( CanWire canWire ) {
getScenario().wireSteps( canWire );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.tngtech.jgiven.confg;

/**
* Represents a configuration value
*/
public enum ConfigValue {
TRUE,
FALSE,
AUTO;

public static ConfigValue fromString( String value ) {
if( "false".equalsIgnoreCase( value ) ) {
return FALSE;
}

if( "true".equalsIgnoreCase( value ) ) {
return TRUE;
}

return AUTO;
}
}
6 changes: 4 additions & 2 deletions jgiven-core/src/main/java/com/tngtech/jgiven/impl/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;

import com.tngtech.jgiven.confg.ConfigValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -16,6 +17,7 @@ public class Config {

private static final String TRUE = "true";
private static final String FALSE = "false";
private static final String AUTO = "auto";
private static final String JGIVEN_REPORT_ENABLED = "jgiven.report.enabled";
private static final String JGIVEN_REPORT_DIR = "jgiven.report.dir";
private static final String JGIVEN_REPORT_TEXT = "jgiven.report.text";
Expand Down Expand Up @@ -57,8 +59,8 @@ public void setReportEnabled( boolean enabled ) {
System.setProperty( JGIVEN_REPORT_ENABLED, "" + enabled );
}

public boolean textColorEnabled() {
return TRUE.equalsIgnoreCase( System.getProperty( JGIVEN_REPORT_TEXT_COLOR, TRUE ) );
public ConfigValue textColorEnabled() {
return ConfigValue.fromString( System.getProperty( JGIVEN_REPORT_TEXT_COLOR, AUTO ) );
}

public boolean textReport() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,5 @@ public THEN then( String translatedGiven ) {
addIntroWord( translatedGiven );
return getThenStage();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,17 @@ protected void assertNotInitialized() {
AssertionUtil.assertTrue( !initialized, "Scenario is already initialized" );
}

/**
* Adds a new section to the scenario
* <h1>EXPERIMENTAL FEATURE</h1>
* This is an experimental feature. It might change in the future.
* If you have any feedback regarding this feature, please let us know
* by creating an issue at https://github.com/TNG/JGiven/issues
* @param sectionTitle the title of the section
* @since 0.11.0
*/
public void section( String sectionTitle ) {
executor.addSection( sectionTitle );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public enum State {

void addIntroWord( String word );

void addSection( String sectionTitle );

void readScenarioState( Object object );

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void addStepMethod( Method paramMethod, List<NamedArgument> arguments, In
StepModel createStepModel( Method paramMethod, List<NamedArgument> arguments, InvocationMode mode ) {
StepModel stepModel = new StepModel();

stepModel.name = getDescription( paramMethod );
stepModel.setName( getDescription( paramMethod ) );

ExtendedDescription extendedDescriptionAnnotation = paramMethod.getAnnotation( ExtendedDescription.class );
if( extendedDescriptionAnnotation != null ) {
Expand All @@ -113,10 +113,10 @@ StepModel createStepModel( Method paramMethod, List<NamedArgument> arguments, In
ParameterFormattingUtil parameterFormattingUtil = new ParameterFormattingUtil( configuration );
List<ObjectFormatter<?>> formatters = parameterFormattingUtil.getFormatter( paramMethod.getParameterTypes(), getNames( arguments ),
paramMethod.getParameterAnnotations() );
stepModel.words = new StepFormatter( stepModel.name, nonHiddenArguments, formatters ).buildFormattedWords();
stepModel.setWords( new StepFormatter( stepModel.getName(), nonHiddenArguments, formatters ).buildFormattedWords() );

if( introWord != null ) {
stepModel.words.add( 0, introWord );
stepModel.addIntroWord( introWord );
introWord = null;
}

Expand Down Expand Up @@ -533,6 +533,15 @@ public void extendedDescriptionUpdated( String extendedDescription ) {
currentStep.setExtendedDescription( extendedDescription );
}

@Override
public void sectionAdded( String sectionTitle ) {
StepModel stepModel = new StepModel();
stepModel.setName( sectionTitle );
stepModel.addWords( new Word( sectionTitle ) );
stepModel.setIsSectionTitle( true );
getCurrentScenarioCase().addStep( stepModel );
}

public ReportModel getReportModel() {
return reportModel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,9 @@ public void failIfPass() {
failIfPass = true;
}

@Override
public void addSection( String sectionTitle ) {
listener.sectionAdded( sectionTitle );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public void attachmentAdded( Attachment attachment ) {}

@Override
public void extendedDescriptionUpdated( String extendedDescription ) {}

@Override
public void sectionAdded( String sectionTitle ) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface ScenarioListener {
void attachmentAdded( Attachment attachment );

void extendedDescriptionUpdated( String extendedDescription );

void sectionAdded( String sectionTitle );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (C) 2009, Progress Software Corporation and/or its
* subsidiaries or affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tngtech.jgiven.impl.util;

import static org.fusesource.jansi.internal.CLibrary.STDOUT_FILENO;
import static org.fusesource.jansi.internal.CLibrary.isatty;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.fusesource.jansi.AnsiOutputStream;
import org.fusesource.jansi.WindowsAnsiOutputStream;

/**
* This is actually a modified copy of AnsiConsole from the jansi project.
* We had to copy it, because we want to be able to disable the tty detection.
*/
public class AnsiUtil {
public static OutputStream wrapOutputStream( final OutputStream stream, boolean ttyDetection ) {

String os = System.getProperty( "os.name" );
if( os.startsWith( "Windows" ) ) {

// On windows we know the console does not interpret ANSI codes..
try {
return new WindowsAnsiOutputStream( stream );
} catch( Throwable ignore ) {
// this happens when JNA is not in the path.. or
// this happens when the stdout is being redirected to a file.
}

// Use the ANSIOutputStream to strip out the ANSI escape sequences.
return new AnsiOutputStream( stream );
}

if( ttyDetection ) {
// We must be on some unix variant..
try {
// If we can detect that stdout is not a tty.. then setup
// to strip the ANSI sequences..
int rc = isatty( STDOUT_FILENO );
if( rc == 0 ) {
return new AnsiOutputStream( stream );
}

// These erros happen if the JNI lib is not available for your platform.
} catch( NoClassDefFoundError ignore ) {} catch( UnsatisfiedLinkError ignore ) {}
}

// By default we assume your Unix tty can handle ANSI codes.
// Just wrap it up so that when we get closed, we reset the
// attributes.
return new FilterOutputStream( stream ) {
@Override
public void close() throws IOException {
write( AnsiOutputStream.REST_CODE );
flush();
super.close();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import java.io.*;

import org.fusesource.jansi.AnsiConsole;

import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.tngtech.jgiven.confg.ConfigValue;

public class PrintWriterUtil {

public static PrintWriter getPrintWriter( File file ) {
try {
return new PrintWriter( file, Charsets.UTF_8.name() );
Expand All @@ -17,12 +15,15 @@ public static PrintWriter getPrintWriter( File file ) {
}
}

public static PrintWriter getPrintWriter( OutputStream outputStream, boolean withColor ) {
if( withColor ) {
outputStream = AnsiConsole.wrapOutputStream( outputStream );
public static PrintWriter getPrintWriter( OutputStream outputStream, ConfigValue colorConfig ) {
OutputStream wrappedStream = outputStream;

if( colorConfig == ConfigValue.TRUE || colorConfig == ConfigValue.AUTO ) {
wrappedStream = AnsiUtil.wrapOutputStream( outputStream, colorConfig == ConfigValue.AUTO );
}

try {
return new PrintWriter( new OutputStreamWriter( outputStream, Charsets.UTF_8.name() ) );
return new PrintWriter( new OutputStreamWriter( wrappedStream, Charsets.UTF_8.name() ) );
} catch( UnsupportedEncodingException e ) {
throw Throwables.propagate( e );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public void visit( StepModel stepModel ) {
this.noDataTablePossible = true;
}

for( Word word : stepModel.words ) {
for( Word word : stepModel.getWords() ) {
if( word.isArg() && !word.isDataTable() ) {
ArgumentHolder holder = new ArgumentHolder();
holder.word = word;
Expand Down
Loading

0 comments on commit e35f74b

Please sign in to comment.