Kotest - YAML matchers

I am happy to tell you all about an upcoming Kotest feature that will be in one of the future versions (hopefully the next one ;) ) of Kotest: YAML matchers

What makes those matchers special for me is that they are my first contribution to the Kotest project. So, “achievement unlocked”, I could say.

The YAML matchers currently cover two aspects:

  • valid YAML

  • equal YAML

and for those, you have four methods:

  • shouldBeValidYaml

  • shouldNotBeValidYaml

  • shouldEqualYaml

  • shouldNotEqualYaml

The usage is quite simple. All of the methods are extension functions on String. Therefore, when importing from io.kotest.assertions.yaml you can call those on any String. E.g. when checking for valid YAML:

"""
foo: bar
""".shouldBeValidYaml()

Equally simple, when comparing YAML (notice the infix notation):

"""
key: value
""" shouldEqualYaml "key: value"

Hopefully, this will help others improve their tests. Additionally, I’d be happy to add more YAML matchers if others have suggestions. Lastly, the whole feature is multiplatform ready, so you can use it in your KMP project.

Implementation details

If anyone is interested, I can go more into the details. It is pretty simple actually. The base for the matchers is the KAML library. It is used for parsing the YAML. Parsing the YAML is already everything that is needed for checking the validity of the YAML. If KAML throws an exception we know that the YAML isn’t valid and the check fails (or passed if you check for not valid).

When checking for equality, we also start with parsing the YAML. Then, we compare the contents of the YAML, again relying on KAML.

And that’s all of the magic in the background.

Multiplatform and Java 8

What had been quite a journey for me was the multiplatform and Java 8 compatibility. Since Kotest supports all multiplatform targets until tier 3 I tried to also cover those targets (I could’ve chosen an easier way but that was my personal challenge). I then found out that KAML does not support all multiplatform targets. Changing this was a quite easy change.

Unfortunately, SnakeYAML Engine KMP, on which KAML is based, did not support Java 8. Therefore, it was incompatible with Kotest. In order to have Java 8 compatibility there, I had to go one level deeper (insert random Inception GIF here).

SnakeYAML uses an urlencoder and this one was the last link in the chain. So, in order to finish my YAML matchers, I had to start here. So I adjusted the urlencoder, in order to modify SnakeYAML, to have update it’s version in KAML. Finally, I could implement the YAML matchers with full multiplatform support.

Of course, I didn’t do everything by myself. I received much help by the maintainers of the respective projects. I'd like to add that everyone was super helpful. It was quite a very nice open source experience.

Although it took longer than expected and more hops than ever expected, I’m glad that I could finish this feature.

0
Subscribe to my newsletter

Read articles from Ronny Bräunlich directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ronny Bräunlich
Ronny Bräunlich