Skip to content

Commit

Permalink
Merge pull request #242 from ahus1/ahus1_tag_id_class_name
Browse files Browse the repository at this point in the history
use full class name as ID of a tag
  • Loading branch information
janschaefer authored Dec 17, 2016
2 parents 28afdbc + 404dc8c commit f2785c9
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
public class TagConfiguration {
private final String annotationType;
private final String annotationFullType;
private boolean ignoreValue;
private boolean explodeArray = true;
private boolean prependType;
Expand All @@ -33,6 +34,7 @@ public class TagConfiguration {

public TagConfiguration( Class<? extends Annotation> tagAnnotation ) {
this.annotationType = tagAnnotation.getSimpleName();
this.annotationFullType = tagAnnotation.getName();
}

public static Builder builder( Class<? extends Annotation> tagAnnotation ) {
Expand Down Expand Up @@ -218,6 +220,10 @@ public String getAnnotationType() {
return annotationType;
}

public String getAnnotationFullType() {
return annotationFullType;
}

/**
* @see com.tngtech.jgiven.annotation.IsTag#href
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,9 @@ public List<Tag> toTags( Annotation annotation ) {
}

private List<Tag> toTags( TagConfiguration tagConfig, Optional<Annotation> annotation ) {
Tag tag = new Tag( tagConfig.getAnnotationType() );
Tag tag = new Tag( tagConfig.getAnnotationFullType() );

tag.setType( tagConfig.getAnnotationType() );

if( !Strings.isNullOrEmpty( tagConfig.getName() ) ) {
tag.setName( tagConfig.getName() );
Expand Down
44 changes: 29 additions & 15 deletions jgiven-core/src/main/java/com/tngtech/jgiven/report/model/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
*/
public class Tag {
/**
* The type of the annotation of the tag
* The full type of the annotation of the tag
*/
private final String type;
private final String fullType;

/**
* The simple type of the annotation of the tag
*/
private String type;

/**
* An optional name of the tag. If not set, the type is the name
Expand Down Expand Up @@ -75,17 +80,17 @@ public class Tag {
*/
private Boolean hideInNav;

public Tag( String type ) {
this.type = type;
public Tag( String fullType ) {
this.fullType = fullType;
}

public Tag( String type, Object value ) {
this( type );
public Tag( String fullType, Object value ) {
this( fullType );
this.value = value;
}

public Tag( String type, String name, Object value ) {
this( type, value );
public Tag( String fullType, String name, Object value ) {
this( fullType, value );
this.name = name;
}

Expand Down Expand Up @@ -137,6 +142,14 @@ public String getStyle() {
return style;
}

public void setType( String type ) {
this.type = type;
}

public String getType() {
return type;
}

public String getHref() {
return href;
}
Expand Down Expand Up @@ -194,14 +207,14 @@ public String getValueString() {

public String toIdString() {
if( value != null ) {
return type + "-" + getValueString();
return fullType + "-" + getValueString();
}
return type;
return fullType;
}

@Override
public int hashCode() {
return Objects.hashCode( getType(), getName(), value );
return Objects.hashCode( getFullType(), getName(), value );
}

@Override
Expand All @@ -216,7 +229,7 @@ public boolean equals( Object obj ) {
return false;
}
Tag other = (Tag) obj;
return Objects.equal( getType(), other.getType() )
return Objects.equal( getFullType(), other.getFullType() )
&& Objects.equal( getName(), other.getName() )
&& Objects.equal( value, other.value );
}
Expand All @@ -242,8 +255,8 @@ public void setName( String name ) {
this.name = name;
}

public String getType() {
return type;
public String getFullType() {
return fullType;
}

public boolean getShownInNavigation() {
Expand All @@ -264,7 +277,8 @@ public void setTags( List<String> tags ) {
}

public Tag copy() {
Tag tag = new Tag( type, name, value );
Tag tag = new Tag( fullType, name, value );
tag.type = this.type;
tag.cssClass = this.cssClass;
tag.color = this.color;
tag.style = this.style;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void testAnnotationWithName() throws Exception {
Tag tag = tags.get( 0 );
assertThat( tag.getName() ).isEqualTo( "AnotherName" );
assertThat( tag.getValues() ).isEmpty();
assertThat( tag.toIdString() ).isEqualTo( "AnnotationWithName" );
assertThat( tag.toIdString() ).isEqualTo( this.getClass().getName() + "$AnnotationWithName" );
}

@IsTag( ignoreValue = true )
Expand All @@ -113,7 +113,7 @@ public void testAnnotationWithIgnoredValueParsing() throws Exception {
Tag tag = tags.get( 0 );
assertThat( tag.getName() ).isEqualTo( "AnnotationWithIgnoredValue" );
assertThat( tag.getValues() ).isEmpty();
assertThat( tag.toIdString() ).isEqualTo( "AnnotationWithIgnoredValue" );
assertThat( tag.toIdString() ).isEqualTo( this.getClass().getName() + "$AnnotationWithIgnoredValue" );
}

@IsTag
Expand Down Expand Up @@ -209,7 +209,7 @@ public void testAnnotationWithParentTag() throws Exception {
List<Tag> tags = getScenarioModelBuilder().toTags( AnnotationWithParentTag.class.getAnnotations()[0] );
assertThat( tags ).hasSize( 1 );
assertThat( tags.get( 0 ).getTags() ).containsAll( Arrays.asList(
"ParentTag", "ParentTagWithValue-SomeValue" ) );
this.getClass().getName() + "$ParentTag", this.getClass().getName() + "$ParentTagWithValue-SomeValue" ) );
}

@IsTag( value = "default" )
Expand Down
8 changes: 7 additions & 1 deletion jgiven-html5-report/src/app/lib/reportCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ jgivenReportApp.controller('JGivenReportCtrl', function ($scope, $rootScope, $do
var tagNameNode = tagService.getTagNameNode(part[2]);
$scope.updateCurrentPageToTagNameNode(tagNameNode, selectedOptions);
}
} else if (part[1] === 'tagid') {
var tag = tagService.getTagByKey(getTagKey({
fullType: part[2],
value: part[3]
}));
$scope.updateCurrentPageToTag(tag, selectedOptions);
} else if (part[1] === 'class') {
$scope.updateCurrentPageToClassName(part[2], selectedOptions);
} else if (part[1] === 'package') {
Expand Down Expand Up @@ -540,7 +546,7 @@ jgivenReportApp.controller('JGivenReportCtrl', function ($scope, $rootScope, $do
if (tag.href) {
return tag.href;
}
return '#tag/' + getTagName(tag) +
return '#tagid/' + getTagId(tag) +
(tag.value ? '/' + $window.encodeURIComponent(tag.value) : '');
};

Expand Down
2 changes: 1 addition & 1 deletion jgiven-html5-report/src/app/lib/tagService.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ jgivenReportApp.factory('tagService', ['dataService', function (dataService) {
var node = createNode(tagToString(tag));

node.url = function () {
return '#tag/' + window.encodeURIComponent(getTagName(tag)) +
return '#tagid/' + window.encodeURIComponent(getTagId(tag)) +
(tag.value ? '/' + window.encodeURIComponent(tag.value) : '');
};

Expand Down
6 changes: 5 additions & 1 deletion jgiven-html5-report/src/app/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ function getTagName (tag) {
return tag.name ? tag.name : tag.type;
}

function getTagId (tag) {
return tag.fullType ? tag.fullType : tag.type;
}

function getTagKey (tag) {
return getTagName(tag) + (tag.value ? '-' + tag.value : '');
return getTagId(tag) + (tag.value ? '-' + tag.value : '');
}

function tagToString (tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ public void fill( Map<String, Tag> tagIdMap ) {
Tag tag = entry.getValue().copy();
tag.setValue( (String) null );

if( !tagTypeMap.containsKey( tag.getType() ) ) {
tagTypeMap.put( tag.getType(), tag );
if( !tagTypeMap.containsKey( tag.getFullType() ) ) {
tagTypeMap.put( tag.getFullType(), tag );
}

TagInstance instance = new TagInstance();
instance.tagType = tag.getType();
instance.tagType = tag.getFullType();
instance.value = entry.getValue().getValueString();

// the description might be generated depending on the value, so it must be stored
// for each tag instance separately
if( !ObjectUtil.equals( entry.getValue().getDescription(), tagTypeMap.get( tag.getType() ).getDescription() ) ) {
if( !ObjectUtil.equals( entry.getValue().getDescription(), tagTypeMap.get( tag.getFullType() ).getDescription() ) ) {
instance.description = entry.getValue().getDescription();
}

// the href might be generated depending on the value, so it must be stored
// for each tag instance separately
if( !ObjectUtil.equals( entry.getValue().getHref(), tagTypeMap.get( tag.getType() ).getHref() ) ) {
if( !ObjectUtil.equals( entry.getValue().getHref(), tagTypeMap.get( tag.getFullType() ).getHref() ) ) {
instance.href = entry.getValue().getHref();
}
tags.put( entry.getKey(), instance );
Expand Down
2 changes: 1 addition & 1 deletion jgiven-html5-report/src/test/app/tagServiceTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe("TagService", function () {
var scenarios = tagService.getScenariosByTag(tagService.getTagByKey('issue-1'));
expect(scenarios.length).toEqual(1);
expect(scenarios[0].tags.length).toEqual(testCases[0].scenarios[0].tagIds.length);
expect(_.map(scenarios[0].tags, getTagKey)).toEqual(['issue-1', 'issue-2', 'categoryB', 'feature-A', 'feature-B', 'something-someA']);
expect(_.map(scenarios[0].tags, getTagKey)).toEqual(['issue-1', 'issue-2', 'categoryB', 'featureA-A', 'featureB-B', 'somethingA-someA']);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void configured_tags_are_reported() throws Throwable {
assertThat( tagIds ).isNotEmpty();
String tagId = tagIds.get( 0 );
assertThat( tagId ).isNotNull();
assertThat( tagId ).isEqualTo( "ConfiguredTag-Test" );
assertThat( tagId ).isEqualTo( ConfiguredTag.class.getName() + "-Test" );
}

@Test
Expand Down Expand Up @@ -333,7 +333,7 @@ public void tags_can_be_added_using_the_current_scenario() {

List<String> tagIds = getScenario().getScenarioModel().getTagIds();
assertThat( tagIds ).hasSize( 1 );
assertThat( tagIds.get( 0 ) ).isEqualTo( "DynamicTag-value" );
assertThat( tagIds.get( 0 ) ).isEqualTo( this.getClass().getName() + "$DynamicTag-value" );
}

static abstract class AbstractStage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void annotations_are_translated_to_tags() throws Throwable {
assertThat( model.getTagIds() ).hasSize( 1 );

String tagId = model.getTagIds().get( 0 );
assertThat( tagId ).isEqualTo( "TestTag-foo, bar, baz" );
assertThat( tagId ).isEqualTo( this.getClass().getName() + "$TestTag-foo, bar, baz" );

Tag tag = reportModel.getTagWithId( tagId );
assertThat( tag ).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void tag_on_step_method_is_recognized() throws Throwable {

getScenario().finished();

assertThat( getScenario().getModel().getTagMap().keySet() ).contains( "StepMethodTag" );
assertThat( getScenario().getModel().getTagMap().keySet() ).contains( "com.tngtech.jgiven.junit.test.GivenTestStep$StepMethodTag" );
}

@Test
Expand All @@ -46,6 +46,6 @@ public void tag_on_stage_class_is_recognized() throws Throwable {

getScenario().finished();

assertThat( getScenario().getModel().getTagMap().keySet() ).contains( "StageTag" );
assertThat( getScenario().getModel().getTagMap().keySet() ).contains( "com.tngtech.jgiven.junit.test.GivenTaggedTestStep$StageTag" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void ensure_all_tags_are_found() throws Throwable {
getScenario().finished();

List<String> tagIds = getScenario().getModel().getLastScenarioModel().getTagIds();
assertThat( tagIds ).containsAll( Arrays.asList( "TestTag" ) );
assertThat( tagIds ).containsAll( Arrays.asList( TestTag.class.getName() ) );

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public SELF the_first_scenario_has_tag( @Quoted String name ) {

public SELF scenario_$_has_tag_$_with_value_$( int i, String name, String value ) {
latestTag = new Tag( name, value ).setPrependType( true );
latestTag.setType( name );
reportModel.getScenarios().get( i - 1 ).addTag( latestTag );
reportModel.addTag( latestTag );
return self();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void tag_annotations_appear_in_the_report_model() {
given().a_test()
.and().the_test_has_a_tag_annotation_named( "TestTag" );
when().the_test_is_executed_with( testFramework );
then().the_report_model_contains_a_tag_named( "TestTag" );
then().the_report_model_contains_a_tag_named( "com.tngtech.jgiven.tests.TestTag" );
}

@Test
Expand Down

0 comments on commit f2785c9

Please sign in to comment.