File: detector_fixtures_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 (100 lines) | stat: -rw-r--r-- 3,363 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require_relative '../spec_helper'

describe DeviceDetector do

  fixture_dir = File.expand_path('../../fixtures/detector', __FILE__)
  fixture_files = Dir["#{fixture_dir}/*.yml"]
  fixture_files.each do |fixture_file|

    describe File.basename(fixture_file) do

      fixtures = nil
      begin
        fixtures = YAML.load(File.read(fixture_file))
      rescue Psych::SyntaxError => e
        fail "Failed to parse #{fixture_file}, reason: #{e}"
      end

      def str_or_nil(string)
        return nil if string == ''
        string
      end

      fixtures.each do |f|

        user_agent = f["user_agent"]
        detector = DeviceDetector.new(user_agent)
        os = detector.send(:os)

        describe user_agent do
          it "should be detected" do
            if detector.bot?
              assert_equal str_or_nil(f["bot"]["name"]), detector.bot_name, "failed bot name detection"
            else
              if f["client"]
                assert_equal str_or_nil(f["client"]["name"]), detector.name, "failed client name detection"
              end

              os_family = str_or_nil(f["os_family"])
              if os_family != "Unknown"
                if os_family.nil?
                  assert_nil os.family, "failed os family detection"
                else
                  assert_equal os_family, os.family, "failed os family detection"
                end

                name = str_or_nil(f["os"]["name"])
                if name.nil?
                  assert_nil os.name, "failed os name detection"
                else
                  assert_equal name, os.name, "failed os name detection"
                end

                short_name = str_or_nil(f["os"]["short_name"])
                if short_name.nil?
                  assert_nil os.short_name, "failed os short name detection"
                else
                  assert_equal short_name, os.short_name, "failed os short name detection"
                end

                os_version = str_or_nil(f["os"]["version"])
                if os_version.nil?
                  assert_nil os.full_version, "failed os version detection"
                else
                  assert_equal os_version, os.full_version, "failed os version detection"
                end
              end
              if f["device"]
                expected_type = str_or_nil(f["device"]["type"])
                actual_type = detector.device_type

                if expected_type != actual_type
                  # puts "\n", f.inspect, expected_type, actual_type, detector.device_name, regex_meta.inspect
                  # debugger
                  # detector.device_type
                end
                if expected_type.nil?
                  assert_nil actual_type, "failed device type detection"
                else
                  assert_equal expected_type, actual_type, "failed device type detection"
                end

                model = str_or_nil(f["device"]["model"])
                model = model.to_s unless model.nil?

                if model.nil?
                  assert_nil detector.device_name, "failed device name detection"
                else
                  assert_equal model, detector.device_name, "failed device name detection"
                end
              end
            end
          end
        end
      end

    end

  end

end