Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Added a new example for the web, and better support on the delegate o…
Browse files Browse the repository at this point in the history
…n closures
  • Loading branch information
chiquitinxx committed Feb 22, 2013
1 parent 3bf4b72 commit 935fcb2
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/main/groovy/org/grooscript/GsConverter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -1126,10 +1126,11 @@ class GsConverter {
} else if (expression.operation.text=='<<') {
//We call add function
//println 'le->'+ expression.leftExpression
addScript('gSmethodCall(')
upgradedExpresion(expression.leftExpression)
addScript('.leftShift(')
addScript(',"leftShift", gSlist([')
upgradedExpresion(expression.rightExpression)
addScript(')')
addScript(']))')
//Regular Expression exact match all
} else if (expression.operation.text=='==~') {
addScript('gSexactMatch(')
Expand Down Expand Up @@ -1502,7 +1503,8 @@ class GsConverter {
if (expression.objectExpression instanceof VariableExpression) {
addParameters = false
def nameFunc = expression.objectExpression.text
addScript("(${nameFunc}.delegate!=undefined?${nameFunc}.apply(${nameFunc}.delegate,[")
//addScript("(${nameFunc}.delegate!=undefined?${nameFunc}.apply(${nameFunc}.delegate,[")
addScript("(${nameFunc}.delegate!=undefined?gSapplyDelegate(${nameFunc},${nameFunc}.delegate,[")
"process${expression.arguments.class.simpleName}"(expression.arguments,false)
addScript("]):${nameFunc}")
"process${expression.arguments.class.simpleName}"(expression.arguments)
Expand Down Expand Up @@ -1600,11 +1602,12 @@ class GsConverter {
"process${expression.objectExpression.class.simpleName}"(expression.objectExpression)
}

addScript(',"')
addScript(',')
//MethodName
addScript(expression.methodAsString)
//addScript(expression.methodAsString)
"process${expression.method.class.simpleName}"(expression.method)

addScript('",gSlist([')
addScript(',gSlist([')
//Parameters
"process${expression.arguments.class.simpleName}"(expression.arguments,false)

Expand Down
32 changes: 28 additions & 4 deletions src/main/resources/META-INF/resources/grooscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ gsBaseClass = {
var result = gSlist([]);
for (ob in this) {
if (typeof this[ob] === "function") {
var item = {
name: ob
};
result.add(item);

if (ob!='getStatic' && ob!='gSwith' && ob!='getProperties' && ob!='getMethods' && ob!='gSconstructor' &&
//TODO We don't know if a function is constructor, atm if function name starts with uppercase, it is
ob[0]!=ob[0].toUpperCase()) {
var item = {
name: ob
};
result.add(item);
}
}
}
return result;
Expand Down Expand Up @@ -2112,6 +2117,12 @@ function gSmethodCall(item,methodName,values) {
return eval(methodName).apply(this,values);
}*/

//Lets check in delegate
if (gSactualDelegate!=null &&
(gSactualDelegate[methodName]!=undefined || gSactualDelegate['methodMissing']!=undefined)) {
return gSmethodCall(gSactualDelegate,methodName,values);
}

//Not exist the method, throw exception
throw 'gSmethodCall Method '+ methodName + ' not exist in '+item;
}
Expand Down Expand Up @@ -2291,4 +2302,17 @@ function gSstringBuffer() {


return object;
}

////////////////////////////////////////////////////////////
// Delegate
////////////////////////////////////////////////////////////
var gSactualDelegate = null;
function gSapplyDelegate(func,delegate,params) {
var oldDelegate = gSactualDelegate;
//console.log('setting delegate');
gSactualDelegate = delegate;
func.apply(delegate,params);
//console.log('desetting delegate');
gSactualDelegate = oldDelegate;
}
9 changes: 9 additions & 0 deletions src/test/groovy/org/grooscript/TestAdvanced.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ class TestAdvanced extends Specification {
!result.assertFails
}

def 'advanced web example'() {
when:
def result = readAndConvert('advanced/AdvancedWebExample',false)

then:
//println 'Console->'+result.gSconsole
!result.assertFails
}

def 'more string features'() {
when:
def result = readAndConvert('advanced/StringSecrets',false)
Expand Down
92 changes: 92 additions & 0 deletions src/test/resources/advanced/AdvancedWebExample.groovy
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>'


0 comments on commit 935fcb2

Please sign in to comment.