Lots of questions were sent my way after the last post about testing the compiler. A lot of them can be answered by providing some example tests that show some real code and demonstrate how a test works in our world. So that's what I'll attempt to do in this post. I'm keeping the tests as simple as possible as complicating the actual code in the test serves no purpose in this case. Also, I'd love to get some feedback on how others are testing compilers. I know there are a lot of people out there who work on similar projects and I'm hoping to find ways of sharing our best practices, ideas, processes, etc. So, please contribute ideas, questions, thoughts, etc. either via email or through the comments (which I'm staying with for now). Positive & Negative ExamplesLet's start off with a very simple positive test. Here's what the code would look like for what may be one of the simplest tests we'd have:
// <Title>A simple demonstration test (positive)</Title> // <Expects Status=Success/> class Test { static int Main() { int a = 3, b = 4; if (a + b == 7) return 0; return 1; } }
Things to note:
Here's a simple example of a test that is expected to generate a compiler error:
// <Title>A simple demonstration test (negative)</Title> // <Expects Status=Error>CS0029.*string.*int</Expects> class Test { static int Main() { return "fail"; } }
The Expects element now has an "Error" value for its Status attribute. This tells our harness we're expecting a compiler error and no generated binary. The error in the actual code will generate the following output from the compiler: "error CS0029: Cannot implicitly convert type 'string' to 'int'." The string "CS0029.*string.*int" in the Expects element is a regular expression that our harness verifies is a match with the compiler's actual output. If the compiler does not generate an error or generates an error but the error's text does not match the given regular expression, the test fails. One thing to note is that the only text that is matched for this test is the actual error code (e.g. CS0029) and the values of all "fill-in" strings (e.g. "string" and "int"). There are a few reasons for this: