ruby/ext/nkf/test.rb

319 строки
10 KiB
Ruby
Исходник Ответственный История

Этот файл содержит неоднозначные символы Юникода!

Этот файл содержит неоднозначные символы Юникода, которые могут быть перепутаны с другими в текущей локали. Если это намеренно, можете спокойно проигнорировать это предупреждение. Используйте кнопку Экранировать, чтобы подсветить эти символы.

$counter = 0
def result(result, message = nil)
$counter += 1
printf("%s %d%s\n",
result ? 'ok' : 'no',
$counter,
message ? ' ... ' + message : '')
end
begin
require 'nkf'
include NKF
rescue LoadError
result(false)
end
result(true)
if nkf('-me', '1')
result(true);
else
result(false);
end
output = nkf('-e', "\033\$@#1#3#2%B")
if output
# print output, "\n"
result(true, output)
else
result(false)
end
output = nkf('-Zj', "\033\$@#1#3#2%B")
if output
# print output, "\n"
result(true, output)
else
result(false)
end
output = "\244\306 " * 1024
old = output.length
output = nkf("-j", output)
if output
# print output, "\n"
result(true, "#{old} #{output.length}")
else
result(false)
end
$detail = false
def test(opt, input, expect)
print "\nINPUT:\n", input if $detail
print "\nEXPECT:\n", expect if $detail
result = nkf(opt, input)
print "\nGOT:\n", result if $detail
print result == expect ? "Ok\n" : "Fail\n"
return result
end
# Basic Conversion
print "\nBasic Conversion test\n\n"
example = {}
example['jis'] = <<'eofeof'.unpack('u')[0]
M1FER<W0@4W1A9V4@&R1"(3DQ(3%^2R%+?D]3&RA"(%-E8V]N9"!3=&%G92`;
M)$)0)TU:&RA"($AI<F%G86YA(!LD0B0B)"0D)B0H)"HD;R1R)',;*$(*2V%T
M86MA;F$@&R1")2(E)"4F)2@E*B5O)7(E<QLH0B!+:6=O=2`;)$(A)B%G(S`C
/029!)E@G(B=!*$`;*$(*
eofeof
#'
example['sjis'] = <<'eofeof'.unpack('u')[0]
M1FER<W0@4W1A9V4@@5B)0(F>ED"6GIAR(%-E8V]N9"!3=&%G92"8I9=Y($AI
M<F%G86YA((*@@J*"I(*F@JB"[8+P@O$*2V%T86MA;F$@@T&#0X-%@T>#28./
>@Y*#DR!+:6=O=2"!18&'@D^"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'.unpack('u')[0]
M&R1"030S424K)2TE+R4Q)3,E+"4N)3`E,B4T&RA""ALD0D$T,5$;*$)!0D-$
M149'86)C9&5F9PH;)$)!-#4M.68;*$(A0",D)5XF*B@I+2L]6UU[?1LD0B%O
M&RA""ALD0D@^,U$E*R4M)2\;*$(]&R1")3$E,R4L)2XE,"4R)30D2!LH0@H;
M)$)(/C-1)5$E5"57)5HE724M(2PE(B$K&RA""ALD0B51)50E51LH0@IH86YK
M86MU(!LD0B51)50E52$B&RA""ALD0B51)50E525S(2,;*$(*&R1"2#XS421.
&.&4;*$(*
eofeof
#`
example['mime.iso2022'] = <<'eofeof'.unpack('u')[0]
M/3])4T\M,C`R,BU*4#]"/T=Y4D%.144W96E23TI566Q/4U9)1WEH2S\]"CT_
M:7-O+3(P,C(M2E`_0C]'>5)!3D5%-V5I4D]*55EL3U-624=Y:$L_/0H]/VES
M;RTR,#(R+4I0/U$_/3%")$(D1B11/3%"*$)?96YD/ST*&R1`)#TD)B0K)$H;
M*$H@/3])4T\M,C`R,BU*4#]"/T=Y4D%.144W96E23U!Y:S=D:'-O4V<]/3\]
M(&5N9"!O9B!L:6YE"CT_25-/+3(P,C(M2E`_0C]'>5)!3D5%-V5I4D]0>6LW
M9&AS;U-G/3T_/2`]/TE33RTR,#(R+4I0/T(_1WE204Y%13=E:5)/4'EK-V1H
M<V]39ST]/ST*0G)O:V5N(&-A<V4*/3])4T\M,C`R,BU*4#]"/T=Y4D%.144W
M96E23U!Y:S=D"FAS;U-G/3T_/2`]/TE33RTR,`HR,BU*4#]"/T=Y4D%.144W
M96E23U!Y:S=D:'-O4V<]/3\]"CT_25-/+3(P,C(M2E`_0C]'>5)!3D5%-V5I
44D]*55EL3QM;2U-624=Y:$L_/0H_
eofeof
#'
example['mime.ans.strict'] = <<'eofeof'.unpack('u')[0]
M&R1"-$$[>B1.)48E.25(&RA""ALD0C1!.WHD3B5&)3DE2!LH0@H;)$(D1B11
M&RA"(&5N9`H;)$(D/20F)"LD2ALH0B`;)$(T03MZ)$X_*3MV&RA"96YD(&]F
M(&QI;F4*&R1"-$$[>B1./RD[=C1!.WHD3C\I.W8;*$(*0G)O:V5N(&-A<V4*
M/3])4T\M,C`R,BU*4#]"/T=Y4D%.144W96E23U!Y:S=D"FAS;U-G/3T_/2`]
M/TE33RTR,`HR,BU*4#]"/T=Y4D%.144W96E23U!Y:S=D:'-O4V<]/3\]"CT_
L25-/+3(P,C(M2E`_0C]'>5)!3D5%-V5I4D]*55EL3QM;2U-624=Y:$L_/0H_
eofeof
#'
example['mime.unbuf.strict'] = <<'eofeof'.unpack('u')[0]
M&R1"-$$[>B1.)48E.25(&RA""ALD0C1!.WHD3B5&)3DE2!LH0@H;)$(D1B11
M&RA"(&5N9`H;)$(D/20F)"LD2ALH0B`;)$(T03MZ)$X_*3MV&RA"96YD(&]F
M(&QI;F4*&R1"-$$[>B1./RD[=C1!.WHD3C\I.W8;*$(*0G)O:V5N(&-A<V4*
M&R1"-$$[>B1./RD;*$)H<V]39ST]/ST@/3])4T\M,C`*,C(M2E`_0C]'>5)!
M3D5%-V5I4D]0>6LW9&AS;U-G/3T_/0H;)$(T03MZ)$XE1ALH0EM+4U9)1WEH
$2S\]"F5I
eofeof
example['mime.ans'] = <<'eofeof'.unpack('u')[0]
M&R1"-$$[>B1.)48E.25(&RA""ALD0C1!.WHD3B5&)3DE2!LH0@H;)$(D1B11
M&RA"(&5N9`H;)$(D/20F)"LD2ALH0B`;)$(T03MZ)$X_*3MV&RA"96YD(&]F
M(&QI;F4*&R1"-$$[>B1./RD[=C1!.WHD3C\I.W8;*$(*0G)O:V5N(&-A<V4*
M&R1"-$$[>B1./RD;*$)H<V]39ST]/ST@&R1"-$$[>B1./RD[=ALH0@H;)$(T
603MZ)$XE1ALH0EM+4U9)1WEH2S\]"@`*
eofeof
#"
example['mime.unbuf'] = <<'eofeof'.unpack('u')[0]
M&R1"-$$[>B1.)48E.25(&RA""ALD0C1!.WHD3B5&)3DE2!LH0@H;)$(D1B11
M&RA"(&5N9`H;)$(D/20F)"LD2ALH0B`;)$(T03MZ)$X_*3MV&RA"96YD(&]F
M(&QI;F4*&R1"-$$[>B1./RD[=C1!.WHD3C\I.W8;*$(*0G)O:V5N(&-A<V4*
M&R1"-$$[>B1./RD;*$)H<V]39ST]/ST@&R1"-$$[>B1./RD[=ALH0@H;)$(T
603MZ)$XE1ALH0EM+4U9)1WEH2S\]"@`*
eofeof
#"
example['mime.base64'] = <<'eofeof'.unpack('u')[0]
M9W-M5"])3&YG<FU#>$I+-&=Q=4,S24LS9W%Q0E%:3TUI-39,,S0Q-&=S5T)1
M43!+9VUA1%9O3T@*9S)+1%1O3'=K8C)1;$E+;V=Q2T-X24MG9W5M0W%*3EEG
<<T=#>$E+9V=U;4,X64Q&9W)70S592VMG<6U""F=Q
eofeof
#"
example['mime.base64.ans'] = <<'eofeof'.unpack('u')[0]
M&R1")$M&?B1I)#LD1D0Z)"TD7B0Y)"PA(D5L-7XV83E9)$<A(ALH0@T*&R1"
M(T<E-R5G)4,E+R1R0C\_="0J)"0D1B0B)&LD*D4Y)$,D1B0B)&LD<R1')#<D
(9R0F)"L;*$(E
eofeof
#'
example['mime.is8859'] = <<'eofeof'.unpack('u')[0]
M/3])4T\M.#@U.2TQ/U$_*CU#-V%V83\_/2`*4&5E<B!4]G)N9W)E;@I,87-S
M92!(:6QL97+X92!0971E<G-E;B`@7"`B36EN(&MA97!H97-T(&AA<B!F86%E
M="!E="!F;V5L(2(*06%R:'5S(%5N:79E<G-I='DL($1%3DU!4DL@(%P@(DUI
<;B!KYG!H97-T(&AA<B!FY65T(&5T(&;X;"$B"@!K
eofeof
example['mime.is8859.ans'] = <<'eofeof'.unpack('u')[0]
M*L=A=F$_(`I0965R(%3V<FYG<F5N"DQA<W-E($AI;&QE<OAE(%!E=&5R<V5N
M("!<(")-:6X@:V%E<&AE<W0@:&%R(&9A865T(&5T(&9O96PA(@I!87)H=7,@
M56YI=F5R<VET>2P@1$5.34%22R`@7"`B36EN(&OF<&AE<W0@:&%R(&;E970@
)970@9OAL(2(*
eofeof
#"
print 'JIS to JIS ... '; test(' ', example['jis'], example['jis'])
print 'JIS to SJIS... '; test('-s', example['jis'], example['sjis'])
print 'JIS to EUC ... '; test('-e', example['jis'], example['euc'])
print 'SJIS to JIS ... '; test('-j', example['sjis'], example['jis'])
print 'SJIS to SJIS... '; test('-s', example['sjis'], example['sjis'])
print 'SJIS to EUC ... '; test('-e', example['sjis'], example['euc'])
print 'EUC to JIS ... '; test(' ', example['euc'], example['jis'])
print 'EUC to SJIS... '; test('-s', example['euc'], example['sjis'])
print 'EUC to EUC ... '; test('-e', example['euc'], example['euc'])
# Ambigous Case
print 'Ambiguous Case. '; test('' , example['amb'], example['amb.euc'])
# Input assumption
print 'SJIS Input assumption '
test('-Sx', example['amb'], example['amb.sjis'])
# X0201 <20><>̾
# X0201->X0208 conversion
# X0208 aphabet -> ASCII
# X0201 <20><><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD>
print "\nX0201 test\n\n"
# -X is necessary to allow X0201 in SJIS
# -Z convert X0208 alphabet to ASCII
print 'X0201 conversion: SJIS '
test('-XZ', example['x0201.sjis'], example['x0201.x0208'])
print 'X0201 conversion: JIS '
test('-Z', example['x0201.jis'], example['x0201.x0208'])
print 'X0201 conversion:SI/SO '
test('-Z', example['x0201.sosi'], example['x0201.x0208'])
print 'X0201 conversion: EUC '
test('-Z', example['x0201.euc'], example['x0201.x0208'])
# -x means X0201 output
print 'X0201 output: SJIS '
test('-xs', example['x0201.euc'], example['x0201.sjis'])
print 'X0201 output: JIS '
test('-xj', example['x0201.sjis'], example['x0201.jis'])
print 'X0201 output: EUC '
test('-xe', example['x0201.jis'], example['x0201.euc'])
# MIME decode
print "\nMIME test\n\n"
# MIME ISO-2022-JP
print "Next test is expeced to Fail.\n"
print 'MIME decode (strict) '
tmp = test('-m', example['mime.iso2022'], example['mime.ans.strict'])
print 'MIME decode (nonstrict)'
tmp = test('-m', example['mime.iso2022'], example['mime.ans'])
# open(OUT,'>tmp1');print OUT pack('u',$tmp);close(OUT);
# unbuf mode implies more pessimistic decode
print 'MIME decode (unbuf) '
test('-mu', example['mime.iso2022'], example['mime.unbuf'])
print 'MIME decode (base64) '
t = test('-mB', example['mime.base64'], example['mime.base64.ans'])
# MIME ISO-8859-1
# Without -l, ISO-8859-1 was handled as X0201.
print 'MIME ISO-8859-1 (Q) '
test('-ml', example['mime.is8859'], example['mime.is8859.ans'])