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 `assertThat` transformations #17

Open leonard84 opened 3 years ago

leonard84 commented 3 years ago

This can be a two step approach.

Spock has spock.util.matcher.HamcrestSupport so the basic approach would be to replace the assertThat invocations with the that() (in expect-blocks) or expect() (in then-blocks) methods of Hamcrest-support.

In a second iteration, the matchers could be analyzed and replaced if there is a simple Spock equivalent, e.g., assertThat(x, is("ok")) would be x == "ok", and assertThat(x, is(not("ok"))) would be x != "ok".

rojyates commented 3 years ago

I’ve been using a bunch of regex search/replaces in IntelliJ. My assertThat search, mainly for the is() flavour, is: assertThat\((.*),\s*is\((.*)\)\) $1 == $2 So the is(not( equivalent would be: assertThat\((.*),\s*is\(not\((.*)\)\)\) $1 != $2

And a more interesting variant to transform assertThrows: assertThrows\((\w+)(?:.class),[\(\)\{\s]+->(.*)[\}]*\) when: "we execute our test"\n$2\n\n then: "a $1 was thrown"\n thrown($1)\n

Although I guess our matchers might be more sophisticated...

masooh commented 3 years ago

Thank you for your input, I'll go for it.

@rojyates If you have more Regex or Replacements which you would like to have, feel free create more issues. I created a new issue template to facilitate the process of creating replacement issues. Even if the complete framework support is on the roadmap I'll focus on the concrete cases by the users which I'll find in the issues.

rojyates commented 3 years ago

@masooh that's a great idea. I'll start adding some of them in. Another useful option would be to make use of https://github.com/marcingrzejszczak/spock-subjects-collaborators-extension, which gives a Spock version for Mockito's @InjectMocks.

hankchan101 commented 3 years ago

btw. Support assertSame would be kind

leonard84 commented 3 years ago

@rojyates IMHO we should focus on supporting the core Spock features first, but feel free to open another ticket for the collaborator extension.