summaryrefslogtreecommitdiff
path: root/lib/spec.rb
blob: d9c7d96465cdb711c488e6c66ee2c37efa0f6384 (plain)
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
class Spec
  class << self
    @@contexts = []

    def context(description, &block)
      @@contexts.push({ description: description, block: block })
    end

    def expect(value)
      Result.new(value)
    end

    def eq(value)
      Proc.new do |actual|
        if actual != value
          raise AssertionError.new("Expected #{value.to_s}, got #{actual.to_s}")
        end
      end
    end

    def call
      @@contexts.each do |context|
        puts name
        puts "  #{context[:description]}"
        context[:block].call
      end
    end
  end

  class Result
    def initialize(value)
      @value = value
    end

    attr_reader :value

    def to(matcher)
      matcher.call(value)
      puts "    Passed!"
    end
  end

  class AssertionError < RuntimeError
  end
end