Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed bug in scan string

  • Loading branch information...
commit 7eee83b6c3d23a84d0b4eb63e6d7c0d5d15b4915 1 parent 5287792
David Åberg authored
Showing with 41 additions and 11 deletions.
  1. +13 −9 src/protobuffs_compile.erl
  2. +28 −2 test/protobuffs_compile_tests.erl
22 src/protobuffs_compile.erl
View
@@ -57,17 +57,18 @@ scan_string(String,BaseName) ->
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);
+ {ok,TokenizedString} = parse_file(ProtoFile),
+ generate_output(TokenizedString,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).
+ {ok,TokenizedString} = parse_file(atom_to_list(ProtoFile) ++ ".proto"),
+ generate_output(TokenizedString,Basename,Options).
-spec scan_string(String :: string(), Basename :: string(), Options :: list()) ->
ok | {error, _}.
scan_string(String,Basename,Options) ->
- generate_output(Options, Basename, String, fun output/4).
+ {ok,TokenizedString,_} = protobuffs_scanner:string(String),
+ generate_output(TokenizedString, Basename, Options).
%%--------------------------------------------------------------------
%% @doc Generats a source .erl file and header file .hrl
@@ -91,8 +92,8 @@ generate_source(ProtoFile,Options) when is_list (ProtoFile) ->
generate_output(Options, Basename, String, fun output_source/4);
generate_source(ProtoFile,Options) when is_atom (ProtoFile) ->
Basename = atom_to_list(ProtoFile) ++ "_pb",
- {ok,String} = parse_file(ProtoFile),
- generate_output(Options, Basename, String, fun output_source/4).
+ {ok,TokenizedString} = parse_file(ProtoFile),
+ generate_output(Options, Basename, TokenizedString, fun output_source/4).
%% @hidden
output(Basename, Messages, Enums, Options) ->
@@ -139,8 +140,11 @@ parse_file(InFile,Acc) ->
Acc
end.
-generate_output(Options, Basename, String, OutputFunction) ->
- {ok, FirstParsed} = parse_string(String),
+generate_output(TokenizedString,Basename,Options) ->
+ generate_output(Options, Basename, TokenizedString, fun output/4).
+
+generate_output(Options, Basename, TokenizedString, OutputFunction) ->
+ {ok, FirstParsed} = parse_string(TokenizedString),
ImportPaths = ["./", "src/"
| proplists:get_value(imports_dir, Options, [])],
Parsed = parse_imports(FirstParsed,
30 test/protobuffs_compile_tests.erl
View
@@ -29,16 +29,42 @@
filter_forms_test_() ->
[].
+scan_string_test_() ->
+ {foreach, fun setup/0, fun teardown/1,
+ [fun test_simple_string/0]}.
+
%%%%%%%%%%%%%%%%%%%%%%%
%%% SETUP FUNCTIONS %%%
%%%%%%%%%%%%%%%%%%%%%%%
setup() ->
- meck:new(protobuff_io).
+ Mods = [protobuffs_io],
+ meck:new(Mods),
+ meck:expect(protobuffs_io, write_file, fun(_File, _Bytes) -> ok end),
+ meck:expect(protobuffs_io, open, fun(_File, _Options) -> {ok, file_ref} end),
+ meck:expect(protobuffs_io, close, fun(file_ref) -> ok end),
+ meck:expect(protobuffs_io, format, fun(file_ref, _FormatString, _WriteFields) -> ok end),
+ Mods.
+
+teardown(Mods) ->
+ meck:unload(Mods).
%%%%%%%%%%%%%%%%%%%%
%%% ACTUAL TESTS %%%
%%%%%%%%%%%%%%%%%%%%
-
+test_simple_string() ->
+ Message =
+"message Person
+{
+ required string name = 1;
+ required string address = 2;
+ required string phone_number = 3;
+ required int32 age = 4;
+}",
+ Result = protobuffs_compile:scan_string(Message,"simple"),
+ [?assertEqual(ok,Result),
+ ?assert(meck:called(protobuffs_io,open,["simple.hrl",'_'])),
+ ?assert(meck:called(protobuffs_io,write_file,["simple.beam",'_'])),
+ ?assert(meck:validate(protobuffs_io))].
%%%%%%%%%%%%%%%%%%%%%%%%
%%% HELPER FUNCTIONS %%%
Please sign in to comment.
Something went wrong with that request. Please try again.