File: ascii_only_spec.rb

package info (click to toggle)
ruby-powerpack 0.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 492 kB
  • sloc: ruby: 819; makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,386 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
# encoding: utf-8

require 'spec_helper'

unless String.method_defined? :ascii_only || defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
  describe 'String#ascii_only' do
    it 'returns same value for string with ASCII chars only' do
      expect('abc'.ascii_only).to eq 'abc'
    end

    it 'returns string without non-ASCII chars' do
      expect("abc\u{6666}".force_encoding("UTF-8").ascii_only).to eq 'abc'
    end

    it 'returns string without non-ASCII chars and with ASCII chars' do
      expect("\u{6666}".force_encoding("UTF-8").ascii_only).to eq ''
    end

    it 'does not change the original string' do
      string = "abc\u{6666}".force_encoding("UTF-8")
      string.ascii_only
      expect(string).to eq "abc\u{6666}".force_encoding("UTF-8")
    end
  end

  describe 'String#ascii_only!' do
    it 'returns same value for string with ASCII chars only' do
      expect('abc'.ascii_only!).to eq 'abc'
    end

    it 'returns string without non-ASCII chars' do
      expect("abc\u{6666}".force_encoding("UTF-8").ascii_only!).to eq 'abc'
    end

    it 'returns string without non-ASCII chars and with ASCII chars' do
      expect("\u{6666}".force_encoding("UTF-8").ascii_only!).to eq ''
    end

    it 'changes the original string' do
      string = "abc\u{6666}".force_encoding("UTF-8")
      string.ascii_only!
      expect(string).to eq 'abc'
    end
  end
end