File: fast_blank_spec.rb

package info (click to toggle)
ruby-fast-blank 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 96 kB
  • sloc: ruby: 107; ansic: 82; makefile: 4
file content (57 lines) | stat: -rw-r--r-- 1,276 bytes parent folder | download
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
$VERBOSE = true

class ::String
  # Stub the original method to make sure it is redefined correctly.
  def blank?
    raise NotImplementedError
  end

  def blank2?
    /\A[[:space:]]*\z/ === self
  end
end

require 'fast_blank'

describe String do
  it "works" do
    expect("".blank?).to eq(true)
    expect(" ".blank?).to eq(true)
    expect("\r\n".blank?).to eq(true)
    "\r\n\v\f\r\s\u0085".blank? == true
  end

  it "provides a parity with active support function" do
    (16*16*16*16).times do |i|
      c = i.chr('UTF-8') rescue nil
      unless c.nil?
        expect("#{i.to_s(16)} #{c.blank_as?}").to eq("#{i.to_s(16)} #{c.blank2?}")
      end
    end


    (256).times do |i|
      c = i.chr('ASCII') rescue nil
      unless c.nil?
        expect("#{i.to_s(16)} #{c.blank_as?}").to eq("#{i.to_s(16)} #{c.blank2?}")
      end
    end
  end

  it "has parity with strip.length" do
    (256).times do |i|
      c = i.chr('ASCII') rescue nil
      unless c.nil?
        expect("#{i.to_s(16)} #{c.strip.length == 0}").to eq("#{i.to_s(16)} #{c.blank?}")
      end
    end
  end

  it "treats \u0000 correctly" do
    # odd I know
    expect("\u0000".strip.length).to eq(0)
    expect("\u0000".blank_as?).to be_falsey
    expect("\u0000".blank?).to be_truthy
  end

end