Repository: incubator-groovy Updated Branches: refs/heads/master 99e705526 -> a6d6adafb
Documentation: add FileTreeBuilder documentation Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/a6d6adaf Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/a6d6adaf Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/a6d6adaf Branch: refs/heads/master Commit: a6d6adafbf3091586029bfe620d318b84850741f Parents: 99e7055 Author: pascalschumacher <pascalschumac...@gmx.net> Authored: Sun Apr 19 17:44:54 2015 +0200 Committer: pascalschumacher <pascalschumac...@gmx.net> Committed: Sun Apr 19 17:44:54 2015 +0200 ---------------------------------------------------------------------- .../doc/core-domain-specific-languages.adoc | 40 ++++++++++- .../test/builder/FileTreeBuilderTest.groovy | 75 ++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/a6d6adaf/src/spec/doc/core-domain-specific-languages.adoc ---------------------------------------------------------------------- diff --git a/src/spec/doc/core-domain-specific-languages.adoc b/src/spec/doc/core-domain-specific-languages.adoc index d35fa82..4870623 100644 --- a/src/spec/doc/core-domain-specific-languages.adoc +++ b/src/spec/doc/core-domain-specific-languages.adoc @@ -1148,4 +1148,42 @@ See <<jmx_jmxbuilder,Working with JMX - JmxBuilder>> for details. ==== FileTreeBuilder -(TBD) \ No newline at end of file +gapi:groovy.util.FileTreeBuilder[FileTreeBuilder] is a builder for generating a file directory structure from a specification. For example, to create the following tree: + +--------- + src/ + |--- main + | |--- groovy + | |--- Foo.groovy + |--- test + |--- groovy + |--- FooTest.groovy +--------- + +You can use a `FileTreeBuilder` like this: + +[source,groovy] +---- +include::{projectdir}/src/spec/test/builder/FileTreeBuilderTest.groovy[tags=example,indent=0] +---- + +To check that everything worked as expected we use the following `assert`s: + +[source,groovy] +---- +include::{projectdir}/src/spec/test/builder/FileTreeBuilderTest.groovy[tags=example_assert,indent=0] +---- + +`FileTreeBuilder` also supports a shorthand syntax: + +[source,groovy] +---- +include::{projectdir}/src/spec/test/builder/FileTreeBuilderTest.groovy[tags=shorthand_syntax,indent=0] +---- + +This produces the same directory structure as above, as shown by these `assert`s: + +[source,groovy] +---- +include::{projectdir}/src/spec/test/builder/FileTreeBuilderTest.groovy[tags=shorthand_syntax_assert,indent=0] +---- \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/a6d6adaf/src/spec/test/builder/FileTreeBuilderTest.groovy ---------------------------------------------------------------------- diff --git a/src/spec/test/builder/FileTreeBuilderTest.groovy b/src/spec/test/builder/FileTreeBuilderTest.groovy new file mode 100644 index 0000000..3a3c699 --- /dev/null +++ b/src/spec/test/builder/FileTreeBuilderTest.groovy @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 builder + +class FileTreeBuilderTest extends GroovyTestCase { + File tmpDir + + void tearDown() { + tmpDir.deleteDir() + } + + void testFileTreeBuilder() { + // tag::example[] + tmpDir = File.createTempDir() + def fileTreeBuilder = new FileTreeBuilder(tmpDir) + fileTreeBuilder.dir('src') { + dir('main') { + dir('groovy') { + file('Foo.groovy', 'println "Hello"') + } + } + dir('test') { + dir('groovy') { + file('FooTest.groovy', 'class FooTest extends GroovyTestCase {}') + } + } + } + // end::example[] + + // tag::example_assert[] + assert new File(tmpDir, '/src/main/groovy/Foo.groovy').text == 'println "Hello"' + assert new File(tmpDir, '/src/test/groovy/FooTest.groovy').text == 'class FooTest extends GroovyTestCase {}' + // end::example_assert[] + } + + void testFileTreeBuilderShortHandSyntax() { + // tag::shorthand_syntax[] + tmpDir = File.createTempDir() + def fileTreeBuilder = new FileTreeBuilder(tmpDir) + fileTreeBuilder.src { + main { + groovy { + 'Foo.groovy'('println "Hello"') + } + } + test { + groovy { + 'FooTest.groovy'('class FooTest extends GroovyTestCase {}') + } + } + } + // end::shorthand_syntax[] + + // tag::shorthand_syntax_assert[] + assert new File(tmpDir, '/src/main/groovy/Foo.groovy').text == 'println "Hello"' + assert new File(tmpDir, '/src/test/groovy/FooTest.groovy').text == 'class FooTest extends GroovyTestCase {}' + // end::shorthand_syntax_assert[] + } + }