Skip to content

Commit

Permalink
Bogus: use file-scoped namespaces. (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Dec 24, 2023
1 parent f93dc16 commit 14b2e50
Show file tree
Hide file tree
Showing 75 changed files with 8,659 additions and 8,735 deletions.
183 changes: 91 additions & 92 deletions Source/Bogus/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,117 +5,116 @@
using System.Runtime.CompilerServices;
using Bogus.Platform;

namespace Bogus
namespace Bogus;

/// <summary>
/// A binder is used in Faker[T] for extracting MemberInfo from T
/// that are candidates for property/field faking.
/// </summary>
public interface IBinder
{
/// <summary>
/// A binder is used in Faker[T] for extracting MemberInfo from T
/// that are candidates for property/field faking.
/// Given T, the method must return a Dictionary[string,MemberInfo] where
/// string is the field/property name and MemberInfo is the reflected
/// member info of the field/property that will be used for invoking
/// and setting values. The returned Dictionary must encompass the full
/// set of viable properties/fields that can be faked on T.
/// </summary>
/// <returns>The full set of MemberInfos for injection.</returns>
Dictionary<string, MemberInfo> GetMembers(Type t);
}

/// <summary>
/// The default binder used in Faker[T] for extracting MemberInfo from T
/// that are candidates for property/field faking.
/// </summary>
public class Binder : IBinder
{
/// <summary>
/// The binding flags to use when reflecting over T.
/// </summary>
protected internal BindingFlags BindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;

/// <summary>
/// Construct a binder with default binding flags. Public/internal properties and public/internal fields.
/// </summary>
public interface IBinder
public Binder()
{
/// <summary>
/// Given T, the method must return a Dictionary[string,MemberInfo] where
/// string is the field/property name and MemberInfo is the reflected
/// member info of the field/property that will be used for invoking
/// and setting values. The returned Dictionary must encompass the full
/// set of viable properties/fields that can be faked on T.
/// </summary>
/// <returns>The full set of MemberInfos for injection.</returns>
Dictionary<string, MemberInfo> GetMembers(Type t);
}

/// <summary>
/// The default binder used in Faker[T] for extracting MemberInfo from T
/// that are candidates for property/field faking.
/// Construct a binder with custom binding flags.
/// </summary>
public class Binder : IBinder
public Binder(BindingFlags bindingFlags)
{
/// <summary>
/// The binding flags to use when reflecting over T.
/// </summary>
protected internal BindingFlags BindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;

/// <summary>
/// Construct a binder with default binding flags. Public/internal properties and public/internal fields.
/// </summary>
public Binder()
{
}

/// <summary>
/// Construct a binder with custom binding flags.
/// </summary>
public Binder(BindingFlags bindingFlags)
{
BindingFlags = bindingFlags;
}

BindingFlags = bindingFlags;
}


/// <summary>
/// Given T, the method will return a Dictionary[string,MemberInfo] where
/// string is the field/property name and MemberInfo is the reflected
/// member info of the field/property that will be used for invocation
/// and setting values. The returned Dictionary must encompass the full
/// set of viable properties/fields that can be faked on T.
/// </summary>
/// <returns>The full set of MemberInfos for injection.</returns>
public virtual Dictionary<string, MemberInfo> GetMembers(Type t)
{
var allReflectedMembers = t.GetAllMembers(this.BindingFlags)
.Select(m => UseBaseTypeDeclaredPropertyInfo(t, m));
/// <summary>
/// Given T, the method will return a Dictionary[string,MemberInfo] where
/// string is the field/property name and MemberInfo is the reflected
/// member info of the field/property that will be used for invocation
/// and setting values. The returned Dictionary must encompass the full
/// set of viable properties/fields that can be faked on T.
/// </summary>
/// <returns>The full set of MemberInfos for injection.</returns>
public virtual Dictionary<string, MemberInfo> GetMembers(Type t)
{
var allReflectedMembers = t.GetAllMembers(this.BindingFlags)
.Select(m => UseBaseTypeDeclaredPropertyInfo(t, m));

var settableMembers = allReflectedMembers
.Where(m =>
var settableMembers = allReflectedMembers
.Where(m =>
{
if( m.GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Any() )
{
if( m.GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Any() )
{
//no compiler generated stuff
return false;
}
//no compiler generated stuff
return false;
}
if( m is PropertyInfo pi )
{
return pi.CanWrite;
}
if( m is PropertyInfo pi )
{
return pi.CanWrite;
}
if( m is FieldInfo fi )
{
//No private fields.
//GitHub Issue #13
return !fi.IsPrivate;
}
if( m is FieldInfo fi )
{
//No private fields.
//GitHub Issue #13
return !fi.IsPrivate;
}
return false;
});
return false;
});

var settableMembersByName = settableMembers.GroupBy(mi => mi.Name);
var settableMembersByName = settableMembers.GroupBy(mi => mi.Name);

//Issue #70 we could get back multiple keys
//when reflecting over a type. Consider:
//
// ClassA { public int Value {get;set} }
// DerivedA : ClassA { public new int Value {get;set;} }
//
//So, when reflecting over DerivedA, grab the first
//reflected MemberInfo that was returned from
//reflection; the second one was the inherited
//ClassA.Value.
return settableMembersByName.ToDictionary(k => k.Key, g => g.First());
}
//Issue #70 we could get back multiple keys
//when reflecting over a type. Consider:
//
// ClassA { public int Value {get;set} }
// DerivedA : ClassA { public new int Value {get;set;} }
//
//So, when reflecting over DerivedA, grab the first
//reflected MemberInfo that was returned from
//reflection; the second one was the inherited
//ClassA.Value.
return settableMembersByName.ToDictionary(k => k.Key, g => g.First());
}

//Issue #389 - Use Declaring Base Type PropertyInfo instead of a DerivedA's
//PropertyInfo because DerivedA's PropertyInfo could say property is not
//write-able.
protected virtual MemberInfo UseBaseTypeDeclaredPropertyInfo(Type t, MemberInfo m)
//Issue #389 - Use Declaring Base Type PropertyInfo instead of a DerivedA's
//PropertyInfo because DerivedA's PropertyInfo could say property is not
//write-able.
protected virtual MemberInfo UseBaseTypeDeclaredPropertyInfo(Type t, MemberInfo m)
{
if( m is PropertyInfo {CanWrite: false} && m.DeclaringType is not null && m.DeclaringType != t )
{
if( m is PropertyInfo {CanWrite: false} && m.DeclaringType is not null && m.DeclaringType != t )
{
var newPropInfo = m.DeclaringType.GetProperty(m.Name, this.BindingFlags);
if( newPropInfo is not null )
return newPropInfo;
}

return m;
var newPropInfo = m.DeclaringType.GetProperty(m.Name, this.BindingFlags);
if( newPropInfo is not null )
return newPropInfo;
}

return m;
}
}
27 changes: 13 additions & 14 deletions Source/Bogus/BogusException.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using System;

namespace Bogus
namespace Bogus;

/// <summary>
/// General exception for Bogus.
/// </summary>
public class BogusException : Exception
{
/// <summary>
/// General exception for Bogus.
/// </summary>
public class BogusException : Exception
public BogusException()
{
public BogusException()
{
}
}

public BogusException(string message) : base(message)
{
}
public BogusException(string message) : base(message)
{
}

public BogusException(string message, Exception innerException) : base(message, innerException)
{
}
public BogusException(string message, Exception innerException) : base(message, innerException)
{
}
}
49 changes: 24 additions & 25 deletions Source/Bogus/Bson/BArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,42 @@
using System.Collections;
using System.Collections.Generic;

namespace Bogus.Bson
namespace Bogus.Bson;

public class BArray : BValue, IEnumerable
{
public class BArray : BValue, IEnumerable
{
private readonly List<BValue> items = new List<BValue>();
private readonly List<BValue> items = new List<BValue>();

public BArray() : base(BValueType.Array)
{
}
public BArray() : base(BValueType.Array)
{
}

public override BValue this[int index]
{
get => items[index];
set => items[index] = value;
}
public override BValue this[int index]
{
get => items[index];
set => items[index] = value;
}

public bool HasValues => items.Count > 0;
public bool HasValues => items.Count > 0;

public int Count => items.Count;
public int Count => items.Count;

public override void Add(BValue v) => items.Add(v);
public override void Add(BValue v) => items.Add(v);

public int IndexOf(BValue item) => items.IndexOf(item);
public int IndexOf(BValue item) => items.IndexOf(item);

public void Insert(int index, BValue item) => items.Insert(index, item);
public void Insert(int index, BValue item) => items.Insert(index, item);

public bool Remove(BValue v) => items.Remove(v);
public bool Remove(BValue v) => items.Remove(v);

public void RemoveAt(int index) => items.RemoveAt(index);
public void RemoveAt(int index) => items.RemoveAt(index);

public override void Clear() => items.Clear();
public override void Clear() => items.Clear();

public virtual bool Contains(BValue v) => items.Contains(v);
public virtual bool Contains(BValue v) => items.Contains(v);

IEnumerator IEnumerable.GetEnumerator()
{
return items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return items.GetEnumerator();
}
}
53 changes: 26 additions & 27 deletions Source/Bogus/Bson/BObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,47 @@
using System.Collections;
using System.Collections.Generic;

namespace Bogus.Bson
namespace Bogus.Bson;

public class BObject : BValue, IEnumerable
{
public class BObject : BValue, IEnumerable
{
private Dictionary<string, BValue> map = new Dictionary<string, BValue>();
private Dictionary<string, BValue> map = new Dictionary<string, BValue>();

public BObject() : base(BValueType.Object)
{
}
public BObject() : base(BValueType.Object)
{
}

public ICollection<string> Keys => map.Keys;
public ICollection<string> Keys => map.Keys;

public ICollection<BValue> Values => map.Values;
public ICollection<BValue> Values => map.Values;

public int Count => map.Count;
public int Count => map.Count;

public override BValue this[string key]
public override BValue this[string key]
{
get
{
get
{
map.TryGetValue(key, out BValue val);
return val;
}
set => map[key] = value;
map.TryGetValue(key, out BValue val);
return val;
}
set => map[key] = value;
}

public override void Clear() => map.Clear();
public override void Clear() => map.Clear();

public override void Add(string key, BValue value) => map.Add(key, value);
public override void Add(string key, BValue value) => map.Add(key, value);


public override bool Contains(BValue v) => map.ContainsValue(v);
public override bool Contains(BValue v) => map.ContainsValue(v);

public override bool ContainsKey(string key) => map.ContainsKey(key);
public override bool ContainsKey(string key) => map.ContainsKey(key);

public bool Remove(string key) => map.Remove(key);
public bool Remove(string key) => map.Remove(key);

public bool TryGetValue(string key, out BValue value) => map.TryGetValue(key, out value);
public bool TryGetValue(string key, out BValue value) => map.TryGetValue(key, out value);

IEnumerator IEnumerable.GetEnumerator()
{
return map.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return map.GetEnumerator();
}
}
Loading

0 comments on commit 14b2e50

Please sign in to comment.