[FYI] Utility class to unit-test XML code completion scenarios

Jesse Glick <[email protected]> Sun, 28 Dec 2003 00:36:09 -0500
Newsgroups gmane.comp.java.netbeans.modules.xml.devel
Organization Sun Microsystems
Message-ID <[email protected]>
I am working on unit tests for the ant/grammar module (Ant code 
completion) and I developed a neat helper class that lets you test that 
the code completion works:

http://www.netbeans.org/unbranded-source/browse/~checkout~/ant/grammar/test/unit/src/org/netbeans/modules/ant/grammar/TestUtil.java?content-type=text/plain

Using this helper, you can write tests like this:

public void testTaskCompletion() throws Exception {
     GrammarQuery g = new AntGrammar();
     String p = "<project default='x'><target 
name='x'><ecHERE/></target></project>";
     HintContext c = TestUtil.createCompletion(p);
     List l = TestUtil.elementNames(g.queryElements(c));
     assertTrue("matched <echo>", l.contains("echo"));
}

The above tests that when you have text like

<project default='x'>
     <target name='x'>
         <ec
            ^
            +- CURSOR HERE

and ask for completions, "<echo>" is offered (a standard Ant task). Very 
easy to write!

You can also use it for e.g.

"... <echo messHERE=''/> ..." (attr names)
"... <echo message='${messHERE'/> ..." (attr values)
"... <echo>${messHERE</echo> ..." (element text contents)

The helper does not require that anything special is loaded in the VM, 
so you can use it in regular code-mode JUnit tests - much faster to run, 
and more predictable, than ide-mode GUI tests. Of course it presupposes 
that the xml/text-edit module is working correctly according to its 
specification - you are only testing the grammar query provider itself.

This could be used to write thorough unit tests for completion according 
to DTD, XML Schema, XSLT, etc. If there is interest in using it for 
existing xml/* modules I would be happy to move the relevant parts of 
the utility class into a shared location in xml/text-edit/test/unit/src/ 
so that others can share it (just CC me to make sure I see your reply).

Enjoy,
-J.

-- 
Jesse Glick <mailto:[email protected]> x22801
NetBeans, Open APIs  <http://www.netbeans.org/>