1extern crate yaml_rust;
20extern crate regex;
21
22pub mod parser;
23pub mod client;
24pub mod ua;
25pub mod os;
26pub mod device;
27mod result;
28mod yaml;
29
30#[cfg(test)]
31mod test {
32 use parser;
33 use client::Client;
34 use ua::UserAgent;
35 use device::Device;
36 use os::OS;
37 use yaml::*;
38 use yaml_rust::{YamlLoader, Yaml};
39 use std::io::prelude::*;
40 use std::fs::{File};
41
42 #[test]
43 fn basic_au_test() {
44 let agent = "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".to_string();
45 let p = parser::Parser::new().unwrap();
46 let c = p.parse(agent);
47 println!("{:?}",c);
48 assert_eq!(Client {
49 user_agent: UserAgent {
50 family: "Mobile Safari".to_string(),
51 major: Some("5".to_string()),
52 minor: Some("1".to_string()),
53 patch: None,
54 },
55 device: Device {
56 family: "iPhone".to_string(),
57 brand: Some("Apple".to_string()),
58 model: Some("iPhone".to_string()),
59 regex: Some("(iPhone)(?:;| Simulator;)".to_string()),
60 },
61 os: OS {
62 family: "iOS".to_string(),
63 major: Some("5".to_string()),
64 minor: Some("1".to_string()),
65 patch: Some("1".to_string()),
66 patch_minor: None,
67 }
68 }, c);
69 }
70
71 #[test]
72 fn test_device() {
73 let p = parser::Parser::new().unwrap();
74 assert!(p.devices_regex.len() > 0);
75 let mut test_file = File::open("src/uap-core/tests/test_device.yaml").unwrap();
76 let mut yaml_str = String::new();
77 let _ = test_file.read_to_string(&mut yaml_str).unwrap();
78 let y = YamlLoader::load_from_str(&yaml_str).unwrap();
79 let cases = from_map(&y[0], "test_cases").unwrap();
80 let failed = filter_map_over_arr(cases, |c| {
81 let uas = from_map(c, "user_agent_string").unwrap().as_str().unwrap();
82 let client = p.parse(uas.to_string());
83
84 let family = compare_with_parsed(&Some(client.device.family.clone()), "family", c, &client);
85 if family.is_some() {
86 return family;
87 }
88
89 let brand = compare_with_parsed(&client.device.brand, "brand", c, &client);
90 if brand.is_some() {
91 return brand;
92 }
93
94 let model = compare_with_parsed(&client.device.model, "model", c, &client);
95 if model.is_some() {
96 return model;
97 }
98 None
99 });
100
101 assert_eq!(0, failed.len());
106 }
107
108 #[test]
109 fn test_user_agent() {
110 let p = parser::Parser::new().unwrap();
111 assert!(p.devices_regex.len() > 0);
112 let mut test_file = File::open("src/uap-core/tests/test_ua.yaml").unwrap();
113 let mut yaml_str = String::new();
114 let _ = test_file.read_to_string(&mut yaml_str).unwrap();
115 let y = YamlLoader::load_from_str(&yaml_str).unwrap();
116 let cases = from_map(&y[0], "test_cases").unwrap();
117 let failed = filter_map_over_arr(cases, |c| {
118 let uas = from_map(c, "user_agent_string").unwrap().as_str().unwrap();
119 let client = p.parse(uas.to_string());
120
121 let family = compare_with_parsed(&Some(client.user_agent.family.clone()), "family", c, &client);
122 if family.is_some() {
123 return family;
124 }
125
126 let major = compare_with_parsed(&client.user_agent.major, "major", c, &client);
127 if major.is_some() {
128 return major;
129 }
130
131 let minor = compare_with_parsed(&client.user_agent.minor, "minor", c, &client);
132 if minor.is_some() {
133 return minor;
134 }
135
136 let patch = compare_with_parsed(&client.user_agent.patch, "patch", c, &client);
137 if patch.is_some() {
138 return patch;
139 }
140
141 None
142 });
143
144 assert_eq!(0, failed.len());
149 }
150
151 #[test]
152 fn test_os() {
153 let p = parser::Parser::new().unwrap();
154 assert!(p.devices_regex.len() > 0);
155 let mut test_file = File::open("src/uap-core/tests/test_os.yaml").unwrap();
156 let mut yaml_str = String::new();
157 let _ = test_file.read_to_string(&mut yaml_str).unwrap();
158 let y = YamlLoader::load_from_str(&yaml_str).unwrap();
159 let cases = from_map(&y[0], "test_cases").unwrap();
160 let failed = filter_map_over_arr(cases, |c| {
161 let uas = from_map(c, "user_agent_string").unwrap().as_str().unwrap();
162 let client = p.parse(uas.to_string());
163
164 let family = compare_with_parsed(&Some(client.os.family.clone()), "family", c, &client);
165 if family.is_some() {
166 return family;
167 }
168
169 let major = compare_with_parsed(&client.os.major, "major", c, &client);
170 if major.is_some() {
171 return major;
172 }
173
174 let minor = compare_with_parsed(&client.os.minor, "minor", c, &client);
175 if minor.is_some() {
176 return minor;
177 }
178
179 let patch = compare_with_parsed(&client.os.patch, "patch", c, &client);
180 if patch.is_some() {
181 return patch;
182 }
183
184 let patch_minor = compare_with_parsed(&client.os.patch_minor, "patch_minor", c, &client);
185 if patch_minor.is_some() {
186 return patch_minor;
187 }
188
189 None
190 });
191
192 assert_eq!(0, failed.len());
197 }
198
199 fn compare_with_parsed(actual: &Option<String>, mapkey: &str, c: &Yaml, client: &Client) -> Option<String> {
200 let opt = from_map(c, mapkey).unwrap();
201 if !opt.is_null() {
202 let value = opt.as_str().unwrap().to_string();
203 let parsed = actual.clone().unwrap_or(String::new());
204 if parsed != value {
205 return Some(format!("{} does not match: {:?}, actual: {:?}", mapkey, c, client));
206 }
207 }
208 None
209 }
210
211}