Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 70efecbb99
Fetching contributors…

Cannot retrieve contributors at this time

file 116 lines (93 sloc) 2.99 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
require File.expand_path(File.dirname(__FILE__) + '/../lib/values')
describe 'values' do
  Cell = Value.new(:alive)

  it 'stores a single field' do
    c = Cell.new(true)
    c.alive.should == true
  end

  Point = Value.new(:x, :y)

  it 'stores multiple values' do
    p = Point.new(0,1)
    p.x.should == 0
    p.y.should == 1
  end

  it 'raises argument errors if not given the right number of arguments' do
    expect { Point.new }.to raise_error(ArgumentError, 'wrong number of arguments, 0 for 2')
  end

  class GraphPoint < Value.new(:x, :y)
    def inspect
      "GraphPoint at #{@x},#{@y}"
    end
  end

  it 'can be inherited from to add methods' do
    c = GraphPoint.new(0,0)
    c.inspect.should == 'GraphPoint at 0,0'
  end

  it 'cannot be mutated' do
    p = Point.new(0,1)
    expect { p.x = 1 }.to raise_error
  end

  class Cow < Value.new(:color)
    def change_color(new_color)
      @color = new_color
    end
  end

  it 'cannot even be mutated inside a sublass with methods' do
    c = Cow.new("red")
    expect { c.change_color("blue") }.to raise_error
  end

  Money = Value.new(:amount, :denomination)

  it 'cannot be mutated using #instance_variable_set' do
    m = Money.new(1, 'USD')
    expect { m.instance_variable_set('@amount', 2) }.to raise_error
  end

  it 'can be instantiated with a hash' do
    one_dollar = Money.with(:amount => 1, :denomination => 'USD')
    one_dollar.amount.should == 1
    one_dollar.denomination.should == 'USD'
  end

  it 'errors if you instantiate it from a hash with unrecognised fields' do
    expect do
      Money.with(:unrecognized_field => 1, :amount => 2, :denomination => 'USD')
    end.to raise_error
  end

  it 'errors if you instantiate it from a hash with missing fields' do
    expect do
      Money.with
    end.to raise_error
  end

  it 'does not error when fields are explicitly nil' do
    expect do
      Money.with(:amount => 1, :denomination => nil)
    end.not_to raise_error
  end

  describe '#hash and equality' do
    Y = Value.new(:x, :y)

    it 'is equal to another value with the same fields' do
      Point.new(0,0).should == Point.new(0,0)
    end

    it 'is not equal to an object with a different class' do
      Point.new(0,0).should_not == Y.new(0,0)
    end

    it 'is not equal to another value with different fields' do
      Point.new(0,0).should_not == Point.new(0,1)
      Point.new(0,0).should_not == Point.new(1,0)
    end

    it 'has an equal hash if the fields are equal' do
      p = Point.new(0,0)
      p.hash.should == Point.new(0,0).hash
    end

    it 'has a non-equal hash if the fields are different' do
      p = Point.new(0,0)
      p.hash.should_not == Point.new(1,0).hash
    end

    it 'does not have an equal hash if the class is different' do
      Point.new(0,0).hash.should_not == Y.new(0,0).hash
    end
  end

  describe '#values' do
    it 'returns an array of field values' do
      Point.new(10, 13).values.should == [10, 13]
    end
  end
end
Something went wrong with that request. Please try again.