Skip to content

Instantly share code, notes, and snippets.

@ldaley
Created August 21, 2013 06:22
Show Gist options
  • Save ldaley/6290912 to your computer and use it in GitHub Desktop.
Save ldaley/6290912 to your computer and use it in GitHub Desktop.
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.model.internal
import org.gradle.api.Action
import org.gradle.model.ModelPathBinder
import org.gradle.model.Open
import spock.lang.Specification
class ModelRegistryBackedModelRulesTest extends Specification {
static class ModelElement {
List<String> names = []
}
static class DerivedThing {
String name
}
static class DerivedThingCreationRule implements Runnable {
private final ModelElement modelElement
private final List<String> things
DerivedThingCreationRule(ModelElement modelElement, @Open List<String> things) {
this.modelElement = modelElement
this.things = things
}
void run() {
modelElement.names.each {
things << new DerivedThing(name: it)
}
}
}
def "can configure by rules"() {
given:
def modelRegistry = new DefaultModelRegistry()
def rules = new ModelRegistryBackedModelRules(modelRegistry)
when:
rules.register("element", new ModelElement())
rules.register("things", [] as List<DerivedThing>)
3.times { int i ->
rules.configure("element", ModelElement, new Action<ModelElement>() {
void execute(ModelElement modelElement) {
modelElement.names << "name$i"
}
})
}
rules.rule(DerivedThingCreationRule, new Action<ModelPathBinder>() {
void execute(ModelPathBinder binder) {
binder.with {
bind "element"
bind "things"
}
}
})
then:
List<DerivedThing> things = modelRegistry.get("things", List)
things*.name == ["name0", "name1", "name2"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment