summaryrefslogtreecommitdiff
path: root/node_modules/ua-parser/cpp/UaParserTest.cpp
blob: 3636fc4e31db91197e381c7c9e5f954e656e4607 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "UaParser.h"
#include <fstream>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <yaml-cpp/yaml.h>
#include <string>

namespace {

const UserAgentParser g_ua_parser("../regexes.yaml");

TEST(UserAgentParser, basic) {
  const auto uagent = g_ua_parser.parse(
    "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 "
    "(KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3");
  ASSERT_EQ("Mobile Safari", uagent.browser.family);
  ASSERT_EQ("5", uagent.browser.major);
  ASSERT_EQ("1", uagent.browser.minor);
  ASSERT_EQ("", uagent.browser.patch);
  ASSERT_EQ("Mobile Safari 5.1.0", uagent.browser.toString());
  ASSERT_EQ("5.1.0", uagent.browser.toVersionString());

  ASSERT_EQ("iOS", uagent.os.family);
  ASSERT_EQ("5", uagent.os.major);
  ASSERT_EQ("1", uagent.os.minor);
  ASSERT_EQ("1", uagent.os.patch);
  ASSERT_EQ("iOS 5.1.1", uagent.os.toString());
  ASSERT_EQ("5.1.1", uagent.os.toVersionString());

  ASSERT_EQ("Mobile Safari 5.1.0/iOS 5.1.1", uagent.toFullString());

  ASSERT_EQ("iPhone", uagent.device.family);

  ASSERT_FALSE(uagent.isSpider());
}

namespace {

std::string string_field(const YAML::Node& root, const std::string& fname) {
  const auto& yaml_field = root[fname];
  return YAML::IsNull(yaml_field) ? "" : yaml_field.to<std::string>();
}

void test_browser_or_os(const char* file_path, const bool browser) {
  std::ifstream in_stream(file_path);
  CHECK(in_stream.good());
  YAML::Parser yaml_parser(in_stream);
  YAML::Node root;
  CHECK(yaml_parser.GetNextDocument(root));
  const auto& test_cases = root["test_cases"];
  for (const auto& test : test_cases) {
    // TODO(alex): add support for JS user agent
    if (test.FindValue("js_ua")) {
      continue;
    }
    const auto major = string_field(test, "major");
    const auto minor = string_field(test, "minor");
    const auto patch = string_field(test, "patch");
    const auto family = string_field(test, "family");
    const auto unparsed = string_field(test, "user_agent_string");
    const auto uagent = g_ua_parser.parse(unparsed);
    const auto& agent = browser ? uagent.browser : uagent.os;
    ASSERT_EQ(major, agent.major);
    ASSERT_EQ(minor, agent.minor);
    ASSERT_EQ(patch, agent.patch);
    ASSERT_EQ(family, agent.family);
  }
}

void test_device(const char* file_path) {
  std::ifstream in_stream(file_path);
  CHECK(in_stream.good());
  YAML::Parser yaml_parser(in_stream);
  YAML::Node root;
  CHECK(yaml_parser.GetNextDocument(root));
  const auto& test_cases = root["test_cases"];
  for (const auto& test : test_cases) {
    const auto unparsed = string_field(test, "user_agent_string");
    const auto uagent = g_ua_parser.parse(unparsed);
    const auto family = string_field(test, "family");
    ASSERT_EQ(family, uagent.device.family);
  }
}

}  // namespace

TEST(BrowserVersion, test_user_agent_parser) {
  test_browser_or_os("../test_resources/test_user_agent_parser.yaml", true);
}

TEST(BrowserVersion, firefox_user_agent_strings) {
  test_browser_or_os("../test_resources/firefox_user_agent_strings.yaml", true);
}

TEST(BrowserVersion, pgts_browser_list) {
  test_browser_or_os("../test_resources/pgts_browser_list.yaml", true);
}

TEST(OsVersion, test_user_agent_parser_os) {
  test_browser_or_os("../test_resources/test_user_agent_parser_os.yaml", false);
}

TEST(OsVersion, additional_os_tests) {
  test_browser_or_os("../test_resources/additional_os_tests.yaml", false);
}

TEST(DeviceFamily, test_device) {
  test_device("../test_resources/test_device.yaml");
}

}  // namespace

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}