Skip to content

Commit c4b5974

Browse files
authored
Capture negative step slice warnings in tests (#12838)
1 parent cb8abb5 commit c4b5974

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

lib/elixir/lib/string.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,7 @@ defmodule String do
22312231
If the first position is after the string ends or after
22322232
the last position of the range, it returns an empty string:
22332233
2234-
iex> String.slice("elixir", 10..3)
2234+
iex> String.slice("elixir", 10..3//1)
22352235
""
22362236
iex> String.slice("a", 1..1500)
22372237
""

lib/elixir/test/elixir/range_test.exs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,11 @@ defmodule RangeTest do
220220
assert Enum.member?(desc, 2)
221221
assert Enum.count(desc) == 3
222222
assert Enum.drop(desc, 1) == [2, 1]
223-
assert Enum.slice([1, 2, 3, 4, 5, 6], desc) == []
223+
224+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
225+
assert Enum.slice([1, 2, 3, 4, 5, 6], desc) == []
226+
end) =~ "negative steps are not supported in Enum.slice/2, pass 3..1//1 instead"
227+
224228
# testing private Enum.aggregate
225229
assert Enum.max(desc) == 3
226230
assert Enum.sum(desc) == 6
@@ -233,7 +237,10 @@ defmodule RangeTest do
233237
desc = %{__struct__: Range, first: 3, last: 1}
234238

235239
assert String.slice("elixir", asc) == "lix"
236-
assert String.slice("elixir", desc) == ""
240+
241+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
242+
assert String.slice("elixir", desc) == ""
243+
end) =~ "negative steps are not supported in String.slice/2, pass 3..1//1 instead"
237244
end
238245
end
239246
end

lib/elixir/test/elixir/string_test.exs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ defmodule StringTest do
751751
end
752752

753753
test "slice/2" do
754-
assert String.slice("elixir", 0..-2) == "elixi"
754+
assert String.slice("elixir", 0..-2//1) == "elixi"
755755
assert String.slice("elixir", 1..3) == "lix"
756756
assert String.slice("elixir", -5..-3) == "lix"
757757
assert String.slice("elixir", -5..3) == "lix"
@@ -770,15 +770,19 @@ defmodule StringTest do
770770
assert String.slice("ειξήριολ", 9..9) == ""
771771
assert String.slice("", 0..0) == ""
772772
assert String.slice("", 1..1) == ""
773-
assert String.slice("あいうえお", -2..-4) == ""
774-
assert String.slice("あいうえお", -10..-15) == ""
775-
assert String.slice("hello あいうえお Unicode", 8..-1) == "うえお Unicode"
773+
assert String.slice("あいうえお", -2..-4//1) == ""
774+
assert String.slice("あいうえお", -10..-15//1) == ""
775+
assert String.slice("hello あいうえお Unicode", 8..-1//1) == "うえお Unicode"
776776
assert String.slice("abc", -1..14) == "c"
777-
assert String.slice("a·̀ͯ‿.⁀:", 0..-2) == "a·̀ͯ‿.⁀"
777+
assert String.slice("a·̀ͯ‿.⁀:", 0..-2//1) == "a·̀ͯ‿.⁀"
778778

779779
assert_raise FunctionClauseError, fn ->
780780
String.slice(nil, 0..1)
781781
end
782+
783+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
784+
assert String.slice("elixir", 0..-2) == "elixi"
785+
end) =~ "negative steps are not supported in String.slice/2, pass 0..-2//1 instead"
782786
end
783787

784788
test "slice/2 with steps" do

0 commit comments

Comments
 (0)