Unit Test That a Class Is Non Copyable, and Other Compile-Time Properties

Unit test that a class is non copyable, and other compile-time properties

You can do it using make. Each test will be a code snippet. Here's a working example with 2 tests for VC++. (I've used 2 batch files for pass test and fail test). I'm using GNU make here.

Makefile:


FAILTEST = .\failtest.bat
PASSTEST = .\passtest.bat

tests: must_fail_but_passes \
must_pass_but_fails

must_fail_but_passes:
@$(FAILTEST) $@.cpp

must_pass_but_fails:
@$(PASSTEST) $@.cpp

must_pass_but_fails.cpp


struct Foo {
int value_;
Foo(void) : value_(0) {}
private:
Foo(const Foo&);
const Foo& operator=(const Foo&);
};

int main()
{
Foo f(12);
return 0;
}

must_fail_but_passes.cpp


struct Foo {
int value_;
Foo(int value) : value_(value) {}
private:
Foo(const Foo&);
const Foo& operator=(const Foo&);
};

int main()
{
Foo f(12);
return 0;
}

passtest.bat


@echo off
cl /nologo %1 >NUL
if %errorlevel% == 0 goto pass
@echo %1 FAILED
:pass

failtest.bat


@echo off
cl /nologo %1 >NUL
if not %errorlevel% == 0 goto pass
@echo %1 FAILED
:pass

Note that cl.exe (i.e. Visual Studio compiler) need to be in your path for this to "just work"

Have fun!

P.S. I doubt that this would make me famous though :-)

How to unit test deliberate compilation errors of template code

Do it in the similar way compiler tests are written. You will have a bit of testing code in some scripting language (shell, perl, tcl etc.) that will run compiler on given snippets of code and check whether the right ones compiled and the right ones did not.

  • gcc uses DejaGnu, which is built on top of expect, which is itself built on top of Tcl.
  • If you use shell script (probably easier, DejaGnu is probably overkill), you might want to look at shUnit2.
  • Perl's Test::Harness system should be mostly easy to use as is.
  • After all, it's not that much more work to run process from C++, so writing a function to try to call compiler on a given string and check whether it outputs error for line where you expect it would not be that hard and than you can integrate it into the other boost.test-based tests.

Test for expected compile time errors in java

You can use another software to run some commands and check its results. It is then easy to check compile-time errors. It is possible to write it as a script in some language or as a separate project in java, but it must be distinct from your project.

Your commands to run should be javac, ant or any other depending on your IDE.

How to test that some code doesn't compile in C++?

Boost does have compilation tests, and they do this by simply putting each of these tests into a single source-file and then try to compile each of these. Boost.Build supports special commands to run tests, which include testing if a file compiles or not.

How write a unit test for verifying compiling error?

If I understand you correctly, you have some piece of code that may not compile and you want to write the unit test that fails if the code really doesn't compile. If that is the case, then you should not write any unit tests. You should understand that you should write unit tests only for your code, not the code that somebody else wrote.

You didn't specify the programming language, so I will do some pseudo-code. Say you are writing a function to add two numbers:

function add(a, b) {
return a + b;
}

Very simple. You should unit test this, e.g. by making tests like the below:

function testAdd() {
assertEquals(4, add(2, 2));
assertEquals(46, add(12, 34));
}

However, you should not write a test that tests whether + operator is working fine. This is the job of whoever wrote the library that implements how + operator really works.

So, if that's the case, don't write any unit tests. Compiling your code is job of your compiler. The compiler should report that there is a compilation error in a proper way. You should not test whether compiler does its job correctly - testing that is the job of the people who are writing the compiler.

Unit testing property modifier

Can you write such a test? Yes; using plain javascript that calls the javascript code your Typescript code transpiles to.

However, should you write such a test?
No.

One of the main reasons to use transpiled and/or compiled languages such Typescript is that the compiler takes on part of the burden of correctness. Specifically, the correctness of the guarantees the language offers.

So sit back and enjoy the heavy lifting the compiler does for you.

One caveat: If your project uses a mix of javascript and Typescript then you might want to consider it, but then it is still probably better to spend that time rewriting the javascript bits to Typescript.

Edit:
Look at the situation from the perspective of the test: what would be necessary for the test to pass now and fail at some later time due to regression?
Well, since the code won't compile if some later edit modifies the readonly property, this edit must include removing the readonly from the property.
But having seen and rejected this guard, what is to stop the editor from removing the test that now fails as well?

So in a sense, the readonly modifier is the test.



Related Topics



Leave a reply



Submit