Example: Test a model-to-text transformation with EUnit

[%
  var sb := new Native("java.lang.StringBuilder");
  process(sb, "", Tree.allInstances.select(s|s.parent.isUndefined()).first());

%][%=sb.toString()%][%

  operation process(sb, indent : String, node : Tree) : String {
    sb.append(indent + "- " + node.label + "\n");
    for (child in node.children) {
    process(sb, indent + "  ", child);
    }
  }
%]
var EXPECTED_PATH  = basedir + "/expected.txt";
var GENERATED_PATH = basedir + "/tree.txt";

var EXPECTED_MODEL = '
@Spec {
  Metamodel "TreeMetamodel" {
    nsUri: "Tree"
  }
}

Model {
  Tree "A" {
    label: "A"
  }
  Tree "B" {
    label: "B"
    parent: Tree "A"
  }
  Tree "C" {
    label: "C"
    parent: Tree "B"
  }
  Tree "D" {
    label: "D"
    parent: Tree "B"
  }
  Tree "E" {
    label: "E"
    parent: Tree "A"
  }
}  
';

@model
operation loadModels() {
  loadHutn("Tree", EXPECTED_MODEL);
}

@test
operation expectedModelGeneratesExpectedFile() {
  runTarget("tree2text");
  assertEqualFiles(EXPECTED_PATH, GENERATED_PATH);
}
- A
  - B
    - C
    - D
  - E
<project default="run-tests">

  <target name="run-tests">
    <epsilon.emf.register file="tree.ecore"/>
    <epsilon.eunit src="tests.eunit" failOnErrors="false">
      <parameter name="basedir" value="${basedir}"/>
    </epsilon.eunit>
  </target>

  <target name="tree2text">
    <epsilon.egl src="tree2text.egl" target="${basedir}/tree.txt">
      <model ref="Tree"/>
    </epsilon.egl>
  </target>

</project>

Check out the code from the SVN:

  • go to the SVN repository
  • navigate to trunk/examples
  • check out the org.eclipse.epsilon.eunit.examples.egl.files project

Once you have checked out/imported the code, to run the example you need to go through the following steps:

  1. register any .ecore metamodels in the org.eclipse.epsilon.eunit.examples.egl.files project
  2. right click the .launch file in the org.eclipse.epsilon.eunit.examples.egl.files project
  3. select Run as... and click the first item in the menu that pops up

What's this?

In this example we show how a model-to-text transformation written in EGL can be tested with EUnit and HUTN.

What are .emf files?

.emf files are Ecore metamodels expressed using the Emfatic textual syntax.

More examples...

Epsilon Object Language
Epsilon Transformation Language
Epsilon Generation Language
Epsilon Validation Language
Epsilon Merging Language
Epsilon Flock
Combining the Epsilon Languages
EuGENia
EUnit

Even more examples...

More examples are available in the examples folder of the SVN repository.