Update find matching test funcs.

When candidates are more than 1, look up to the dirnames and try to find best
matched file. In this way, we can take advantage of dirname based namespace
rules. If the best matched file is not clear, it behaves like it did.
This repeated code can be extracted into a helper function.