Fix attribute selection with %{<attr>[<num>]}, always returned first attribute

Add tests