summaryrefslogtreecommitdiff
path: root/node_modules/ua-parser/js/test/ua.js
blob: 5b1c37868dd61dbeb165660320a1c88c7c8cc72c (plain)
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
var assert = require('assert'),
    UA = require('../lib/ua').UA,
    makeParser = require('../lib/ua').makeParser;

suite('UA object', function() {
  test('UA constructor with no arguments', function() {
    var ua = new UA();
    assert.strictEqual(ua.family, 'Other');
    assert.strictEqual(ua.major, null);
    assert.strictEqual(ua.minor, null);
    assert.strictEqual(ua.patch, null);
  });

  test('UA constructor with valid arguments', function() {
    var ua = new UA('Firefox', '16', '3', 'beta');
    assert.strictEqual(ua.family, 'Firefox');
    assert.strictEqual(ua.major, '16');
    assert.strictEqual(ua.minor, '3');
    assert.strictEqual(ua.patch, 'beta');
  });

  test('UA#toVersionString with only numerical args', function() {
    assert.strictEqual(new UA('Firefox', '16', '3', '2').toVersionString(), '16.3.2');
  });

  test('UA#toVersionString with non numerical patch version', function() {
    assert.strictEqual(new UA('Firefox', '16', '3', 'beta').toVersionString(), '16.3beta');
  });

  test('UA#toString for known UA', function() {
    assert.strictEqual(new UA('Firefox', '16', '3', '2').toString(), 'Firefox 16.3.2');
  });

  test('UA#toString for unknown UA', function() {
    assert.strictEqual(new UA().toString(), 'Other');
  });
});


suite('UA parser', function() {
  test('makeParser returns a function', function() {
    assert.equal(typeof makeParser([]), 'function');
  });

  test('Unexpected args don\'t throw', function() {
    var parse = makeParser([]);
    assert.doesNotThrow(function() { parse('Foo'); });
    assert.doesNotThrow(function() { parse(''); });
    assert.doesNotThrow(function() { parse(); });
    assert.doesNotThrow(function() { parse(null); });
    assert.doesNotThrow(function() { parse({}); });
    assert.doesNotThrow(function() { parse(123); });
  });

  test('Parser returns an instance of UA when unsuccessful at parsing', function() {
    assert.ok(makeParser([])('bar') instanceof UA);
  });

  test('Parser returns an instance of UA when sucessful', function() {
    var parse = makeParser([{regex: 'foo'}]);
    assert.ok(parse('foo') instanceof UA);
  });

  test('Parser correctly identifies UA name', function() {
    var parse = makeParser([{regex: '(foo)'}]);
    assert.strictEqual(parse('foo').family, 'foo');
  });

  test('Parser correctly identifies version numbers', function() {
    var parse = makeParser([{regex: '(foo) (\\d)\\.(\\d)\\.(\\d)'}]),
        ua = parse('foo 1.2.3');
    assert.strictEqual(ua.family, 'foo');
    assert.strictEqual(ua.major, '1');
    assert.strictEqual(ua.minor, '2');
    assert.strictEqual(ua.patch, '3');
  });

  test('Parser correctly processes replacements', function() {
    var parse = makeParser([{
      regex: '(foo) (\\d)\\.(\\d).(\\d)',
      family_replacement: '$1bar',
      v1_replacement: 'a',
      v2_replacement: 'b',
      v3_replacement: 'c'
    }]);
  
    var ua = parse('foo 1.2.3');
    assert.strictEqual(ua.family, 'foobar');
    assert.strictEqual(ua.major, 'a');
    assert.strictEqual(ua.minor, 'b');
    assert.strictEqual(ua.patch, 'c');
  });
});