A general theory of rank testing