vim-erlang/vim-erlang-runtime

Couple of in -> out examples.

aerosol opened this issue · 2 comments

Hi,

Watching the repo, keep up the good work. Hopefully I could provide with you some tricky cases:

original:

rand_pprint_slice() ->
    F = fun pprint/3,
    Rand = fun() ->
            Bytes = crypto:rand_bytes(random:uniform(?MAX_BIN_SIZE)),
            Pos = random:uniform(byte_size(Bytes)),
            Len = random:uniform(byte_size(Bytes)),
            {Bytes, Pos, Len}
    end,
    Tests = [ Rand() || _ <- lists:seq(1, ?RUNS) ],
    Title = fun(Size, Slice) ->
            iolist_to_binary(io_lib:format("Random pprint w/ slice: (~p) ~p", [Size, Slice]))
    end,
    [ { Title(byte_size(Bytes), {Pos, Len}), fun() -> ?assertEqual(ok, F(Bytes, {Pos, Len}, [])) end }
        || { Bytes, Pos, Len } <- Tests ].

after indent:

rand_pprint_slice() ->
    F = fun pprint/3,
    Rand = fun() ->
        Bytes = crypto:rand_bytes(random:uniform(?MAX_BIN_SIZE)),
        Pos = random:uniform(byte_size(Bytes)),
        Len = random:uniform(byte_size(Bytes)),
        {Bytes, Pos, Len}
end,
   Tests = [ Rand() || _ <- lists:seq(1, ?RUNS) ],
   Title = fun(Size, Slice) ->
       iolist_to_binary(io_lib:format("Random pprint w/ slice: (~p) ~p", [Size, Slice]))
end,
   [ { Title(byte_size(Bytes), {Pos, Len}), fun() -> ?assertEqual(ok, F(Bytes, {Pos, Len}, [])) end }
   || { Bytes, Pos, Len } <- Tests ].

And another tricky one:

original:

rand_pprint_opts() ->
    F = fun pprint/2,
    CustomPrinter = fun(B) when is_list(B) -> works end,
    OptsMap = [
        %% Option                          %% Predicate
        { {return,  binary},               fun erlang:is_binary/1  },
        { {return,  iolist},               fun erlang:is_list/1    },

        { {printer, CustomPrinter},        fun(works) -> true; (_) -> false end  },
            { {invalid, option},               fun({'EXIT', {badarg, _}}) -> true; (O) -> O end }
            ],
    Range = lengthOptsMap),
    Rand = fun() ->
        Input = crypto:rand_bytes(random:uniform(?MAX_BIN_SIZE)),
        {Opt, Predicate} = lists:nth(random:uniform(Range), OptsMap),
        {Input, Opt, Predicate}
    end,
    Tests = [ Rand() || _ <- lists:seq(1, ?RUNS) ],
    Title = fun(Opt) ->
            iolist_to_binary([ "Random pprint w/ opt: ", io_lib:format("~p", [Opt]) ]) end,
    [ { Title(Opt), fun() -> ?assertEqual(true, Pred( catch( F(I, [Opt]) ) )) end }
             || {I, Opt, Pred} <- Tests ].

after indent:

rand_pprint_opts() ->
    F = fun pprint/2,
    CustomPrinter = fun(B) when is_list(B) -> works end,
                                                       OptsMap = [
                                                                  %% Option                          %% Predicate
                                                                  { {return,  binary},               fun erlang:is_binary/1  },
                                                                  { {return,  iolist},               fun erlang:is_list/1    },

                                                                  { {printer, CustomPrinter},        fun(works) -> true; (_) -> false end  },
                                                       { {invalid, option},               fun({'EXIT', {badarg, _}}) -> true; (O) -> O end }
],
 Range = lengthOptsMap),
                       Rand = fun() ->
                           Input = crypto:rand_bytes(random:uniform(?MAX_BIN_SIZE)),
                           {Opt, Predicate} = lists:nth(random:uniform(Range), OptsMap),
                           {Input, Opt, Predicate}
end,
   Tests = [ Rand() || _ <- lists:seq(1, ?RUNS) ],
   Title = fun(Opt) ->
       iolist_to_binary([ "Random pprint w/ opt: ", io_lib:format("~p", [Opt]) ]) end,
                                                                                     [ { Title(Opt), fun() -> ?assertEqual(true, Pred( catch( F(I, [Opt]) ) )) end }
                                                                                     || {I, Opt, Pred} <- Tests ].

Thank you (keep them coming ;) )

After 486a3d2, the first example is indented like the following:

rand_pprint_slice() ->
    F = fun pprint/3,
    Rand = fun() ->
                  Bytes = crypto:rand_bytes(random:uniform(?MAX_BIN_SIZE)),
                  Pos = random:uniform(byte_size(Bytes)),
                  Len = random:uniform(byte_size(Bytes)),
                  {Bytes, Pos, Len}
           end,
    Tests = [ Rand() || _ <- lists:seq(1, ?RUNS) ],
    Title = fun(Size, Slice) ->
                   iolist_to_binary(io_lib:format("Random pprint w/ slice: (~p) ~p", [Size, Slice]))
            end,
    [ { Title(byte_size(Bytes), {Pos, Len}), fun() -> ?assertEqual(ok, F(Bytes, {Pos, Len}, [])) end }
      || { Bytes, Pos, Len } <- Tests ].

The second example is indented like the following (after the removal of the syntactically invalid closing paren after the word "lengthOptsMap"):

rand_pprint_opts() ->
    F = fun pprint/2,
    CustomPrinter = fun(B) when is_list(B) -> works end,
    OptsMap = [
               %% Option                          %% Predicate
               { {return,  binary},               fun erlang:is_binary/1  },
               { {return,  iolist},               fun erlang:is_list/1    },

               { {printer, CustomPrinter},        fun(works) -> true; (_) -> false end  },
               { {invalid, option},               fun({'EXIT', {badarg, _}}) -> true; (O) -> O end }
              ],
    Range = lengthOptsMap,
    Rand = fun() ->
                  Input = crypto:rand_bytes(random:uniform(?MAX_BIN_SIZE)),
                  {Opt, Predicate} = lists:nth(random:uniform(Range), OptsMap),
                  {Input, Opt, Predicate}
           end,
    Tests = [ Rand() || _ <- lists:seq(1, ?RUNS) ],
    Title = fun(Opt) ->
                   iolist_to_binary([ "Random pprint w/ opt: ", io_lib:format("~p", [Opt]) ]) end,
    [ { Title(Opt), fun() -> ?assertEqual(true, Pred( catch( F(I, [Opt]) ) )) end }
      || {I, Opt, Pred} <- Tests ].

I'm not satisfied though, since

    Rand = fun() ->
                  Input = crypto:rand_bytes(random:uniform(?MAX_BIN_SIZE)),

should be

    Rand = fun() ->
                   Input = crypto:rand_bytes(random:uniform(?MAX_BIN_SIZE)),

(notice the one extra space :) ) By "should be" I mean that Emacs works this way, and since that is the de facto Erlang indentation standard, I want to follow that (unless there is a good reason).

Also, it is not very nice that the syntactically invalid extra ')' makes the script crazy and does the following:

Range = lengthOptsMap),
                       Rand = fun() ->

The "when" keyword and the "-spec" attribute are not handled at all by the script yet, so right now please don't submit errors that are based on those. But otherwise, the tricky cases are welcome :)

Thanks for your feedback.
As for syntax errors, I wouldn't expect the script to behave nicely. The missing ) was my mistake, I didn't notice it :-)

Totally +1 on following what emacs does.