Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Created December 6, 2010 22:22
Show Gist options
  • Save beccasaurus/731095 to your computer and use it in GitHub Desktop.
Save beccasaurus/731095 to your computer and use it in GitHub Desktop.
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 {
public class When_duplicates_are_searched_by_size {
static ICommand Command;
Establish context =
() => { Command = new CommandLineParser().Parse(@"--size --whatif Samples\First Samples\Second".Args()); };
Because of = () => Command.Execute();
It should_find_files_of_the_same_size =
() => Command.AllDuplicates().ShouldContainOnly(@"Samples\First\content-full-first.txt",
@"Samples\Second\content-full-second.txt",
@"Samples\First\size-first.txt",
@"Samples\Second\size-second.txt");
}
public class When_duplicates_are_searched_by_name {
static ICommand Command;
Establish context =
() => { Command = new CommandLineParser().Parse(@"--name --whatif Samples\First Samples\Second".Args()); };
Because of = () => Command.Execute();
It should_find_files_with_the_same_name =
() => Command.AllDuplicates().ShouldContainOnly(@"Samples\First\name.txt",
@"Samples\Second\name.txt");
}
public class When_duplicates_are_searched_by_contents {
static ICommand Command;
Establish context =
() => { Command = new CommandLineParser().Parse(@"--content --whatif Samples\First Samples\Second".Args()); };
Because of = () => Command.Execute();
It should_find_files_with_the_same_contents =
() => Command.AllDuplicates().ShouldContainOnly(@"Samples\First\content-full-first.txt",
@"Samples\Second\content-full-second.txt");
}
public class When_duplicates_are_searched_by_head_contents {
static ICommand Command;
Establish context =
() => { Command = new CommandLineParser().Parse(@"--content --first 5 --whatif Samples\First Samples\Second".Args()); };
Because of = () => Command.Execute();
It should_find_files_with_the_same_contents_in_the_first_five_bytes=
() => Command.AllDuplicates().ShouldContainOnly(@"Samples\First\content-full-first.txt",
@"Samples\Second\content-full-second.txt",
@"Samples\First\content-head-first.txt",
@"Samples\Second\content-head-second.txt");
}
public class When_duplicates_are_searched_by_tail_contents {
static ICommand Command;
Establish context =
() => { Command = new CommandLineParser().Parse(@"--content --last 5 --whatif Samples\First Samples\Second".Args()); };
Because of = () => Command.Execute();
It should_find_files_with_the_same_contents_in_the_first_five_bytes =
() => Command.AllDuplicates().ShouldContainOnly(@"Samples\First\content-full-first.txt",
@"Samples\Second\content-full-second.txt",
@"Samples\First\content-tail-first.txt",
@"Samples\Second\content-tail-second.txt");
}
public class When_duplicates_are_searched_by_head_and_tail_contents {
static ICommand Command;
Establish context =
() => { Command = new CommandLineParser().Parse(@"--content --first 5 --last 5 --whatif Samples\First Samples\Second".Args()); };
Because of = () => Command.Execute();
It should_find_files_with_the_same_contents_in_the_first_and_last_five_bytes =
() => Command.AllDuplicates().ShouldContainOnly(@"Samples\First\content-full-first.txt",
@"Samples\Second\content-full-second.txt");
}
// Extension Methods
internal static class Extensions {
public static string[] Args(this string args) {
return args.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
static T As<T>(this object instance) {
return (T) instance;
}
public static IEnumerable<string> AllDuplicates(this object instance) {
return instance.As<FindDuplicatesCommand>().Duplicates.SelectMany(x => x);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using DuplicateFinder.Core.Commands;
using Machine.Specifications;
namespace DuplicateFinder.Core.Integration.Tests {
public class FindingDuplicates {
It can_find_files_with_the_same_size =()=> {
RunCommand(@"--size --whatif Samples\First Samples\Second");
AllDuplicates.ShouldContainOnly(@"Samples\First\content-full-first.txt",
@"Samples\Second\content-full-second.txt",
@"Samples\First\size-first.txt",
@"Samples\Second\size-second.txt");
};
It can_find_files_with_the_same_name =()=> {
RunCommand(@"--name --whatif Samples\First Samples\Second");
AllDuplicates.ShouldContainOnly(@"Samples\First\name.txt", @"Samples\Second\name.txt");
};
It can_find_files_with_the_same_contents =()=> {
RunCommand(@"--content --whatif Samples\First Samples\Second");
AllDuplicates.ShouldContainOnly(@"Samples\First\content-full-first.txt", @"Samples\Second\content-full-second.txt");
};
It can_find_files_with_the_same_head_content =()=> {
RunCommand(@"--content --first 5 --whatif Samples\First Samples\Second");
AllDuplicates.ShouldContainOnly(@"Samples\First\content-full-first.txt",
@"Samples\Second\content-full-second.txt",
@"Samples\First\content-head-first.txt",
@"Samples\Second\content-head-second.txt");
};
It can_find_files_with_the_same_tail_content =()=> {
RunCommand(@"--content --last 5 --whatif Samples\First Samples\Second");
AllDuplicates.ShouldContainOnly(@"Samples\First\content-full-first.txt",
@"Samples\Second\content-full-second.txt",
@"Samples\First\content-tail-first.txt",
@"Samples\Second\content-tail-second.txt");
};
It can_find_files_with_the_same_head_and_tail_content =()=> {
RunCommand(@"--content --first 5 --last 5 --whatif Samples\First Samples\Second");
AllDuplicates.ShouldContainOnly(@"Samples\First\content-full-first.txt", @"Samples\Second\content-full-second.txt");
}
// Helpers
static ICommand Command;
static ICommand RunCommand(string command) {
var args = command.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
Command = new CommandLineParser().Parse(args);
Command.Execute();
return Command;
}
static IEnumerable<string> AllDuplicates {
get { return (Command as FindDuplicatesCommand).Duplicates.SelectMany(x => x); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment