Skip to content

Commit

Permalink
[griddynamics#1028][feature] - Reusing code with macros
Browse files Browse the repository at this point in the history
  • Loading branch information
rsvato committed Aug 23, 2013
1 parent 7d1a51d commit 38ee512
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class MacroExpandVisitor(val macrodefs: Map[String, Macro], replacements: mutabl
case closure: ClosureExpression => copyClosureExpression(closure)
case arguments: ArgumentListExpression => new ArgumentListExpression(arguments.getExpressions.map(copy))
case mapEntry: MapEntryExpression => new MapEntryExpression(copy(mapEntry.getKeyExpression), copy(mapEntry.getValueExpression))
case prop: PropertyExpression => new PropertyExpression(copy(prop.getObjectExpression), copy(prop.getProperty))
}

private def copyClosureExpression(closure: ClosureExpression): ClosureExpression = {
Expand Down
12 changes: 11 additions & 1 deletion backend/src/test/resources/groovy/Macros.genesis
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ template {

defmacro "map": { $key, $mapValue ->
withMap {
text = "Test"
text = $vars.myvar
map = { [ $key: $mapValue ] }
}
}

defmacro "defvar": { $name ->
$name = {
description = "Variable from template"
isOptional = false
}
}

defmacro "bad_require": { $message ->
require {
$message { 1 == 2 }
Expand All @@ -47,6 +54,9 @@ template {
}

workflow("maps") {
variables {
macro:defvar(myvar)
}
steps {
macro:map("operation", "subst")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.griddynamics.genesis.cache.NullCacheManager
import com.griddynamics.genesis.template.VersionedTemplate
import com.griddynamics.genesis.api
import com.griddynamics.genesis.plugin.{StepBuilderFactory, StepBuilder}
import com.griddynamics.genesis.service.TemplateDefinition
import com.griddynamics.genesis.service.{VariableDescription, TemplateDefinition}
import com.griddynamics.genesis.api.Failure
import com.griddynamics.genesis.workflow.Step
import scala.beans.BeanProperty
Expand Down Expand Up @@ -65,9 +65,15 @@ class MacrosTest extends AssertionsForJUnit with MockitoSugar with DSLTestUniver
def testMacroWithMap() {
val template: Option[TemplateDefinition] = templateService.findTemplate(0, "Macros", "0.1", 0)
val result = template.get.getWorkflow("maps").get
val steps = result.embody(Map())
val values: Map[String, String] = steps.regular(0).newStep.actualStep.asInstanceOf[StepWithMap].values
val steps = result.embody(Map("myvar" -> "1024"))
val variables: Seq[VariableDescription] = result.variableDescriptions
assert(variables.size == 1)
assert(variables.exists(_.name == "myvar"))
val step: StepWithMap = steps.regular(0).newStep.actualStep.asInstanceOf[StepWithMap]
val values: Map[String, String] = step.values
val text = step.name
expectResult(Map("operation" -> "subst"))(values)
expectResult("1024")(text)
}
}

Expand Down

0 comments on commit 38ee512

Please sign in to comment.