Skip to content

Instantly share code, notes, and snippets.

View nonnb's full-sized avatar

Stuart Corrans nonnb

View GitHub Profile
@nonnb
nonnb / XUnitTestCases.cs
Created December 17, 2021 11:37
XUnit - Test Runner to show descriptive name for Use Case
using System.Collections;
using System.Collections.Generic;
using Xunit;
namespace TestNames
{
public class UnitTest1
{
[Theory]
[ClassData(typeof(MyTestModel))]
@nonnb
nonnb / NUnitCaseName.cs
Last active December 17, 2021 11:34
NUnit - Adding Test Case Name to Data Driven Class Data
using System.Collections.Generic;
using NUnit.Framework;
namespace NUnitNames
{
public class Tests
{
private static readonly IEnumerable<MyTestModel> MyTestModels = new[]
{
new MyTestModel
@nonnb
nonnb / SneakyThrowExAntiPattern.cs
Last active February 24, 2020 17:04
EnrichingExceptions and retaining StackTrace
void Main()
{
try
{
// We want the stacktrace to drill through here.
BuggyMethod();
}
catch (Exception ex)
{
// Direct Mutation of the exception reference doesn't reset the stacktrace, although may be dubious practice (wrapped InnerException is arguably a better, mutation free alternative)
// Original Code:
// public static bool IsNull_yes(dynamic source)
// {
// if (source.x == null || source.x.y == null) return true;
// return false;
// }
// Decompiled with dotPeek
public static bool IsNull_exception(object source)
{
@nonnb
nonnb / NinjectAlternativeToAbstractFactory.cs
Created March 12, 2017 10:49
StackOverflow21285090 - Alternatives to Abstract Factory pattern using Ninject
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Ninject;
using Ninject.Modules;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
@nonnb
nonnb / ChickenAndEggPrimesGenerator.cs
Last active March 1, 2017 08:29
Generators, Iterators
using System.Collections.Generic;
using System.Linq;
namespace Generators
{
public static class PrimesGenerator
{
public static IEnumerable<int> GetPrimes()
{
// Given just 2, we can determine all other primes
OP needs to dynamically find a filtered row count across (almost) tables in a database, based on a master reference table
containing the column names. OP then wants to further restrict this result set by seeking only specific values of the result.
AFAIK this can only be done with dynamic SQL. Here I build up a nasty dynamic sql query which consists of a CTE unioning
all the grouped table row counts meeting the master table criteria, and then applying an overall grouped sum at the bottom.
@nonnb
nonnb / gist:a2bd872fa50915635663
Created July 15, 2015 15:09
StackOverflow 31432943
using System.Collections.Generic;
using System.Linq;
using Moq;
using NUnit.Framework;
namespace DB
{
public class Person
{
public int PersonId { get; set; }
@nonnb
nonnb / File1.cs
Last active August 29, 2015 14:23
SO 31035657 - Sparse Matrix Row and Column Sum performance
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
namespace ClassLibrary8
{
// Yoav's Code
public void Test_GetNext()
{
Task t1 = Task.Factory.StartNew(() => { getSerial(); });
Task t2 = Task.Factory.StartNew(() => { getSerial(); });
Task t3 = Task.Factory.StartNew(() => { getSerial(); });
Task t4 = Task.Factory.StartNew(() => { getSerial(); });
Task t5 = Task.Factory.StartNew(() => { getSerial(); });
Task.WaitAll(t1, t2, t3, t4, t5);