Skip to content

Commit

Permalink
Add a test for partial user state reset (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeZ99 authored Sep 26, 2022
1 parent 5e675e9 commit c15c2c6
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions test/saxy/partial_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule Saxy.PartialTest do
"</foo>"
]

assert {:ok, events} = parse_partial(chunks)
assert {:ok, events} = parse_partial(chunks) |> Partial.terminate()

events = Enum.reverse(events)

Expand All @@ -40,23 +40,40 @@ defmodule Saxy.PartialTest do
]
end

test "fetch user state from a partial" do
chunks = ["<foo>", "sdf</foo>", ""]
partial = parse_partial(chunks)

assert Partial.get_state(partial) == [
{:end_element, "foo"},
{:characters, "sdf"},
{:start_element, {"foo", []}},
{:start_document, []}
]
end

test "resets user state" do
chunks = ["<some>c", "hun", "k</some>"]
partial = parse_partial(chunks)
{:cont, partial} = Partial.parse(partial, "", [:made_up_thing])
{:ok, state} = Partial.terminate(partial)
assert state == [{:end_document, {}}, :made_up_thing]
end

defp parse_partial(chunks) do
assert {:ok, partial} = Partial.new(StackHandler, [])

partial =
Enum.reduce(chunks, partial, fn chunk, acc ->
assert {:cont, partial} = Partial.parse(acc, chunk)
partial
end)

Partial.terminate(partial)
Enum.reduce(chunks, partial, fn chunk, acc ->
assert {:cont, partial} = Partial.parse(acc, chunk)
partial
end)
end

test "parses XML document partially line by line" do
for fixture <- @fixtures do
data_chunks = stream_fixture(fixture)

assert {:ok, _state} = parse_partial(data_chunks)
assert {:ok, _state} = parse_partial(data_chunks) |> Partial.terminate()
end
end

Expand Down

0 comments on commit c15c2c6

Please sign in to comment.