|
| 1 | +%% @copyright 2020 Hinagiku Soranoba All Rights Reserved. |
| 2 | + |
| 3 | +-module(bbmustache_escript_SUITE). |
| 4 | +-include_lib("common_test/include/ct.hrl"). |
| 5 | +-include_lib("eunit/include/eunit.hrl"). |
| 6 | + |
| 7 | +%%---------------------------------------------------------------------------------------------------------------------- |
| 8 | +%% 'common_test' Callback API |
| 9 | +%%---------------------------------------------------------------------------------------------------------------------- |
| 10 | + |
| 11 | +all() -> |
| 12 | + [ |
| 13 | + {group, long}, |
| 14 | + {group, short}, |
| 15 | + {group, with_args}, |
| 16 | + |
| 17 | + help, all_key_and_format_type, multiple_data_files, enoent |
| 18 | + ]. |
| 19 | + |
| 20 | +groups() -> |
| 21 | + [ |
| 22 | + {long, [version, help, key_type]}, |
| 23 | + {short, [version, help, key_type]}, |
| 24 | + {with_args, [version, help]} |
| 25 | + ]. |
| 26 | + |
| 27 | +init_per_suite(Config) -> |
| 28 | + Escript = case os:getenv("CMD_TOOL") of |
| 29 | + Path when is_list(Path) -> Path |
| 30 | + end, |
| 31 | + [{escript, Escript} | Config]. |
| 32 | + |
| 33 | +end_per_suite(_) -> |
| 34 | + ok. |
| 35 | + |
| 36 | +init_per_testcase(TestCase, Config) -> |
| 37 | + Args = case {group_name(Config), TestCase} of |
| 38 | + {long, version} -> ["--version"]; |
| 39 | + {short, version} -> ["-v"]; |
| 40 | + {with_args, version} -> ["-v", "file"]; |
| 41 | + |
| 42 | + {long, help} -> ["--help"]; |
| 43 | + {short, help} -> ["-h"]; |
| 44 | + {with_args, help} -> ["-h", "file"]; |
| 45 | + {undefined, help} -> []; |
| 46 | + |
| 47 | + {long, key_type} -> ["--key-type", "atom", |
| 48 | + "--data-file", data_file_name(atom, basic, Config), |
| 49 | + ?config(data_dir, Config) ++ "template.mustache"]; |
| 50 | + {short, key_type} -> ["-k", "atom", |
| 51 | + "-d", data_file_name(atom, basic, Config), |
| 52 | + ?config(data_dir, Config) ++ "template.mustache"]; |
| 53 | + _ -> [] |
| 54 | + end, |
| 55 | + [{args, Args} | Config]. |
| 56 | + |
| 57 | +end_per_testcase(_, _) -> |
| 58 | + ok. |
| 59 | + |
| 60 | +%%---------------------------------------------------------------------------------------------------------------------- |
| 61 | +%% Common Test Functions |
| 62 | +%%---------------------------------------------------------------------------------------------------------------------- |
| 63 | + |
| 64 | +version(Config) -> |
| 65 | + Got = run(Config), |
| 66 | + ?assertMatch({match, _}, re:run(Got, "bbmustache v[0-9]\.[0-9]\.[0-9].*")). |
| 67 | + |
| 68 | +help(Config) -> |
| 69 | + Got = run(Config), |
| 70 | + ?assertMatch({match, _}, re:run(Got, "Usage: .*")). |
| 71 | + |
| 72 | +key_type(Config) -> |
| 73 | + Got = run(Config), |
| 74 | + Expect = read_file("template.result", Config), |
| 75 | + ?assertEqual(Expect, Got). |
| 76 | + |
| 77 | +all_key_and_format_type(Config) -> |
| 78 | + KeyTypes = [atom, string, binary, undefined], |
| 79 | + FormatTypes = [basic, assoc, maps], |
| 80 | + Expect = read_file("template.result", Config), |
| 81 | + TempalteFile = ?config(data_dir, Config) ++ "template.mustache", |
| 82 | + ok = lists:foreach(fun({KeyType, FormatType}) -> |
| 83 | + ct:log("KeyType = ~p, FormatType = ~p", [KeyType, FormatType]), |
| 84 | + Got = run(Config, options(KeyType, FormatType, Config) ++ [TempalteFile]), |
| 85 | + ?assertEqual(Expect, Got) |
| 86 | + end, [{K, F} || K <- KeyTypes, F <- FormatTypes]). |
| 87 | + |
| 88 | +multiple_data_files(Config) -> |
| 89 | + KeyTypes = [atom, string, binary, undefined], |
| 90 | + FormatTypes = [basic, assoc, maps], |
| 91 | + Expect = read_file("template.overlay.result", Config), |
| 92 | + TempalteFile = ?config(data_dir, Config) ++ "template.mustache", |
| 93 | + ok = lists:foreach(fun({KeyType, FormatType}) -> |
| 94 | + ct:log("KeyType = ~p, FormatType = ~p", [KeyType, FormatType]), |
| 95 | + Got = run(Config, options(KeyType, FormatType, Config) |
| 96 | + ++ ["-d", data_file_name(KeyType, overlays, Config)] ++ [TempalteFile]), |
| 97 | + ?assertEqual(Expect, Got) |
| 98 | + end, [{K, F} || K <- KeyTypes, F <- FormatTypes]). |
| 99 | + |
| 100 | +enoent(Config) -> |
| 101 | + Got0 = run(Config, ["-d", "no_file", ?config(data_dir, Config) ++ "template.mustache"]), |
| 102 | + ?assertEqual(<<"ERROR: no_file is unable to read. (enoent)\n">>, Got0), |
| 103 | + |
| 104 | + Got1 = run(Config, ["no_file"]), |
| 105 | + ?assertEqual(<<"ERROR: no_file is unable to read.\n">>, Got1). |
| 106 | + |
| 107 | +%%---------------------------------------------------------------------------------------------------------------------- |
| 108 | +%% Internal Functions |
| 109 | +%%---------------------------------------------------------------------------------------------------------------------- |
| 110 | + |
| 111 | +-spec group_name([term()]) -> atom(). |
| 112 | +group_name(Config) -> |
| 113 | + proplists:get_value(name, ?config(tc_group_properties, Config)). |
| 114 | + |
| 115 | +-spec run([term()]) -> binary(). |
| 116 | +run(Config) -> |
| 117 | + run(Config, []). |
| 118 | + |
| 119 | +-spec run([term()], [string()]) -> binary(). |
| 120 | +run(Config, Args) -> |
| 121 | + Cmd = ?config(escript, Config) ++ " " ++ string:join(?config(args, Config) ++ Args, " "), |
| 122 | + ct:log("$ ~s", [Cmd]), |
| 123 | + Ret = os:cmd(Cmd), |
| 124 | + ct:log("~s", [Ret]), |
| 125 | + list_to_binary(Ret). |
| 126 | + |
| 127 | +-spec read_file(file:filename_all(), [term()]) -> binary(). |
| 128 | +read_file(FileName, Config) -> |
| 129 | + {ok, File} = file:read_file(filename:join([?config(data_dir, Config), FileName])), |
| 130 | + File. |
| 131 | + |
| 132 | +-spec data_file_name(atom(), atom(), [term()]) -> string(). |
| 133 | +data_file_name(undefined, FormatType, Config) -> |
| 134 | + data_file_name(string, FormatType, Config); |
| 135 | +data_file_name(KeyType, FormatType, Config) -> |
| 136 | + ?config(data_dir, Config) ++ atom_to_list(KeyType) ++ "." ++ atom_to_list(FormatType). |
| 137 | + |
| 138 | +-spec options(atom(), atom(), [term()]) -> [string()]. |
| 139 | +options(undefined, FormatType, Config) -> |
| 140 | + ["-d", data_file_name(string, FormatType, Config)]; |
| 141 | +options(KeyType, FormatType, Config) -> |
| 142 | + ["-k", atom_to_list(KeyType), |
| 143 | + "-d", data_file_name(KeyType, FormatType, Config)]. |
| 144 | + |
0 commit comments