Skip to content

Instantly share code, notes, and snippets.

Avatar

J. B. Rainsberger jbrains

View GitHub Profile
View NonRepeatingNumbers.cs
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
//Using Maybe.cs
namespace ArrayStructure
{
[TestFixture]
public class Tests
{
@jbrains
jbrains / PointOfSaleTerminal.java
Last active Nov 28, 2016 — forked from CoryFoy/sample.rb
TDD no Expectations
View PointOfSaleTerminal.java
package ca.jbrains.pos;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.HashMap;
public class PointOfSaleTerminal {
public static void main(String[] args) throws IOException {
final InputStreamReader commandSource = new InputStreamReader(System.in);
View gist:2a29465457bddcca0c45
-- Thanks to @kimwallmark for teaching me `maybe` and showing me how a single lookup.
-- I had the brilliant idea of using `snd`, even though I dislike the name. :)
fizzbuzz :: Integer -> String
fizzbuzz n = maybe (show n) snd $ classify n
where
classify n = find (\(m, _) -> n `mod` m == 0) [(15, "Fizzbuzz"), (5, "Buzz"), (3, "Fizz")]
View gist:9451941
# SMELL I don't like the name gemfile_lock_file, but I don't know
# what else to call it. I want to distinguish it from Gemfile.
def extract_gems_from_gemfile_lock_file(contents)
gems = []
# Extract all the non-empty lines between and excluding 'specs:' and 'PLATFORMS'
contents.each_line do |line|
line = line.strip
# It takes a few seconds to understand this algorithm,
# but I can't yet justify replacing it with a functional
# approach. Again, it depends what the reader understands
View make-diagrams.sh
#!/bin/bash
download_yuml_image() {
local file="$1"
local output_directory="$2"
instructions=$(tr "\\n" "," < <(cat $file))
image_basename=$(basename $(echo $file | sed -E "s/(.+)\.yuml$/\1/"))
image_filename="${output_directory}/${image_basename}.png"
@jbrains
jbrains / gist:1259077
Created Oct 3, 2011 — forked from philipschwarz/gist:1241175
Separating use from construction - Before
View gist:1259077
public class BusinessObject {
/** @deprecated */
public void actionMethod() {
actionMethod(Service.getInstance());
}
// A SEAM! A SEAM! New clients should use me!!
public void actionMethod(ServiceDelegate serviceDelegate) {
// Other things
serviceDelegate.doService();
@jbrains
jbrains / Cell.java
Created Jan 20, 2011 — forked from patrickwilsonwelsh/RulesForNextGenerationTest
One way to remove duplication from PWW's tests
View Cell.java
package rules;
public class Cell {
protected int numberOfLiveNeighbors;
private CellState state;
public Cell() {
this.state = CellState.DEAD;
}
View mock_problems.rb
# common use of mocks in a Rails app
it "updates the requested book" do
mock_book = mock(:book)
Book.should_receive(:find).with("37").and_return(mock_book)
mock_book.should_receive(:update_attributes).with({:these => 'params'})
put :update, :id => "37", :book => {:these => 'params'}
end
# without mocks a more readable, less brittle test
it "updates the requested book" do
View SellOneItemTest.java
ackage ca.jbrains.pos.test;
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
public class SellOneItemTest {
private POSDisplay posDisplay = new POSDisplay();
private Sale sale;
View comparing_expect_then_act_with_act_then_verify.groovy
public class CreateActionTest {
def mockery = new JUnit4Mockery();
def modelRepository = mockery.mock(ModelRepository.class); // corresponds to class methods on Model in Rails
def model = new Model(); // treated as Entity (DDD sense)
def controller = new Controller(modelRepository);
@Before
public void supposeModelRepositoryCreatesOurModelObject() {
mockery.stub(modelRepository).create().will(returnValue(model));