Class: Opal::Rubyspec::FiltersRewriter

Inherits:
Opal::Rewriters::Base show all
Defined in:
opal/lib/opal/rewriters/rubyspec/filters_rewriter.rb

Constant Summary collapse

RUBYSPEC_DSL =
%i[describe it context].freeze

Constants inherited from Opal::Rewriters::Base

Opal::Rewriters::Base::DUMMY_LOCATION

Instance Attribute Summary

Attributes inherited from Opal::Rewriters::Base

#current_node

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Opal::Rewriters::Base

#append_to_body, #begin_with_stmts, #error, #prepend_to_body, #process, #s, s, #stmts_of

Constructor Details

#initializeFiltersRewriter

Returns a new instance of FiltersRewriter.



29
30
31
# File 'opal/lib/opal/rewriters/rubyspec/filters_rewriter.rb', line 29

def initialize
  @specs_stack = []
end

Class Method Details

.clear_filters!Object



24
25
26
# File 'opal/lib/opal/rewriters/rubyspec/filters_rewriter.rb', line 24

def clear_filters!
  @filters = []
end

.filter(spec_name) ⇒ Object Also known as: fails, fails_badly



13
14
15
# File 'opal/lib/opal/rewriters/rubyspec/filters_rewriter.rb', line 13

def filter(spec_name)
  filters << spec_name
end

.filtered?(spec_name) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'opal/lib/opal/rewriters/rubyspec/filters_rewriter.rb', line 20

def filtered?(spec_name)
  filters.include?(spec_name)
end

.filtersObject



9
10
11
# File 'opal/lib/opal/rewriters/rubyspec/filters_rewriter.rb', line 9

def filters
  @filters ||= []
end

Instance Method Details

#current_spec_nameObject



71
72
73
# File 'opal/lib/opal/rewriters/rubyspec/filters_rewriter.rb', line 71

def current_spec_name
  @specs_stack.join(' ')
end

#fixture(file, *args) ⇒ Object

Adapted from: spec/mspec/lib/mspec/helpers/fixture.rb



76
77
78
79
80
81
# File 'opal/lib/opal/rewriters/rubyspec/filters_rewriter.rb', line 76

def fixture(file, *args)
  path = File.dirname(file)
  path = path[0..-7] if path[-7..-1] == '/shared'
  fixtures = path[-9..-1] == '/fixtures' ? '' : 'fixtures'
  File.join(path, fixtures, args)
end

#on_send(node) ⇒ Object



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
# File 'opal/lib/opal/rewriters/rubyspec/filters_rewriter.rb', line 35

def on_send(node)
  _recvr, method_name, *args = *node

  if rubyspec_dsl?(method_name)
    spec_name, _ = *args.first
    begin
      @specs_stack.push(spec_name)
      if skip?
        s(:nil)
      else
        super
      end
    ensure
      @specs_stack.pop
    end
  elsif method_name == :fixture
    # We want to expand the fixture paths before autoload happens.
    if args.all? { |i| i.type == :str }
      as = args.map { |i| i.children.first }
      s(:str, fixture(*as))
    else
      super
    end
  else
    super
  end
end

#rubyspec_dsl?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'opal/lib/opal/rewriters/rubyspec/filters_rewriter.rb', line 67

def rubyspec_dsl?(method_name)
  RUBYSPEC_DSL.include?(method_name)
end

#skip?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'opal/lib/opal/rewriters/rubyspec/filters_rewriter.rb', line 63

def skip?
  self.class.filtered?(current_spec_name)
end