Skip to content

Commit

Permalink
Add support code for specs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomstuart committed Oct 28, 2011
1 parent c554123 commit 3a07340
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'nothing'
require 'support/pair'
require 'support/helpers'
require 'support/matchers'

RSpec.configure do |c|
c.include Helpers
end
76 changes: 76 additions & 0 deletions spec/support/ffi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require 'nothing'
require 'support/pair'

module Nothing
module FFI
def to_integer(n)
TIMES[n][-> integer { integer + 1 }][0]
end

def from_integer(integer)
n = ZERO

integer.times do
n = INCREMENT[n]
end

n
end

def to_boolean(b)
IF[b][true][false]
end

def from_boolean(boolean)
if boolean then TRUE else FALSE end
end

def to_pair(p)
Pair.new(LEFT[p], RIGHT[p])
end

def from_pair(pair)
PAIR[pair.left][pair.right]
end

def to_array(l)
array = []

until to_boolean(IS_EMPTY[l])
array.push(FIRST[l])
l = REST[l]
end

array
end

def from_array(array)
l = EMPTY

until array.empty?
l = UNSHIFT[l][array.last]
array.pop
end

l
end

CHARSET = '0123456789BFiuz'.chars.entries # for encoding digits, "Fizz" and "Buzz"

def to_char(c)
CHARSET.at(to_integer(c))
end

def from_char(char)
from_integer(CHARSET.index(char))
end

def to_string(s)
to_array(s).map { |c| to_char(c) }.join
end

def from_string(string)
from_array(string.chars.map { |char| from_char(char) })
end
end
end
25 changes: 25 additions & 0 deletions spec/support/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'support/ffi'
require 'support/pair'

module Helpers
include Nothing::FFI

def representation_of(value)
case value
when Integer
from_integer value
when TrueClass, FalseClass
from_boolean value
when Pair
pair = Pair.new(representation_of(value.left), representation_of(value.right))
from_pair pair
when Array
array = value.map { |v| representation_of v }
from_array array
when String
from_string value
else
fail "don't know how to represent #{value.inspect}"
end
end
end
28 changes: 28 additions & 0 deletions spec/support/matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'support/ffi'

RSpec::Matchers.define :represent do |value|
include Nothing::FFI

def represents?(representation, value)
case value
when Integer
to_integer(representation) == value
when TrueClass, FalseClass
to_boolean(representation) == value
when Pair
pair = to_pair(representation)
represents?(pair.left, value.left) && represents?(pair.right, value.right)
when Array
array = to_array(representation)
(array.length == value.length) && array.zip(value).all? { |r, v| represents?(r, v) }
when String
to_string(representation) == value
else
fail "don't know how to represent #{value.inspect}"
end
end

match do |representation|
represents?(representation, value)
end
end
1 change: 1 addition & 0 deletions spec/support/pair.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pair = Struct.new :left, :right

0 comments on commit 3a07340

Please sign in to comment.