Skip to content

Instantly share code, notes, and snippets.

View beccasaurus's full-sized avatar

Rebecca Taylor beccasaurus

View GitHub Profile
@beccasaurus
beccasaurus / PrintDotNetVersionInfo.cs
Created November 27, 2010 21:44
PrintDotNetVersionInfo
using System;
using System.Reflection;
public class PrintDotNetVersionInfo {
public static void Main(string[] args) {
PrintVersionInfo();
}
public static void PrintVersionInfo() {
Console.WriteLine(".NET Version: " + System.Environment.Version.ToString());
@beccasaurus
beccasaurus / AssertThrows.cs
Created November 28, 2010 07:34
NUnit AssertThrows() ... when [ExpectedException] won't work for you
// NOTE: Yes, NUNit has an [ExpectedException] attribute, but:
// - Sometimes I want to have a few assertions in the same [Test]
// - AssertThrows(() => { code }) is nicer than ExpectedException, IMHO
//
// NOTE: This is implemented as extension methods so it doesn't matter what
// class your NUnit tests are in ... if you don't want to have to say
// this.AsserThrows() (instead of just AssertThrows), you could add
// these methods to a baseclass that your [TestFixture] classes inherit from.
// Usage:
@beccasaurus
beccasaurus / FindSampleSpecs_A.cs
Created December 6, 2010 22:22
Which is easier to understand/maintain? A or B?
using System;
using System.Collections.Generic;
using System.Linq;
using DuplicateFinder.Core.Commands;
using Machine.Specifications;
namespace DuplicateFinder.Core.Integration.Tests {
@beccasaurus
beccasaurus / fill_in_fields.rb
Created December 16, 2010 07:27
fill_in_fields (i keep re-implementing this ...)
# Usage:
# fill_in_fields :foo => 'bar' # fill_in 'foo', :with => 'bar'
# fill_in_fields :user, :name => 'bob' # fill_in 'user_name', :with => 'bob'
# fill_in_fields :contact, :address, :street => '6 cedar rd' # fill_in 'contact_address_street_, :with => '6 cedar rd'
def fill_in_fields *args
field, value = args.pop.first
prefix = ''
while args.any?
prefix += args.shift.to_s + "_"
end
using System;
using System.Threading.Tasks;
namespace AppThatRunsItself {
public class App {
public static void Main(string[] args) {
// we might instantiate a host and run an app with it
var host = new Hosting.Host();
var dotNet40_thing = new Task(() => Console.WriteLine("Hello from task"));
@beccasaurus
beccasaurus / dependant.rb
Created January 11, 2011 22:43
Ruby-ifying
# Ruby syntax notes:
#
# - the last value of a method is implicitly returned. 'return' is typically only used for short-circuiting
# - attr_reader :foo is the same as def foo; @foo; end
# - get_foo and set_foo methods are very Java-esque and you should typically not name methods like that in Ruby
# - instead of @array.push(1), use the push operator. @array << 1
# - if you want to do something conditionally on one line, the conditional comes *after* the code, eg. x = 5 if @dogs.any?
# - except for single line blocks, eg. each {|x| ... }, variable names should not be abbreviated
# - parenthesis are optional and should only be used when they help make the code easier to understand (subject to debate)
#
@beccasaurus
beccasaurus / Times.cs
Created January 21, 2011 20:06
5.Times(i => ...};
using System;
public static class Int32TimesExtension {
public static void Times(this int numberOfTimes, Action<int> action) {
for (int i = 0; i < numberOfTimes; i++)
action.Invoke(i);
}
}
public class Program {
@beccasaurus
beccasaurus / should_have_attributes.rb
Created February 4, 2011 22:38
should_have_attributes
module ShouldHaveAttributes
class Validator
def initialize object
@object = object
end
def method_missing name, *args, &block
# puts "#{@object}.#{name}.should == #{args.first.inspect}"
@object.send(name).should == args.first
using System;
using System.Reflection;
namespace NUnit.Framework {
public static class ShouldHavePropertiesExtension {
/// <summary>For checking lots of properties on an object</summary>
/// <remarks>
/// Lets you say:
/// <code>
var eatFood = new Action<object>(food => Console.WriteLine("I eat {0}", food));
var foods = new List<string> { "banana", "bagel", "pizza" };
foods.ForEach(food => eatFood.Invoke(food));
// I eat banana
// I eat bagel
// I eat pizza