Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

move tests to a separate file

  • Loading branch information...
commit 55d26a3547bfc5aa6cfb17d779c0f2db8bda5348 1 parent 3734fd1
Ali Yakamercan authored
Showing with 107 additions and 120 deletions.
  1. +0 −120 src/json_rec.erl
  2. +107 −0 test/json_rec_tests.erl
View
120 src/json_rec.erl
@@ -29,30 +29,6 @@
%%%-------------------------------------------------------------------
-module(json_rec).
--ifdef(TEST).
--include_lib("eunit/include/eunit.hrl").
-
--record(simple, {
- one,
- two
- }).
-
--record(simplet2l, {
- two
- }).
-
--record(deep, {
- simple,
- second = []
- }).
-
-
--compile({parse_transform, exprecs}).
--export_records([simple,simplet2l,deep]).
-
--endif.
-
-
-export([
to_rec/3,
to_json/2
@@ -231,99 +207,3 @@ module_rec_fields(Ms, Rec ) ->
module_get(Ms, Field, Rec) ->
M = module_has_rec(Ms, Rec),
M:'#get-'(Field,Rec).
-
-
-
--ifdef(TEST).
-
-new(<<"simple">>) ->
- '#new-simple'();
-new(<<"simplet2l">>) ->
- '#new-simplet2l'();
-new(<<"deep">>) ->
- '#new-deep'();
-new(_RecName) -> undefined.
-
-rec(#simple{}) -> true;
-rec(#simplet2l{}) -> true;
-rec(#deep{}) -> true;
-rec(_Rec) -> false.
-
-
-simple_json_data() ->
- ["{\"one\":1,\"two\":2}",
- #simple{ one = 1, two = 2}].
-
-simple_json_t2l_data() ->
- ["{\"two\":[1,2,3]}",
- #simplet2l{ two = {1,2,3}}].
-unknown_json_data() ->
- ["{\"one\":1,\"two\":2}",
- [{<<"two">>, 2},{<<"one">>,1}]].
-
-deep_json_data() ->
- Simple = "{\"simple\":{\"one\":1,\"two\":2}",
- Deep = Simple++"}",
- [Deep,
- #deep{ simple = #simple{ one = 1, two = 2}
- }].
-
-deep_deep_json_data() ->
- Simple = "\"simple\":{\"one\":1,\"two\":2}",
- Deep = "{"++Simple++",\"second\":[{"++Simple ++ "},{" ++ Simple ++ "},{" ++ Simple ++"}]}",
- [Deep,
- #deep{ simple = #simple{ one = 1, two = 2},
- second = [ #simple{ one = 1, two = 2},
- #simple{ one = 1, two = 2},
- #simple{ one = 1, two = 2}
- ]
- }
- ].
-
-
-simple_test() ->
- [Json, Rec] = simple_json_data(),
- NewRec = ?MODULE:to_rec(mochijson2:decode(Json),?MODULE,?MODULE:new(<<"simple">>)),
- ?assertEqual(Rec, NewRec).
-
-deep_test() ->
- [Json, Rec] = deep_json_data(),
- NewRec = ?MODULE:to_rec(mochijson2:decode(Json),?MODULE,?MODULE:new(<<"deep">>)),
- ?assertEqual(Rec, NewRec).
-
-deep_deep_test() ->
- [Json, Rec] = deep_deep_json_data(),
- New = ?MODULE:to_rec(mochijson2:decode(Json),?MODULE,?MODULE:new(<<"deep">>)),
- ?assertEqual(Rec, New).
-
-unknown_test() ->
- [Json, Rec] = unknown_json_data(),
- New = ?MODULE:to_rec(mochijson2:decode(Json),?MODULE,?MODULE:new(<<"unknown">>)),
- ?assertEqual(Rec, New).
-
-to_json_simple_test() ->
- [_Json, Rec] = simple_json_data(),
-
- Conv = ?MODULE:to_json(Rec, ?MODULE),
- Sjson= lists:flatten(mochijson2:encode(Conv)),
-
- New = ?MODULE:to_rec(mochijson2:decode(Sjson),?MODULE,?MODULE:new(<<"simple">>)),
- ?assertEqual(Rec,New).
-
-to_json_deep_test() ->
- [_Json, Rec] = deep_json_data(),
- Conv = ?MODULE:to_json(Rec,?MODULE),
- Sjson = lists:flatten(mochijson2:encode(Conv)),
-
-
- New = ?MODULE:to_rec(mochijson2:decode(Sjson), ?MODULE, ?MODULE:new(<<"deep">>)),
- ?assertEqual(Rec,New).
-
-to_json_deep_deep_test() ->
- [_Json, Rec] = deep_deep_json_data(),
- Conv = ?MODULE:to_json(Rec,?MODULE),
- Sjson = lists:flatten(mochijson2:encode(Conv)),
- New = ?MODULE:to_rec(mochijson2:decode(Sjson), ?MODULE, ?MODULE:new(<<"deep">>)),
- ?assertEqual(Rec,New).
-
--endif.
View
107 test/json_rec_tests.erl
@@ -0,0 +1,107 @@
+-module(json_rec_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+-record(simple, {
+ one,
+ two
+ }).
+
+-record(simplet2l, {
+ two
+ }).
+
+-record(deep, {
+ simple,
+ second = []
+ }).
+
+
+-compile({parse_transform, exprecs}).
+-export_records([simple,simplet2l,deep]).
+-export([new/1]).
+
+new(<<"simple">>) ->
+ '#new-simple'();
+new(<<"simplet2l">>) ->
+ '#new-simplet2l'();
+new(<<"deep">>) ->
+ '#new-deep'();
+new(_RecName) -> undefined.
+
+simple_json_data() ->
+ ["{\"one\":1,\"two\":2}",
+ #simple{ one = 1, two = 2}].
+
+simple_json_t2l_data() ->
+ ["{\"two\":[1,2,3]}",
+ #simplet2l{ two = {1,2,3}}].
+unknown_json_data() ->
+ ["{\"one\":1,\"two\":2}",
+ [{<<"two">>, 2},{<<"one">>,1}]].
+
+deep_json_data() ->
+ Simple = "{\"simple\":{\"one\":1,\"two\":2}",
+ Deep = Simple++"}",
+ [Deep,
+ #deep{ simple = #simple{ one = 1, two = 2}
+ }].
+
+deep_deep_json_data() ->
+ Simple = "\"simple\":{\"one\":1,\"two\":2}",
+ Deep = "{"++Simple++",\"second\":[{"++Simple ++ "},{" ++ Simple ++ "},{" ++ Simple ++"}]}",
+ [Deep,
+ #deep{ simple = #simple{ one = 1, two = 2},
+ second = [ #simple{ one = 1, two = 2},
+ #simple{ one = 1, two = 2},
+ #simple{ one = 1, two = 2}
+ ]
+ }
+ ].
+
+
+simple_test() ->
+ [Json, Rec] = simple_json_data(),
+ NewRec = json_rec:to_rec(mochijson2:decode(Json),json_rec_tests,new(<<"simple">>)),
+ ?assertEqual(Rec, NewRec).
+
+deep_test() ->
+ [Json, Rec] = deep_json_data(),
+ NewRec = json_rec:to_rec(mochijson2:decode(Json),json_rec_tests,new(<<"deep">>)),
+ ?assertEqual(Rec, NewRec).
+
+deep_deep_test() ->
+ [Json, Rec] = deep_deep_json_data(),
+ New = json_rec:to_rec(mochijson2:decode(Json),json_rec_tests,new(<<"deep">>)),
+ ?assertEqual(Rec, New).
+
+unknown_test() ->
+ [Json, Rec] = unknown_json_data(),
+ New = json_rec:to_rec(mochijson2:decode(Json),json_rec_tests,new(<<"unknown">>)),
+ ?assertEqual(Rec, New).
+
+to_json_simple_test() ->
+ [_Json, Rec] = simple_json_data(),
+
+ Conv = json_rec:to_json(Rec, json_rec_tests),
+ Sjson= lists:flatten(mochijson2:encode(Conv)),
+
+ New = json_rec:to_rec(mochijson2:decode(Sjson),json_rec_tests,new(<<"simple">>)),
+ ?assertEqual(Rec,New).
+
+to_json_deep_test() ->
+ [_Json, Rec] = deep_json_data(),
+ Conv = json_rec:to_json(Rec,json_rec_tests),
+ Sjson = lists:flatten(mochijson2:encode(Conv)),
+
+
+ New = json_rec:to_rec(mochijson2:decode(Sjson), json_rec_tests, new(<<"deep">>)),
+ ?assertEqual(Rec,New).
+
+to_json_deep_deep_test() ->
+ [_Json, Rec] = deep_deep_json_data(),
+ Conv = json_rec:to_json(Rec,json_rec_tests),
+ Sjson = lists:flatten(mochijson2:encode(Conv)),
+ New = json_rec:to_rec(mochijson2:decode(Sjson), json_rec_tests, new(<<"deep">>)),
+ ?assertEqual(Rec,New).
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.