Package net.cscott.jdoctest
Class JDoctest
java.lang.Object
net.cscott.jdoctest.JDoctest
- All Implemented Interfaces:
Taglet
JDoctest implementing doctests via a @doc.test taglet. This tag can be
used in any kind of
Element. It is not an
inline tag. A "@doc.test" tag specifies an interactive javascript
session, in an environment where the class' package has been imported.
The output of the javascript session should match the output
provided.- Author:
- C. Scott Ananian
- Tests:
- This is an example of a test which passes:
js> "a".equals("a") true
(EXPECT FAIL) This is an example of a test which fails:js> 1+2 5
Doctest failure: 1+2 Expected: 5 Actual: 3 Diff:
53 (JDoctest.java#68)We can write tests which expect exceptions to be thrown, although it's a little clunky (implementing doctest.ELLIPSIS or doctest.IGNORE_EXCEPTION_DETAIL would make this nicer):js> try { > java.lang.String("hi").charAt(3); > } catch (e) { > print(e.javaException.getClass().getName()) > } java.lang.StringIndexOutOfBoundsException
This demonstrates that the current package has been imported:js> JDoctest [JavaClass net.cscott.jdoctest.JDoctest] js> Version.PACKAGE_NAME jdoctest
Note that results referencing object hashes are properly handled in the output comparison function, even when Rhino gives a different hash at runtime:js> o = new java.lang.Object() java.lang.Object@1d2068d js> o java.lang.Object@1d2068d js> new java.lang.Object() java.lang.Object@1ac2f9c
Doctests are evaluated using the most recent version of ECMAScript that Rhino supports, so modern syntax may be used:js> let squares = [1, 2, 3].map(x => x * x) js> `squares: ${squares.join(", ")}` squares: 1, 4, 9
We can even access private members! (On JDK 16 and later, this requires --add-opens java.base/java.lang=ALL-UNNAMED.)js> new JDoctest().testsExpectedPass 0
-
Nested Class Summary
Nested classes/interfaces inherited from interface jdk.javadoc.doclet.Taglet
Taglet.Location -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionWill return all locations, since@doc.testcan be used in overview, module, package, type, constructor, method, and field documentation.getName()Return the name of this custom tag.voidinit(DocletEnvironment env, Doclet doclet) Initialize this Taglet.booleanWill return false since@doc.testis not an inline tag.Given a list ofDocTrees representing this custom tag, return its string representation.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface jdk.javadoc.doclet.Taglet
isBlockTag
-
Constructor Details
-
JDoctest
public JDoctest()
-
-
Method Details
-
getName
Return the name of this custom tag. -
getAllowedLocations
Will return all locations, since@doc.testcan be used in overview, module, package, type, constructor, method, and field documentation.- Specified by:
getAllowedLocationsin interfaceTaglet- Tests:
js> new JDoctest().getAllowedLocations() [OVERVIEW, MODULE, PACKAGE, TYPE, CONSTRUCTOR, METHOD, FIELD]
-
isInlineTag
public boolean isInlineTag()Will return false since@doc.testis not an inline tag.- Specified by:
isInlineTagin interfaceTaglet- Tests:
js> new JDoctest().isInlineTag() false
-
init
Initialize this Taglet. We fetch theReporterfrom the standard doclet (if that is the doclet in use) so that doctest failures can be reported as javadoc errors. -
toString
Given a list ofDocTrees representing this custom tag, return its string representation.
-