forked from BFergerson/Arthur
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- [BFergerson#28] Added Elvis operator
- Loading branch information
1 parent
027b7a6
commit 9683b53
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...ovy/com/codebrig/arthur/observe/structure/filter/operator/misc/ElvisOperatorFilter.groovy
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,38 @@ | ||
package com.codebrig.arthur.observe.structure.filter.operator.misc | ||
|
||
import com.codebrig.arthur.SourceNode | ||
import com.codebrig.arthur.observe.structure.StructureFilter | ||
import com.codebrig.arthur.observe.structure.filter.InternalRoleFilter | ||
import com.codebrig.arthur.observe.structure.filter.MultiFilter | ||
import com.codebrig.arthur.observe.structure.filter.RoleFilter | ||
|
||
/** | ||
* Match by Elvis operator | ||
* | ||
* @version 0.1 | ||
* @since 0.4 | ||
* @author <a href="mailto:[email protected]">Val Pecaoco</a> | ||
*/ | ||
class ElvisOperatorFilter extends StructureFilter<ElvisOperatorFilter, Void> { | ||
|
||
private final MultiFilter filter | ||
|
||
ElvisOperatorFilter() { | ||
filter = MultiFilter.matchAll( | ||
new RoleFilter("IF"), new RoleFilter("EXPRESSION"), new RoleFilter("RIGHT") | ||
) | ||
} | ||
|
||
@Override | ||
boolean evaluate(SourceNode node) { | ||
boolean result = filter.evaluate(node) | ||
if (result) { | ||
def matched = MultiFilter.matchAll( | ||
new RoleFilter("THEN"), | ||
new InternalRoleFilter("if") | ||
).getFilteredNodes(node.children) | ||
return !matched.hasNext() | ||
} | ||
return result | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...com/codebrig/arthur/observe/structure/filter/operator/misc/ElvisOperatorFilterTest.groovy
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,41 @@ | ||
package com.codebrig.arthur.observe.structure.filter.operator.misc | ||
|
||
import com.codebrig.arthur.ArthurTest | ||
import com.codebrig.arthur.SourceLanguage | ||
import com.codebrig.arthur.observe.structure.filter.FunctionFilter | ||
import com.codebrig.arthur.observe.structure.filter.MultiFilter | ||
import com.codebrig.arthur.observe.structure.filter.NameFilter | ||
import gopkg.in.bblfsh.sdk.v1.protocol.generated.Encoding | ||
import org.junit.Test | ||
|
||
import static org.junit.Assert.* | ||
|
||
class ElvisOperatorFilterTest extends ArthurTest { | ||
|
||
@Test | ||
void elvisOperator_Php() { | ||
assertElvisOperatorPresent(new File("src/test/resources/same/operators/Operators.php")) | ||
} | ||
|
||
private static void assertElvisOperatorPresent(File file) { | ||
assertElvisOperatorPresent(file, "") | ||
} | ||
|
||
private static void assertElvisOperatorPresent(File file, String qualifiedName) { | ||
def language = SourceLanguage.getSourceLanguage(file) | ||
def resp = client.parse(file.name, file.text, language.key, Encoding.UTF8$.MODULE$) | ||
|
||
def foundElvisOperator = false | ||
def functionFilter = new FunctionFilter() | ||
def nameFilter = new NameFilter("elvisOperator") | ||
MultiFilter.matchAll(functionFilter, nameFilter).getFilteredNodes(language, resp.uast).each { | ||
assertEquals(qualifiedName + "elvisOperator()", it.name) | ||
|
||
new ElvisOperatorFilter().getFilteredNodes(it).each { | ||
assertFalse(foundElvisOperator) | ||
foundElvisOperator = true | ||
} | ||
} | ||
assertTrue(foundElvisOperator) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -48,4 +48,10 @@ function ternaryOperator() | |
$b = ($a == 1) ? 20 : 30; | ||
} | ||
|
||
function elvisOperator() | ||
{ | ||
$a = 20; | ||
$b = $a ?: 30; | ||
} | ||
|
||
?> |