Permalink
Browse files

more tests, fails om repeted

  • Loading branch information...
freke committed Oct 6, 2011
1 parent 8b29378 commit 8091c1a2c060b33a986789b7b44ed84aee734127
View
@@ -0,0 +1,5 @@
{level,details}.
{incl_mods,
[protobuffs,
protobuffs_compile,
protobuffs_file]}.
View
@@ -1,8 +1,12 @@
{erl_opts, [debug_info,warn_missing_spec]}.
{deps,[{proper,"1.*", {git, "git://github.com/manopapad/proper.git", "master"}}
{deps,[{proper,"1.*", {git, "git://github.com/manopapad/proper.git", "master"}},
{meck,"0.*", {git, "https://github.com/eproxus/meck.git", {branch, "master"}}}
]}.
{clean_files, ["*~","**/*~","**/*.beam","logs/*"]}.
{cover_enabled, true}.
{cover_enabled, true}.
{eunit_opts, [verbose,
{report, {eunit_surefire, [{dir, "."}]}}]}.
View
@@ -23,7 +23,13 @@
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
%% OTHER DEALINGS IN THE SOFTWARE.
-module(protobuffs_compile).
-export([scan_file/1, scan_file/2, scan_string/2, scan_string/3, generate_source/1, generate_source/2]).
-ifdef(TEST).
-compile(export_all).
-else.
-export([scan_file/1, scan_file/2, scan_string/2, scan_string/3,
generate_source/1, generate_source/2]).
-endif.
-record(collected,{enum=[], msg=[], extensions=[]}).
@@ -46,13 +52,17 @@ scan_string(String,BaseName) ->
%% output_ebin_dir,
%% imports_dir
%%--------------------------------------------------------------------
-spec scan_file(ProtoFile :: string(), Options :: list()) ->
-spec scan_file(ProtoFile :: string() | atom(), Options :: list()) ->
ok | {error, _}.
scan_file(ProtoFile,Options) when is_list(ProtoFile) ->
Basename = filename:basename(ProtoFile, ".proto") ++ "_pb",
{ok,String} = parse_file(ProtoFile),
scan_string(String,Basename,Options);
scan_file(ProtoFile,Options) when is_atom(ProtoFile) ->
Basename = atom_to_list(ProtoFile) ++ "_pb",
{ok,String} = parse_file(atom_to_list(ProtoFile) ++ ".proto"),
scan_string(String,Basename,Options).
-spec scan_string(String :: string(), Basename :: string(), Options :: list()) ->
ok | {error, _}.
scan_string(String,Basename,Options) ->
@@ -68,8 +78,11 @@ scan_string(String,Basename,Options) ->
%%--------------------------------------------------------------------
-spec generate_source(ProtoFile :: string()) ->
ok | {error, _}.
generate_source(ProtoFile) when is_atom(ProtoFile) ->
generate_source(atom_to_list(ProtoFile),[]);
generate_source(ProtoFile) ->
generate_source(ProtoFile,[]).
Basename = filename:basename(ProtoFile, ".proto"),
generate_source(Basename,[]).
%%--------------------------------------------------------------------
%% @doc Generats a source .erl file and header file .hrl
@@ -80,7 +93,7 @@ generate_source(ProtoFile) ->
-spec generate_source(ProtoFile :: string(), Options :: list()) ->
ok | {error, _}.
generate_source(ProtoFile,Options) when is_list (ProtoFile) ->
Basename = filename:basename(ProtoFile, ".proto") ++ "_pb",
Basename = ProtoFile ++ "_pb",
{ok,String} = parse_file(ProtoFile),
{ok,FirstParsed} = parse_string(String),
ImportPaths = ["./", "src/" | proplists:get_value(imports_dir, Options, [])],
@@ -97,7 +110,7 @@ parse_imports(Parsed, Path) ->
parse_imports([], _Path, Acc) ->
lists:reverse(Acc);
parse_imports([{import, File} = Head | Tail], Path, Acc) ->
case file:path_open(Path, File, [read]) of
case protobuffs_file:path_open(Path, File, [read]) of
{ok, F, Fullname} ->
file:close(F),
{ok,String} = parse_file(Fullname),
@@ -124,24 +137,21 @@ output(Basename, Messages, Enums, Options) ->
HeaderPath ->
HeaderFile = filename:join(HeaderPath,Basename) ++ ".hrl"
end,
case proplists:get_bool(debug_info,Options) of
true -> DebugInfo = debug_info;
false -> DebugInfo = []
end,
error_logger:info_msg("Writing header file to ~p~n",[HeaderFile]),
ok = write_header_include_file(HeaderFile, Messages),
PokemonBeamFile = filename:dirname(code:which(?MODULE)) ++ "/pokemon_pb.beam",
PokemonBeamFile = code:where_is_file("pokemon_pb.beam"),
{ok,{_,[{abstract_code,{_,Forms}}]}} = beam_lib:chunks(PokemonBeamFile, [abstract_code]),
Forms1 = filter_forms(Messages, Enums, Forms, Basename, []),
{ok, _, Bytes, _Warnings} = compile:forms(Forms1, [return]++DebugInfo),
{ok, _, Bytes, _Warnings} = protobuffs_file:compile_forms(Forms1, proplists:get_value(compile_flags,Options,[])),
case proplists:get_value(output_ebin_dir,Options) of
undefined ->
BeamFile = Basename ++ ".beam";
BeamPath ->
BeamFile = filename:join(BeamPath,Basename) ++ ".beam"
end,
error_logger:info_msg("Writing beam file to ~p~n",[BeamFile]),
file:write_file(BeamFile, Bytes).
protobuffs_file:write_file(BeamFile, Bytes).
%% @hidden
output_source (Basename, Messages, Enums, Options) ->
@@ -163,18 +173,18 @@ output_source (Basename, Messages, Enums, Options) ->
SrcFile = filename:join(SrcPath,Basename) ++ ".erl"
end,
error_logger:info_msg("Writing src file to ~p~n",[SrcFile]),
file:write_file(SrcFile, erl_prettypr:format(erl_syntax:form_list (Forms1))).
protobuffs_file:write_file(SrcFile, erl_prettypr:format(erl_syntax:form_list (Forms1))).
%% @hidden
parse_file(FileName) ->
{ok, InFile} = file:open(FileName, [read]),
{ok, InFile} = protobuffs_file:open(FileName, [read]),
String = parse_file(InFile,[]),
file:close(InFile),
{ok,String}.
%% @hidden
parse_file(InFile,Acc) ->
case io:request(InFile,{get_until,prompt,protobuffs_scanner,token,[1]}) of
case protobuffs_file:request(InFile) of
{ok,Token,_EndLine} ->
parse_file(InFile,Acc ++ [Token]);
{error,token} ->
@@ -493,21 +503,21 @@ resolve_types ([], _, _, Acc) ->
%% @hidden
write_header_include_file(Basename, Messages) ->
{ok, FileRef} = file:open(Basename, [write]),
{ok, FileRef} = protobuffs_file:open(Basename, [write]),
[begin
OutFields = [{string:to_lower(A), Optional, Default} || {_, Optional, _, A, Default} <- lists:keysort(1, Fields)],
if
OutFields /= [] ->
io:format(FileRef, "-record(~s, {~n ", [string:to_lower(Name)]),
protobuffs_file:format(FileRef, "-record(~s, {~n ", [string:to_lower(Name)]),
WriteFields = generate_field_definitions(OutFields),
FormatString = string:join(["~s" || _ <- lists:seq(1, length(WriteFields))], ",~n "),
io:format(FileRef, FormatString, WriteFields),
io:format(FileRef, "~n}).~n~n", []);
protobuffs_file:format(FileRef, FormatString, WriteFields),
protobuffs_file:format(FileRef, "~n}).~n~n", []);
true ->
ok
end
end || {Name, Fields} <- Messages],
file:close(FileRef).
protobuffs_file:close(FileRef).
%% @hidden
generate_field_definitions(Fields) ->
View
@@ -0,0 +1,31 @@
%%% @author David AAberg <davabe@hotmail.com>
%%% @copyright (C) 2011, David AAberg
%%% @doc
%%%
%%% @end
%%% Created : 25 Sep 2011 by David AAberg <davabe@hotmail.com>
-module(protobuffs_file).
-export([open/2,path_open/3,close/1,format/3,request/1,compile_forms/2,write_file/2]).
open(File, Options) ->
file:open(File,Options).
path_open(Path, File, Modes) ->
file:path_open(Path, File, Modes).
close(FileRef) ->
file:close(FileRef).
format(FileRef, FormatString, WriteFields) ->
io:format(FileRef, FormatString, WriteFields).
request(InFile) ->
io:request(InFile,{get_until,prompt,protobuffs_scanner,token,[1]}).
compile_forms(Forms, Options) ->
compile:forms(Forms, [return] ++ Options).
write_file(File, Bytes) ->
file:write_file(File,Bytes).
@@ -138,21 +138,33 @@ test_proto_files(Config) ->
TestProtoFile = fun(ProtoFile,Acc) ->
Path = filename:absname(ProtoFile),
Message = filename:basename(ProtoFile,".proto"),
test_server:format("Testcase ~p, parse file ~p~n",
test_server:format("~n===Testcase ~p, testing message ~p===~n",
[self(), Message]),
test_server:format("Testcase ~p, parse file ~p",
[self(), Path]),
protobuffs_compile:scan_file(
Path,
[{imports_dir, [filename:join([DataDir,"proto"]),
filename:join([DataDir,"proto","import"])]}]),
test_server:format("Testcase ~p, testing message ~p~n",
[self(), Message]),
Test = list_to_atom("proper_protobuffs_"++Message),
Acc andalso proper:quickcheck(
proper:numtests(
NumTests,
protobuffs_proper:Test()),[quiet])
Result = proper:quickcheck(
proper:numtests(
NumTests,
protobuffs_proper:Test()),[long_result,
{on_output, fun(".",_) -> ok;
(S,F) -> test_server:format(S,F)
end}]),
case Result of
true -> test_server:format("Test ~p: ok~n~n~n",[Message]),
Acc andalso true;
F -> test_server:format("Test ~p: Failed with ~p~n~n~n",[Message,Result]),
false
end
end,
lists:foldl(TestProtoFile,true,ProtoFiles).
case lists:foldl(TestProtoFile,true,ProtoFiles) of
true -> ok;
_ -> ct:fail("One or more property test cases failed")
end.
%%---------------------------------------------------------------------
%% Help flies
@@ -0,0 +1,59 @@
%%% @author David Åberg <davabe@hotmail.com>
%%% @copyright (C) 2011, David AAberg
%%% @doc
%%%
%%% @end
%%% Created : 25 Sep 2011 by David AAberg <davabe@hotmail.com>
-module(protobuffs_compile_tests).
-compile(export_all).
-include_lib("proper/include/proper.hrl").
-include_lib("eunit/include/eunit.hrl").
setup() ->
Modules = [protobuffs_file],
meck:new(Modules),
meck:expect(protobuffs_file,open,fun(_,_) -> {ok,in_file} end),
meck:expect(protobuffs_file,close,fun(_) -> ok end),
meck:expect(protobuffs_file,request,fun(_) -> {eof,dummy} end),
meck:expect(protobuffs_file,compile_forms,fun(_,_) -> {ok, dummy, <<"Bytest">>, dummy} end),
meck:expect(protobuffs_file,write_file,fun(_,_) -> ok end),
meck:expect(protobuffs_file,format,fun(_,_,_) -> ok end),
meck:expect(protobuffs_file,path_open, fun(Path,FileName,_) -> {ok, io_device, filename:join([Path,FileName])} end),
Modules.
cleanup(Modules) ->
meck:unload(Modules).
scan_file_test_() ->
{foreach,
fun setup/0,
fun cleanup/1,
[?_assertMatch(ok,protobuffs_compile:scan_file(dummy_file)),
?_assertMatch(ok,protobuffs_compile:scan_file("dummy_file.proto"))]}.
scan_string_test_() ->
{setup,
fun setup/0,
fun cleanup/1,
[?_assertMatch(ok,protobuffs_compile:scan_string("","dummy"))]}.
generate_source_test_() ->
{foreach,
fun setup/0,
fun cleanup/1,
[?_assertMatch(ok,protobuffs_compile:generate_source(dummy_file)),
?_assertMatch(ok,protobuffs_compile:generate_source("dummy_file.proto"))]}.
parse_imports_test_() ->
{foreach,
fun setup/0,
fun cleanup/1,
[?_assertMatch([],protobuffs_compile:parse_imports([],dummy_path)),
?_assertMatch([{import , dummy_import_file}],
protobuffs_compile:parse_imports(
[{import,dummy_import_file}],"dummy_path")),
?_assertMatch([what_ever],protobuffs_compile:parse_imports([what_ever],dummy_path))]}.
Oops, something went wrong.

0 comments on commit 8091c1a

Please sign in to comment.