File: unicode_normalized_spec.rb

package info (click to toggle)
ruby2.5 2.5.5-3%2Bdeb10u4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,532 kB
  • sloc: ruby: 732,598; ansic: 669,262; xml: 25,363; yacc: 20,963; javascript: 6,680; sh: 3,610; lisp: 2,627; makefile: 596; python: 198; sed: 76; perl: 62; awk: 36; asm: 35
file content (74 lines) | stat: -rw-r--r-- 2,767 bytes parent folder | download | duplicates (2)
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
# -*- encoding: utf-8 -*-
require File.expand_path('../../../spec_helper', __FILE__)

describe "String#unicode_normalized?" do
  before :each do
    @nfc_normalized_str = "\u1e9b\u0323"
    @nfd_normalized_str = "\u017f\u0323\u0307"
    @nfkc_normalized_str = "\u1e69"
    @nfkd_normalized_str = "\u0073\u0323\u0307"
  end

  it "returns true if string is in the specified normalization form" do
    @nfc_normalized_str.unicode_normalized?(:nfc).should == true
    @nfd_normalized_str.unicode_normalized?(:nfd).should == true
    @nfkc_normalized_str.unicode_normalized?(:nfkc).should == true
    @nfkd_normalized_str.unicode_normalized?(:nfkd).should == true
  end

  it "returns false if string is not in the supplied normalization form" do
    @nfd_normalized_str.unicode_normalized?(:nfc).should == false
    @nfc_normalized_str.unicode_normalized?(:nfd).should == false
    @nfc_normalized_str.unicode_normalized?(:nfkc).should == false
    @nfc_normalized_str.unicode_normalized?(:nfkd).should == false
  end

  it "defaults to the nfc normalization form if no forms are specified" do
    @nfc_normalized_str.unicode_normalized?.should == true
    @nfd_normalized_str.unicode_normalized?.should == false
  end

  it "returns true if string is empty" do
    "".unicode_normalized?.should == true
  end

  it "returns true if string does not contain any unicode codepoints" do
    "abc".unicode_normalized?.should == true
  end

  it "raises an Encoding::CompatibilityError if the string is not in an unicode encoding" do
    lambda { @nfc_normalized_str.force_encoding("ISO-8859-1").unicode_normalized? }.should raise_error(Encoding::CompatibilityError)
  end

  it "raises an ArgumentError if the specified form is invalid" do
    lambda { @nfc_normalized_str.unicode_normalized?(:invalid_form) }.should raise_error(ArgumentError)
  end

  it "returns true if str is in Unicode normalization form (nfc)" do
    str = "a\u0300"
    str.unicode_normalized?(:nfc).should be_false
    str.unicode_normalize!(:nfc)
    str.unicode_normalized?(:nfc).should be_true
  end

  it "returns true if str is in Unicode normalization form (nfd)" do
    str = "a\u00E0"
    str.unicode_normalized?(:nfd).should be_false
    str.unicode_normalize!(:nfd)
    str.unicode_normalized?(:nfd).should be_true
  end

  it "returns true if str is in Unicode normalization form (nfkc)" do
    str = "a\u0300"
    str.unicode_normalized?(:nfkc).should be_false
    str.unicode_normalize!(:nfkc)
    str.unicode_normalized?(:nfkc).should be_true
  end

  it "returns true if str is in Unicode normalization form (nfkd)" do
    str = "a\u00E0"
    str.unicode_normalized?(:nfkd).should be_false
    str.unicode_normalize!(:nfkd)
    str.unicode_normalized?(:nfkd).should be_true
  end
end