File: version_extractor_spec.rb

package info (click to toggle)
ruby-device-detector 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 3,104 kB
  • sloc: ruby: 1,224; makefile: 5
file content (80 lines) | stat: -rw-r--r-- 1,952 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require_relative '../spec_helper'

describe DeviceDetector::VersionExtractor do

  subject { DeviceDetector::VersionExtractor.new(user_agent, regex_meta) }

  alias :extractor :subject

  describe '#call' do

    describe 'extractor without version' do

      let(:user_agent) { 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; Avant Browser; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)' }

      let(:regex_meta) do
        {
          :regex => 'Avant Browser',
          :name => 'Avant Browser',
          :version => ''
        }
      end

      it 'returns nil' do
        extractor.call.must_equal ''
      end

    end

    describe 'regex with dynamic matching' do

      let(:user_agent) { 'Mozilla/5.0 (X11; U; Linux i686; nl; rv:1.8.1b2) Gecko/20060821 BonEcho/2.0b2 (Debian-1.99+2.0b2+dfsg-1)' }
      let(:version) { 'BonEcho (2.0)' }
      let(:regex_meta) do
        {
          :regex => '(BonEcho|GranParadiso|Lorentz|Minefield|Namoroka|Shiretoko)/(\d+[\.\d]+)',
          :name => 'Firefox',
          :version => '$1 ($2)'
        }
      end

      it 'returns the correct version' do
        extractor.call.must_equal version
      end

      it 'removes trailing white spaces' do
        regex_meta[:version] = regex_meta[:version] + '   '
        extractor.call.must_equal version
      end

    end

    describe 'extractor with fixed version' do

      let(:user_agent) { 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' }
      let(:regex_meta) do
        {
          :regex => 'MSIE.*Trident/4.0',
          :version => '8.0'
        }
      end

      it 'returns the correct version' do
        extractor.call.must_equal '8.0'
      end

    end

    describe 'unknown user agent' do

      let(:user_agent) { 'garbage' }
      let(:regex_meta) { {} }

      it 'returns nil' do
        extractor.call.must_be_nil
      end

    end
  end
end