Using reflection to write assertions


(Stefanos Zachariadis) #1

Hi all,

I recently wrote a library (
https://moto.co.de/blog/writing_reflective_test_assertions_with_swift.html )
to use to write assertions for one of my projects ( https://cyclema.ps ).
The goal of the exercise is: I like to write things using TDD. In order to
assert that things are produced the way I expect them, I have around 2
choices:

1. Make them equatable or
2. Assert individual fields

1. Is a lot of code that I need to maintain and I effectively modify my
prod code just for tests - my prod code probably doesn’t need equatable
items.
2. Is a pain :slight_smile:

MCAssertReflectiveEqual uses reflection to make these assertions using the
Mirror api, it recurses into the items and compares every single field -
please have a look and let me know if you have any feedback, good or bad?

I’ve got some questions if anyone knows:

- Mirror.displayStyle does not seem to have a type for closures /
functions. What’s the reason for this?
- When I find an enum, things get tricky. Currently I compare the string
representation (String(describing: enum)), but I’d love to be able to get
something like an ordinal generically. Is there a reason why I can’t? Also,
could I somehow use the identify function (===) to compare enums?

Thanks,

Stefanos