| /******************************************************************************* |
| * Copyright (c) 2014 Bosch Software Innovations GmbH and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * and Eclipse Distribution License v1.0 which accompany this distribution. |
| * |
| * The Eclipse Public License is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * The Eclipse Distribution License is available at |
| * http://www.eclipse.org/org/documents/edl-v10.php. |
| * |
| * Contributors: |
| * Bosch Software Innovations GmbH - Please refer to git log |
| * |
| *******************************************************************************/ |
| package org.eclipse.vorto.fbeditor.tests.formatter |
| |
| import com.google.inject.Inject |
| import org.eclipse.vorto.fbeditor.FunctionblockInjectorProvider |
| import org.eclipse.vorto.functionblock.FunctionblockModel |
| import org.eclipse.vorto.functionblock.impl.FunctionblockPackageImpl |
| import org.eclipse.xtext.formatting.INodeModelFormatter |
| import org.eclipse.xtext.junit4.AbstractXtextTests |
| import org.eclipse.xtext.junit4.InjectWith |
| import org.eclipse.xtext.junit4.XtextRunner |
| import org.eclipse.xtext.junit4.util.ParseHelper |
| import org.eclipse.xtext.resource.XtextResource |
| import org.junit.BeforeClass |
| import org.junit.Test |
| import org.junit.runner.RunWith |
| |
| @RunWith(typeof(XtextRunner)) |
| @InjectWith(typeof(FunctionblockInjectorProvider)) |
| class FbDslFormatterTest extends AbstractXtextTests { |
| |
| @Inject extension ParseHelper<FunctionblockModel> parserHelper; |
| @Inject extension INodeModelFormatter formatter; |
| |
| @BeforeClass |
| def static void initializeModel() { |
| FunctionblockPackageImpl.init(); |
| } |
| |
| @Test |
| def void testFormattingForBasicBlock() { |
| val expectedText = ''' |
| functionblock BasicFunctionBlock { |
| displayname "BasicFunctionBlock" |
| description "A Basic desription." |
| vendor www.bosch.com |
| category demo |
| version 2.1.1 |
| |
| configuration { |
| mandatory strproperty as string "Demo property" |
| optional intproperty as string "Demo property" |
| } |
| |
| status { |
| mandatory currentStatus as string "Demo Status" |
| } |
| |
| fault { |
| mandatory faultProperty as string |
| } |
| |
| operations { |
| start(param as string) |
| } |
| |
| }''' |
| val rawText = '''functionblock BasicFunctionBlock { displayname "BasicFunctionBlock" description "A Basic desription." vendor |
| www.bosch.com category demo version 2.1.1 configuration { mandatory strproperty as string "Demo property" optional intproperty as |
| string "Demo property" } status { |
| mandatory currentStatus as string "Demo Status" } fault { mandatory faultProperty as string } operations { start(param as string) }}''' |
| rawText.assertFormattedAs(expectedText) |
| } |
| |
| @Test |
| def void testFormattingWithManyElements() { |
| val expectedText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration { |
| mandatory str as string "description" |
| mandatory intprop as int "description" |
| } |
| |
| status { |
| optional stat as COLOR "Demo" |
| mandatory somethingelse as int "int property" |
| } |
| |
| fault { |
| mandatory opt as Simple "Demo" |
| mandatory withconstraint as int <MIN 1,MAX 100> |
| } |
| |
| operations { |
| start(param as string,onemore as int) "starting" |
| stopping(param as string) |
| } |
| |
| } |
| |
| entity Simple { |
| mandatory randomProperty as string |
| } |
| |
| enum COLOR { |
| RED, |
| GREEN, |
| BLUE |
| }''' |
| |
| val rawText = ''' |
| functionblock ComplexFunctionBlock |
| { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration |
| { |
| mandatory str as string "description" |
| mandatory intprop as int "description" |
| } |
| |
| status |
| { |
| optional stat as COLOR "Demo" |
| mandatory somethingelse as int "int property" |
| } |
| |
| fault |
| { |
| mandatory opt as Simple "Demo" |
| mandatory withconstraint as int <MIN 1 , MAX 100> |
| } |
| |
| operations |
| { |
| start(param as string , onemore as int) "starting" |
| stopping(param as string) |
| } |
| |
| } |
| |
| entity Simple |
| { |
| mandatory randomProperty as string |
| } |
| |
| enum COLOR { |
| RED , GREEN , BLUE |
| }''' |
| rawText.assertFormattedAs(expectedText) |
| } |
| |
| @Test |
| def void testSingleTabIndendation() { |
| val expectedText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration { |
| } |
| |
| status { |
| } |
| |
| fault { |
| } |
| |
| operations { |
| } |
| |
| }''' |
| |
| val rawText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration { |
| } |
| |
| status { |
| } |
| |
| fault { |
| } |
| |
| operations { |
| } |
| |
| }''' |
| rawText.assertFormattedAs(expectedText) |
| } |
| |
| @Test |
| def void testNoDoubleIndendation() { |
| val expectedText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| }''' |
| |
| val rawText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| }''' |
| expectedText.assertNotFormattedAs(rawText) |
| } |
| |
| @Test |
| def void testSameLineBraces() { |
| val expectedText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration { |
| } |
| |
| status { |
| } |
| |
| fault { |
| } |
| |
| operations { |
| } |
| |
| }''' |
| |
| val rawText = ''' |
| functionblock ComplexFunctionBlock |
| { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration |
| |
| { |
| } |
| |
| status |
| { |
| } |
| |
| fault |
| { |
| } |
| |
| operations |
| {}}''' |
| rawText.assertFormattedAs(expectedText) |
| } |
| |
| @Test |
| def void testEnumWithLineBreaks() { |
| val expectedText = ''' |
| functionblock ComplexFunctionBlock { |
| } |
| |
| enum COLOR { |
| RED, |
| GREEN, |
| BLUE |
| }''' |
| |
| val rawText = ''' |
| functionblock ComplexFunctionBlock |
| { |
| } |
| enum COLOR {RED , GREEN , BLUE}''' |
| rawText.assertFormattedAs(expectedText) |
| } |
| |
| @Test |
| def void testNoSpaceBetweenParameters() { |
| val expectedText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration { |
| } |
| |
| status { |
| } |
| |
| fault { |
| } |
| |
| operations { |
| start(param as string,onemore as int) "starting" |
| stopping(param as string,morespaceparam as int) |
| } |
| |
| }''' |
| |
| val rawText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| configuration { |
| } |
| status { |
| } |
| fault { |
| } |
| operations { |
| start(param as string , onemore as int) "starting" |
| stopping ( param as string , morespaceparam as int ) |
| } |
| }''' |
| rawText.assertFormattedAs(expectedText) |
| } |
| |
| @Test |
| def void testNoSpaceBetweenConstraints() { |
| val expectedText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration { |
| } |
| |
| status { |
| } |
| |
| fault { |
| mandatory withconstraint as int <MIN 1,MAX 100> |
| mandatory propB as string <REGEX "[a-z]",STRLEN 4> |
| mandatory propC as float <MAX 199.1,MIN 120.2> |
| } |
| |
| operations { |
| } |
| |
| }''' |
| |
| val rawText = ''' |
| functionblock ComplexFunctionBlock |
| { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| configuration {} |
| status{} |
| |
| fault |
| { |
| mandatory withconstraint as int <MIN 1 , MAX 100> |
| mandatory propB as string <REGEX "[a-z]" , STRLEN 4> |
| mandatory propC as float <MAX 199.1 , MIN 120.2> |
| } |
| operations {} |
| }''' |
| rawText.assertFormattedAs(expectedText) |
| } |
| |
| @Test |
| def void testOneLineBreakBetweenSections() { |
| val expectedText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration { |
| mandatory str as string "description" |
| mandatory intprop as int "description" |
| } |
| |
| status { |
| optional stat as COLOR "Demo" |
| mandatory somethingelse as int "int property" |
| } |
| |
| fault { |
| mandatory opt as Simple "Demo" |
| mandatory withconstraint as int <MIN 1,MAX 100> |
| } |
| |
| operations { |
| start(param as string,onemore as int) "starting" |
| stopping(param as string) |
| } |
| |
| } |
| |
| entity Simple { |
| mandatory randomProperty as string |
| } |
| |
| enum COLOR { |
| RED, |
| GREEN, |
| BLUE |
| }''' |
| |
| val rawText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| configuration { |
| mandatory str as string "description" |
| mandatory intprop as int "description" |
| } |
| status { |
| mandatory stat as string "Demo" |
| } |
| fault { |
| mandatory opt as Simple "Demo" |
| mandatory withconstraint as int <MIN 1,MAX 100> |
| } |
| operations { |
| start(param as string,onemore as int) "starting" |
| stopping(param as string) |
| } |
| } |
| entity Simple { |
| mandatory randomProperty as string |
| } |
| enum COLOR { |
| RED, |
| GREEN, |
| BLUE |
| }''' |
| expectedText.assertNotFormattedAs(rawText) |
| } |
| |
| @Test |
| def void testFormattingWithOnlyFbMetadata() { |
| val expectedText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| }''' |
| |
| val rawText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| }''' |
| rawText.assertFormattedAs(expectedText) |
| } |
| |
| @Test |
| def void testFormattingWithOptionalConfigurationProperty() { |
| val expectedText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration { |
| optional str as string "description" |
| optional intprop as int "description" |
| } |
| |
| }''' |
| |
| val rawText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration { |
| optional str as string "description" |
| optional intprop as int "description" |
| } |
| }''' |
| rawText.assertFormattedAs(expectedText) |
| } |
| |
| @Test |
| def void testFormattingWithOptionalMandatoryConfigurationAndStatusProperty() { |
| val expectedText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration { |
| optional str as string "description" |
| optional intprop as int "description" |
| } |
| |
| status { |
| optional str as string "description" |
| mandatory intprop as int "description" |
| } |
| |
| }''' |
| |
| val rawText = ''' |
| functionblock ComplexFunctionBlock { |
| displayname "ComplexFunctionBlock" |
| description "Complex Function block description" |
| vendor www.bosch.com |
| category complexcategory |
| version 1.1.1 |
| |
| configuration { |
| optional str as string "description" |
| optional intprop as int "description" |
| } |
| |
| status { |
| optional str as string "description" |
| mandatory intprop as int "description" |
| } |
| }''' |
| rawText.assertFormattedAs(expectedText) |
| } |
| |
| @Test |
| def void testFormattingWithEvents() { |
| val expectedText = ''' |
| functionblock Sample { |
| displayname "Sample" |
| description "Function block model for Sample" |
| vendor www.bosch.com |
| category demo |
| version 1.0.0 |
| |
| configuration { |
| mandatory testconfig as string |
| } |
| |
| status { |
| mandatory teststatus as string |
| } |
| |
| fault { |
| optional testfault as string |
| } |
| |
| events { |
| testEvent { |
| mandatory test as string |
| } |
| |
| refreshevent { |
| mandatory refresh as string |
| } |
| |
| } |
| |
| operations { |
| simpleOperation() |
| complexOperation(arg1 as string,arg2 as int) |
| } |
| |
| }''' |
| |
| val rawText = ''' |
| functionblock Sample { displayname "Sample" description "Function block model for Sample" vendor www.bosch.com category demo version 1.0.0 |
| |
| configuration { mandatory testconfig as string } |
| |
| status { mandatory teststatus as string } |
| |
| fault { optional testfault as string } |
| |
| events { testEvent { mandatory test as string } |
| |
| refreshevent { mandatory refresh as string } |
| |
| } |
| |
| operations { simpleOperation() complexOperation(arg1 as string,arg2 as int) } |
| }''' |
| |
| rawText.assertFormattedAs(expectedText) |
| |
| } |
| |
| @Test |
| def void testFormattingWithEmptyEvents() { |
| val expectedText = ''' |
| functionblock Sample { |
| displayname "Sample" |
| description "Function block model for Sample" |
| vendor www.bosch.com |
| category demo |
| version 1.0.0 |
| |
| configuration { |
| mandatory testconfig as string |
| } |
| |
| status { |
| mandatory teststatus as string |
| } |
| |
| fault { |
| optional testfault as string |
| } |
| |
| events { |
| testEvent { |
| } |
| |
| refreshevent { |
| } |
| |
| } |
| |
| operations { |
| simpleOperation() |
| complexOperation(arg1 as string,arg2 as int) |
| } |
| |
| }''' |
| |
| val rawText = ''' |
| functionblock Sample { displayname "Sample" description "Function block model for Sample" vendor www.bosch.com category demo version 1.0.0 |
| |
| configuration { mandatory testconfig as string } |
| |
| status { mandatory teststatus as string } |
| |
| fault { optional testfault as string } |
| |
| events { testEvent {} |
| |
| refreshevent {} |
| |
| } |
| |
| operations { simpleOperation() complexOperation(arg1 as string,arg2 as int) } |
| }''' |
| |
| rawText.assertFormattedAs(expectedText) |
| |
| } |
| |
| |
| def void assertFormattedAs(CharSequence input, CharSequence expected) { |
| val expectedText = expected.toString |
| val formattedText = (input.parse.eResource as XtextResource).parseResult.rootNode.format(0, input.length).formattedText |
| assertEquals(expectedText, formattedText) |
| } |
| |
| def void assertNotFormattedAs(CharSequence input, CharSequence unexpected) { |
| val unExpectedText = unexpected.toString |
| val formattedText = (input.parse.eResource as XtextResource).parseResult.rootNode.format(0, input.length).formattedText |
| assertNotEquals(unExpectedText, formattedText) |
| } |
| } |