-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathTestResourceTests.cs
More file actions
116 lines (102 loc) · 3.78 KB
/
Copy pathTestResourceTests.cs
File metadata and controls
116 lines (102 loc) · 3.78 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;
using Xunit.Sdk;
using YamlDotNet.RepresentationModel;
namespace UAParser.Tests
{
public class TestResourceTests
{
[Fact]
public void can_run_device_tests()
{
RunTests("UAParser.Tests.TestResources.test_device.yaml", DeviceYamlTestCase.ReadFromMap);
}
[Fact]
public void can_run_additional_os_tests()
{
RunTests("UAParser.Tests.TestResources.additional_os_tests.yaml", OSYamlTestCase.ReadFromMap);
}
[Fact]
public void can_run_firefox_user_agent_string_tests()
{
RunTests("UAParser.Tests.TestResources.firefox_user_agent_strings.yaml", UserAgentYamlTestCase.ReadFromMap);
}
[Fact]
public void can_run_pgts_browser_list_tests()
{
RunTests("UAParser.Tests.TestResources.pgts_browser_list.yaml", UserAgentYamlTestCase.ReadFromMap);
}
[Fact]
public void can_run_user_agent_parser_tests()
{
RunTests("UAParser.Tests.TestResources.test_ua.yaml", UserAgentYamlTestCase.ReadFromMap);
}
[Fact]
public void can_run_user_agent_parser_os_tests()
{
RunTests("UAParser.Tests.TestResources.test_os.yaml", OSYamlTestCase.ReadFromMap);
}
internal void RunTests<TTestCase>(
string resourceName,
Func<Dictionary<string, string>, TTestCase> testCaseFunction) where TTestCase : YamlTestCase
{
List<TTestCase> testCases = GetTestCases(
resourceName,
"test_cases",
testCaseFunction);
RunTestCases(testCases);
}
private static void RunTestCases<TTestCase>(List<TTestCase> testCases) where TTestCase : YamlTestCase
{
Parser parser = Parser.GetDefault();
Assert.NotEmpty(testCases);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < testCases.Count; i++)
{
var tc = testCases[i];
if (tc == null)
continue;
var clientInfo = parser.Parse(tc.UserAgent);
try
{
tc.Verify(clientInfo);
}
catch (Exception ex)
{
sb.AppendLine($"test case {(i + 1)}: {ex.Message}");
}
}
Assert.True(0 == sb.Length, "Failed tests: " + Environment.NewLine + sb);
}
public List<TTestCase> GetTestCases<TTestCase>(
string resourceName,
string yamlNodeName,
Func<Dictionary<string, string>, TTestCase> testCaseFunction)
{
string yamlContent = this.GetTestResources(resourceName);
YamlStream yaml = new YamlStream();
yaml.Load(new StringReader(yamlContent));
//reading overall configurations
var regexConfigNode = (YamlMappingNode)yaml.Documents[0].RootNode;
var regexConfig = new Dictionary<string, YamlNode>();
foreach (var entry in regexConfigNode.Children)
{
regexConfig.Add(((YamlScalarNode)entry.Key).Value, entry.Value);
}
var testCasesNode = (YamlSequenceNode)regexConfig[yamlNodeName];
List<TTestCase> testCases = testCasesNode.ConvertToDictionaryList()
.Select(configMap =>
{
if (!configMap.ContainsKey("js_ua")) //we deliberately skip tests with js-user agents
return testCaseFunction(configMap);
return default(TTestCase);
})
.ToList();
return testCases;
}
}
}