Skip to content

Instantly share code, notes, and snippets.

@spmallette
Created October 21, 2020 18:51
Show Gist options
  • Save spmallette/2f8a81c2803f1771037f82d857bb40bb to your computer and use it in GitHub Desktop.
Save spmallette/2f8a81c2803f1771037f82d857bb40bb to your computer and use it in GitHub Desktop.
test generator
/*
* 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.
*/
import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph
import org.apache.tinkerpop.gremlin.process.traversal.translator.PythonTranslator
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine
import org.apache.commons.text.StringEscapeUtils
import java.io.File
import javax.script.SimpleBindings
import groovy.io.FileType
import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal
// file is overwritten on each generation
radishGremlinFile = new File('../src/main/python/radish/gremlin.py')
// assumes globally unique scenario names for keys with list of Gremlin traversals as they appear
gremlins = [:].withDefault{[]}
gremlinGroovyScriptEngine = new GremlinGroovyScriptEngine()
pythonTranslator = PythonTranslator.of('g')
g = traversal().withGraph(EmptyGraph.instance())
bindings = new SimpleBindings()
bindings.put('g', g)
featureDir = new File('../gremlin-test/features')
featureDir.eachFileRecurse (FileType.FILES) { file ->
// only process gherkin files with .feature extension
if (file.name.endsWith('.feature')) {
def currentGremlin = ''
def openTriples = false
def scenarioName = ''
file.withReader { Reader reader ->
reader.eachLine { String line ->
def cleanLine = line.trim();
if (cleanLine.startsWith('Scenario:')) {
scenarioName = cleanLine.split(':')[1].trim()
} else if (cleanLine.startsWith('And the graph should return')) {
gremlins[scenarioName] << StringEscapeUtils.unescapeJava(cleanLine.substring(cleanLine.indexOf("\""), cleanLine.lastIndexOf("\"")))
} else if (cleanLine.startsWith("\"\"\"")) {
openTriples = !openTriples
if (!openTriples)
gremlins[scenarioName] << currentGremlin
} else if (openTriples) {
currentGremlin += cleanLine
}
}
}
}
}
radishGremlinFile.withWriter('UTF-8') { Writer writer ->
writer.writeLine('#\n' +
'# Licensed to the Apache Software Foundation (ASF) under one\n' +
'# or more contributor license agreements. See the NOTICE file\n' +
'# distributed with this work for additional information\n' +
'# regarding copyright ownership. The ASF licenses this file\n' +
'# to you under the Apache License, Version 2.0 (the\n' +
'# "License"); you may not use this file except in compliance\n' +
'# with the License. You may obtain a copy of the License at\n' +
'# \n' +
'# http://www.apache.org/licenses/LICENSE-2.0\n' +
'# \n' +
'# Unless required by applicable law or agreed to in writing,\n' +
'# software distributed under the License is distributed on an\n' +
'# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n' +
'# KIND, either express or implied. See the License for the\n' +
'# specific language governing permissions and limitations\n' +
'# under the License.\n' +
'#\n')
writer.writeLine(
'from gremlin_python.process.anonymous_traversal import traversal\n' +
'from gremlin_python.process.graph_traversal import __\n' +
'from gremlin_python.structure.graph import Graph\n' +
'from gremlin_python.process.traversal import Barrier, Cardinality, P, TextP, Pop, Scope, Column, Order, Direction, T, Pick, Operator, IO, WithOptions\n')
writer.writeLine("g = traversal().withGraph(Graph())")
writer.writeLine('gremlins = {')
gremlins.each { k,v ->
writer.write(" '")
writer.write(k)
writer.write("':[")
def gremlinItty = v.iterator()
while (gremlinItty.hasNext()) {
def t = gremlinGroovyScriptEngine.eval(gremlinItty.next(), bindings)
writer.write(pythonTranslator.translate(t.bytecode).script)
if (gremlinItty.hasNext()) writer.write(',')
}
writer.writeLine('],')
}
writer.writeLine('}')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment