Class: Matest::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/matest/example.rb

Direct Known Subclasses

ExampleBeforeAssertion

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example_block, description, lets) ⇒ Example

Returns a new instance of Example.



4
5
6
7
8
9
10
11
12
# File 'lib/matest/example.rb', line 4

def initialize(example_block, description, lets)
  @example_block__for_internal_use = ExampleBlock.new(example_block)
  @description__for_internal_use = description
  @lets__for_internal_use = lets
  lets.each do |let|
    self.class.let(let.var_name, &let.block)
    send(let.var_name) if let.bang
  end
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



3
4
5
# File 'lib/matest/example.rb', line 3

def code
  @code
end

Class Method Details

.let(var_name, &block) ⇒ Object



30
31
32
33
34
# File 'lib/matest/example.rb', line 30

def self.let(var_name, &block)
  define_method(var_name) do
    instance_variable_set(:"@#{var_name}__from_let", block.call)
  end
end

.local_var(var_name) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/matest/example.rb', line 36

def self.local_var(var_name)
  define_method(var_name) do
    instance_variable_get(:"@#{var_name}")
  end
  define_method("#{var_name}=") do |value|
    instance_variable_set(:"@#{var_name}", value)
  end
end

Instance Method Details

#callObject



26
27
28
# File 'lib/matest/example.rb', line 26

def call
  instance_eval(&example_block.block)
end

#descriptionObject



22
23
24
# File 'lib/matest/example.rb', line 22

def description
  @description__for_internal_use
end

#example_blockObject



18
19
20
# File 'lib/matest/example.rb', line 18

def example_block
  @example_block__for_internal_use
end

#just_before_assertionObject

def without_block

the_new = self.clone
the_new.instance_variable_set(:@example_block__for_internal_use, nil)
the_new

end



68
69
70
71
# File 'lib/matest/example.rb', line 68

def just_before_assertion
  # return a clone of self, but with
  ExampleBeforeAssertion.new(example_block.block, description, lets)
end

#letsObject



14
15
16
# File 'lib/matest/example.rb', line 14

def lets
  @lets__for_internal_use
end

#track_letsObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/matest/example.rb', line 51

def track_lets
  instance_variables.select {|var|
    var.to_s =~ /__from_let\Z/
  }.map {|var|
    name = var.to_s
    name["__from_let"] = ""
    name[0] = ""
    [name, instance_variable_get(var)]
  }
end

#track_variablesObject



45
46
47
48
49
# File 'lib/matest/example.rb', line 45

def track_variables
  instance_variables.reject {|var|
    var.to_s =~ /__for_internal_use\Z/ || var.to_s =~ /__from_let\Z/
  }.map {|var| [var, instance_variable_get(var)] }
end