Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions UAParser.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
namespace UAParser.ConsoleApp
{
using System;
using System.Linq;
using System;
using System.Linq;

static class Program
{
static void Main(string[] args)
static class Program
{
if (args.Any(arg => arg == "-?" || arg == "-h" || arg == "--help"))
{
Help();
return;
}
static void Main(string[] args)
{
if (args.Any(arg => arg == "-?" || arg == "-h" || arg == "--help"))
{
Help();
return;
}

var uaParser = Parser.GetDefault();
string uaString;
while ((uaString = Console.In.ReadLine()) != null)
{
uaString = uaString.Trim();
if (uaString.Length == 0)
continue;
var c = uaParser.Parse(uaString);
Console.WriteLine("Agent : {0}", c.UA);
Console.WriteLine("OS : {0}", c.OS);
Console.WriteLine("Device: {0}", c.Device);
}
}
var uaParser = Parser.GetDefault();
string uaString;
while ((uaString = Console.In.ReadLine()) != null)
{
uaString = uaString.Trim();
if (uaString.Length == 0)
continue;
var c = uaParser.Parse(uaString);
Console.WriteLine("Agent : {0}", c.UA);
Console.WriteLine("OS : {0}", c.OS);
Console.WriteLine("Device: {0}", c.Device);
}
}

static void Help()
{
static void Help()
{
Console.WriteLine(@"UAParser
Copyright 2015 " + "S\u00f8ren Enem\u00e6rke" + @"
https://github.com/tobie/ua-parser

This application accepts user agent strings (one per line) from standard
input, parses them and then emits the identified agent, operating system and
device for each string.");
}
}
}
}
139 changes: 70 additions & 69 deletions UAParser.Tests/DeviceYamlTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,95 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UAParser.Implementations;
using Xunit;

namespace UAParser.Tests
{
public class DeviceYamlTestCase : YamlTestCase
{
public static DeviceYamlTestCase ReadFromMap(Dictionary<string, string> map)
public class DeviceYamlTestCase : YamlTestCase
{
DeviceYamlTestCase tc = new DeviceYamlTestCase()
{
UserAgent = map["user_agent_string"],
Family = map["family"],
public static DeviceYamlTestCase ReadFromMap(Dictionary<string, string> map)
{
DeviceYamlTestCase tc = new DeviceYamlTestCase()
{
UserAgent = map["user_agent_string"],
Family = map["family"],

};
return tc;
}
};
return tc;
}

public string Family { get; set; }
public string Family { get; set; }

public override void Verify(ClientInfo clientInfo)
{
Assert.NotNull(clientInfo);
AssertMatch(Family,clientInfo.Device.Family,"Family");
public override void Verify(ClientInfo clientInfo)
{
Assert.NotNull(clientInfo);
AssertMatch(Family, clientInfo.Device.Family, "Family");
}
}
}

public class OSYamlTestCase : YamlTestCase
{
public static OSYamlTestCase ReadFromMap(Dictionary<string, string> map)
public class OSYamlTestCase : YamlTestCase
{
OSYamlTestCase tc = new OSYamlTestCase()
{
UserAgent = map["user_agent_string"],
Family = map["family"],
Major = map["major"],
Minor = map["minor"],
Patch = map["patch"],
PatchMinor = map["patch_minor"]
};
return tc;
}
public static OSYamlTestCase ReadFromMap(Dictionary<string, string> map)
{
OSYamlTestCase tc = new OSYamlTestCase()
{
UserAgent = map["user_agent_string"],
Family = map["family"],
Major = map["major"],
Minor = map["minor"],
Patch = map["patch"],
PatchMinor = map["patch_minor"]
};
return tc;
}

public string Family { get; set; }
public string Major { get; set; }
public string Minor { get; set; }
public string Patch { get; set; }
public string PatchMinor { get; set; }
public string Family { get; set; }
public string Major { get; set; }
public string Minor { get; set; }
public string Patch { get; set; }
public string PatchMinor { get; set; }

public override void Verify(ClientInfo clientInfo)
{
Assert.NotNull(clientInfo);
AssertMatch(Family, clientInfo.OS.Family, "Family");
AssertMatch(Major, clientInfo.OS.Major, "Major");
AssertMatch(Minor, clientInfo.OS.Minor, "Minor");
AssertMatch(Patch, clientInfo.OS.Patch, "Patch");
AssertMatch(PatchMinor, clientInfo.OS.PatchMinor, "PatchMinor");
public override void Verify(ClientInfo clientInfo)
{
Assert.NotNull(clientInfo);
AssertMatch(Family, clientInfo.OS.Family, "Family");
AssertMatch(Major, clientInfo.OS.Major, "Major");
AssertMatch(Minor, clientInfo.OS.Minor, "Minor");
AssertMatch(Patch, clientInfo.OS.Patch, "Patch");
AssertMatch(PatchMinor, clientInfo.OS.PatchMinor, "PatchMinor");

}
}
}

public class UserAgentYamlTestCase : YamlTestCase
{
public static UserAgentYamlTestCase ReadFromMap(Dictionary<string, string> map)
public class UserAgentYamlTestCase : YamlTestCase
{
UserAgentYamlTestCase tc = new UserAgentYamlTestCase()
{
UserAgent = map["user_agent_string"],
Family = map["family"],
Major = map["major"],
Minor = map["minor"],
Patch = map["patch"],
};
return tc;
}
public static UserAgentYamlTestCase ReadFromMap(Dictionary<string, string> map)
{
UserAgentYamlTestCase tc = new UserAgentYamlTestCase()
{
UserAgent = map["user_agent_string"],
Family = map["family"],
Major = map["major"],
Minor = map["minor"],
Patch = map["patch"],
};
return tc;
}

public string Family { get; set; }
public string Major { get; set; }
public string Minor { get; set; }
public string Patch { get; set; }
public string Family { get; set; }
public string Major { get; set; }
public string Minor { get; set; }
public string Patch { get; set; }

public override void Verify(ClientInfo clientInfo)
{
Assert.NotNull(clientInfo);
AssertMatch(Family, clientInfo.UA.Family, "Family");
AssertMatch(Major, clientInfo.UA.Major, "Major");
AssertMatch(Minor, clientInfo.UA.Minor, "Minor");
AssertMatch(Patch, clientInfo.UA.Patch, "Patch");
public override void Verify(ClientInfo clientInfo)
{
Assert.NotNull(clientInfo);
AssertMatch(Family, clientInfo.UA.Family, "Family");
AssertMatch(Major, clientInfo.UA.Major, "Major");
AssertMatch(Minor, clientInfo.UA.Minor, "Minor");
AssertMatch(Patch, clientInfo.UA.Patch, "Patch");

}
}
}
}
12 changes: 6 additions & 6 deletions UAParser.Tests/InternalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ internal static List<Dictionary<string, string>> ConvertToDictionaryList(this Ya
}
internal static Dictionary<string, string> ConvertToDictionary(this YamlMappingNode yamlNode)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (var key in yamlNode.Children.Keys)
{
dic[key.ToString()] = yamlNode.Children[key].ToString();
}
return dic;
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (var key in yamlNode.Children.Keys)
{
dic[key.ToString()] = yamlNode.Children[key].ToString();
}
return dic;
}
internal static string GetTestResources(this object self, string name)
{
Expand Down
2 changes: 1 addition & 1 deletion UAParser.Tests/TestResourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public List<TTestCase> GetTestCases<TTestCase>(
.Select(configMap =>
{
if (!configMap.ContainsKey("js_ua")) //we deliberately skip tests with js-user agents
return testCaseFunction(configMap);
return testCaseFunction(configMap);
return default(TTestCase);
})
.ToList();
Expand Down
14 changes: 7 additions & 7 deletions UAParser.Tests/YamlParsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ from e in rn.Children
{
var configNode = kvPair.Value;
var valueDic = from node in configNode ?? Enumerable.Empty<YamlNode>()
select node as YamlMappingNode
select node as YamlMappingNode
into node
where node != null
select node.Children
.Where(e => e.Key is YamlScalarNode && e.Value is YamlScalarNode)
.GroupBy(e => e.Key.ToString(), e => e.Value.ToString(), StringComparer.OrdinalIgnoreCase)
.ToDictionary(e => e.Key, e => e.Last(), StringComparer.OrdinalIgnoreCase)
where node != null
select node.Children
.Where(e => e.Key is YamlScalarNode && e.Value is YamlScalarNode)
.GroupBy(e => e.Key.ToString(), e => e.Value.ToString(), StringComparer.OrdinalIgnoreCase)
.ToDictionary(e => e.Key, e => e.Last(), StringComparer.OrdinalIgnoreCase)
into cm
select cm;
select cm;

string name = kvPair.Key;
var minimalLookupList = minimal.ReadMapping(name).ToList();
Expand Down
31 changes: 16 additions & 15 deletions UAParser.Tests/YamlTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UAParser.Implementations;
using Xunit;

namespace UAParser.Tests
{
public abstract class YamlTestCase
{
public string UserAgent { get; set; }
public abstract void Verify(ClientInfo clientInfo);

protected void AssertMatch<T>(T expected, T actual, string type)
public abstract class YamlTestCase
{
if (typeof(T) == typeof(string))
{
string exp = expected as string;
string act = actual as string;
public string UserAgent { get; set; }
public abstract void Verify(ClientInfo clientInfo);

protected void AssertMatch<T>(T expected, T actual, string type)
{
if (typeof(T) == typeof(string))
{
string exp = expected as string;
string act = actual as string;

if (string.IsNullOrEmpty(exp) && string.IsNullOrEmpty(act))
return;
}
if (string.IsNullOrEmpty(exp) && string.IsNullOrEmpty(act))
return;
}

Assert.True(expected.Equals(actual), type+" did not match. (expected:" + expected + " actual:" + actual + ") in " + UserAgent);
Assert.True(expected.Equals(actual), type + " did not match. (expected:" + expected + " actual:" + actual + ") in " + UserAgent);
}
}
}
}
50 changes: 50 additions & 0 deletions UAParser/Abstractions/IUAParserOutput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#region Apache License, Version 2.0
//
// Copyright 2014 Atif Aziz
// Portions Copyright 2012 Søren Enemærke
//
// 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.
//
#endregion


namespace UAParser.Abstractions
{
/// <summary>
/// Representing the parse results. Structure of this class aligns with the
/// ua-parser-output WebIDL structure defined in this document: https://github.com/ua-parser/uap-core/blob/master/docs/specification.md
/// </summary>
public interface IUAParserOutput
{
/// <summary>
/// The user agent string, the input for the UAParser
/// </summary>
string String { get; }

/// <summary>
/// The OS parsed from the user agent string
/// </summary>
// ReSharper disable once InconsistentNaming
OS OS { get; }
/// <summary>
/// The Device parsed from the user agent string
/// </summary>
Device Device { get; }
// ReSharper disable once InconsistentNaming
/// <summary>
/// The User Agent parsed from the user agent string
/// </summary>
UserAgent UA { get; }
}

}
Loading