network-infra-utility 0.2.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +21 -0
  3. data/CHANGELOG.md +31 -4
  4. data/Gemfile +2 -0
  5. data/Gemfile.lock +70 -0
  6. data/Rakefile +1 -1
  7. data/bin/dns-query +834 -0
  8. data/bin/geo-doc +135 -0
  9. data/bin/geo-get +1 -1
  10. data/document/ASNum/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +242 -0
  11. data/document/DNSQuery/345/267/245/345/205/267/344/275/277/347/224/250/346/226/271/346/263/225.md +248 -0
  12. data/document/Geo/345/221/275/344/273/244/345/267/245/345/205/267/344/275/277/347/224/250/346/226/271/346/263/225.md +441 -0
  13. data/document/IP/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +297 -0
  14. data/document/MAC/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +296 -0
  15. data/document/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/344/275/277/347/224/250/346/226/271/346/263/225.md +764 -0
  16. data/network-infra-utility.gemspec +4 -2
  17. data/network.rb +3 -1
  18. data/service/geodb/GeoAPI.md +1 -0
  19. data/service/geodb/geodb.rb +278 -1
  20. data/service/ssh/README.md +942 -0
  21. data/service/ssh/bin/ssh-client +198 -0
  22. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/345/212/237/350/203/275/351/234/200/346/261/202/346/226/207/346/241/243.md +292 -0
  23. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/350/257/246/347/273/206/350/256/276/350/256/241/346/226/207/346/241/243.md +1521 -0
  24. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/350/275/257/344/273/266/350/256/276/350/256/241/346/226/207/346/241/243.md +2493 -0
  25. data/service/ssh/ext/ssh_core/bin/ssh_core.cmd +28 -0
  26. data/service/ssh/ext/ssh_core/config/sys.config +0 -0
  27. data/service/ssh/ext/ssh_core/config/vm.args +0 -0
  28. data/service/ssh/ext/ssh_core/local_deps/jsx/CHECKSUM +1 -0
  29. data/service/ssh/ext/ssh_core/local_deps/jsx/LICENSE +21 -0
  30. data/service/ssh/ext/ssh_core/local_deps/jsx/README.md +696 -0
  31. data/service/ssh/ext/ssh_core/local_deps/jsx/VERSION +1 -0
  32. data/service/ssh/ext/ssh_core/local_deps/jsx/contents.tar.gz +0 -0
  33. data/service/ssh/ext/ssh_core/local_deps/jsx/metadata.config +15 -0
  34. data/service/ssh/ext/ssh_core/local_deps/jsx/rebar.config +17 -0
  35. data/service/ssh/ext/ssh_core/local_deps/jsx/rebar.lock +1 -0
  36. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx.app.src +10 -0
  37. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx.erl +506 -0
  38. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_config.erl +393 -0
  39. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_config.hrl +18 -0
  40. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_consult.erl +81 -0
  41. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_decoder.erl +1909 -0
  42. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_encoder.erl +116 -0
  43. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_parser.erl +1214 -0
  44. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_to_json.erl +408 -0
  45. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_to_term.erl +389 -0
  46. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_verify.erl +121 -0
  47. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx.erl +506 -0
  48. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_config.erl +393 -0
  49. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_config.hrl +18 -0
  50. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_consult.erl +81 -0
  51. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_decoder.erl +1909 -0
  52. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_encoder.erl +116 -0
  53. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_parser.erl +1214 -0
  54. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_to_json.erl +408 -0
  55. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_to_term.erl +389 -0
  56. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_verify.erl +121 -0
  57. data/service/ssh/ext/ssh_core/rebar.config +24 -0
  58. data/service/ssh/ext/ssh_core/rebar.lock +1 -0
  59. data/service/ssh/ext/ssh_core/src/ssh_auth_engine.erl +156 -0
  60. data/service/ssh/ext/ssh_core/src/ssh_channel_stm.erl +232 -0
  61. data/service/ssh/ext/ssh_core/src/ssh_codec.erl +83 -0
  62. data/service/ssh/ext/ssh_core/src/ssh_conn_sup.erl +48 -0
  63. data/service/ssh/ext/ssh_core/src/ssh_conn_worker.erl +535 -0
  64. data/service/ssh/ext/ssh_core/src/ssh_core.app.src +36 -0
  65. data/service/ssh/ext/ssh_core/src/ssh_core_app.erl +11 -0
  66. data/service/ssh/ext/ssh_core/src/ssh_core_sup.erl +46 -0
  67. data/service/ssh/ext/ssh_core/src/ssh_infra_sup.erl +104 -0
  68. data/service/ssh/ext/ssh_core/src/ssh_ipc.hrl +80 -0
  69. data/service/ssh/ext/ssh_core/src/ssh_ipc_coalesce.erl +94 -0
  70. data/service/ssh/ext/ssh_core/src/ssh_ipc_gateway.erl +467 -0
  71. data/service/ssh/ext/ssh_core/src/ssh_ipc_proto.erl +95 -0
  72. data/service/ssh/ext/ssh_core/src/ssh_jump_chain.erl +101 -0
  73. data/service/ssh/ext/ssh_core/src/ssh_keepalive_mgr.erl +222 -0
  74. data/service/ssh/ext/ssh_core/src/ssh_known_hosts_proxy.erl +67 -0
  75. data/service/ssh/ext/ssh_core/src/ssh_port_fwd.erl +225 -0
  76. data/service/ssh/ext/ssh_core/src/ssh_sftp_session.erl +250 -0
  77. data/service/ssh/ext/ssh_core/src/ssh_sftp_sup.erl +62 -0
  78. data/service/ssh/ext/ssh_core_rs/Cargo.lock +2345 -0
  79. data/service/ssh/ext/ssh_core_rs/Cargo.toml +30 -0
  80. data/service/ssh/ext/ssh_core_rs/bin/ssh_core_rs +34 -0
  81. data/service/ssh/ext/ssh_core_rs/bin/ssh_core_rs.cmd +40 -0
  82. data/service/ssh/ext/ssh_core_rs/src/channel.rs +296 -0
  83. data/service/ssh/ext/ssh_core_rs/src/coalesce.rs +143 -0
  84. data/service/ssh/ext/ssh_core_rs/src/codec.rs +71 -0
  85. data/service/ssh/ext/ssh_core_rs/src/conn.rs +628 -0
  86. data/service/ssh/ext/ssh_core_rs/src/gateway.rs +389 -0
  87. data/service/ssh/ext/ssh_core_rs/src/handler.rs +293 -0
  88. data/service/ssh/ext/ssh_core_rs/src/keepalive.rs +194 -0
  89. data/service/ssh/ext/ssh_core_rs/src/main.rs +351 -0
  90. data/service/ssh/ext/ssh_core_rs/src/portfwd.rs +378 -0
  91. data/service/ssh/ext/ssh_core_rs/src/proto.rs +198 -0
  92. data/service/ssh/ext/ssh_core_rs/src/sftp.rs +294 -0
  93. data/service/ssh/lib/network_infra_utility/ssh/automation/macro_engine.rb +213 -0
  94. data/service/ssh/lib/network_infra_utility/ssh/client.rb +257 -0
  95. data/service/ssh/lib/network_infra_utility/ssh/config/schema.rb +90 -0
  96. data/service/ssh/lib/network_infra_utility/ssh/config/settings.rb +103 -0
  97. data/service/ssh/lib/network_infra_utility/ssh/config/store.rb +90 -0
  98. data/service/ssh/lib/network_infra_utility/ssh/ipc/coalesce.rb +83 -0
  99. data/service/ssh/lib/network_infra_utility/ssh/ipc/errors.rb +36 -0
  100. data/service/ssh/lib/network_infra_utility/ssh/ipc/router.rb +212 -0
  101. data/service/ssh/lib/network_infra_utility/ssh/ipc/transport.rb +81 -0
  102. data/service/ssh/lib/network_infra_utility/ssh/security/host_key.rb +211 -0
  103. data/service/ssh/lib/network_infra_utility/ssh/security/vault.rb +211 -0
  104. data/service/ssh/lib/network_infra_utility/ssh/session/history.rb +56 -0
  105. data/service/ssh/lib/network_infra_utility/ssh/session/manager.rb +92 -0
  106. data/service/ssh/lib/network_infra_utility/ssh/session/session.rb +109 -0
  107. data/service/ssh/lib/network_infra_utility/ssh/session/tree.rb +95 -0
  108. data/service/ssh/lib/network_infra_utility/ssh/terminal/ansi_parser.rb +435 -0
  109. data/service/ssh/lib/network_infra_utility/ssh/terminal/buffer.rb +78 -0
  110. data/service/ssh/lib/network_infra_utility/ssh/terminal/emulator.rb +159 -0
  111. data/service/ssh/lib/network_infra_utility/ssh/terminal/logger.rb +195 -0
  112. data/service/ssh/lib/network_infra_utility/ssh/terminal/screen.rb +212 -0
  113. data/service/ssh/lib/network_infra_utility/ssh/terminal/theme.rb +127 -0
  114. data/service/ssh/lib/network_infra_utility/ssh/version.rb +7 -0
  115. data/service/ssh/lib/network_infra_utility/ssh.rb +44 -0
  116. data/support/basic/as_num.rb +221 -0
  117. data/support/basic/mac_address.rb +281 -0
  118. data/version.rb +1 -1
  119. metadata +138 -1
@@ -0,0 +1,408 @@
1
+ %% The MIT License
2
+
3
+ %% Copyright (c) 2010-2013 alisdair sullivan <alisdairsullivan@yahoo.ca>
4
+
5
+ %% Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ %% of this software and associated documentation files (the "Software"), to deal
7
+ %% in the Software without restriction, including without limitation the rights
8
+ %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ %% copies of the Software, and to permit persons to whom the Software is
10
+ %% furnished to do so, subject to the following conditions:
11
+
12
+ %% The above copyright notice and this permission notice shall be included in
13
+ %% all copies or substantial portions of the Software.
14
+
15
+ %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ %% THE SOFTWARE.
22
+
23
+
24
+ -module(jsx_to_json).
25
+
26
+ -export([to_json/2, format/2]).
27
+ -export([init/1, handle_event/2]).
28
+ -export([start_json/0, start_json/1]).
29
+ -export([start_object/1, start_array/1, finish/1, insert/2, get_key/1, get_value/1]).
30
+
31
+
32
+ -record(config, {
33
+ space = 0,
34
+ indent = 0,
35
+ depth = 0,
36
+ newline = <<$\n>>
37
+ }).
38
+
39
+ -type config() :: proplists:proplist().
40
+
41
+
42
+ -spec to_json(Source::jsx:json_term(), Config::jsx_config:options()) -> binary().
43
+
44
+ to_json(Source, Config) when is_list(Config) ->
45
+ (jsx:encoder(?MODULE, Config, jsx_config:extract_config(Config ++ [escaped_strings])))(Source).
46
+
47
+
48
+ -spec format(Source::binary(), Config::jsx_config:options()) -> jsx:json_text().
49
+
50
+ format(Source, Config) when is_binary(Source) andalso is_list(Config) ->
51
+ (jsx:decoder(?MODULE, Config, jsx_config:extract_config(Config ++ [escaped_strings])))(Source);
52
+ format(_, _) -> erlang:error(badarg).
53
+
54
+
55
+ parse_config(Config) -> parse_config(Config, #config{}).
56
+
57
+ parse_config([{space, Val}|Rest], Config) when is_integer(Val), Val > 0 ->
58
+ parse_config(Rest, Config#config{space = Val});
59
+ parse_config([space|Rest], Config) ->
60
+ parse_config(Rest, Config#config{space = 1});
61
+ parse_config([{indent, Val}|Rest], Config) when is_integer(Val), Val > 0 ->
62
+ parse_config(Rest, Config#config{indent = Val});
63
+ parse_config([indent|Rest], Config) ->
64
+ parse_config(Rest, Config#config{indent = 1});
65
+ parse_config([{newline, Val}|Rest], Config) when is_binary(Val) ->
66
+ parse_config(Rest, Config#config{newline = Val});
67
+ parse_config([{K, _}|Rest] = Options, Config) ->
68
+ case lists:member(K, jsx_config:valid_flags()) of
69
+ true -> parse_config(Rest, Config)
70
+ ; false -> erlang:error(badarg, [Options, Config])
71
+ end;
72
+ parse_config([K|Rest] = Options, Config) ->
73
+ case lists:member(K, jsx_config:valid_flags()) of
74
+ true -> parse_config(Rest, Config)
75
+ ; false -> erlang:error(badarg, [Options, Config])
76
+ end;
77
+ parse_config([], Config) ->
78
+ Config.
79
+
80
+
81
+ -define(start_object, <<"{">>).
82
+ -define(start_array, <<"[">>).
83
+ -define(end_object, <<"}">>).
84
+ -define(end_array, <<"]">>).
85
+ -define(colon, <<":">>).
86
+ -define(comma, <<",">>).
87
+ -define(quote, <<"\"">>).
88
+ -define(space, <<" ">>).
89
+ -define(newline, <<"\n">>).
90
+
91
+
92
+ -type state() :: {unicode:charlist(), #config{}}.
93
+ -spec init(Config::config()) -> state().
94
+
95
+ init(Config) -> {[], parse_config(Config)}.
96
+
97
+
98
+ -spec handle_event(Event::any(), State::state()) -> state().
99
+
100
+ handle_event(end_json, State) -> get_value(State);
101
+
102
+ handle_event(start_object, State) -> start_object(State);
103
+ handle_event(end_object, State) -> finish(State);
104
+
105
+ handle_event(start_array, State) -> start_array(State);
106
+ handle_event(end_array, State) -> finish(State);
107
+
108
+ handle_event({Type, Event}, {_, Config} = State) -> insert(encode(Type, Event, Config), State).
109
+
110
+
111
+ encode(string, String, _Config) ->
112
+ [?quote, String, ?quote];
113
+ encode(key, Key, _Config) ->
114
+ [?quote, Key, ?quote];
115
+ encode(literal, Literal, _Config) ->
116
+ erlang:atom_to_list(Literal);
117
+ encode(integer, Integer, _Config) ->
118
+ erlang:integer_to_list(Integer);
119
+ encode(float, Float, _Config) ->
120
+ io_lib:format("~p", [Float]).
121
+
122
+
123
+ space(Config) ->
124
+ case Config#config.space of
125
+ 0 -> <<>>
126
+ ; X when X > 0 -> binary:copy(?space, X)
127
+ end.
128
+
129
+
130
+ indent(Config) ->
131
+ case Config#config.indent of
132
+ 0 -> <<>>
133
+ ; X when X > 0 -> <<(Config#config.newline)/binary, (binary:copy(?space, X * Config#config.depth))/binary>>
134
+ end.
135
+
136
+
137
+ indent_or_space(Config) ->
138
+ case Config#config.indent > 0 of
139
+ true -> indent(Config)
140
+ ; false -> space(Config)
141
+ end.
142
+
143
+
144
+ %% internal state is a stack and a config object
145
+ %% `{Stack, Config}`
146
+ %% the stack is a list of in progress objects/arrays
147
+ %% `[Current, Parent, Grandparent,...OriginalAncestor]`
148
+ %% an object has the representation on the stack of
149
+ %% `{object, Object}`
150
+ %% of if there's a key with a yet to be matched value
151
+ %% `{object, Key, Object}`
152
+ %% an array looks like
153
+ %% `{array, Array}`
154
+ %% `Object` and `Array` are utf8 encoded binaries
155
+
156
+ start_json() -> {[], #config{}}.
157
+
158
+ start_json(Config) when is_list(Config) -> {[], parse_config(Config)}.
159
+
160
+ %% allocate a new object on top of the stack
161
+ start_object({Stack, Config = #config{depth = Depth}}) ->
162
+ {[{object, ?start_object}] ++ Stack, Config#config{depth = Depth + 1}}.
163
+
164
+ %% allocate a new array on top of the stack
165
+ start_array({Stack, Config = #config{depth = Depth}}) ->
166
+ {[{array, ?start_array}] ++ Stack, Config#config{depth = Depth + 1}}.
167
+
168
+ %% finish an object or array and insert it into the parent object if it exists
169
+ finish({Stack, Config = #config{depth = Depth}}) ->
170
+ NewConfig = Config#config{depth = Depth - 1},
171
+ finish_({Stack, NewConfig}).
172
+
173
+ finish_({[{object, <<"{">>}], Config}) -> {<<"{}">>, Config};
174
+ finish_({[{array, <<"[">>}], Config}) -> {<<"[]">>, Config};
175
+ finish_({[{object, <<"{">>}|Rest], Config}) -> insert(<<"{}">>, {Rest, Config});
176
+ finish_({[{array, <<"[">>}|Rest], Config}) -> insert(<<"[]">>, {Rest, Config});
177
+ finish_({[{object, Object}], Config}) ->
178
+ {[Object, indent(Config), ?end_object], Config};
179
+ finish_({[{object, Object}|Rest], Config}) ->
180
+ insert([Object, indent(Config), ?end_object], {Rest, Config});
181
+ finish_({[{array, Array}], Config}) ->
182
+ {[Array, indent(Config), ?end_array], Config};
183
+ finish_({[{array, Array}|Rest], Config}) ->
184
+ insert([Array, indent(Config), ?end_array], {Rest, Config});
185
+ finish_(_) -> erlang:error(badarg).
186
+
187
+ %% insert a value when there's no parent object or array
188
+ insert(Value, {[], Config}) ->
189
+ {Value, Config};
190
+ %% insert a key or value into an object or array, autodetects the 'right' thing
191
+ insert(Key, {[{object, Object}|Rest], Config}) ->
192
+ {[{object, Key, Object}] ++ Rest, Config};
193
+ insert(Value, {[{object, Key, ?start_object}|Rest], Config}) ->
194
+ {
195
+ [{object, [
196
+ ?start_object,
197
+ indent(Config),
198
+ Key,
199
+ ?colon,
200
+ space(Config),
201
+ Value
202
+ ]}] ++ Rest,
203
+ Config
204
+ };
205
+ insert(Value, {[{object, Key, Object}|Rest], Config}) ->
206
+ {
207
+ [{object, [
208
+ Object,
209
+ ?comma,
210
+ indent_or_space(Config),
211
+ Key,
212
+ ?colon,
213
+ space(Config),
214
+ Value
215
+ ]}] ++ Rest,
216
+ Config
217
+ };
218
+ insert(Value, {[{array, ?start_array}|Rest], Config}) ->
219
+ {[{array, [?start_array, indent(Config), Value]}] ++ Rest, Config};
220
+ insert(Value, {[{array, Array}|Rest], Config}) ->
221
+ {
222
+ [{array, [Array,
223
+ ?comma,
224
+ indent_or_space(Config),
225
+ Value
226
+ ]}] ++ Rest,
227
+ Config
228
+ };
229
+ insert(_, _) -> erlang:error(badarg).
230
+
231
+
232
+ get_key({[{object, Key, _}|_], _}) -> Key;
233
+ get_key(_) -> erlang:error(badarg).
234
+
235
+
236
+ get_value({Value, _Config}) ->
237
+ try unicode:characters_to_binary(Value)
238
+ catch error:_ -> erlang:error(badarg)
239
+ end;
240
+ get_value(_) -> erlang:error(badarg).
241
+
242
+
243
+
244
+ %% eunit tests
245
+
246
+ -ifdef(TEST).
247
+ -include_lib("eunit/include/eunit.hrl").
248
+
249
+
250
+ config_test_() ->
251
+ [
252
+ {"empty config", ?_assertEqual(#config{}, parse_config([]))},
253
+ {"unspecified indent/space", ?_assertEqual(
254
+ #config{space=1, indent=1},
255
+ parse_config([space, indent])
256
+ )},
257
+ {"specific indent", ?_assertEqual(
258
+ #config{indent=4},
259
+ parse_config([{indent, 4}])
260
+ )},
261
+ {"specific space", ?_assertEqual(
262
+ #config{space=2},
263
+ parse_config([{space, 2}])
264
+ )},
265
+ {"specific space and indent", ?_assertEqual(
266
+ #config{space=2, indent=2},
267
+ parse_config([{space, 2}, {indent, 2}])
268
+ )},
269
+ {"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
270
+ {"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
271
+ ].
272
+
273
+
274
+ space_test_() ->
275
+ [
276
+ {"no space", ?_assertEqual(<<>>, space(#config{space=0}))},
277
+ {"one space", ?_assertEqual(<<" ">>, space(#config{space=1}))},
278
+ {"four spaces", ?_assertEqual(<<" ">>, space(#config{space=4}))}
279
+ ].
280
+
281
+
282
+ indent_test_() ->
283
+ [
284
+ {"no indent", ?_assertEqual(<<>>, indent(#config{indent=0, depth=1}))},
285
+ {"indent 1 depth 1", ?_assertEqual(
286
+ <<?newline/binary, <<" ">>/binary>>,
287
+ indent(#config{indent=1, depth=1})
288
+ )},
289
+ {"indent 1 depth 2", ?_assertEqual(
290
+ <<?newline/binary, <<" ">>/binary>>,
291
+ indent(#config{indent=1, depth=2})
292
+ )},
293
+ {"indent 4 depth 1", ?_assertEqual(
294
+ <<?newline/binary, <<" ">>/binary>>,
295
+ indent(#config{indent=4, depth=1})
296
+ )},
297
+ {"indent 4 depth 2", ?_assertEqual(
298
+ <<?newline/binary, <<" ">>/binary, <<" ">>/binary>>,
299
+ indent(#config{indent=4, depth=2})
300
+ )}
301
+ ].
302
+
303
+
304
+ indent_or_space_test_() ->
305
+ [
306
+ {"no indent so space", ?_assertEqual(
307
+ <<" ">>,
308
+ indent_or_space(#config{space=1, indent=0, depth=1})
309
+ )},
310
+ {"indent so no space", ?_assertEqual(
311
+ <<?newline/binary, <<" ">>/binary>>,
312
+ indent_or_space(#config{space=1, indent=1, depth=1})
313
+ )}
314
+ ].
315
+
316
+
317
+ encode_test_() ->
318
+ [
319
+ {"0.0", ?_assert(encode(float, 0.0, #config{}) =:= ["0.0"])},
320
+ {"1.0", ?_assert(encode(float, 1.0, #config{}) =:= ["1.0"])},
321
+ {"-1.0", ?_assert(encode(float, -1.0, #config{}) =:= ["-1.0"])},
322
+ {"3.1234567890987654321",
323
+ ?_assert(
324
+ encode(float, 3.1234567890987654321, #config{}) =:= ["3.1234567890987655"])
325
+ },
326
+ {"1.0e23", ?_assert(encode(float, 1.0e23, #config{}) =:= ["1.0e23"])},
327
+ {"0.3", ?_assert(encode(float, 3.0/10.0, #config{}) =:= ["0.3"])},
328
+ {"0.0001", ?_assert(encode(float, 0.0001, #config{}) =:= ["0.0001"])},
329
+ {"0.00001", ?_assert(encode(float, 0.00001, #config{}) =:= ["1.0e-5"])},
330
+ {"0.00000001", ?_assert(encode(float, 0.00000001, #config{}) =:= ["1.0e-8"])},
331
+ {"1.0e-323", ?_assert(encode(float, 1.0e-323, #config{}) =:= ["1.0e-323"])},
332
+ {"1.0e308", ?_assert(encode(float, 1.0e308, #config{}) =:= ["1.0e308"])},
333
+ {"min normalized float",
334
+ ?_assert(
335
+ encode(float, math:pow(2, -1022), #config{}) =:= ["2.2250738585072014e-308"]
336
+ )
337
+ },
338
+ {"max normalized float",
339
+ ?_assert(
340
+ encode(float, (2 - math:pow(2, -52)) * math:pow(2, 1023), #config{})
341
+ =:= ["1.7976931348623157e308"]
342
+ )
343
+ },
344
+ {"min denormalized float",
345
+ ?_assert(encode(float, math:pow(2, -1074), #config{}) =:= ["5.0e-324"])
346
+ },
347
+ {"max denormalized float",
348
+ ?_assert(
349
+ encode(float, (1 - math:pow(2, -52)) * math:pow(2, -1022), #config{})
350
+ =:= ["2.225073858507201e-308"]
351
+ )
352
+ },
353
+ {"hello world", ?_assert(encode(string, <<"hello world">>, #config{})
354
+ =:= [<<"\"">>, <<"hello world">>, <<"\"">>]
355
+ )},
356
+ {"key", ?_assert(encode(key, <<"key">>, #config{}) =:= [<<"\"">>, <<"key">>, <<"\"">>])},
357
+ {"1", ?_assert(encode(integer, 1, #config{}) =:= "1")},
358
+ {"-1", ?_assert(encode(integer, -1, #config{}) =:= "-1")},
359
+ {"true", ?_assert(encode(literal, true, #config{}) =:= "true")},
360
+ {"false", ?_assert(encode(literal, false, #config{}) =:= "false")},
361
+ {"null", ?_assert(encode(literal, null, #config{}) =:= "null")}
362
+ ].
363
+
364
+
365
+ format_test_() ->
366
+ % {minified version, pretty version}
367
+ Cases = [
368
+ {"empty object", <<"{}">>, <<"{}">>},
369
+ {"empty array", <<"[]">>, <<"[]">>},
370
+ {"single key object", <<"{\"k\":\"v\"}">>, <<"{\n \"k\": \"v\"\n}">>},
371
+ {"single member array", <<"[true]">>, <<"[\n true\n]">>},
372
+ {"multiple key object",
373
+ <<"{\"k\":\"v\",\"x\":\"y\"}">>,
374
+ <<"{\n \"k\": \"v\",\n \"x\": \"y\"\n}">>
375
+ },
376
+ {"multiple member array",
377
+ <<"[1.0,2.0,3.0]">>,
378
+ <<"[\n 1.0,\n 2.0,\n 3.0\n]">>
379
+ },
380
+ {"nested structure",
381
+ <<"[[{},[],true],{\"k\":\"v\",\"x\":\"y\"}]">>,
382
+ <<"[\n [\n {},\n [],\n true\n ],\n {\n \"k\": \"v\",\n \"x\": \"y\"\n }\n]">>
383
+ }
384
+ ],
385
+ [{Title, ?_assertEqual(Min, jsx:minify(Pretty))} || {Title, Min, Pretty} <- Cases] ++
386
+ [{Title, ?_assertEqual(Pretty, jsx:prettify(Min))} || {Title, Min, Pretty} <- Cases].
387
+
388
+ custom_newline_test_() ->
389
+ [
390
+ {"single key object", ?_assert(
391
+ jsx:format(<<"{\"k\":\"v\"}">>, [space, {indent, 2}, {newline, <<$\r>>}])
392
+ =:= <<"{\r \"k\": \"v\"\r}">>)
393
+ }
394
+ ].
395
+
396
+ handle_event_test_() ->
397
+ Data = jsx:test_cases() ++ jsx:special_test_cases(),
398
+ [
399
+ {
400
+ Title, ?_assertEqual(
401
+ JSON,
402
+ lists:foldl(fun handle_event/2, init([]), Events ++ [end_json])
403
+ )
404
+ } || {Title, JSON, _, Events} <- Data
405
+ ].
406
+
407
+
408
+ -endif.