Skip to content

Instantly share code, notes, and snippets.

@jhgbrt
jhgbrt / ProcessHelper.cs
Last active September 30, 2015 15:18
C#/.Net class for launching a CommandLine process and capturing stdout/stderr
public class ProcessHelper
{
/// <summary>
/// Starts a command line process, redirecting both StdOut and StdErr.
/// </summary>
/// <param name="fileName">Path to the executable</param>
/// <param name="arguments">Command line arguments</param>
/// <param name="onErr">Action to perform when data from the child process is read from StdErr. The action takes 2 parameters: a Process instance (representing the child process) and a string (the data it wrote to StdErr)</param>
/// <param name="onOut">Action to perform when data from the child process is read from StdOut. The action takes 2 parameters: a Process instance (representing the child process) and a string (the data it wrote to StdOut)</param>
/// <returns></returns>
@jhgbrt
jhgbrt / moq_verifications.cs
Created May 30, 2012 19:23
Verifying method calls with Moq
[Test]
public void UsingMoqWithVerifications()
{
// Arrange
var dependency = new Mock<IDependency>();
// unfortunately there does not seem to be a simple way to set multiple return values with Moq...
var returnValues = new[] {7, 3};
var index = -1;
dependency.Setup(x => x.DoSomething(It.IsAny<SomeComplexType>(), It.IsAny<int>()))
[Test]
public void EnsureDependencyIsCalledWithCorrectArguments()
{
var dependency = MockRepository.GenerateMock<IDependency>();
dependency.Expect(x => x.DoSomething(null, 0))
.IgnoreArguments().Return(7).Repeat.Once();
dependency.Expect(x => x.DoSomething(null, 0))
.IgnoreArguments().Return(3).Repeat.Once();
var systemUnderTest = new SystemUnderTest(dependency);
@jhgbrt
jhgbrt / GZipCompressDecompress.cs
Last active February 22, 2023 16:02
C# gzipstream
public static byte Compress(string text)
{
using (var ms = new MemoryStream())
{
using (var zip = new GZipStream(ms,
CompressionMode.Compress))
using (var writer = new StreamWriter(zip, Encoding.UTF8))
{
writer.Write(text);
}
@jhgbrt
jhgbrt / ByteArraySegment.cs
Last active December 30, 2015 08:30
helper class to manage a segment of a byte array, without copying the underlying array
/// <summary>
/// Helper class to manage a (part of) an array of T (similar to the BCL's ArraySegment, but actually providing some useful functionality)
/// </summary>
public class ArraySegment<T> : IEnumerable<T>
{
private readonly T[] _rawArray;
private readonly int _offset;
private readonly int _length;
public ArraySegment(T[] rawArray, int offset, int length)
@jhgbrt
jhgbrt / Builder.cs
Last active August 27, 2015 08:32
Basic Functional Fluent Builder
using System;
using System.Collections.Generic;
// Factory methods provided in non-generic abstract base class
public abstract class Builder
{
public static Builder<T> For<T>() where T : new()
{
return new Builder<T>(() => new T());
}
@jhgbrt
jhgbrt / gist:9df92cb0a140804ea01c
Created November 23, 2015 14:55
azure api mgr trace
{
"traceId": "[snip]",
"traceEntries": {
"inbound": [
{
"source": "api-inspector",
"timestamp": "2015-11-23T14:38:14.3755947Z",
"elapsed": "00:00:00.0980973",
"data": {
"request": {
@jhgbrt
jhgbrt / Logger.cs
Last active October 24, 2016 07:35
using System;
using System.IO;
namespace Tools.Infrastructure
{
class Logger
{
static object _lock = new object();
public static void Info(string message)
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Reflection;
namespace Tools
{
@jhgbrt
jhgbrt / IBANChecksum.cs
Last active March 6, 2020 10:38 — forked from DvdKhl/IBANChecksum.cs
Fast C# IBAN Checksum Verifier / Validation
//Copyright (C) 2013 DvdKhl
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
//to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
//and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE