PhSt Transform - A library for building protocols on the fly

phst_transform is now 1.0.0 and available on hex.pm.

This library allows you to do build transforms of complex elixir data structures without any prior knowledge of the layout of the data. You supply it a map of anonymous functions that operate on the basic types you want to change, and it transforms the original data structure by using those anonymous functions.

Here’s an example from the tests:

test "implement scrub of empty values from map" do
    data = %{ :a => nil, :b => "", :c => "a"}
    replace_empty = fn(string, _d) -> if( string == "", do: nil , else: string) end
    replace_nil = fn(map, _depth) ->  for {k, v} <- map, v != nil , into: %{}, do: {k, v} end
    potion = %{ BitString => replace_empty, Map => replace_nil}

    assert PhStTransform.transform(data, potion) == %{:c => "a"}
end

Currently I am using this library to work on a project to convert ExUnit assertions into invariant properties of the function being tested. Once you have an invariant, you can then do property style testing on the function. Using this library, I’ve created tools that given an elixir data structure can create a function that can decide if any other data structure is either similar or congruent to the original data structure. It’s also fairly straightforward to mutate a data structure.

The key thing about all this is that the code doesn’t need to know anything about the underlying data structures so it can be automatically applied to any assertion of the == type.

i.e. a single assertion test can be turned into as many tests as you want to run.

6 Likes

If you want to play with the very early implementation of the testing library it’s at

2 Likes