Module: Ruby2JS::Filter::MiniTestJasmine

Includes:
SEXP
Defined in:
lib/ruby2js/filter/minitest-jasmine.rb

Constant Summary collapse

RELOPS =
[:<, :<=, :==, :>=, :>].
map {|sym| Parser::AST::Node.new :sym, [sym]}

Instance Method Summary collapse

Methods included from SEXP

#S, #s

Instance Method Details

#initialize(*args) ⇒ Object



10
11
12
13
# File 'lib/ruby2js/filter/minitest-jasmine.rb', line 10

def initialize(*args)
  @jasmine_describe = nil
  super
end

#on_block(node) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ruby2js/filter/minitest-jasmine.rb', line 45

def on_block(node)
  call = node.children.first
  return super unless call.children.first == nil

  if call.children[1] == :describe
    begin
      describe, @jasmine_describe = @jasmine_describe, true
      s(:block, *node.children[0..-2], process(node.children.last))
    ensure
      @jasmine_describe = describe
    end
  elsif @jasmine_describe and call.children[1] == :before
    process s(:block, s(:send, nil, :beforeEach, *call.children[2..-1]),
      *node.children[1..-1])
  elsif @jasmine_describe and call.children[1] == :after
    process s(:block, s(:send, nil, :afterEach, *call.children[2..-1]),
      *node.children[1..-1])
  else
    super
  end
end

#on_class(node) ⇒ Object



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
# File 'lib/ruby2js/filter/minitest-jasmine.rb', line 18

def on_class(node)
  name, inheritance, *body = node.children
  return super unless inheritance == s(:const, s(:const, nil,
    :Minitest), :Test)

  if body.length == 1 and body.first.type == :begin
    body = body.first.children
  end

  body = body.map do |bnode|
    if bnode.type == :def and bnode.children.first =~ /^test_/
      s(:block, s(:send, nil, :it, s(:str, 
        bnode.children.first.to_s.sub(/^test_/, '').gsub('_', ' '))),
        s(:args), bnode.children.last)
    elsif bnode.type == :def and bnode.children.first == :setup
      s(:block, s(:send, nil, :before), s(:args), bnode.children.last)
    elsif bnode.type == :def and bnode.children.first == :teardown
      s(:block, s(:send, nil, :after), s(:args), bnode.children.last)
    else
      bnode
    end
  end

  process s(:block, s(:send, nil, :describe, s(:sym, name.children[1])),
    s(:args), s(:begin, *body))
end

#on_send(node) ⇒ Object



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ruby2js/filter/minitest-jasmine.rb', line 67

def on_send(node)
  target, method, *args = node.children
  if target
    if method==:must_be && args.length==2 && RELOPS.include?(args[0])
      process s(:send, nil, :assert_operator, target, *args)
    elsif method==:must_be_close_to && [1,2].include?(args.length)
      process s(:send, nil, :assert_in_delta, target, *args)
    elsif method==:must_be_within_delta && [1,2].include?(args.length)
      process s(:send, nil, :assert_in_delta, target, *args)
    elsif method==:must_be_nil && args.length == 0
      process s(:send, nil, :assert_nil, target)
    elsif method==:must_equal && args.length == 1
      process s(:send, nil, :assert_equal, args.first, target)
    elsif method==:must_include && args.length == 1
      process s(:send, nil, :assert_includes, target, args.first)
    elsif method==:must_match && args.length == 1
      process s(:send, nil, :assert_match, args.first, target)

    elsif method==:cant_be && args.length==2 && RELOPS.include?(args[0])
      process s(:send, nil, :refute_operator, target, *args)
    elsif method==:cant_be_close_to && [1,2].include?(args.length)
      process s(:send, nil, :refute_in_delta, target, *args)
    elsif method==:cant_be_within_delta && [1,2].include?(args.length)
      process s(:send, nil, :refute_in_delta, target, *args)
    elsif method==:cant_be_nil && args.length == 0
      process s(:send, nil, :refute_nil, target)
    elsif method==:cant_equal && args.length == 1
      process s(:send, nil, :refute_equal, args.first, target)
    elsif method==:cant_include && args.length == 1
      process s(:send, nil, :refute_includes, target, args.first)
    elsif method==:cant_match && args.length == 1
      process s(:send, nil, :refute_match, args.first, target)

    else
      super
    end

  else
    if method == :assert and args.length == 1
      process s(:send, s(:send, nil, :expect, args.first), :toBeTruthy)
    elsif method == :assert_equal and args.length == 2
      if [:str, :int, :float].include? args.first.type
        process s(:send, s(:send, nil, :expect, args.last), :toBe,
          args.first)
      else
        process s(:send, s(:send, nil, :expect, args.last), :toEqual,
          args.first)
      end
    elsif method == :assert_in_delta and [2,3].include? args.length
      delta = (args.length == 3 ? args.last : s(:float, 0.001))
      process s(:send, s(:send, nil, :expect, args[1]), :toBeCloseTo,
        args.first, delta)
    elsif method == :assert_includes and args.length == 2
      process s(:send, s(:send, nil, :expect, args.first), :toContain,
        args.last)
    elsif method == :assert_match and args.length == 2
      process s(:send, s(:send, nil, :expect, args.last), :toMatch,
        args.first)
    elsif method == :assert_nil and args.length == 1
      process s(:send, s(:send, nil, :expect, args.first), :toBeNull)
    elsif method==:assert_operator && args.length==3 && args[1].type==:sym
      if args[1].children.first == :<
        process s(:send, s(:send, nil, :expect, args.first),
          :toBeLessThan, args.last)
      elsif args[1].children.first == :<=
        process s(:send, s(:send, nil, :expect, args.last),
          :toBeGreaterThan, args.first)
      elsif args[1].children.first == :>
        process s(:send, s(:send, nil, :expect, args.first),
          :toBeGreaterThan, args.last)
      elsif args[1].children.first == :>=
        process s(:send, s(:send, nil, :expect, args.last),
          :toBeLessThan, args.first)
      elsif args[1].children.first == :==
        process s(:send, nil, :assert_equal, args.last, args.first)
      else
        super
      end

    elsif method == :refute and args.length == 1
      process s(:send, s(:send, nil, :expect, args.first), :toBeFalsy)
    elsif method == :refute_equal and args.length == 2
      if [:str, :int, :float].include? args.first.type
        process s(:send, s(:attr, s(:send, nil, :expect, args.last), 
          :not), :toBe, args.first)
      else
        process s(:send, s(:attr, s(:send, nil, :expect, args.last), 
          :not), :toEqual, args.first)
      end
    elsif method == :refute_in_delta and [2,3].include? args.length
      delta = (args.length == 3 ? args.last : s(:float, 0.001))
      process s(:send, s(:send, nil, :expect, args[1]), :toBeCloseTo,
        args.first, delta)
    elsif method == :refute_includes and args.length == 2
      process s(:send, s(:attr, s(:send, nil, :expect, args.first), 
        :not), :toContain, args.last)
    elsif method == :refute_match and args.length == 2
      process s(:send, s(:attr, s(:send, nil, :expect, args.last), 
        :not), :toMatch, args.first)
    elsif method == :refute_nil and args.length == 1
      process s(:send, s(:attr, s(:send, nil, :expect, args.first), 
        :not), :toBeNull)
    elsif method==:refute_operator && args.length==3 && args[1].type==:sym
      if args[1].children.first == :<=
        process s(:send, s(:send, nil, :expect, args.first),
          :toBeGreaterThan, args.last)
      elsif args[1].children.first == :<
        process s(:send, s(:attr, s(:send, nil, :expect, args.last), 
          :not), :toBeLessThan, args.first)
      elsif args[1].children.first == :>
        process s(:send, s(:attr, s(:send, nil, :expect, args.first),
          :not), :toBeGreaterThan, args.last)
      elsif args[1].children.first == :>=
        process s(:send, s(:send, nil, :expect, args.first),
          :toBeLessThan, args.last)
      elsif args[1].children.first == :==
        process s(:send, nil, :refute_equal, args.last, args.first)
      else
        super
      end

    else
      super
    end
  end
end