Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 54A1E185CC for ; Wed, 2 Dec 2015 23:02:03 +0000 (UTC) Received: (qmail 41896 invoked by uid 500); 2 Dec 2015 23:02:02 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 41419 invoked by uid 500); 2 Dec 2015 23:02:02 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 40341 invoked by uid 99); 2 Dec 2015 23:02:01 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Dec 2015 23:02:01 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A0C13E048C; Wed, 2 Dec 2015 23:02:01 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: kxepal@apache.org To: commits@couchdb.apache.org Date: Wed, 02 Dec 2015 23:02:22 -0000 Message-Id: In-Reply-To: <420563d655c74cdd8457841366bdc54d@git.apache.org> References: <420563d655c74cdd8457841366bdc54d@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [22/50] couchdb commit: updated refs/heads/1.x.x to 921006f Port 190-json-stream-parse.t etap test suite to eunit Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/b91efbb1 Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/b91efbb1 Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/b91efbb1 Branch: refs/heads/1.x.x Commit: b91efbb1c81d343d7b3789cba60ce0a6217590ac Parents: 3e54543 Author: Alexander Shorin Authored: Wed Jun 4 13:22:13 2014 +0400 Committer: Alexander Shorin Committed: Thu Dec 3 00:51:57 2015 +0300 ---------------------------------------------------------------------- test/couchdb/Makefile.am | 1 + test/couchdb/json_stream_parse_tests.erl | 151 +++++++++++++++++++++ test/etap/190-json-stream-parse.t | 184 -------------------------- test/etap/Makefile.am | 1 - 4 files changed, 152 insertions(+), 185 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/b91efbb1/test/couchdb/Makefile.am ---------------------------------------------------------------------- diff --git a/test/couchdb/Makefile.am b/test/couchdb/Makefile.am index d664a58..2987411 100644 --- a/test/couchdb/Makefile.am +++ b/test/couchdb/Makefile.am @@ -48,6 +48,7 @@ eunit_files = \ couchdb_update_conflicts_tests.erl \ couchdb_vhosts_tests.erl \ couchdb_views_tests.erl \ + json_stream_parse_tests.erl \ test_request.erl \ test_web.erl \ include/couch_eunit.hrl http://git-wip-us.apache.org/repos/asf/couchdb/blob/b91efbb1/test/couchdb/json_stream_parse_tests.erl ---------------------------------------------------------------------- diff --git a/test/couchdb/json_stream_parse_tests.erl b/test/couchdb/json_stream_parse_tests.erl new file mode 100644 index 0000000..92303b6 --- /dev/null +++ b/test/couchdb/json_stream_parse_tests.erl @@ -0,0 +1,151 @@ +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. + +-module(json_stream_parse_tests). + +-include("couch_eunit.hrl"). + +-define(CASES, + [ + {1, "1", "integer numeric literial"}, + {3.1416, "3.14160", "float numeric literal"}, % text representation may truncate, trail zeroes + {-1, "-1", "negative integer numeric literal"}, + {-3.1416, "-3.14160", "negative float numeric literal"}, + {12.0e10, "1.20000e+11", "float literal in scientific notation"}, + {1.234E+10, "1.23400e+10", "another float literal in scientific notation"}, + {-1.234E-10, "-1.23400e-10", "negative float literal in scientific notation"}, + {10.0, "1.0e+01", "yet another float literal in scientific notation"}, + {123.456, "1.23456E+2", "yet another float literal in scientific notation"}, + {10.0, "1e1", "yet another float literal in scientific notation"}, + {<<"foo">>, "\"foo\"", "string literal"}, + {<<"foo", 5, "bar">>, "\"foo\\u0005bar\"", "string literal with \\u0005"}, + {<<"">>, "\"\"", "empty string literal"}, + {<<"\n\n\n">>, "\"\\n\\n\\n\"", "only new lines literal"}, + {<<"\" \b\f\r\n\t\"">>, "\"\\\" \\b\\f\\r\\n\\t\\\"\"", + "only white spaces string literal"}, + {null, "null", "null literal"}, + {true, "true", "true literal"}, + {false, "false", "false literal"}, + {<<"null">>, "\"null\"", "null string literal"}, + {<<"true">>, "\"true\"", "true string literal"}, + {<<"false">>, "\"false\"", "false string literal"}, + {{[]}, "{}", "empty object literal"}, + {{[{<<"foo">>, <<"bar">>}]}, "{\"foo\":\"bar\"}", + "simple object literal"}, + {{[{<<"foo">>, <<"bar">>}, {<<"baz">>, 123}]}, + "{\"foo\":\"bar\",\"baz\":123}", "another simple object literal"}, + {[], "[]", "empty array literal"}, + {[[]], "[[]]", "empty array literal inside a single element array literal"}, + {[1, <<"foo">>], "[1,\"foo\"]", "simple non-empty array literal"}, + {[1199344435545.0, 1], "[1199344435545.0,1]", + "another simple non-empty array literal"}, + {[false, true, 321, null], "[false, true, 321, null]", "array of literals"}, + {{[{<<"foo">>, [123]}]}, "{\"foo\":[123]}", + "object literal with an array valued property"}, + {{[{<<"foo">>, {[{<<"bar">>, true}]}}]}, + "{\"foo\":{\"bar\":true}}", "nested object literal"}, + {{[{<<"foo">>, []}, {<<"bar">>, {[{<<"baz">>, true}]}}, + {<<"alice">>, <<"bob">>}]}, + "{\"foo\":[],\"bar\":{\"baz\":true},\"alice\":\"bob\"}", + "complex object literal"}, + {[-123, <<"foo">>, {[{<<"bar">>, []}]}, null], + "[-123,\"foo\",{\"bar\":[]},null]", + "complex array literal"} + ] +). + + +raw_json_input_test_() -> + Tests = lists:map( + fun({EJson, JsonString, Desc}) -> + {Desc, + ?_assert(equiv(EJson, json_stream_parse:to_ejson(JsonString)))} + end, ?CASES), + {"Tests with raw JSON string as the input", Tests}. + +one_byte_data_fun_test_() -> + Tests = lists:map( + fun({EJson, JsonString, Desc}) -> + DataFun = fun() -> single_byte_data_fun(JsonString) end, + {Desc, + ?_assert(equiv(EJson, json_stream_parse:to_ejson(DataFun)))} + end, ?CASES), + {"Tests with a 1 byte output data function as the input", Tests}. + +test_multiple_bytes_data_fun_test_() -> + Tests = lists:map( + fun({EJson, JsonString, Desc}) -> + DataFun = fun() -> multiple_bytes_data_fun(JsonString) end, + {Desc, + ?_assert(equiv(EJson, json_stream_parse:to_ejson(DataFun)))} + end, ?CASES), + {"Tests with a multiple bytes output data function as the input", Tests}. + + +%% Test for equivalence of Erlang terms. +%% Due to arbitrary order of construction, equivalent objects might +%% compare unequal as erlang terms, so we need to carefully recurse +%% through aggregates (tuples and objects). +equiv({Props1}, {Props2}) -> + equiv_object(Props1, Props2); +equiv(L1, L2) when is_list(L1), is_list(L2) -> + equiv_list(L1, L2); +equiv(N1, N2) when is_number(N1), is_number(N2) -> + N1 == N2; +equiv(B1, B2) when is_binary(B1), is_binary(B2) -> + B1 == B2; +equiv(true, true) -> + true; +equiv(false, false) -> + true; +equiv(null, null) -> + true. + +%% Object representation and traversal order is unknown. +%% Use the sledgehammer and sort property lists. +equiv_object(Props1, Props2) -> + L1 = lists:keysort(1, Props1), + L2 = lists:keysort(1, Props2), + Pairs = lists:zip(L1, L2), + true = lists:all( + fun({{K1, V1}, {K2, V2}}) -> + equiv(K1, K2) andalso equiv(V1, V2) + end, + Pairs). + +%% Recursively compare tuple elements for equivalence. +equiv_list([], []) -> + true; +equiv_list([V1 | L1], [V2 | L2]) -> + equiv(V1, V2) andalso equiv_list(L1, L2). + +single_byte_data_fun([]) -> + done; +single_byte_data_fun([H | T]) -> + {<>, fun() -> single_byte_data_fun(T) end}. + +multiple_bytes_data_fun([]) -> + done; +multiple_bytes_data_fun(L) -> + N = crypto:rand_uniform(0, 7), + {Part, Rest} = split(L, N), + {list_to_binary(Part), fun() -> multiple_bytes_data_fun(Rest) end}. + +split(L, N) when length(L) =< N -> + {L, []}; +split(L, N) -> + take(N, L, []). + +take(0, L, Acc) -> + {lists:reverse(Acc), L}; +take(N, [H|L], Acc) -> + take(N - 1, L, [H | Acc]). http://git-wip-us.apache.org/repos/asf/couchdb/blob/b91efbb1/test/etap/190-json-stream-parse.t ---------------------------------------------------------------------- diff --git a/test/etap/190-json-stream-parse.t b/test/etap/190-json-stream-parse.t deleted file mode 100755 index 49ea58f..0000000 --- a/test/etap/190-json-stream-parse.t +++ /dev/null @@ -1,184 +0,0 @@ -#!/usr/bin/env escript -% Licensed under the Apache License, Version 2.0 (the "License"); you may not -% use this file except in compliance with the License. You may obtain a copy of -% the License at -% -% http://www.apache.org/licenses/LICENSE-2.0 -% -% Unless required by applicable law or agreed to in writing, software -% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -% License for the specific language governing permissions and limitations under -% the License. - -main(_) -> - test_util:init_code_path(), - etap:plan(99), - case (catch test()) of - ok -> - etap:end_tests(); - Other -> - etap:diag("Test died abnormally: ~p", [Other]), - etap:bail("Bad return value.") - end, - ok. - -test() -> - crypto:start(), - ok = test_raw_json_input(), - ok = test_1_byte_data_function(), - ok = test_multiple_bytes_data_function(). - - -test_raw_json_input() -> - etap:diag("Tests with raw JSON string as the input."), - lists:foreach( - fun({EJson, JsonString, Desc}) -> - etap:is( - equiv(EJson, json_stream_parse:to_ejson(JsonString)), - true, - Desc) - end, - cases()), - ok. - - -test_1_byte_data_function() -> - etap:diag("Tests with a 1 byte output data function as the input."), - lists:foreach( - fun({EJson, JsonString, Desc}) -> - DataFun = fun() -> single_byte_data_fun(JsonString) end, - etap:is( - equiv(EJson, json_stream_parse:to_ejson(DataFun)), - true, - Desc) - end, - cases()), - ok. - - -test_multiple_bytes_data_function() -> - etap:diag("Tests with a multiple bytes output data function as the input."), - lists:foreach( - fun({EJson, JsonString, Desc}) -> - DataFun = fun() -> multiple_bytes_data_fun(JsonString) end, - etap:is( - equiv(EJson, json_stream_parse:to_ejson(DataFun)), - true, - Desc) - end, - cases()), - ok. - - -cases() -> - [ - {1, "1", "integer numeric literial"}, - {3.1416, "3.14160", "float numeric literal"}, % text representation may truncate, trail zeroes - {-1, "-1", "negative integer numeric literal"}, - {-3.1416, "-3.14160", "negative float numeric literal"}, - {12.0e10, "1.20000e+11", "float literal in scientific notation"}, - {1.234E+10, "1.23400e+10", "another float literal in scientific notation"}, - {-1.234E-10, "-1.23400e-10", "negative float literal in scientific notation"}, - {10.0, "1.0e+01", "yet another float literal in scientific notation"}, - {123.456, "1.23456E+2", "yet another float literal in scientific notation"}, - {10.0, "1e1", "yet another float literal in scientific notation"}, - {<<"foo">>, "\"foo\"", "string literal"}, - {<<"foo", 5, "bar">>, "\"foo\\u0005bar\"", "string literal with \\u0005"}, - {<<"">>, "\"\"", "empty string literal"}, - {<<"\n\n\n">>, "\"\\n\\n\\n\"", "only new lines literal"}, - {<<"\" \b\f\r\n\t\"">>, "\"\\\" \\b\\f\\r\\n\\t\\\"\"", - "only white spaces string literal"}, - {null, "null", "null literal"}, - {true, "true", "true literal"}, - {false, "false", "false literal"}, - {<<"null">>, "\"null\"", "null string literal"}, - {<<"true">>, "\"true\"", "true string literal"}, - {<<"false">>, "\"false\"", "false string literal"}, - {{[]}, "{}", "empty object literal"}, - {{[{<<"foo">>, <<"bar">>}]}, "{\"foo\":\"bar\"}", - "simple object literal"}, - {{[{<<"foo">>, <<"bar">>}, {<<"baz">>, 123}]}, - "{\"foo\":\"bar\",\"baz\":123}", "another simple object literal"}, - {[], "[]", "empty array literal"}, - {[[]], "[[]]", "empty array literal inside a single element array literal"}, - {[1, <<"foo">>], "[1,\"foo\"]", "simple non-empty array literal"}, - {[1199344435545.0, 1], "[1199344435545.0,1]", - "another simple non-empty array literal"}, - {[false, true, 321, null], "[false, true, 321, null]", "array of literals"}, - {{[{<<"foo">>, [123]}]}, "{\"foo\":[123]}", - "object literal with an array valued property"}, - {{[{<<"foo">>, {[{<<"bar">>, true}]}}]}, - "{\"foo\":{\"bar\":true}}", "nested object literal"}, - {{[{<<"foo">>, []}, {<<"bar">>, {[{<<"baz">>, true}]}}, - {<<"alice">>, <<"bob">>}]}, - "{\"foo\":[],\"bar\":{\"baz\":true},\"alice\":\"bob\"}", - "complex object literal"}, - {[-123, <<"foo">>, {[{<<"bar">>, []}]}, null], - "[-123,\"foo\",{\"bar\":[]},null]", - "complex array literal"} - ]. - - -%% Test for equivalence of Erlang terms. -%% Due to arbitrary order of construction, equivalent objects might -%% compare unequal as erlang terms, so we need to carefully recurse -%% through aggregates (tuples and objects). -equiv({Props1}, {Props2}) -> - equiv_object(Props1, Props2); -equiv(L1, L2) when is_list(L1), is_list(L2) -> - equiv_list(L1, L2); -equiv(N1, N2) when is_number(N1), is_number(N2) -> - N1 == N2; -equiv(B1, B2) when is_binary(B1), is_binary(B2) -> - B1 == B2; -equiv(true, true) -> - true; -equiv(false, false) -> - true; -equiv(null, null) -> - true. - - -%% Object representation and traversal order is unknown. -%% Use the sledgehammer and sort property lists. -equiv_object(Props1, Props2) -> - L1 = lists:keysort(1, Props1), - L2 = lists:keysort(1, Props2), - Pairs = lists:zip(L1, L2), - true = lists:all( - fun({{K1, V1}, {K2, V2}}) -> - equiv(K1, K2) andalso equiv(V1, V2) - end, - Pairs). - - -%% Recursively compare tuple elements for equivalence. -equiv_list([], []) -> - true; -equiv_list([V1 | L1], [V2 | L2]) -> - equiv(V1, V2) andalso equiv_list(L1, L2). - - -single_byte_data_fun([]) -> - done; -single_byte_data_fun([H | T]) -> - {<>, fun() -> single_byte_data_fun(T) end}. - - -multiple_bytes_data_fun([]) -> - done; -multiple_bytes_data_fun(L) -> - N = crypto:rand_uniform(0, 7), - {Part, Rest} = split(L, N), - {list_to_binary(Part), fun() -> multiple_bytes_data_fun(Rest) end}. - -split(L, N) when length(L) =< N -> - {L, []}; -split(L, N) -> - take(N, L, []). - -take(0, L, Acc) -> - {lists:reverse(Acc), L}; -take(N, [H|L], Acc) -> - take(N - 1, L, [H | Acc]). http://git-wip-us.apache.org/repos/asf/couchdb/blob/b91efbb1/test/etap/Makefile.am ---------------------------------------------------------------------- diff --git a/test/etap/Makefile.am b/test/etap/Makefile.am index b92faf1..72718e8 100644 --- a/test/etap/Makefile.am +++ b/test/etap/Makefile.am @@ -32,7 +32,6 @@ fixture_files = \ fixtures/test.couch tap_files = \ - 190-json-stream-parse.t \ 200-view-group-no-db-leaks.t \ 201-view-group-shutdown.t \ 210-os-proc-pool.t \