Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Partial Grammar Testing
Partial Grammar Testing [message #948575] Thu, 18 October 2012 06:41 Go to next message
Markus Wolf is currently offline Markus WolfFriend
Messages: 153
Registered: July 2009
Senior Member
Hi there,

I was looking for a way to do partial grammar testing.
All examples I've found relate to XText 1.x but I am using the current XText 2.3.1.

Can anyone point me to the right direction?

Regards
Re: Partial Grammar Testing [message #948894 is a reply to message #948575] Thu, 18 October 2012 13:45 Go to previous messageGo to next message
Michael Ernst is currently offline Michael ErnstFriend
Messages: 49
Registered: July 2010
Member
I have almost written all my grammar tests with Xtend like this way.

package foo

import com.google.inject.Inject
import de.MyProvider
import de.Expression
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.junit4.util.ParseHelper
import org.junit.Test
import org.junit.runner.RunWith

import static org.junit.Assert.*
import org.eclipse.emf.common.util.BasicDiagnostic

@RunWith(typeof(XtextRunner))
@InjectWith(typeof(de.MyInjectorProvider)) // generated by Xtext for your grammar
class ExpressionJavaValidatorTest {

    // class to test
    @Inject ExpressionJavaValidator validator

    // utility to parse code snippets
    @Inject ParseHelper<Expression> parseHelper

    def parse(String expression) {
        parseHelper.parse(expression)
    }
    
    @Test
    def void testInvalidMathOperation() {
        var expression = parse("1 + \"test\"") // a dummy expression 
        var chain = new BasicDiagnostic();
        validator.validate(expression, chain, null); 
        assertTrue(chain.severity != 0) // test that the expression contains an error
    }
	
}

Re: Partial Grammar Testing [message #949592 is a reply to message #948894] Fri, 19 October 2012 05:24 Go to previous messageGo to next message
Markus Wolf is currently offline Markus WolfFriend
Messages: 153
Registered: July 2009
Senior Member
Hi,

thanks. Its exactly the way I currently write my tests. But if you have a very complex and big grammar it is not easy to identify bugs, because it is not always obvious which parser rule is executed during the creation of an ast. Specially if there are some options which are probably ambiguous and would require predicates.
Re: Partial Grammar Testing [message #954993 is a reply to message #948575] Tue, 23 October 2012 12:15 Go to previous messageGo to next message
Scott Finnie is currently offline Scott FinnieFriend
Messages: 94
Registered: October 2011
Member
Have you looked at xtext-utils unit testing?

Not sure it'll give you all you need, esp wrt e.g. predicates. But it does provide some pretty nice features, from testing individual phrases against rules right up to whole files.

hth, apologies if not what you're looking for.
Re: Partial Grammar Testing [message #955960 is a reply to message #948575] Wed, 24 October 2012 06:08 Go to previous message
Markus Wolf is currently offline Markus WolfFriend
Messages: 153
Registered: July 2009
Senior Member
It is exactly what I was looking or. It took me some time to recognize it is not included with xtext.
Thanks
Previous Topic:Eclipse is getting slow with time
Next Topic:quick fix for add import using JvmType ?
Goto Forum:
  


Current Time: Fri Apr 19 02:50:40 GMT 2024

Powered by FUDForum. Page generated in 0.02128 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top