Skip to content
Permalink
Browse files

修改模拟的值提供器

  • Loading branch information...
Kation committed Jan 16, 2018
1 parent 045c626 commit 632b096dffee89383945e5008d456989b0852874
Showing with 73 additions and 11 deletions.
  1. +70 −0 src/Wodsoft.ComBoost.Mock/MockValueKeyCollection.cs
  2. +3 −11 src/Wodsoft.ComBoost.Mock/MockValueProvider.cs
@@ -0,0 +1,70 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wodsoft.ComBoost.Mock
{
public class MockValueKeyCollection : ValueKeyCollection
{
private Dictionary<string, string> lowerKeys;

public MockValueKeyCollection(IEnumerable<string> keys, bool ignoreCase) : base(keys)
{
if (ignoreCase)
{
lowerKeys = new Dictionary<string, string>();
foreach (var key in keys)
{
var lower = key.ToLower();
if (lowerKeys.ContainsKey(lower))
continue;
lowerKeys.Add(lower, key);
}
}
}

public override bool ContainsKey(string key)
{
if (lowerKeys != null)
return lowerKeys.ContainsKey(key.ToLower());
return base.ContainsKey(key);
}

public override bool ContainsPrefix(string prefix, params char[] separators)
{
if (lowerKeys != null)
{
prefix = prefix.ToLower();
return lowerKeys.Keys.Any(t => t == prefix || separators.Any(x => t.StartsWith(prefix + x)));
}
return base.ContainsPrefix(prefix, separators);
}

public override IDictionary<string, string> GetKeysFromPrefix(string prefix, params char[] separators)
{
if (lowerKeys != null)
{
prefix = prefix.ToLower();
var data = lowerKeys.Where(t => separators.Any(x => t.Key.StartsWith(prefix + x))).ToDictionary(t =>
{
var text = t.Value.Substring(prefix.Length);
int i = text.IndexOfAny(separators, 1);
if (i == -1)
return text.Substring(1);
return text.Substring(1, i - 1);
}, t =>
{
var text = t.Value.Substring(prefix.Length);
int i = text.IndexOfAny(separators, 1);
if (i == -1)
return t.Value;
return t.Value.Substring(0, prefix.Length) + text.Substring(0, i);
});
return data;
}
return base.GetKeysFromPrefix(prefix, separators);
}
}
}
@@ -10,28 +10,20 @@ namespace Wodsoft.ComBoost.Mock
{
public class MockValueProvider : EmptyValueProvider
{
private Dictionary<string, object> _Values;

public MockValueProvider()
{
_Values = new Dictionary<string, object>();
IgnoreCase = false;
}

public bool IgnoreCase { get; set; }

private ReadOnlyCollection<string> _Keys;
public override IReadOnlyCollection<string> Keys
private MockValueKeyCollection _Keys;
public override IValueKeyCollection Keys
{
get
{
if (_Keys == null)
{
IEnumerable<string> keys = base.Keys;
if (IgnoreCase)
keys = keys.Select(t => t.ToLower());
_Keys = new ReadOnlyCollection<string>(keys.Distinct().ToList());
}
_Keys = new MockValueKeyCollection(base.Keys, IgnoreCase);
return _Keys;
}
}

0 comments on commit 632b096

Please sign in to comment.
You can’t perform that action at this time.