Search code examples
c#listspec#model-based-testing

How to reset _version list to prevent duplicate states in Spec Explorer


I've made a Spec Explorer project that has a list as system variable. Almost in every rule this list is adapted to get the correct results. But when I explore my project I get duplicate states. If I compare these states the only difference I find is:

_version: 25 System.Int32
_version: 23 System.Int32

After googling why this happens I found that each time a string is altered the version changes. So even though version 23 and 25 contain the same string, the version number makes Spec Explorer think they are different, so it produces 2 states. Is there anyway to fix this? Like resetting the string or maybe there is a way to force Spec Explorer to accept this.


Solution

  • Here is an idea for a possible root cause:

    The rich state objects like Set, Sequence or Map are immutable. This means, if you want to add an element the "Add"-method returns a new object with the changed content. Important is that this returned object still is detected as the same (but changed content) instance by Spec Explorer. So you should always reuse this returned object. You can do this by assigning the changed object back to the static state variable of you program model:

    _version = _version.Add(15);
    

    If in your program instead a complete new object is now created on every call of your rule method with a "new"-operator and this new object is copied to the static state variable of you program model you will get always a new instance and Spec Explorer will detect them as different instances, even if the content is the same.

    _newversion = new Set<int>();  
    ...  
    _version = _newversion;
    

    Hope this helps in your case ...