Training F #: Is it possible to create a test case to verify the signature?

When unit testing several hundred lines of F # code, I realized that it would be useful not only to check the output, but also the signature. The reason is that if the code is verified for release and then changes are made after release that change the signature, I would like to know why the signature has changed so that either the test case can be updated for a new signature, or flagged as a problem.

Can I create a test case to verify the signature? If so, how?

+4
source share
2 answers

As Stephen said, if you write some unit tests for your code, unit tests will usually call a function with values ​​of the type that the function needs to automatically verify the signature (if you change the signature, you won’t be able to compile your tests).

Another option that is suitable for libraries is to use F # interface files ( .fsi ). The interface file defines the types of public functions in the implementation file ( .fs ), and this is also a good place for documentation.

If you (accidentally) change the type of your implementation, your code will not compile unless you update the type in the interface file.

You probably want to save the interface file manually (see F # library sources for a good example), but you can get the initial value by calling the compiler with --sig:mylibrary.fsi . You can probaby use this switch to automate testing (and test diff between signature files after each compilation).

+2
source

I think the best approach would be to simply provide test cases that span the borders of your signature. for example, to make sure the return type is int ,

 let x:int = someFunc() //you'll get a compiler error if the return type changes 

In fact, I would expect that only by virtue of exhaustive testing of your public API will you definitely verify signatures. Especially in a language such as F #, which has a relatively strict static type system.

I suppose you could also risk using reflection to validate signatures, but to be honest, I don't think that would be a good investment of time.

+2
source

Source: https://habr.com/ru/post/1437209/


All Articles