Are there unit tests?

I see plenty of tests written in Swift in the test dir (I suppose I would consider these regression tests, but that's just semantics), but are there unit tests for individual classes written in C++? For example, I was looking at ConnectedComponents in Sema/ConstraintGraph.cpp. Seems like a good candidate for unit testing, but it's in an anonymous namespace which means it's just local to that file.

There are some unit tests under the unittests directory.

1 Like

As a newcomer to the codebase I was really confused about this the other day. How do folks chose between writing tests in plain swift vs .cpp tests?

2 Likes

As a newcomer too, I don't know how one can deal with code this complex without lots of unit tests. Definitely not feeling smart enough (or patient enough) to assist.

I generally look at related codepaths and see how those are tested, and write new tests (or new cases for existing tests) accordingly. E.g. typically, I'm testing the LLVM IR generated for some particular Swift code. That has a bunch of tests under test/IRGen with FileCheck, so I will add new integration tests there using FileCheck.

If you're testing some type-checker diagnostics, there are diagnostics under test/Sema with %target-typecheck-verify-swift using expected-error and similar, so you'd use that style.

2 Likes

Historically most of the Sema related code has been tested via Swift code snippets because it's easy and integrates well with existing LLVM infrastructure (namely FileCheck). I believe that besides having this end-to-end sort of testing it's also very important to have pure unit tests to verify certain components in isolation, so now there is unittest/Sema which some infrastructure to test some aspects of constraint solver, which uses gtest infra. If you are looking to do some work in AST/Sema components please feel free to ping me.

5 Likes

@audulus I think it would be great if we could get some tests for ConnectedComponents in isolation, feel free to make that class visible and accessible for testing (probably best way would be via friend class to the unit test class).

1 Like