Skip to content

Instantly share code, notes, and snippets.

@neenjaw
Created November 24, 2020 13:43
Show Gist options
  • Select an option

  • Save neenjaw/9f6b15f39a64d80a1f0f4b01e39306d2 to your computer and use it in GitHub Desktop.

Select an option

Save neenjaw/9f6b15f39a64d80a1f0f4b01e39306d2 to your computer and use it in GitHub Desktop.
example parsing csv in elixir
defmodule Meta.CSVParser do
@csv_parser_module TestCSVParser
alias Meta.AssertParser
defp define_parser() do
unless Code.ensure_loaded?(@csv_parser_module) do
NimbleCSV.define(@csv_parser_module, separator: ",", escape: ~S'"')
end
end
def parse_string(meta_string) do
{:ok, stream} = meta_string |> StringIO.open()
parse_stream(stream)
end
def parse_stream(stream_source \\ :stdio) do
define_parser()
stream = IO.stream(stream_source, :line)
Kernel.apply(@csv_parser_module, :parse_stream, [stream, [skip_headers: false]])
|> Stream.map(&line_to_entry/1)
|> Enum.into(%{})
end
@doc """
"""
def line_to_entry(line) do
[test_name, pre_assert_code, assertion_block_count | assertion_code_blocks] = line
case assertion_block_count do
count when count != "1" ->
{
test_name,
%{error: "unable to use meta from test cases which have more than one assertion"}
}
_ ->
{:ok, command, expected} = assertion_code_blocks |> hd |> AssertParser.parse()
command =
case pre_assert_code do
"" -> command
code when is_binary(code) -> code <> "\n" <> command
end
{
test_name,
%{
cmd: command,
expected: expected
}
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment