From 300a669d7cc6f67553f7000a916d42f20a31d1ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4fer?= Date: Sat, 4 Apr 2015 19:57:46 +0200 Subject: [PATCH] Made $$ escape a single $ in a step method (fixed #19) --- .../tngtech/jgiven/report/model/ReportModelBuilder.java | 9 +++++++-- .../com/tngtech/jgiven/report/model/StepFormatter.java | 4 +++- .../com/tngtech/jgiven/format/StepFormatterTest.java | 1 + .../jgiven/report/model/ReportModelBuilderTest.java | 2 -- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModelBuilder.java b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModelBuilder.java index 232620429a..bd82160aba 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModelBuilder.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModelBuilder.java @@ -87,6 +87,12 @@ private String camelCaseToReadableText( String camelCase ) { } public void addStepMethod( Method paramMethod, List arguments, InvocationMode mode ) { + StepModel stepModel = createStepModel( paramMethod, arguments, mode ); + currentStep = stepModel; + writeStep( stepModel ); + } + + StepModel createStepModel( Method paramMethod, List arguments, InvocationMode mode ) { StepModel stepModel = new StepModel(); Description description = paramMethod.getAnnotation( Description.class ); @@ -112,8 +118,7 @@ public void addStepMethod( Method paramMethod, List arguments, In } stepModel.setStatus( mode.toStepStatus() ); - currentStep = stepModel; - writeStep( stepModel ); + return stepModel; } private List filterHiddenArguments( List arguments, Annotation[][] parameterAnnotations ) { diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/StepFormatter.java b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/StepFormatter.java index cbc1ca2134..65ece38366 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/StepFormatter.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/StepFormatter.java @@ -71,7 +71,9 @@ public List buildFormattedWords() { List words = Splitter.on( ' ' ).splitToList( stepDescription ); for( int i = 0; i < words.size(); i++ ) { String word = words.get( i ); - if( word.startsWith( "$" ) ) { + if( word.equals( "$$" ) ) { // $$ escapes a single $ + formattedWords.add( new Word( "$" ) ); + } else if( word.startsWith( "$" ) ) { int argEnd = findArgumentEnd( i, words ); formatArgument( formattedWords, argCount, word ); if( argEnd != -1 ) { diff --git a/jgiven-core/src/test/java/com/tngtech/jgiven/format/StepFormatterTest.java b/jgiven-core/src/test/java/com/tngtech/jgiven/format/StepFormatterTest.java index ddab652723..c9ae34f581 100644 --- a/jgiven-core/src/test/java/com/tngtech/jgiven/format/StepFormatterTest.java +++ b/jgiven-core/src/test/java/com/tngtech/jgiven/format/StepFormatterTest.java @@ -61,6 +61,7 @@ public static Object[][] formatterTestCases() { { "$or should not$", asList( false ), new NotFormatter(), "", "not" }, { "$or not$", asList( false ), new NotFormatter(), "", "not" }, { "$", asList( true ), null, "", "true" }, + { "$$ foo", asList( true ), null, "", "\\$ foo true" }, { "$", asList( 5d ), new PrintfFormatter(), "%.2f", "5[.,]00" }, }; } diff --git a/jgiven-core/src/test/java/com/tngtech/jgiven/report/model/ReportModelBuilderTest.java b/jgiven-core/src/test/java/com/tngtech/jgiven/report/model/ReportModelBuilderTest.java index 880fd2d886..faecaa8ec7 100644 --- a/jgiven-core/src/test/java/com/tngtech/jgiven/report/model/ReportModelBuilderTest.java +++ b/jgiven-core/src/test/java/com/tngtech/jgiven/report/model/ReportModelBuilderTest.java @@ -223,7 +223,5 @@ public void abstract_steps_should_appear_in_the_report_model() throws Throwable getScenario().finished(); StepModel step = getScenario().getModel().getFirstStepModelOfLastScenario(); assertThat( step.words.get( 0 ).getFormattedValue() ).isEqualTo( "abstract step" ); - } - }