Search code examples
unit-testingracketabstract-data-type

How to unittest a struct without exposing accessor


Suppose I want to build a Vector library. It has a Vector struct:

(struct vector (x y z))

Now I want to write unit test for vector module in the test module. for testing I need to access x y and z. But I do not want to expose vector-{xyz}. Is there a way to provide accessor only for the unit tests?


Solution

  • Indeed I found the a way to hide things using submodule

     ;; vector.rkt
     (struct vector (x y z))
    
     (module* private-test #f
               (provide vector-x vector-y vector-z))
    

    I could then require the private-test only for the test purpose.

     ;; test.rkt
     (require (submod "vector.rkt" private-test)