Rob Elliot
01/10/2022, 3:11 PMthen
blocks in a compiler plugin?
I still really miss the sheer simplicity of the syntax of Spock's tests.christophsturm
01/10/2022, 4:19 PMRob Elliot
01/10/2022, 4:27 PMclass EmailSpec extends Specification {
EmailService emailService = EmailService()
SmtpServer smtpServer = SmtpServer()
def 'can send an email'() {
given:
def message = EmailMessage(
emailAddress,
emailSubject,
emailBody
)
when:
emailService.send(message)
then:
smtpServer.messages == [message]
where:
emailAddress = "<mailto:jane@example.com|jane@example.com>"
emailSubject = "Hello World!"
emailBody = "Please to meet you"
}
}
Rob Elliot
01/10/2022, 4:32 PMthen
block - it's just one or more boolean expressions. I prefer this to shouldBe
or mustBe
or is
or whatever, it's so clean, and because of power assertion you can reuse lots of stdlib boolean methods / functions and still get all the feedback you need on fail.
• Moving data setup to the end of the test. Keeps the intent much, much clearer.
• This is minor, but given
, when
, then
being labels makes them "feel" much more part of the structure of the test than just comments, but without the noise of them being brace or parenthesis delimited blocks.
You can get closeish in out of the box Kotlin with a bit of work, but it's never quite as clean and so never quite as readable.Joffrey
01/10/2022, 5:56 PMJoffrey
01/10/2022, 5:57 PMRob Elliot
01/10/2022, 5:58 PMRob Elliot
01/10/2022, 5:59 PMJoffrey
01/10/2022, 6:01 PMYou don't need to, that's the beauty of power assert.So how can I see on CI the reason for the failure as explained in those assertions? I mean the why of the assert within the failure message
Joffrey
01/10/2022, 6:03 PMRob Elliot
01/10/2022, 6:04 PMwhen
& then
blocks in a Spock test if you want to assert and then continue.Joffrey
01/10/2022, 6:06 PMRob Elliot
01/10/2022, 6:19 PMgiven
, when
and then
in my tests anyway as comments. Any test should have that format (optional declaration of initial state, input, expectation), and separating them clearly helps with both reading and thinking through exactly what it is you are testing.
As I said in my "Things I like" comment above making them labels rather than comments makes it easier to read in my opinion, though this would be less so with Kotlin's label@
syntax.
then
has semantic meaning because it changes boolean expressions into assertions.server
01/11/2022, 1:13 AMserver
01/11/2022, 1:13 AMserver
01/11/2022, 1:15 AMserver
01/11/2022, 1:15 AMephemient
01/11/2022, 2:11 AMephemient
01/11/2022, 2:12 AMchristophsturm
01/11/2022, 8:20 AMserver
01/11/2022, 8:21 AMchristophsturm
01/11/2022, 8:22 AMgiven
and when
with describe, and then with it
lhwdev
01/11/2022, 8:23 AMsomeAssertFunction(original expr)
. For example then { a == b }
would become then { assert(a == b) }
. (here, then {}
is a simple marker.)
• Comparing some well-known types (like String, Int) causes 'unused expression', so need to suppress that warning through DiagnosticSuppressor
That's all, this one seems fairy easy to implement.server
01/11/2022, 8:25 AMserver
01/11/2022, 8:25 AMlhwdev
01/11/2022, 8:26 AMephemient
01/11/2022, 8:26 AMephemient
01/11/2022, 8:27 AM@CsvSource
(whose delimiters can be changed to |
if you like)ephemient
01/11/2022, 8:29 AMlhwdev
01/11/2022, 8:29 AMwhere {
item(1, 2, 3)
item(4, 5, 6)
}
Compiler plugins cannot do that. Extending kotlin itself for this is overkill.christophsturm
01/11/2022, 8:37 AMserver
01/11/2022, 8:58 AMserver
01/11/2022, 8:59 AMchristophsturm
01/11/2022, 9:45 AMserver
01/11/2022, 11:32 AMsam
01/12/2022, 2:27 AMserver
01/17/2022, 9:30 AMserver
01/17/2022, 9:32 AMserver
01/17/2022, 9:33 AMserver
01/17/2022, 9:34 AMserver
01/17/2022, 9:37 AMCondition not satisfied:
stack.size() == 2
| | |
| 1 false
[push me]
Rob Elliot
01/17/2022, 9:38 AMserver
01/17/2022, 9:38 AMserver
01/17/2022, 9:39 AMRob Elliot
01/17/2022, 9:39 AMserver
01/17/2022, 9:41 AMserver
01/17/2022, 9:42 AMRob Elliot
01/17/2022, 9:43 AMlhwdev
01/17/2022, 9:54 AM.map {}
even more than Groovy; it is more readable, intuitive to me.server
01/17/2022, 9:59 AMRob Elliot
01/17/2022, 10:05 AMwhere:
table inside and at the bottom of the test function, but still reference its variables in the test name and in the test - that makes the test cleaner to read. It goes straight from the name to expressing what matters about the test, without a bunch of setup syntax above it.christophsturm
01/17/2022, 10:32 AM@Rob Elliot is spread operator doable as kotlin plugin?no. kotlin plugins are not for introducing new syntax. power assert is a good example. it just improves the error message that a normal assert would produce. the source code would still compile without the plugin.
Rob Elliot
01/17/2022, 10:39 AMchristophsturm
01/17/2022, 10:41 AMRob Elliot
01/17/2022, 10:43 AMchristophsturm
01/17/2022, 10:43 AMserver
01/17/2022, 12:46 PMserver
01/17/2022, 12:51 PMchristophsturm
01/17/2022, 12:56 PMserver
01/17/2022, 1:09 PM