File: characters_strategy_spec.rb

package info (click to toggle)
ruby-string-direction 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 212 kB
  • sloc: ruby: 657; makefile: 7; sh: 3
file content (107 lines) | stat: -rw-r--r-- 2,650 bytes parent folder | download | duplicates (3)
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
99
100
101
102
103
104
105
106
107
require 'spec_helper'

describe StringDirection::CharactersStrategy do
  describe '#run' do
    let(:english) { 'English' }
    let(:arabic) { 'العربية' }

    subject { described_class.new.run(string) }

    context 'when both left-to-right and right-to-left characters are present' do
      let(:string) { arabic + english }

      it "returns 'bidi'" do
        expect(subject).to eq 'bidi'
      end
    end

    context 'when right-to-left character are present but none of left-to-right' do
      let(:string) { arabic }

      it "returns 'rtl'" do
        expect(subject).to eq 'rtl'
      end
    end

    context 'when left-to-right character are present but none of right-to-left' do
      let(:string) { english }

      it "returns 'ltr'" do
        expect(subject).to eq 'ltr'
      end
    end

    context 'when neither left-to-right nor right-to-left characters are present' do
      let(:string) { ' ' }

      it 'returns nil' do
        expect(subject).to be_nil
      end
    end

    context 'when default right-to-left scripts are changed' do
      let(:new_rtl_script) { 'Latin' }
      let(:old_rtl_script) { 'Arabic' }

      context 'when there are characters from an added right-to-left script' do
        let(:string) { english }

        it 'treats them as right-to-left chracters' do
          StringDirection.configure do |config|
            config.rtl_scripts << new_rtl_script
          end

          expect(subject).to eq 'rtl'
        end
      end

      context 'when there are characters from a deleted right-to-left script ' do
        let(:string) { arabic }

        it 'treats them as left-to-right characters' do
          StringDirection.configure do |config|
            config.rtl_scripts.delete(old_rtl_script)
          end

          expect(subject).to eq 'ltr'
        end
      end

      after :each do
        StringDirection.reset_configuration
      end
    end

    context 'when special characters are present' do
      let(:string) do
        mark = "\u0903"
        punctuation = '_'
        symbol = '€'
        separator = ' '
        other = "\u0005"

        arabic + mark + punctuation + symbol + separator + other
      end

      it 'ignores them' do
        expect(subject).to eq 'rtl'
      end
    end

    context 'when an object responding to #to_s is given' do
      let(:string) do
        class StringDirection::TestObject
          def to_s
            'English'
          end
        end

        StringDirection::TestObject.new
      end

      it 'takes as string the result of #to_s method' do
        expect(subject).to eq('ltr')
      end
    end
  end
end