Skip to content

Commit

Permalink
Fixed String#ascii_only? specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Jan 23, 2012
1 parent d8ebc1d commit f56582d
Showing 1 changed file with 41 additions and 56 deletions.
97 changes: 41 additions & 56 deletions core/string/ascii_only_spec.rb
Expand Up @@ -4,76 +4,61 @@

with_feature :encoding do
describe "String#ascii_only?" do
it "returns true if the String contains only US-ASCII characters" do
"hello".ascii_only?.should be_true
end

it "returns true if the String contains only US-ASCII characters
and is encoded as US-ASCII" do
"hello".force_encoding(Encoding::US_ASCII).ascii_only?.should be_true
"hello".encode(Encoding::US_ASCII).ascii_only?.should be_true
end

it "returns true if the String contains only US-ASCII characters
and is encoded as UTF-8" do
"hello".force_encoding('UTF-8').ascii_only?.should be_true
"hello".encode('UTF-8').ascii_only?.should be_true
end

it "returns true for all single-character US-ASCII Strings" do
0.upto(127) do |n|
n.chr.ascii_only?.should be_true
describe "with ASCII only characters" do
it "returns true if the encoding is UTF-8" do
[ ["hello", true],
["hello".encode('UTF-8'), true],
["hello".force_encoding('UTF-8'), true],
].should be_computed_by(:ascii_only?)
end
end

it "returns false for the first non-US-ASCII single-character String" do
chr = 128.chr
chr.encoding.should == Encoding::ASCII_8BIT
chr.ascii_only?.should be_false
end

it "returns false if the String contains only non-US-ASCII characters" do
"\u{6666}".ascii_only?.should be_false
end
it "returns true if the encoding is US-ASCII" do
"hello".force_encoding(Encoding::US_ASCII).ascii_only?.should be_true
"hello".encode(Encoding::US_ASCII).ascii_only?.should be_true
end

it "returns false if the String contains only non-US-ASCII characters
and is encoded as UTF-8" do
"\u{6666}".force_encoding('UTF-8').ascii_only?.should be_false
"\u{6666}".encode('UTF-8').ascii_only?.should be_false
it "returns true for all single-character UTF-8 Strings" do
0.upto(127) do |n|
n.chr.ascii_only?.should be_true
end
end
end

it "returns false if the String contains only non-ASCII characters
and is encoded as US-ASCII" do
"\u{6666}".force_encoding(Encoding::US_ASCII).ascii_only?.should be_false
end
describe "with non-ASCII only characters" do
it "returns false if the encoding is ASCII-8BIT" do
chr = 128.chr
chr.encoding.should == Encoding::ASCII_8BIT
chr.ascii_only?.should be_false
end

it "returns false if the String contains US-ASCII and non-US-ASCII characters" do
"hello, \u{6666}".ascii_only?.should be_false
end
it "returns false if the String contains any non-ASCII characters" do
[ ["\u{6666}", false],
["hello, \u{6666}", false],
["\u{6666}".encode('UTF-8'), false],
["\u{6666}".force_encoding('UTF-8'), false],
].should be_computed_by(:ascii_only?)
end

it "returns false if the String contains US-ASCII and non-US-ASCII characters
and is encoded as US-ASCII" do
"hello, \u{6666}".force_encoding(Encoding::US_ASCII).ascii_only?.should be_false
end
it "returns false if the encoding is US-ASCII" do
[ ["\u{6666}".force_encoding(Encoding::US_ASCII), false],
["hello, \u{6666}".force_encoding(Encoding::US_ASCII), false],
].should be_computed_by(:ascii_only?)
end

it "returns false if the String contains US-ASCII and non-US-ASCII characters
and is encoded as UTF-8" do
"hello, \u{6666}".encode('UTF-8').ascii_only?.should be_false
"hello, \u{6666}".force_encoding('UTF-8').ascii_only?.should be_false
end

it "accepts no arguments" do
lambda { "Glark".ascii_only?('?') }.should raise_error(ArgumentError)
it "returns true for the empty String with an ASCII-compatible encoding" do
"".ascii_only?.should be_true
"".encode('UTF-8').ascii_only?.should be_true
end

it "returns true for the empty String with an ASCII-compatiable encoding" do
''.ascii_only?.should be_true
''.encode('UTF-8').ascii_only?.should be_true
it "returns false for the empty String with a non-ASCII-compatible encoding" do
"".force_encoding('UTF-16LE').ascii_only?.should be_false
"".encode('UTF-16BE').ascii_only?.should be_false
end

it "returns false for the empty String with a non-ASCII-compatiable encoding" do
''.force_encoding('UTF-16LE').ascii_only?.should be_false
''.encode('UTF-16BE').ascii_only?.should be_false
it "returns false for a non-empty String with non-ASCII-compatible encoding" do
"\x78\x00".force_encoding("UTF-16LE").ascii_only?.should be_false
end
end
end

0 comments on commit f56582d

Please sign in to comment.