forked from ua-parser/uap-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ua_parser_test.cpp
161 lines (125 loc) · 5.13 KB
/
ua_parser_test.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
# Copyright 2013 Andrew Punch
#
# Licensed under the Apache License, Version 2.0 (the 'License')
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
*/
#include <string>
#include <fstream>
#include <gtest/gtest.h>
#include "ua_parser.h"
static const std::string TEST_RESOURCES_DIR="../test_resources/";
class UaParserTest : public testing::Test {
protected:
bool yaml_isnull(const YAML::Node &node) {
return node.Type()==YAML::NodeType::Null;
}
void runUserAgentTestsFromYAML(const std::string &file_name) {
std::ifstream in(file_name.c_str());
if (!in.good()) {
FAIL() << "Could not open YAML file" << file_name;
return;
}
YAML::Parser parser(in);
YAML::Node doc;
parser.GetNextDocument(doc);
const YAML::Node &test_cases = doc["test_cases"];
for (YAML::Iterator i=test_cases.begin(); i!=test_cases.end(); i++) {
// Inputs to Parse()
const YAML::Node& test_case = *i;
std::string user_agent_string = test_case["user_agent_string"].to<std::string>();
// The expected results
ua_parser::Browser expected;
if (!yaml_isnull(test_case["family"])) expected.family = test_case["family"].to<std::string>();
if (!yaml_isnull(test_case["major"])) expected.major = test_case["major"].to<std::string>();
if (!yaml_isnull(test_case["minor"])) expected.minor = test_case["minor"].to<std::string>();
if (!yaml_isnull(test_case["patch"])) expected.patch = test_case["patch"].to<std::string>();
// js_ua not supported
if (test_case.FindValue("js_ua"))
continue;
ua_parser::Parser user_agent_parser("../regexes.yaml");
ua_parser::UserAgent result = user_agent_parser.Parse(user_agent_string);
EXPECT_EQ(expected, result.browser)<<user_agent_string;
}
}
void runOSTestsFromYAML(const std::string &file_name) {
std::ifstream in(file_name.c_str());
if (!in.good()) {
FAIL() << "Could not open YAML file" << file_name;
return;
}
YAML::Parser parser(in);
YAML::Node doc;
parser.GetNextDocument(doc);
const YAML::Node &test_cases = doc["test_cases"];
for (YAML::Iterator i=test_cases.begin(); i!=test_cases.end(); i++) {
// Inputs to Parse()
const YAML::Node &test_case = *i;
std::string user_agent_string = test_case["user_agent_string"].to<std::string>();
// The expected results
ua_parser::OperatingSystem expected;
if (!yaml_isnull(test_case["family"])) expected.os = test_case["family"].to<std::string>();
if (!yaml_isnull(test_case["major"])) expected.major = test_case["major"].to<std::string>();
if (!yaml_isnull(test_case["minor"])) expected.minor = test_case["minor"].to<std::string>();
if (!yaml_isnull(test_case["patch"])) expected.patch = test_case["patch"].to<std::string>();
if (!yaml_isnull(test_case["patch_minor"])) expected.patch_minor = test_case["patch_minor"].to<std::string>();
ua_parser::Parser user_agent_parser("../regexes.yaml");
ua_parser::UserAgent result = user_agent_parser.Parse(user_agent_string);
EXPECT_EQ(result.os, expected) << user_agent_string;
}
}
void runDeviceTestsFromYAML(const std::string &file_name) {
std::ifstream in(file_name.c_str());
if (!in.good()) {
FAIL() << "Could not open YAML file" << file_name;
return;
}
YAML::Parser parser(in);
YAML::Node doc;
parser.GetNextDocument(doc);
const YAML::Node &test_cases = doc["test_cases"];
for (YAML::Iterator i=test_cases.begin(); i!=test_cases.end(); i++) {
const YAML::Node &test_case = *i;
// Inputs to Parse()
std::string user_agent_string = test_case["user_agent_string"].to<std::string>();
// The expected results
ua_parser::Device expected = test_case["family"].to<std::string>();
ua_parser::Parser user_agent_parser("../regexes.yaml");
ua_parser::UserAgent result = user_agent_parser.Parse(user_agent_string);
EXPECT_EQ(result.device, expected)<<user_agent_string;
}
}
};
TEST_F(UaParserTest, TestBrowserscopeStrings) {
runUserAgentTestsFromYAML(
TEST_RESOURCES_DIR+"test_user_agent_parser.yaml");
}
TEST_F(UaParserTest, TestBrowserscopeStringsOS) {
runOSTestsFromYAML(
TEST_RESOURCES_DIR+"test_user_agent_parser_os.yaml");
}
TEST_F(UaParserTest, TestStringsOS) {
runOSTestsFromYAML(
TEST_RESOURCES_DIR+"additional_os_tests.yaml");
}
TEST_F(UaParserTest, TestStringsDevice) {
runDeviceTestsFromYAML(
TEST_RESOURCES_DIR+"test_device.yaml");
}
TEST_F(UaParserTest, TestMozillaStrings) {
runUserAgentTestsFromYAML(
TEST_RESOURCES_DIR+"firefox_user_agent_strings.yaml");
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}