This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new example for the web, and better support on the delegate o…
…n closures
- Loading branch information
1 parent
3bf4b72
commit 935fcb2
Showing
4 changed files
with
138 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package advanced | ||
|
||
/** | ||
* User: jorgefrancoleza | ||
* Date: 22/02/13 | ||
*/ | ||
|
||
class Tests { | ||
def 'failTest'() { | ||
def a = 2 | ||
|
||
then: | ||
check(a == 1) | ||
} | ||
|
||
def 'okTest'() { | ||
check(1 == 1) | ||
} | ||
} | ||
|
||
Tests.metaClass.static.doTest = { | ||
def result = [] | ||
def tests = new Tests() | ||
tests.metaClass.check = { exp -> | ||
if (!exp) { | ||
throw new Exception() | ||
} | ||
} | ||
|
||
tests.metaClass.methods.findAll { !['setProperty','setMetaClass','invokeMethod','getProperty','getMetaClass','__$swapInit', | ||
'wait','toString','notifyAll','notify','hashCode','getClass','equals','doTest','check'].contains(it.name) } | ||
.each { MetaMethod method -> | ||
|
||
def test = new Expando(name:method.name,fail:false) | ||
try { | ||
tests."${method.name}"() | ||
} catch (e) { | ||
test.fail = true | ||
} | ||
result << test | ||
} | ||
|
||
return result | ||
} | ||
|
||
def result = Tests.doTest() | ||
assert result.size() == 2 | ||
assert result.findAll { it.fail == true}.size() == 1 | ||
|
||
|
||
class LittleDsl { | ||
|
||
def StringBuffer text = new StringBuffer('') | ||
|
||
def static build(Closure closure) { | ||
def builder = new LittleDsl() | ||
closure.delegate = builder | ||
closure() | ||
return builder.text.toString() | ||
} | ||
|
||
def add(textToAdd) { | ||
text << textToAdd | ||
} | ||
|
||
def methodMissing(String name,args) { | ||
text << "<${name}>" | ||
if (args.last() instanceof Closure) { | ||
def clo = args.last() | ||
//clo.delegate = this | ||
clo() | ||
} | ||
text << "</${name}>" | ||
} | ||
} | ||
|
||
def text = LittleDsl.build { | ||
if (result.size()<=0) { | ||
p { add 'No results!' } | ||
} else { | ||
ul { | ||
def i = 0 | ||
result.each { item -> | ||
li { add "(${i++}) Name: ${item.name} Fail:${item.fail}"} | ||
} | ||
} | ||
} | ||
} | ||
|
||
assert text == '<ul><li>(0) Name: failTest Fail:true</li><li>(1) Name: okTest Fail:false</li></ul>' | ||
|
||
|