Extract matching logic from test