Erlang
(page 127)
test/prop_generators.erl
Question 6 is the most difficult task that I have encountered while reading the book (So far I have read only the first 4 chapters several times.).
The first difficulty - for the input parameter of the generator,
file(Name) ->
?SIZED(
Size,
lines(Size, {'$call', ?MODULE, file_open, [Name, [read,write,raw]]})
).
it requires the name (path) of the file (path to the file). It must be implemented in some way (the author is silent on how to do this, since this is not a significant part of the question). The complexity is compounded by the fact that creating a path to a file (temporary file) in itself is not such an easy task.
I solved the generation of a temporary file using the following functions:
mktemp() ->
mktemp("tmp").
-spec mktemp(Prefix) -> Result
when Prefix :: string(),
Result :: TempFile :: file:filename().
mktemp(Prefix) ->
Rand = integer_to_list(binary:decode_unsigned(crypto:strong_rand_bytes(8)), 36),
TempDir = filename:basedir(user_cache, Prefix),
os:cmd("mkdir " ++ "\"" ++ TempDir ++ "\""),
TempFilePath = filename:join(TempDir, Rand),
TempFilePath.
and implement property based test of their functions Project source code.
Then I tried using the suggested generator file/1. When starting the test,
rebar3 proper -m prop_solutions -p prop_make_tmp_file -n 1
I noticed that Automated Symbolic Call do not work:
===>
0/1 properties passed, 1 failed
===> Failed test cases:
prop_solutions:prop_make_tmp_file() -> {'EXIT',
{undef,
[{prop_solutions,file_open,
[".../AppData/Local/tmp/Cache/15U3A5WBAWWOS",
This means that when trying to call a function file_open, it is not in the expected place. Very strange. 
Replaced Automated Symbolic Call with Symbolic Call this error has been corrected.
===> Testing prop_solutions:prop_make_tmp_file()
.
OK: Passed 1 test(s).
===>
1/1 properties passed
So, I will continue my research work. I hoped that I had find solutions and everything will work correctly.
Unfortunately, this did not lead to the desired result - the created temporary file does not contain anything. I tested this by introducing the logging function io:format and also directly checked what the files were inside.
That functions that should be called using Symbolic Call are not called all the way.
Also I noticed that the compiler does not perceive symbolic function calls as using them. To prevent the compiler from printing warning messages - I added an extra line to the beginning of the test code file.
-compile([{nowarn_unused_function, [{ file_open, 2}, {file_write, 2}]}]).
So, I will continue my research work with this functionality (look at PropEr documentation). I hope that I will find solutions and everything will work correctly and predictable.