Skip to content

Commit

Permalink
Merge pull request #24 from pityq/master
Browse files Browse the repository at this point in the history
add LinkingNote annotation
  • Loading branch information
danielbodart committed Sep 2, 2015
2 parents 5ad8698 + 60c7270 commit 62e9bfd
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/com/googlecode/yatspec/junit/LinkingNote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.googlecode.yatspec.junit;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@YatspecAnnotation
public @interface LinkingNote {
String message();
Class[] links();

}
28 changes: 28 additions & 0 deletions src/com/googlecode/yatspec/rendering/LinkingNoteRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.googlecode.yatspec.rendering;

import com.googlecode.totallylazy.Callable1;
import com.googlecode.totallylazy.Sequence;
import com.googlecode.yatspec.junit.LinkingNote;

import static com.googlecode.totallylazy.Sequences.sequence;
import static com.googlecode.yatspec.junit.SpecRunner.outputDirectory;
import static com.googlecode.yatspec.parsing.Text.wordify;
import static com.googlecode.yatspec.rendering.html.HtmlResultRenderer.htmlResultFile;
import static java.lang.String.format;

public class LinkingNoteRenderer implements Renderer<LinkingNote>{

@Override
public String render(LinkingNote linkingNoteNotes) throws Exception {
return format(linkingNoteNotes.message(), (Object[])sequence(linkingNoteNotes.links()).map(link()).toArray(String.class));
}

private Callable1<Class, String> link() {
return new Callable1<Class, String>() {
@Override
public String call(Class aClass) throws Exception {
return format("<a href='%s'>%s</a>", htmlResultFile(outputDirectory(), aClass), wordify(aClass.getSimpleName()));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import com.googlecode.totallylazy.Predicate;
import com.googlecode.totallylazy.Xml;
import com.googlecode.yatspec.Creator;
import com.googlecode.yatspec.junit.LinkingNote;
import com.googlecode.yatspec.junit.Notes;
import com.googlecode.yatspec.junit.SpecResultListener;
import com.googlecode.yatspec.parsing.Files;
import com.googlecode.yatspec.parsing.JavaSource;
import com.googlecode.yatspec.rendering.Content;
import com.googlecode.yatspec.rendering.NotesRenderer;
import com.googlecode.yatspec.rendering.Renderer;
import com.googlecode.yatspec.rendering.ScenarioTableHeaderRenderer;
import com.googlecode.yatspec.rendering.*;
import com.googlecode.yatspec.state.Result;
import com.googlecode.yatspec.state.ScenarioTableHeader;
import com.googlecode.yatspec.state.Status;
Expand Down Expand Up @@ -54,6 +52,7 @@ public String render(Result result) throws Exception {
group.registerRenderer(instanceOf(ScenarioTableHeader.class), callable(new ScenarioTableHeaderRenderer()));
group.registerRenderer(instanceOf(JavaSource.class), callable(new JavaSourceRenderer()));
group.registerRenderer(instanceOf(Notes.class), callable(new NotesRenderer()));
group.registerRenderer(instanceOf(LinkingNote.class), callable(new LinkingNoteRenderer()));
group.registerRenderer(instanceOf(Content.class), asString());
sequence(customRenderers).fold(group, registerRenderer());
for (Class document : Creator.optionalClass("org.jdom.Document")) {
Expand Down
29 changes: 29 additions & 0 deletions test/com/googlecode/yatspec/junit/LinkingNoteRendererTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.googlecode.yatspec.junit;

import com.googlecode.yatspec.rendering.LinkingNoteRenderer;
import com.googlecode.yatspec.state.TestMethod;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

import static com.googlecode.totallylazy.Sequences.sequence;
import static com.googlecode.yatspec.junit.SpecRunner.outputDirectory;
import static com.googlecode.yatspec.parsing.TestParser.parseTestMethods;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

@RunWith(SpecRunner.class)
public class LinkingNoteRendererTest {
@Test
@LinkingNote(message="Classes specified in the linking note should result in links to the resulted yatspec output eg. %s, %s", links = {String.class, Integer.class})
public void shouldRenderLinkedNotesAsNotesWithLinksToTheYatspecOutputOfTheTestClassesSpecified() throws Exception {
assertThat(theRenderedValueOfTheLinkingNoteOfThisMethod(),
is("Classes specified in the linking note should result in links to the resulted yatspec output eg. <a href='" + outputDirectory() +"/java/lang/String.html'>String</a>, <a href='" + outputDirectory() + "/java/lang/Integer.html'>Integer</a>"));
}

private String theRenderedValueOfTheLinkingNoteOfThisMethod() throws Exception {
final List<TestMethod> methods = parseTestMethods(getClass());
return new LinkingNoteRenderer().render(sequence(sequence(methods).first().getAnnotations()).safeCast(LinkingNote.class).head());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public void takeTheSquareRoot(String radicand, String result) throws Exception {
then(theResult(), is(valueOf(result)));
}

@Test
@LinkingNote(message = "The details of how the Linking Note works can be seen in the %s", links = {LinkingNoteRendererTest.class})
public void testWithALinkingNote() throws Exception {

}

@Test
public void printEmptyTestName() throws Exception {

Expand Down

0 comments on commit 62e9bfd

Please sign in to comment.