|
Re: Invalid content passes unit test? [message #1703751 is a reply to message #1703744] |
Tue, 04 August 2015 16:31 |
|
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
HOME: http://www.lorenzobettini.it
TDD Book: https://leanpub.com/tdd-buildautomation-ci
Xtext Book: https://www.packtpub.com/application-development/implementing-domain-specific-languages-xtext-and-xtend-second-edition
|
|
|
|
Powered by
FUDForum. Page generated in 0.01869 seconds