executable specifications for xtext
TRANSCRIPT
![Page 1: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/1.jpg)
Executable Specifications
for XtextMoritz Eysholdt
![Page 2: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/2.jpg)
You’re Developing a Language
![Page 3: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/3.jpg)
Three Roles
• Language Designer (You)• The Domain Expert• Language Users
• yes, I simplify
![Page 4: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/4.jpg)
Three Roles (1/3)
• You know something about Xtext
• You have some idea about Language DesignYou
![Page 5: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/5.jpg)
Three Roles (2/3)
• Deep conceptual knowledge about the domain
• Defines requirements for language
• Has design ideas/expectations?• Needs to be satisfied with the
language
Domain Expert
![Page 6: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/6.jpg)
Three Roles (3/3)
• Need to understand language• Will use language regularly
LanguageUsers
![Page 7: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/7.jpg)
Challenge
The single most important task during the project:
communicate how touse the language
![Page 8: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/8.jpg)
Examples to the Rescue
• create example documents• and talk about them
![Page 9: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/9.jpg)
Four Important Use Cases
• Specify the Language• Develop IDE (smart editor, code gen, ...)• Pass Acceptance Test• Teach Language
![Page 10: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/10.jpg)
Use Case #1
• Specify the language• Discuss examples with customer• You guide and prototype• Customer knows use cases• You or both design
• Result: Specification by Examples
![Page 11: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/11.jpg)
Use Case #2
• Develop IDE: Needs automated tests• Use Examples as Test Data
• Extend examples with corner cases• Enhance examples with test expectations
• not having automated tests...
• ...is like not using a seat belt in a roller coaster
• ...will degenerate your code, ‘cause you fear change
![Page 12: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/12.jpg)
Use Case #3
• Acceptance Testing: Customer wants to know...• ...if and how a uses case is implemented
• Prove by Example
• ...if the IDE (editor, code generator) works• Prove by Passed Test Case
![Page 13: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/13.jpg)
Use Case #4
• Teach The Language• Explain language by Examples
• Learning a Language is a Challenge. Help the Users.
• Users won’t accept a language if they don’t understand it
• More Users -> better Return Of Investment
![Page 14: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/14.jpg)
Demo
Xpect
![Page 15: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/15.jpg)
How does this work?
• Tests are JUnit tests (with custom runner)• Tests can be configured with setups• Tests can have parameters
![Page 16: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/16.jpg)
JUnit
import org.junit.runner.RunWith;import org.xpect.runner.XpectRunner;import org.xpect.runner.Xpect;
@RunWith(XpectRunner.class)public class MyTest {
@Xpect public void myXpectTest(IStringExpectation expectation) { expectation.assertEquals("actual"); }
@Test public void myJUnitTest() { Assert.assertEquals("expected", "actual"); }}
// XPECT myXpectTest --> expected
![Page 17: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/17.jpg)
Test Setup & Config
import org.xpect.xtext.lib.setup.XtextStandaloneSetup;import org.xpect.xtext.lib.setup.XtextWorkspaceSetup;
@RunWith(XpectRunner.class)@XpectSetup(XtextStandaloneSetup.class)public class MyTest {
}
/* XPECT_SETUP MyTest ResourceSet { ThisFile {} File "test2.dmodel" {} }END_SETUP */
![Page 18: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/18.jpg)
Test Parameters
@RunWith(XpectRunner.class)@XpectSetup(XtextStandaloneSetup.class)public class InterpreterTest { @Xpect public void evaludated( @StringExpectation IStringExpectation expectation, @ThisOffset Expression expression) { BigDecimal actual = new Calculator().evaluate(expression); expectation.assertEquals(actual); }}
// XPECT evaludated --> 313 + 4 * 7;
![Page 19: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/19.jpg)
Annotations define Parameter Source
• From Setup@ThisModel // access to the root EObject@ThisResource // access to the EMF Resource@ThisOffset // access to the offset or current EObject
• From XPECT myTest <parsedParameter> --> ...@ParameterParser(syntax="arg0=INT ‘x’ arg1=STRING+ arg2=OFFSET")
• Expectations@StringExpectation // string@LinesExpectation // list of strings, one item per line@CommaSeparatedValuesExpectation // list, comma separated items
![Page 20: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/20.jpg)
Comparison
Xpect Plain Junit
Specify Language yes no
Integration Test yes yes
Acceptance Test yes no
Teach Language yes no
IDE support for Test Data yes no
Simple Evolution of Test Data yes no
Unit Test (Without Parsing Test Data) no yes
![Page 21: Executable specifications for xtext](https://reader033.vdocuments.net/reader033/viewer/2022042509/555e2380d8b42a6a4c8b4d48/html5/thumbnails/21.jpg)
Thank You
http://www.xpect-tests.org
fork me at github :)