Skip to content

Commit 86bcd43

Browse files
authored
Merge pull request #643 from 4VServices/511-support-sjs
Add language parameter to mlGenerateUnitTestSuite
2 parents 7a3a885 + d80c433 commit 86bcd43

File tree

2 files changed

+201
-66
lines changed

2 files changed

+201
-66
lines changed

src/main/groovy/com/marklogic/gradle/MarkLogicPlugin.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ class MarkLogicPlugin implements Plugin<Project> {
344344
String unitTestGroup = "ml-gradle Unit Test"
345345
project.task("mlGenerateUnitTestSuite", type: GenerateUnitTestSuiteTask, group: unitTestGroup,
346346
description: "Generate a marklogic-unit-test test suite. The test suite files are written to src/test/ml-modules/root/test/suites by default; use -PsuitesPath to override this. " +
347-
"Can use -PsuiteName to override the name of the test suite, and -PtestName to override the name of the test module.")
347+
"Can use -PsuiteName to override the name of the test suite, -PtestName to override the name of the test module, and -Planguage to specify \"sjs\" or \"xqy\" test code.")
348348
project.task("mlUnitTest", type: UnitTestTask, group: unitTestGroup, description: "Run tests found under /test/suites in the modules database. " +
349349
"Connects to MarkLogic via the REST API server defined by mlTestRestPort (or by mlRestPort if mlTestRestPort is not set), and uses mlRest* properties for authentication. " +
350350
"Use -PunitTestResultsPath to override where test result files are written, which defaults to build/test-results/marklogic-unit-test. " +

src/main/groovy/com/marklogic/gradle/task/test/GenerateUnitTestSuiteTask.groovy

Lines changed: 200 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,31 @@ import org.gradle.api.tasks.TaskAction
2121

2222
class GenerateUnitTestSuiteTask extends MarkLogicTask {
2323

24-
static String getSampleTestsXqy() {
25-
return """xquery version '1.0-ml';
24+
abstract class GenerateTestSuite {
25+
abstract String getSampleTests();
26+
abstract String getSetup();
27+
abstract String getSetupName();
28+
abstract String getTeardown();
29+
abstract String getTeardownName();
30+
abstract String getSuiteSetup();
31+
abstract String getSuiteSetupName();
32+
abstract String getSuiteTeardown();
33+
abstract String getSuiteTeardownName();
34+
abstract String getExtension();
35+
}
36+
37+
class XQueryTestSuite extends GenerateTestSuite {
38+
String getSampleTests() {
39+
return """xquery version '1.0-ml';
2640
2741
import module namespace test = 'http://marklogic.com/test' at '/test/test-helper.xqy';
2842
29-
test:assert-true(fn:true()),
43+
test:success(),
3044
test:log("\$testName COMPLETE....")"""
31-
}
45+
}
3246

33-
static String getSetupXqy() {
34-
return """xquery version '1.0-ml';
47+
String getSetup() {
48+
return """xquery version '1.0-ml';
3549
3650
import module namespace test = 'http://marklogic.com/test' at '/test/test-helper.xqy';
3751
@@ -42,10 +56,14 @@ import module namespace test = 'http://marklogic.com/test' at '/test/test-helper
4256
Each setup runs in its own transaction.
4357
:)
4458
test:log("\$testName Setup COMPLETE....")"""
45-
}
59+
}
4660

47-
static String getTeardownXqy() {
48-
return """xquery version '1.0-ml';
61+
String getSetupName() {
62+
return "/setup.xqy";
63+
}
64+
65+
String getTeardown() {
66+
return """xquery version '1.0-ml';
4967
5068
import module namespace test = 'http://marklogic.com/test' at '/test/test-helper.xqy';
5169
@@ -55,10 +73,14 @@ import module namespace test = 'http://marklogic.com/test' at '/test/test-helper
5573
If no test-specific teardown is required, this file may be deleted.
5674
:)
5775
test:log("\$testName Teardown COMPLETE....")"""
58-
}
76+
}
5977

60-
static String getSuiteSetupXqy() {
61-
return """xquery version '1.0-ml';
78+
String getTeardownName() {
79+
return "/teardown.xqy";
80+
}
81+
82+
String getSuiteSetup() {
83+
return """xquery version '1.0-ml';
6284
6385
import module namespace test = 'http://marklogic.com/test' at '/test/test-helper.xqy';
6486
@@ -68,10 +90,14 @@ import module namespace test = 'http://marklogic.com/test' at '/test/test-helper
6890
If no suite-specific setup is required, this file may be deleted.
6991
:)
7092
test:log("\$suiteName Suite Setup COMPLETE....")"""
71-
}
93+
}
7294

73-
static String getSuiteTeardownXqy() {
74-
return """xquery version '1.0-ml';
95+
String getSuiteSetupName() {
96+
return "/suite-setup.xqy";
97+
}
98+
99+
String getSuiteTeardown() {
100+
return """xquery version '1.0-ml';
75101
76102
import module namespace test = 'http://marklogic.com/test' at '/test/test-helper.xqy';
77103
@@ -80,60 +106,169 @@ import module namespace test = 'http://marklogic.com/test' at '/test/test-helper
80106
If no suite-specific teardown is required, this file may be deleted.
81107
:)
82108
test:log("\$suiteName Suite Teardown ENDING....")"""
109+
}
110+
111+
String getSuiteTeardownName() {
112+
return "/suite-teardown.xqy";
113+
}
114+
115+
String getExtension() {
116+
return ".xqy";
117+
}
118+
119+
}
120+
121+
class JavaScriptTestSuite extends GenerateTestSuite {
122+
String getSampleTests() {
123+
return """"use strict";
124+
125+
const test = require('/test/test-helper.xqy');
126+
127+
test.success(),
128+
test.log("\$testName COMPLETE....")"""
129+
}
130+
131+
String getSetup() {
132+
return """"use strict";
133+
134+
const test = require('/test/test-helper.xqy');
135+
136+
declareUpdate();
137+
138+
/*
139+
This module will be run before each test in your suite.
140+
Here you might insert a document into the test database that each of your tests will modify.
141+
If no test-specific setup is required, this file may be deleted.
142+
Each setup runs in its own transaction.
143+
*/
144+
test.log("\$testName Setup COMPLETE....")"""
145+
}
146+
147+
String getSetupName() {
148+
return "/setup.sjs";
149+
}
150+
151+
String getTeardown() {
152+
return """"use strict";
153+
154+
const test = require('/test/test-helper.xqy');
155+
156+
declareUpdate();
157+
158+
/*
159+
This module will run after each test in your suite.
160+
You might use this module to remove the document inserted by the test setup module.
161+
If no test-specific teardown is required, this file may be deleted.
162+
*/
163+
test.log("\$testName Teardown COMPLETE....")"""
164+
}
165+
166+
String getTeardownName() {
167+
return "/teardown.sjs";
168+
}
169+
170+
String getSuiteSetup() {
171+
return """"use strict";
172+
173+
const test = require('/test/test-helper.xqy');
174+
175+
declareUpdate();
176+
177+
/*
178+
Runs once when your suite is started.
179+
You can use this to insert some data that will not be modified over the course of the suite's tests.
180+
If no suite-specific setup is required, this file may be deleted.
181+
*/
182+
test.log("\$suiteName Suite Setup COMPLETE....")"""
183+
}
184+
185+
String getSuiteSetupName() {
186+
return "/suiteSetup.sjs";
187+
}
188+
189+
String getSuiteTeardown() {
190+
return """"use strict";
191+
192+
const test = require('/test/test-helper.xqy');
193+
194+
declareUpdate();
195+
196+
/*
197+
Runs once when your suite is finished, to clean up after the suite's tests.
198+
If no suite-specific teardown is required, this file may be deleted.
199+
*/
200+
test.log("\$suiteName Suite Teardown ENDING....")"""
201+
}
202+
203+
String getSuiteTeardownName() {
204+
return "/suiteTeardown.sjs";
205+
}
206+
207+
String getExtension() {
208+
return ".sjs";
209+
}
210+
83211
}
84212

85213
@Internal
86214
CommandLineArguments arguments
87215

88-
@TaskAction
89-
void generateTestSuite() {
90-
this.arguments = new CommandLineArguments()
91-
92-
def binding = ["testName":arguments.testName, "suiteName":arguments.suiteName]
93-
def engine = new groovy.text.SimpleTemplateEngine()
94-
95-
project.file(arguments.suitePath).mkdirs()
96-
def sampleTestsXqyString = getSampleTestsXqy()
97-
processTemplateString(engine, binding, arguments.suitePath, "/" + arguments.testName + ".xqy", sampleTestsXqyString)
98-
def suiteSetupXqyString = getSuiteSetupXqy()
99-
processTemplateString(engine, binding, arguments.suitePath, "/suite-setup.xqy", suiteSetupXqyString)
100-
def suiteTeardownXqyString = getSuiteTeardownXqy()
101-
processTemplateString(engine, binding, arguments.suitePath, "/suite-teardown.xqy", suiteTeardownXqyString)
102-
def setupXqyString = getSetupXqy()
103-
processTemplateString(engine, binding, arguments.suitePath, "/setup.xqy", setupXqyString)
104-
def teardownXqyString = getTeardownXqy()
105-
processTemplateString(engine, binding, arguments.suitePath, "/teardown.xqy", teardownXqyString)
106-
107-
println "Finished generating test suite."
108-
}
109-
110-
def processTemplateString(engine, binding, targetBaseDir, templateFilePath, templateString) {
111-
def template = engine.createTemplate(templateString).make(binding)
112-
def templateResult = template.toString()
113-
def targetFilepath = targetBaseDir + templateFilePath
114-
project.file(targetFilepath).write(templateResult)
216+
@TaskAction
217+
void generateTestSuite() {
218+
this.arguments = new CommandLineArguments()
219+
GenerateTestSuite generator = this.arguments.lang == CommandLineArguments.Language.JAVASCRIPT ? new JavaScriptTestSuite() : new XQueryTestSuite();
220+
221+
def binding = ["testName":arguments.testName, "suiteName":arguments.suiteName]
222+
def engine = new groovy.text.SimpleTemplateEngine()
223+
224+
project.file(arguments.suitePath).mkdirs()
225+
processTemplateString(engine, binding, arguments.suitePath, "/" + arguments.testName + generator.getExtension(), generator.getSampleTests())
226+
processTemplateString(engine, binding, arguments.suitePath, generator.getSuiteSetupName(), generator.getSuiteSetup())
227+
processTemplateString(engine, binding, arguments.suitePath, generator.getSuiteTeardownName(), generator.getSuiteTeardown())
228+
processTemplateString(engine, binding, arguments.suitePath, generator.getSetupName(), generator.getSetup())
229+
processTemplateString(engine, binding, arguments.suitePath, generator.getTeardownName(), generator.getTeardown())
230+
231+
println "Finished generating test suite."
232+
}
233+
234+
def processTemplateString(engine, binding, targetBaseDir, templateFilePath, templateString) {
235+
def template = engine.createTemplate(templateString).make(binding)
236+
def templateResult = template.toString()
237+
def targetFilepath = targetBaseDir + templateFilePath
238+
project.file(targetFilepath).write(templateResult)
115239
println "Generated test suite file: " + targetFilepath
116-
}
117-
118-
class CommandLineArguments {
119-
String suitesPath = "src/test/ml-modules/root/test/suites"
120-
String suiteName = 'SampleTestSuite'
121-
String suitePath
122-
String testName = 'sample-tests'
123-
124-
CommandLineArguments() {
125-
if (project.hasProperty("suitesPath")) {
126-
this.suitesPath = project.property("suitesPath")
127-
}
128-
if (project.hasProperty('suiteName')) {
129-
this.suiteName = project.property('suiteName')
130-
}
131-
this.suitePath = this.suitesPath + '/' + suiteName
132-
if (project.hasProperty('testName')) {
133-
this.testName = project.property('testName')
134-
}
135-
136-
suitePath = suitesPath + "/" + suiteName
137-
}
138-
}
240+
}
241+
242+
class CommandLineArguments {
243+
public enum Language { XQUERY, JAVASCRIPT }
244+
String suitesPath = "src/test/ml-modules/root/test/suites"
245+
String suiteName = 'SampleTestSuite'
246+
String suitePath
247+
String testName = 'sample-tests'
248+
Language lang
249+
250+
CommandLineArguments() {
251+
if (project.hasProperty("suitesPath")) {
252+
this.suitesPath = project.property("suitesPath")
253+
}
254+
if (project.hasProperty('suiteName')) {
255+
this.suiteName = project.property('suiteName')
256+
}
257+
this.suitePath = this.suitesPath + '/' + suiteName
258+
if (project.hasProperty('testName')) {
259+
this.testName = project.property('testName')
260+
}
261+
if (project.hasProperty('language')) {
262+
if (project.property('language').equals('sjs')) {
263+
lang = Language.JAVASCRIPT
264+
} else {
265+
lang = Language.XQUERY
266+
}
267+
} else {
268+
lang = Language.XQUERY
269+
}
270+
271+
suitePath = suitesPath + "/" + suiteName
272+
}
273+
}
139274
}

0 commit comments

Comments
 (0)