I’m writing a file parser for a particular file format called DIMACS. And I’d like to ask about how to supply data for the test suite. I’m planning two genre of test cases, which I’ll call small and large.
I’d like to write several small test cases, each containing a literal string which represents a small number (5 to 10) of lines file content.
In Common Lisp we have the functions with-input-from-string and its cousin make-string-input-stream, whose argument is a string (often a literal string), which gets opened and you are returned a stream, which acts like a file pointer. The caller may access the characters in this string with the same API as accessing the characters of a file opened for reading. This use particularly useful in test cases, as test cases can have many small examples of file content, directly in the test case code itself, and you can call the file processing functions directly on the open stream.
(define-test "test-case-1" (with-input-from-string (stream "c this string contains DIMACS content c simple_v3_c2.cnf c p cnf 3 2 1 -3 0 2 3 -1 0") (assert-true (equal '((1 -3) (2 3 -1)) (read-dimacs-file stream)))))
QUESTION 1: How does such a concept translate to Scala? I suppose it could be some concept inherited from the Java library. Or perhaps I need to make the parser read from some object which is an iterator of characters?
It would be great if the object supported
peek-ahead-one-char function, so I could examine the next character without consuming it. This would make the parser easier to write, as I could probably just translate the existing code I already have in another programming language, without missing the weird corner cases.
QUESTION 2: What is the correct way to open a file by relative-path relative to the test case directory, or relative to the project directory?
For the large test cases, I’d like to put certain benchmark files, which may be large into the project directory structure and have the test case locate it and open it. I certainly do not want the test suite to make assumptions about the full path to the benchmark files.