1
0
mirror of https://github.com/BDDSM/YY.EventLogReaderAssistant.git synced 2025-07-16 22:24:15 +02:00

Сделал возможным добавить в тесты внутрение вспомогательные классы

This commit is contained in:
YPermitin
2020-04-12 00:02:19 +05:00
parent e5967a9230
commit 223789c1d9
6 changed files with 68 additions and 153 deletions

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Data.SqlTypes; using System.Data.SqlTypes;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("YY.EventLogAssistant.Tests")]
namespace YY.EventLogAssistant.Services namespace YY.EventLogAssistant.Services
{ {
internal static class DateTimeExtensions internal static class DateTimeExtensions

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("YY.EventLogAssistant.Tests")]
namespace YY.EventLogAssistant.Services namespace YY.EventLogAssistant.Services
{ {
internal static class IntExtensions internal static class IntExtensions

View File

@ -1,7 +1,9 @@
using System; using System;
using System.Data.SQLite; using System.Data.SQLite;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
[assembly: InternalsVisibleTo("YY.EventLogAssistant.Tests")]
namespace YY.EventLogAssistant.Services namespace YY.EventLogAssistant.Services
{ {
internal static class SQLiteExtensions internal static class SQLiteExtensions

View File

@ -1,7 +1,9 @@
using System; using System;
using System.IO; using System.IO;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
[assembly: InternalsVisibleTo("YY.EventLogAssistant.Tests")]
namespace YY.EventLogAssistant.Services namespace YY.EventLogAssistant.Services
{ {
internal class StreamLineReader : IDisposable internal class StreamLineReader : IDisposable

View File

@ -1,6 +1,8 @@
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("YY.EventLogAssistant.Tests")]
namespace YY.EventLogAssistant.Services namespace YY.EventLogAssistant.Services
{ {
internal static class StreamReaderExtensions internal static class StreamReaderExtensions

View File

@ -1,190 +1,15 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
[assembly: InternalsVisibleTo("YY.EventLogAssistant.Tests")]
namespace YY.EventLogAssistant.Services namespace YY.EventLogAssistant.Services
{ {
internal static class StringExtensions internal static class StringExtensions
{ {
public static long From16To10(this string Str) #region Private Member Variables
{
return Convert.ToInt64(Str.ToUpper(), 16);
}
public static string RemoveQuotes(this string s)
{
string functionReturnValue = s;
if (functionReturnValue.StartsWith("\""))
{
functionReturnValue = functionReturnValue.Substring(1);
}
if (functionReturnValue.EndsWith("\""))
{
functionReturnValue = functionReturnValue.Substring(0, functionReturnValue.Length - 1);
}
return functionReturnValue;
}
public static string RemoveBraces(this string sourceString)
{
return sourceString.Replace("}", "").Replace("{", "");
}
public static int ToInt32(this string sourceString)
{
return Convert.ToInt32(sourceString);
}
public static long ToInt64(this string sourceString)
{
return Convert.ToInt64(sourceString);
}
public static string ConvertEncoding(this string s, Encoding source, Encoding result)
{
byte[] souceBytes = source.GetBytes(s);
byte[] resultBytes = Encoding.Convert(source, result, souceBytes);
return result.GetString(resultBytes);
//return result.GetString(source.GetBytes(s));
}
public static string FromAnsiToUTF8(this string s)
{
return ConvertEncoding(s,
Encoding.GetEncoding(1252),
Encoding.UTF8);
}
public static string FromWIN1251ToUTF8(this string s)
{
Encoding win1251 = CodePagesEncodingProvider.Instance.GetEncoding(1251);
return ConvertEncoding(s, win1251, Encoding.UTF8);
}
public static string FromWIN1252ToUTF8(this string s)
{
return ConvertEncoding(s,
Encoding.GetEncoding("windows-1252"),
Encoding.UTF8);
}
public static Guid ToGuid(this string s)
{
Guid guidFromString = Guid.Empty;
Guid.TryParse(s, out guidFromString);
return guidFromString;
}
public static bool IsLike(this string s, string pattern)
{
// Characters matched so far
int matched = 0;
// Loop through pattern string
for (int i = 0; i < pattern.Length; )
{
// Check for end of string
if (matched > s.Length)
return false;
// Get next pattern character
char c = pattern[i++];
if (c == '[') // Character list
{
// Test for exclude character
bool exclude = (i < pattern.Length && pattern[i] == '!');
if (exclude)
i++;
// Build character list
int j = pattern.IndexOf(']', i);
if (j < 0)
j = s.Length;
HashSet<char> charList = CharListToSet(pattern.Substring(i, j - i));
i = j + 1;
if (charList.Contains(s[matched]) == exclude)
return false;
matched++;
}
else if (c == '?') // Any single character
{
matched++;
}
else if (c == '#') // Any single digit
{
if (!Char.IsDigit(s[matched]))
return false;
matched++;
}
else if (c == '*') // Zero or more characters
{
if (i < pattern.Length)
{
// Matches all characters until
// next character in pattern
char next = pattern[i];
int j = s.IndexOf(next, matched);
if (j < 0)
return false;
matched = j;
}
else
{
// Matches all remaining characters
matched = s.Length;
break;
}
}
else // Exact character
{
if (matched >= s.Length || c != s[matched])
return false;
matched++;
}
}
// Return true if all characters matched
return (matched == s.Length);
}
private static HashSet<char> CharListToSet(string charList)
{
HashSet<char> set = new HashSet<char>();
for (int i = 0; i < charList.Length; i++)
{
if ((i + 1) < charList.Length && charList[i + 1] == '-')
{
// Character range
char startChar = charList[i++];
i++; // Hyphen
char endChar = (char)0;
if (i < charList.Length)
endChar = charList[i++];
for (int j = startChar; j <= endChar; j++)
set.Add((char)j);
}
else set.Add(charList[i]);
}
return set;
}
public static string GetHashMD5(this string input)
{
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
sb.Append(hash[i].ToString("X2"));
return sb.ToString();
}
private static readonly Dictionary<int, char> CharactersToMap = new Dictionary<int, char> private static readonly Dictionary<int, char> CharactersToMap = new Dictionary<int, char>
{ {
@ -215,17 +40,97 @@ namespace YY.EventLogAssistant.Services
{173, '-'} {173, '-'}
}; };
public static string ConvertFromWindowsToUnicode(this string txt) #endregion
#region Public Methods
public static long From16To10(this string sourceValue)
{ {
Encoding utf8 = Encoding.GetEncoding("utf-8"); return Convert.ToInt64(sourceValue.ToUpper(), 16);
Encoding win1251 = Encoding.GetEncoding("windows-1252");
byte[] utf8Bytes = win1251.GetBytes(txt);
byte[] win1251Bytes = Encoding.Convert(win1251, utf8, utf8Bytes);
string result = win1251.GetString(win1251Bytes);
return result;
} }
public static string RemoveQuotes(this string sourceValue)
{
string functionReturnValue = sourceValue;
if (functionReturnValue.StartsWith("\""))
{
functionReturnValue = functionReturnValue.Substring(1);
}
if (functionReturnValue.EndsWith("\""))
{
functionReturnValue = functionReturnValue.Substring(0, functionReturnValue.Length - 1);
}
return functionReturnValue;
}
public static string RemoveBraces(this string sourceString)
{
return sourceString.Replace("}", "").Replace("{", "");
}
public static int ToInt32(this string sourceString)
{
return Convert.ToInt32(sourceString);
}
public static long ToInt64(this string sourceString)
{
return Convert.ToInt64(sourceString);
}
public static string FromWin1251ToUTF8(this string sourceValue)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding utf8 = Encoding.GetEncoding("UTF-8");
Encoding win1251 = Encoding.GetEncoding("windows-1251");
return ConvertEncoding(sourceValue, win1251, utf8);
}
public static Guid ToGuid(this string sourceValue)
{
Guid guidFromString = Guid.Empty;
Guid.TryParse(sourceValue, out guidFromString);
return guidFromString;
}
#endregion
#region Private Methods
private static string ConvertEncoding(this string sourceString, Encoding source, Encoding result)
{
byte[] souceBytes = source.GetBytes(sourceString);
byte[] resultBytes = Encoding.Convert(result, source, souceBytes);
return source.GetString(resultBytes);
}
private static HashSet<char> CharListToSet(string charList)
{
HashSet<char> set = new HashSet<char>();
for (int i = 0; i < charList.Length; i++)
{
if ((i + 1) < charList.Length && charList[i + 1] == '-')
{
// Character range
char startChar = charList[i++];
i++; // Hyphen
char endChar = (char)0;
if (i < charList.Length)
endChar = charList[i++];
for (int j = startChar; j <= endChar; j++)
set.Add((char)j);
}
else set.Add(charList[i]);
}
return set;
}
#endregion
} }
} }