masooh / intellij-junit-to-spock-converter

JUnit to Spock Converter IntelliJ Plugin: Converts JUnit to Spock. See https://plugins.jetbrains.com/plugin/12335-groovyfier
MIT License
8 stars 3 forks source link

Support assertThrows() conversion, with and without variable declaration #21

Open rojyates opened 3 years ago

rojyates commented 3 years ago

Conversion Request

I would like the plugin to replace

Exception exception = assertThrows(MyCustomException.class, () -> validator.validateInput(someData));

or, when split across lines:

Exception exception = assertThrows(MyCustomException.class, 
    () -> validator.validateInput(someData));

with

 when: "we execute our test"
 validator.validateInput(someData)

 then: "a MyCustomException was thrown"
 Exception exception = thrown(MyCustomException)

and, for the case where the exception is not captured as a variable, replace:

assertThrows(MyCustomException.class, () -> validator.validateInput(someData));

or, when split across lines:

assertThrows(MyCustomException.class, 
    () -> validator.validateInput(someData));

with

 when: "we execute our test"
 validator.validateInput(someData)

 then: "a MyCustomException was thrown"
 thrown(MyCustomException)

Regex

Here's the Regex that I currently use for the first case search:

([\w\.]+\s*\w+)\s*=\s*assertThrows\((\w+)(?:.class),[\(\)\{\r\n\s]+->(.*)[\}]*\)

and for the replace template:

when: "we execute our test"\n$3\n\n then: "a $2 was thrown"\n $1 = thrown($2)\n

Here's the Regex that I currently use for the second case search:

assertThrows\((\w+)(?:.class),[\(\)\{\r\n\s]+->(.*)[\}]*\)

and for the replace template:

when: "we execute our test"\n$2\n\n then: "a $1 was thrown"\n thrown($1)\n
masooh commented 3 years ago

Hello @rojyates Thank you for these well defined conversion requests. I'll go for them piece by piece. You can see my current prios in the newly created Kanban Board: https://github.com/masooh/intellij-junit-to-spock-converter/projects/1.