UNPKG

7.83 kBtext/coffeescriptView Raw
1
2
3############################################################################################################
4# njs_path = require 'path'
5# # njs_fs = require 'fs'
6# join = njs_path.join
7#...........................................................................................................
8CND = require 'cnd'
9rpr = CND.rpr
10badge = 'HOLLERITH-CODEC/tests'
11log = CND.get_logger 'plain', badge
12info = CND.get_logger 'info', badge
13whisper = CND.get_logger 'whisper', badge
14alert = CND.get_logger 'alert', badge
15debug = CND.get_logger 'debug', badge
16warn = CND.get_logger 'warn', badge
17help = CND.get_logger 'help', badge
18urge = CND.get_logger 'urge', badge
19echo = CND.echo.bind CND
20#...........................................................................................................
21# suspend = require 'coffeenode-suspend'
22# step = suspend.step
23# ### TAINT experimentally using `later` in place of `setImmediate` ###
24# later = suspend.immediately
25#...........................................................................................................
26test = require 'guy-test'
27CODEC = require './main'
28ƒ = CND.format_number
29
30
31#-----------------------------------------------------------------------------------------------------------
32@[ "codec encodes and decodes numbers" ] = ( T ) ->
33 key = [ 'foo', 1234, 5678, ]
34 key_bfr = CODEC.encode key
35 T.eq key, CODEC.decode key_bfr
36 whisper "key length: #{key_bfr.length}"
37
38#-----------------------------------------------------------------------------------------------------------
39@[ "codec encodes and decodes dates" ] = ( T ) ->
40 key = [ 'foo', ( new Date() ), 5678, ]
41 key_bfr = CODEC.encode key
42 T.eq key, CODEC.decode key_bfr
43 whisper "key length: #{key_bfr.length}"
44
45#-----------------------------------------------------------------------------------------------------------
46@[ "codec accepts long numbers" ] = ( T ) ->
47 key = [ 'foo', ( i for i in [ 0 .. 1000 ] ), 'bar', ]
48 key_bfr = CODEC.encode key
49 T.eq key, CODEC.decode key_bfr
50 whisper "key length: #{key_bfr.length}"
51
52#-----------------------------------------------------------------------------------------------------------
53@[ "codec accepts long texts" ] = ( T ) ->
54 long_text = ( new Array 1e4 ).join '#'
55 key = [ 'foo', [ long_text, long_text, long_text, long_text, ], 42, ]
56 key_bfr = CODEC.encode key
57 T.eq key, CODEC.decode key_bfr
58 whisper "key length: #{key_bfr.length}"
59
60#-----------------------------------------------------------------------------------------------------------
61@[ "codec preserves critical escaped characters (roundtrip) (1)" ] = ( T ) ->
62 text = 'abc\x00\x00\x00\x00def'
63 key = [ 'xxx', [ text, ], 0, ]
64 key_bfr = CODEC.encode key
65 T.eq key, CODEC.decode key_bfr
66
67#-----------------------------------------------------------------------------------------------------------
68@[ "codec preserves critical escaped characters (roundtrip) (2)" ] = ( T ) ->
69 text = 'abc\x01\x01\x01\x01def'
70 key = [ 'xxx', [ text, ], 0, ]
71 key_bfr = CODEC.encode key
72 T.eq key, CODEC.decode key_bfr
73
74#-----------------------------------------------------------------------------------------------------------
75@[ "codec preserves critical escaped characters (roundtrip) (3)" ] = ( T ) ->
76 text = 'abc\x00\x01\x00\x01def'
77 key = [ 'xxx', [ text, ], 0, ]
78 key_bfr = CODEC.encode key
79 T.eq key, CODEC.decode key_bfr
80
81#-----------------------------------------------------------------------------------------------------------
82@[ "codec preserves critical escaped characters (roundtrip) (4)" ] = ( T ) ->
83 text = 'abc\x01\x00\x01\x00def'
84 key = [ 'xxx', [ text, ], 0, ]
85 key_bfr = CODEC.encode key
86 T.eq key, CODEC.decode key_bfr
87
88#-----------------------------------------------------------------------------------------------------------
89@[ "codec accepts private type (1)" ] = ( T ) ->
90 key = [ { type: 'price', value: 'abc', }, ]
91 key_bfr = CODEC.encode key
92 T.eq key, CODEC.decode key_bfr
93
94#-----------------------------------------------------------------------------------------------------------
95@[ "codec accepts private type (2)" ] = ( T ) ->
96 key = [ 123, 456, { type: 'price', value: 'abc', }, 'xxx', ]
97 key_bfr = CODEC.encode key
98 T.eq key, CODEC.decode key_bfr
99
100#-----------------------------------------------------------------------------------------------------------
101@[ "codec decodes private type with custom decoder" ] = ( T ) ->
102 value = '/usr/local/lib/node_modules/coffee-script/README.md'
103 matcher = [ value, ]
104 encoded_value = value.split '/'
105 key = [ { type: 'route', value: encoded_value, }, ]
106 key_bfr = CODEC.encode key
107 #.........................................................................................................
108 decoded_key = CODEC.decode key_bfr, ( type, value ) ->
109 return value.join '/' if type is 'route'
110 throw new Error "unknown private type #{rpr type}"
111 #.........................................................................................................
112 T.eq matcher, decoded_key
113
114#-----------------------------------------------------------------------------------------------------------
115@[ "codec decodes private type with custom encoder and decoder" ] = ( T ) ->
116 route = '/usr/local/lib/node_modules/coffee-script/README.md'
117 parts = route.split '/'
118 key = [ { type: 'route', value: route, }, ]
119 matcher_1 = [ { type: 'route', value: parts, }]
120 matcher_2 = [ route, ]
121 #.........................................................................................................
122 encoder = ( type, value ) ->
123 return value.split '/' if type is 'route'
124 throw new Error "unknown private type #{rpr type}"
125 #.........................................................................................................
126 decoder = ( type, value ) ->
127 return value.join '/' if type is 'route'
128 throw new Error "unknown private type #{rpr type}"
129 #.........................................................................................................
130 key_bfr = CODEC.encode key, encoder
131 debug '©T4WKz', CODEC.rpr_of_buffer key_bfr
132 decoded_key_1 = CODEC.decode key_bfr
133 T.eq matcher_1, decoded_key_1
134 decoded_key_2 = CODEC.decode key_bfr, decoder
135 T.eq matcher_2, decoded_key_2
136
137#-----------------------------------------------------------------------------------------------------------
138@[ "private type takes default shape when handler returns use_fallback" ] = ( T ) ->
139 matcher = [ 84, { type: 'bar', value: 108, }, ]
140 key = [ { type: 'foo', value: 42, }, { type: 'bar', value: 108, }, ]
141 key_bfr = CODEC.encode key
142 #.........................................................................................................
143 decoded_key = CODEC.decode key_bfr, ( type, value, use_fallback ) ->
144 return value * 2 if type is 'foo'
145 return use_fallback
146 #.........................................................................................................
147 T.eq matcher, decoded_key
148
149
150#-----------------------------------------------------------------------------------------------------------
151@_main = ->
152 test @, 'timeout': 2500
153
154
155
156
157
158
159