File: grapheme_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (98 lines) | stat: -rw-r--r-- 3,820 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require "./spec_helper"

describe String::Grapheme do
  it ".new" do
    String::Grapheme.new("foo", 0...1, 'f').@cluster.should eq 'f'
    String::Grapheme.new("foo", 0...2, 'o').@cluster.should eq "fo"
    String::Grapheme.new("foo", 1...3, 'o').@cluster.should eq "oo"
  end

  it "#to_s" do
    String::Grapheme.new("foo").to_s.should eq "foo"
    String::Grapheme.new('f').to_s.should eq "f"

    String.build do |io|
      String::Grapheme.new("foo").to_s(io)
    end.should eq "foo"
    String.build do |io|
      String::Grapheme.new('f').to_s(io)
    end.should eq "f"
  end

  it "#inspect" do
    String::Grapheme.new("foo").inspect.should eq %(String::Grapheme("foo"))
    String::Grapheme.new('f').inspect.should eq %(String::Grapheme('f'))
  end

  it "#size" do
    String::Grapheme.new("foo").size.should eq 3
    String::Grapheme.new("πŸ™‚πŸ™‚").size.should eq 2
    String::Grapheme.new('f').size.should eq 1
    String::Grapheme.new('πŸ™‚').size.should eq 1
  end

  it "#bytesize" do
    String::Grapheme.new("foo").bytesize.should eq 3
    String::Grapheme.new("πŸ™‚πŸ™‚").bytesize.should eq 8
    String::Grapheme.new('f').bytesize.should eq 1
    String::Grapheme.new('πŸ™‚').bytesize.should eq 4
  end

  it "#==" do
    String::Grapheme.new('f').should eq String::Grapheme.new('f')
    String::Grapheme.new('f').should_not eq String::Grapheme.new("f")
    String::Grapheme.new("foo").should eq String::Grapheme.new("foo")
  end

  it ".break?" do
    String::Grapheme.break?('a', 'b').should be_true

    String::Grapheme.break?('\r', '\n').should be_false
    String::Grapheme.break?('\r', 'a').should be_true
    String::Grapheme.break?('a', '\n').should be_true

    String::Grapheme.break?('o', '\u0308').should be_false
  end
end

describe String do
  it "#grapheme_size" do
    "foo".grapheme_size.should eq 3
    "πŸ™‚πŸ™‚".grapheme_size.should eq 2
    "f".grapheme_size.should eq 1
    "πŸ™‚".grapheme_size.should eq 1
  end

  it "#graphemes" do
    "abc".graphemes.map(&.to_s).should eq ["a", "b", "c"]
    "mΓΆp".graphemes.map(&.to_s).should eq ["m", "ΓΆ", "p"]
    "möp".graphemes.map(&.to_s).should eq ["m", "o\u0308", "p"]
    "뢴".graphemes.map(&.to_s).should eq ["\u1105\u116c\u11ab"]
    "\r\n".graphemes.map(&.to_s).should eq ["\r\n"]
  end

  # These are just a couple of manual tests, the lot of automated specs is in grapheme_break_spec.cr
  describe "#each_grapheme" do
    it_iterates_graphemes "", [] of String
    it_iterates_graphemes "\x00", [Char::ZERO]
    it_iterates_graphemes "x", ['x']
    it_iterates_graphemes "basic", ['b', 'a', 's', 'i', 'c']
    it_iterates_graphemes "möp", ['m', "o\u0308", 'p']
    it_iterates_graphemes "\r\n", ["\r\n"]
    it_iterates_graphemes "\n\n", ['\n', '\n']
    it_iterates_graphemes "\t*", ['\t', '*']
    it_iterates_graphemes "뢴", ["\u1105\u116C\u11AB"]
    it_iterates_graphemes "ܐ܏ܒܓܕ", ['\u0710', "\u070F\u0712", '\u0713', '\u0715']
    it_iterates_graphemes "ΰΈ³", ['\u0E33']
    it_iterates_graphemes "ΰΈ³ΰΈ³", ["\u0E33\u0E33"]
    it_iterates_graphemes "ΰΈͺΰΈ£ΰΈ°ΰΈ­ΰΈ³", ['\u0E2A', '\u0E23', '\u0E30', "\u0E2D\u0E33"]
    it_iterates_graphemes "*뢴*", ['*', "\u1105\u116C\u11AB", '*']
    it_iterates_graphemes "*πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘©*", ['*', "\u{1F469}\u200D\u2764\uFE0F\u200D\u{1F48B}\u200D\u{1F469}", '*']
    it_iterates_graphemes "πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘©", ["\u{1F469}\u200D\u2764\uFE0F\u200D\u{1F48B}\u200D\u{1F469}"]
    it_iterates_graphemes "πŸ‹πŸ½β€β™€οΈ", ["\u{1F3CB}\u{1F3FD}\u200D\u2640\uFE0F"]
    it_iterates_graphemes "πŸ™‚", ['\u{1F642}']
    it_iterates_graphemes "πŸ™‚πŸ™‚", ['\u{1F642}', '\u{1F642}']
    it_iterates_graphemes "πŸ‡©πŸ‡ͺ", ["\u{1F1E9}\u{1F1EA}"]
    it_iterates_graphemes "πŸ³οΈβ€πŸŒˆ", ["\u{1F3F3}\uFE0F\u200D\u{1F308}"]
  end
end