Skip to content

Instantly share code, notes, and snippets.

function setup(method)
{
method = fake;
};
function fake()
{
return "faked";
}
function real()
{
/*
The simplest(?!?!) way to get JSON.NET to handle serialization of properties with an interface type.
usage:
var settings = new JsonSettings();
settings.Converters.Add(new InterfaceConverter<IDataStoreSettings, AccessDataStoreSettings>());
JsonConvert.DeserializeObject<Configuration>(contents, settings);
*/
internal class InterfaceConverter<To, From> : JsonConverter
@karlseguin
karlseguin / gist:643625
Created October 24, 2010 15:50
add cache-busting to images within stylesheets
require 'zlib'
@@cssPath = '../public/stylesheets/' #relative to where the script is running
@@imagePath = '../public/images/' #relative to where the script is running
@@styleSheets = ['myCssFile1.css', 'AntherCssFile.css']
def crc(fileName)
fileName = @@imagePath + fileName.slice(10..-1)
contents = File.read(fileName) ; nil
Zlib.crc32(contents,0).to_s(16)
@karlseguin
karlseguin / gist:644811
Created October 25, 2010 11:31
Testing without a mock
//re how I would test it (http://openmymind.net/2010/10/20/My-Slow-Transition-Away-From-Mocks) without a mock
[Test]
public void CalculatesTheMessagesHash()
{
var logger = new LoggingService(new MyRealEncryptor());
var message = new Message{Body = "Some Body", Application = new Application(23) };
Assert.AreEqual("E99A18C428CB38D5F260853678922E03", logger. CalculateHash(message));
}
@karlseguin
karlseguin / gist:741933
Created December 15, 2010 13:22
the most fail style ever?
//from https://github.com/mongodb/mongo-csharp-driver/blob/master/Driver/Core/MongoDatabase.cs
#region public indexers
public MongoCollection<BsonDocument> this[
string collectionName
] {
get { return GetCollection(collectionName); }
}
public MongoCollection<BsonDocument> this[
string collectionName,
@karlseguin
karlseguin / gist:782653
Created January 17, 2011 09:35
I know it's probably wrong, but I prefer to inline (and repeat) most of my variables in a test
#repeated, inline variables
it "should update the email" do
put :update, {:id => 1, :email => 'leto@dune.gov'}
User.count({:id => 1, :email => 'leto@dune.gov'}).should == 1
end
#extracted variables
it "should update the email" do
id = 1
@karlseguin
karlseguin / gist:782765
Created January 17, 2011 11:57
Handling nhibernate exceptions
using System;
using System.Data.SqlClient;
using NHibernate.Exceptions;
public class ExceptionConverter : ISQLExceptionConverter
{
public Exception Convert(AdoExceptionContextInfo context)
{
var exception = ADOExceptionHelper.ExtractDbException(context.SqlException) as SqlException;
if (exception != null)
@karlseguin
karlseguin / gist:782799
Created January 17, 2011 12:32
try to bust through proxies for a remote address
private static readonly string[] _ipHeaderOrder = new[] { "HTTP_X_FORWARDED_FOR", "HTTP_X_CLUSTER_CLIENT_IP", "REMOTE_ADDR" };
public static string ClientAddress(this HttpRequest request)
{
foreach (string header in _ipHeaderOrder)
{
string ipAddress = request.ServerVariables[header];
if (ipAddress != null)
{
return ipAddress;
@karlseguin
karlseguin / gist:809495
Created February 3, 2011 13:58
Mock IEnumerable matching.
//Forgive the poor example
public ICollection<User> LoadUsersByStatus(ICollection<UserStatus> statuses)
{
return _service.LoadUsersByStatus(statuses);
}
[Test]
public void GetsTheUsersFromTheService()
{
@karlseguin
karlseguin / support for blank values
Created February 6, 2011 05:06
prevent rails from encoding a a json fragment
class JsonFragment
def initialize(value)
@value = value
end
def as_json(o = nil)
self
end
def encode_json(e = nil)
@value.blank? ? 'null' : @value