Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Invalid content passes unit test?(Behaviour as expected in runtime instance)
Invalid content passes unit test? [message #1703744] Tue, 04 August 2015 15:36 Go to next message
Scott Finnie is currently offline Scott FinnieFriend
Messages: 94
Registered: October 2011
Member
I have a unit test for my grammar (following the docs). The test passes with an invalid example; however entering the same example in a runtime instance is handled correctly (i.e. errors shown in the IDE).

Would appreciate any pointers.

Grammar as follows:

grammar spreadsheet.Loader with org.eclipse.xtext.common.Terminals

generate loader "http://www.spreadsheet/Loader"

Specification:
	(columns += Column)*;
	
Column:
	'col' name=ID '{'
		'header' ':' 	header=STRING
		'type' ':' 		type=ID
	'}'
	;


Unit Test:

package spreadsheet.loader.tests

import org.eclipse.xtext.junit4.XtextRunner
import spreadsheet.LoaderInjectorProvider
import org.eclipse.xtext.junit4.InjectWith
import org.junit.runner.RunWith
import com.google.inject.Inject
import org.eclipse.xtext.junit4.util.ParseHelper
import spreadsheet.loader.Specification
import org.junit.Test
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertNotNull

@InjectWith(LoaderInjectorProvider)
@RunWith(XtextRunner)
class TestGrammar {
	
	@Inject
	ParseHelper<Specification> parser
	
	@Test
	def void testParseSpecification() {
		val spec = parser.parse(
			'''col surname foo {
				header: "Surname"
				type: String bar
			}'''
			)
		assertNotNull(spec)
		val col = spec.columns.head
		assertEquals("surname", col.name)
	}
}


[The "foo" and "bar" tokens should cause parsing to fail].

Thanks.
Re: Invalid content passes unit test? [message #1703751 is a reply to message #1703744] Tue, 04 August 2015 16:31 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 04/08/2015 17:36, Scott Finnie wrote:
> I have a unit test for my grammar (following the docs). The test passes
> with an invalid example; however entering the same example in a runtime
> instance is handled correctly (i.e. errors shown in the IDE).
>
> Would appreciate any pointers.
>
> Grammar as follows:
>
> grammar spreadsheet.Loader with org.eclipse.xtext.common.Terminals
>
> generate loader "http://www.spreadsheet/Loader"
>
> Specification:
> (columns += Column)*;
>
> Column:
> 'col' name=ID '{'
> 'header' ':' header=STRING
> 'type' ':' type=ID
> '}'
> ;
>
>
> Unit Test:
>
>
> package spreadsheet.loader.tests
>
> import org.eclipse.xtext.junit4.XtextRunner
> import spreadsheet.LoaderInjectorProvider
> import org.eclipse.xtext.junit4.InjectWith
> import org.junit.runner.RunWith
> import com.google.inject.Inject
> import org.eclipse.xtext.junit4.util.ParseHelper
> import spreadsheet.loader.Specification
> import org.junit.Test
> import static org.junit.Assert.assertEquals
> import static org.junit.Assert.assertNotNull
>
> @InjectWith(LoaderInjectorProvider)
> @RunWith(XtextRunner)
> class TestGrammar {
>
> @Inject
> ParseHelper<Specification> parser
>
> @Test
> def void testParseSpecification() {
> val spec = parser.parse(
> '''col surname foo {
> header: "Surname"
> type: String bar
> }'''
> )
> assertNotNull(spec)
> val col = spec.columns.head
> assertEquals("surname", col.name)
> }
> }
>
>
> [The "foo" and "bar" tokens should cause parsing to fail].
>
> Thanks.
>

Hi

parsing succeeds, and you need to check for errors explicitly: you need a

@Inject extension ValidationTestHelper

and then explicitly check that

for example if you do

spec.assertNoErrors

the test will fail (and show the errors).

You may want to assert a specific error: ValidationTestHelper provides
many assertError methods for that purpose.

hope this helps
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Re: Invalid content passes unit test? [message #1703817 is a reply to message #1703751] Wed, 05 August 2015 08:07 Go to previous message
Scott Finnie is currently offline Scott FinnieFriend
Messages: 94
Registered: October 2011
Member
Quote:
parsing succeeds, and you need to check for errors explicitly: you need a

@Inject extension ValidationTestHelper

and then explicitly check that

for example if you do

spec.assertNoErrors

the test will fail (and show the errors).

You may want to assert a specific error: ValidationTestHelper provides
many assertError methods for that purpose.


Thanks Lorenzo. And indeed if I'd remembered page 123 of your book I would have known that...

Appreciate your help.

-S.

Previous Topic:IXtextBuilderParticipant ClassNotFoundException
Next Topic:Null annotations in JvmGenericType
Goto Forum:
  


Current Time: Tue Mar 19 06:46:09 GMT 2024

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

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

Back to the top