Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix the JSON extractor, add unit test

  • Loading branch information...
commit 1a2bc1a84722f12a1b8e636b49583bc3f454834f 1 parent 36b3a1b
@rzezeski authored
View
2  Makefile
@@ -1,6 +1,6 @@
REBAR = $(shell pwd)/rebar
-.PHONY: deps rel stagedevrel
+.PHONY: deps rel stagedevrel test
all: deps compile
View
1  rebar.config
@@ -1,5 +1,6 @@
{cover_enabled, true}.
{erl_opts, [debug_info, {parse_transform, lager_transform}]}.
+{eunit_opts, [verbose]}.
{deps,
[
View
3  src/yz_json_extractor.erl
@@ -93,8 +93,7 @@ extract_field(CurrentName) ->
-spec extract_element(binary()) -> fun((binary(), state()) -> state()).
extract_element(CurrentName) ->
fun(Element, S) ->
- Fields = S#state.fields,
- S#state{fields=[{CurrentName, Element}|Fields]}
+ extract_fields(CurrentName, Element, S)
end.
-spec new_field_name(binary() | undefined, binary(), binary()) -> binary().
View
19 test/test.json
@@ -0,0 +1,19 @@
+{"name":"ryan",
+ "age":29,
+ "pets":["smokey", "bandit"],
+ "books":[
+ {"title":"Introduction to Information Retrieval",
+ "authors":["Christopher D. Manning",
+ "Prabhakar Raghavan",
+ "Hinrich Schütze"]},
+ {"title":"Principles of Distributed Database Systems",
+ "authors":["M. Tamer Özsu", "Patrick Valduriez"]}
+ ],
+ "type":null,
+ "alive":true,
+ "married":false,
+ "a_number":1.1e6,
+ "lucky_numbers":[13,17,21],
+ "misc":{},
+ "kids":[]
+}
View
31 test/yz_json_extractor_tests.erl
@@ -0,0 +1,31 @@
+-module(yz_json_extractor_tests).
+-compile(export_all).
+
+-include_lib("eunit/include/eunit.hrl").
+
+json_extract_test() ->
+ {ok, TestJSON} = file:read_file("../test/test.json"),
+ Result = yz_json_extractor:extract(TestJSON),
+ Expect =
+ [{<<"name">>,<<"ryan">>},
+ {<<"age">>,29},
+ {<<"pets">>,<<"smokey">>},
+ {<<"pets">>,<<"bandit">>},
+ {<<"books_title">>,<<"Introduction to Information Retrieval">>},
+ {<<"books_title">>,<<"Principles of Distributed Database Systems">>},
+ {<<"books_authors">>,<<"Christopher D. Manning">>},
+ {<<"books_authors">>,<<"Prabhakar Raghavan">>},
+ {<<"books_authors">>,<<"Hinrich Schütze">>},
+ {<<"books_authors">>,<<"M. Tamer Özsu">>},
+ {<<"books_authors">>,<<"Patrick Valduriez">>},
+ {<<"alive">>,true},
+ {<<"married">>,false},
+ {<<"a_number">>,1.1e6},
+ {<<"lucky_numbers">>,13},
+ {<<"lucky_numbers">>,17},
+ {<<"lucky_numbers">>,21}],
+
+ %% Do one at a time so failure is easier to understand
+ ?assertEqual(length(Expect), length(Result)),
+ Pairs = lists:zip(lists:sort(Expect), lists:sort(Result)),
+ [?assertEqual(E,R) || {E,R} <- Pairs].
Please sign in to comment.
Something went wrong with that request. Please try again.