1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# -*- encoding: ascii-8bit -*-
require_relative '../../../spec_helper'
require_relative '../fixtures/classes'
require_relative 'shared/basic'
require_relative 'shared/unicode'
require_relative 'shared/taint'
describe "Array#pack with format 'U'" do
it_behaves_like :array_pack_basic, 'U'
it_behaves_like :array_pack_basic_non_float, 'U'
it_behaves_like :array_pack_arguments, 'U'
it_behaves_like :array_pack_unicode, 'U'
end
describe "Array#pack with format 'u'" do
it_behaves_like :array_pack_basic, 'u'
it_behaves_like :array_pack_basic_non_float, 'u'
it_behaves_like :array_pack_arguments, 'u'
it_behaves_like :array_pack_taint, 'u'
it "encodes an empty string as an empty string" do
[""].pack("u").should == ""
end
it "appends a newline to the end of the encoded string" do
["a"].pack("u").should == "!80``\n"
end
it "encodes one element per directive" do
["abc", "DEF"].pack("uu").should == "#86)C\n#1$5&\n"
end
it "prepends the length of each segment of the input string as the first character (+32) in each line of the output" do
["abcdefghijklm"].pack("u7").should == "&86)C9&5F\n&9VAI:FML\n!;0``\n"
end
it "encodes 1, 2, or 3 characters in 4 output characters (uuencoding)" do
[ [["a"], "!80``\n"],
[["ab"], "\"86(`\n"],
[["abc"], "#86)C\n"],
[["abcd"], "$86)C9```\n"],
[["abcde"], "%86)C9&4`\n"],
[["abcdef"], "&86)C9&5F\n"],
[["abcdefg"], "'86)C9&5F9P``\n"],
].should be_computed_by(:pack, "u")
end
it "emits a newline after complete groups of count / 3 input characters when passed a count modifier" do
["abcdefg"].pack("u3").should == "#86)C\n#9&5F\n!9P``\n"
end
it "implicitly has a count of 45 when passed '*', 0, 1, 2 or no count modifier" do
s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
r = "M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n%86%A86$`\n"
[ [[s], "u", r],
[[s], "u*", r],
[[s], "u0", r],
[[s], "u1", r],
[[s], "u2", r],
].should be_computed_by(:pack)
end
it "encodes all ascii characters" do
[ [["\x00\x01\x02\x03\x04\x05\x06"], "'``$\"`P0%!@``\n"],
[["\a\b\t\n\v\f\r"], "'!P@)\"@L,#0``\n"],
[["\x0E\x0F\x10\x11\x12\x13\x14\x15\x16"], ")\#@\\0$1(3%!46\n"],
[["\x17\x18\x19\x1a\e\x1c\x1d\x1e\x1f"], ")%Q@9&AL<'1X?\n"],
[["!\"\#$%&'()*+,-./"], "/(2(C)\"4F)R@I*BLL+2XO\n"],
[["0123456789"], "*,\#$R,S0U-C<X.0``\n"],
[[":;<=>?@"], "'.CL\\/3X_0```\n"],
[["ABCDEFGHIJKLMNOPQRSTUVWXYZ"], ":04)#1$5&1TA)2DM,34Y/4%%24U155E=865H`\n"],
[["[\\]^_`"], "&6UQ=7E]@\n"],
[["abcdefghijklmnopqrstuvwxyz"], ":86)C9&5F9VAI:FML;6YO<'%R<W1U=G=X>7H`\n"],
[["{|}~"], "$>WQ]?@``\n"],
[["\x7f\xc2\x80\xc2\x81\xc2\x82\xc2\x83"], ")?\\*`PH'\"@L*#\n"],
[["\xc2\x84\xc2\x85\xc2\x86\xc2\x87\xc2"], ")PH3\"A<*&PH?\"\n"],
[["\x88\xc2\x89\xc2\x8a\xc2\x8b\xc2\x8c"], ")B,*)PHK\"B\\*,\n"],
[["\xc2\x8d\xc2\x8e\xc2\x8f\xc2\x90\xc2"], ")PHW\"CL*/PI#\"\n"],
[["\x91\xc2\x92\xc2\x93\xc2\x94\xc2\x95"], ")D<*2PI/\"E,*5\n"],
[["\xc2\x96\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3\xa6"], ")HL.CPZ3#I<.F\n"],
[["\xc3\xa7\xc3\xa8\xc3\xa9\xc3\xaa\xc3"], ")PZ?#J,.IPZK#\n"],
[["\xab\xc3\xac\xc3\xad\xc3\xae\xc3\xaf"], ")J\\.LPZW#KL.O\n"],
[["\xc3\xb0\xc3\xb1\xc3\xb2\xc3\xb3\xc3"], ")P[##L<.RP[/#\n"],
[["\xb4\xc3\xb5\xc3\xb6\xc3\xb7\xc3\xb8"], ")M,.UP[;#M\\.X\n"],
[["\xc3\xb9\xc3\xba\xc3\xbb\xc3\xbc\xc3"], ")P[G#NL.[P[S#\n"],
[["\xbd\xc3\xbe\xc3\xbf"], "%O<.^P[\\`\n"]
].should be_computed_by(:pack, "u")
end
it "calls #to_str to convert an object to a String" do
obj = mock("pack m string")
obj.should_receive(:to_str).and_return("abc")
[obj].pack("u").should == "#86)C\n"
end
it "raises a TypeError if #to_str does not return a String" do
obj = mock("pack m non-string")
lambda { [obj].pack("u") }.should raise_error(TypeError)
end
it "raises a TypeError if passed nil" do
lambda { [nil].pack("u") }.should raise_error(TypeError)
end
it "raises a TypeError if passed an Integer" do
lambda { [0].pack("u") }.should raise_error(TypeError)
lambda { [bignum_value].pack("u") }.should raise_error(TypeError)
end
it "sets the output string to US-ASCII encoding" do
["abcd"].pack("u").encoding.should == Encoding::US_ASCII
end
end
|