@@ -19,6 +19,7 @@ package org.testingisdocumenting.webtau.cli.repl
1919import groovy.transform.PackageScope
2020import org.testingisdocumenting.webtau.cfg.WebTauConfig
2121import org.testingisdocumenting.webtau.console.ansi.Color
22+ import org.testingisdocumenting.webtau.data.render.PrettyPrintable
2223import org.testingisdocumenting.webtau.documentation.DocumentationArtifacts
2324import org.testingisdocumenting.webtau.reporter.stacktrace.StackTraceUtils
2425import org.testingisdocumenting.webtau.runner.standalone.StandaloneTest
@@ -36,8 +37,11 @@ class ReplCommands {
3637 @PackageScope
3738 static InteractiveTests interactiveTests
3839
39- private static TestsSelection testsSelection = new TestsSelection ()
40- private static List<StandaloneTest > availableScenarios = []
40+ @PackageScope
41+ static TestsSelection testsSelection = new TestsSelection ()
42+
43+ @PackageScope
44+ static List<StandaloneTest > availableScenarios = []
4145
4246 static WebTauConfig getReloadConfig () {
4347 return reloadConfig()
@@ -51,12 +55,12 @@ class ReplCommands {
5155 return WebTauConfig . cfg
5256 }
5357
54- static boolean getTestSelected () {
58+ static boolean getTestFileSelected () {
5559 return testsSelection. testFilePath != null
5660 }
5761
5862 static void select (Object ... selectors ) {
59- if (testSelected ) {
63+ if (testFileSelected ) {
6064 selectScenariosAndValidate(selectors)
6165 displaySelectedScenarios()
6266 } else {
@@ -65,32 +69,32 @@ class ReplCommands {
6569 return
6670 }
6771
68- selectTest (selectors[0 ])
72+ selectTestFile (selectors[0 ])
6973 }
7074 }
7175
7276 static void s (Object ... selectors ) {
7377 select(selectors)
7478 }
7579
76- static void getRun () {
77- runSelected ()
80+ static Object getRun () {
81+ return new ProxyToHandleNegativeIndexAndRunScenarios ()
7882 }
7983
80- static void getR () {
81- runSelected ()
84+ static Object getR () {
85+ return getRun ()
8286 }
8387
84- static void getSelect () {
85- displaySelectedScenarios ()
88+ static Object getSelect () {
89+ return new ProxyToHandleNegativeIndexAndDisplayScenarios ()
8690 }
8791
88- static void getS () {
89- getSelect()
92+ static Object getS () {
93+ return getSelect()
9094 }
9195
9296 static void run (Object ... selectors ) {
93- if (testSelected ) {
97+ if (testFileSelected ) {
9498 selectScenariosAndValidate(selectors)
9599
96100 if (testsSelection. scenarios) {
@@ -102,7 +106,7 @@ class ReplCommands {
102106 return
103107 }
104108
105- selectTest (selectors[0 ])
109+ selectTestFile (selectors[0 ])
106110
107111 if (testsSelection. testFilePath) {
108112 runSelected()
@@ -177,44 +181,46 @@ class ReplCommands {
177181 }
178182
179183 private static void listTestFilesOrScenarios () {
180- if (! testSelected ) {
184+ if (! testFileSelected ) {
181185 displayTestFiles()
182186 } else {
183187 displayScenarios(testsSelection. testFilePath)
184188 }
185189 }
186190
187- private static void selectTest (Object selector ) {
191+ private static void selectTestFile (Object selector ) {
188192 if (selector instanceof String ) {
189- selectTestByRegexp (selector)
193+ selectTestFileByRegexp (selector)
190194 return
191195 }
192196
193197 if (selector instanceof Integer ) {
194- selectTestByIndex (selector)
198+ selectTestFileByIndex (selector)
195199 return
196200 }
197201
198- out(Color . RED , " can't select test using: " + selector)
202+ out(Color . RED , " can't select test file using: " + selector)
199203 testsSelection. testFilePath = ' '
200204 testsSelection. scenarios = []
201205 }
202206
203- private static void selectTestByIndex (Integer idx ) {
204- if (idx < 0 || idx >= interactiveTests. testFilePaths. size()) {
205- out(Color . RED , ' enter a test index between 0 and ' + interactiveTests. testFilePaths. size())
207+ private static void selectTestFileByIndex (Integer idx ) {
208+ def size = interactiveTests. testFilePaths. size()
209+ idx = IndexSelection . convertNegativeIdxToAbsolute(size, idx)
210+ if (idx < 0 || idx >= size) {
211+ out(Color . RED , ' enter a test file index between 0 and ' + (size - 1 ))
206212 return
207213 }
208214
209215 testsSelection. testFilePath = interactiveTests. testFilePathByIdx(idx)
210216 displayScenarios(testsSelection. testFilePath)
211217 }
212218
213- private static void selectTestByRegexp (String regexp ) {
219+ private static void selectTestFileByRegexp (String regexp ) {
214220 def pattern = Pattern . compile(regexp, Pattern . CASE_INSENSITIVE )
215221 def found = interactiveTests. firstTestFilePathByRegexp(pattern)
216222 if (! found) {
217- out(Color . RED , " didn't find a test matching regexp: " + regexp)
223+ out(Color . RED , " didn't find a test file matching regexp: " + regexp)
218224 return
219225 }
220226
@@ -230,7 +236,7 @@ class ReplCommands {
230236 testsSelection. scenarios = []
231237
232238 withIssues. each {
233- out(Color . RED , " didn't find a test matching: " + it. input)
239+ out(Color . RED , " didn't find a scenario matching: " + it. input)
234240 }
235241 } else {
236242 testsSelection. scenarios = selector. inputAndResults. scenario
@@ -277,4 +283,31 @@ class ReplCommands {
277283 Color . PURPLE , testsSelection. testFilePath, ' ' ,
278284 Color . YELLOW , scenario)
279285 }
286+
287+ // <s 1> is treated as method "s" call with parameter <1>
288+ // <s -1> is treated as "getS() - 1" so we need to handle this in a way to make it feel like <s 1>
289+ // <s> still needs to display selected scenarios, so when <s> without minus index is called we need to render
290+ private static class ProxyToHandleNegativeIndexAndDisplayScenarios implements PrettyPrintable {
291+ Object minus (Integer indexToMakeNegative ) {
292+ select(indexToMakeNegative * -1 )
293+ return null
294+ }
295+
296+ @Override
297+ void prettyPrint () {
298+ displaySelectedScenarios()
299+ }
300+ }
301+
302+ private static class ProxyToHandleNegativeIndexAndRunScenarios implements PrettyPrintable {
303+ Object minus (Integer indexToMakeNegative ) {
304+ run(indexToMakeNegative * -1 )
305+ return null
306+ }
307+
308+ @Override
309+ void prettyPrint () {
310+ runSelected()
311+ }
312+ }
280313}
0 commit comments