File: control_flow_spec.rb

package info (click to toggle)
ruby-temple 0.10.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 476 kB
  • sloc: ruby: 3,347; makefile: 6
file content (90 lines) | stat: -rw-r--r-- 1,976 bytes parent folder | download | duplicates (2)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'spec_helper'

describe Temple::Filters::ControlFlow do
  before do
    @filter = Temple::Filters::ControlFlow.new
  end

  it 'should process blocks' do
    expect(@filter.call([:block, 'loop do',
      [:static, 'Hello']
    ])).to eq [:multi,
                     [:code, 'loop do'],
                     [:static, 'Hello'],
                     [:code, 'end']]
  end

  it 'should process if' do
    expect(@filter.call([:if, 'condition',
      [:static, 'Hello']
    ])).to eq [:multi,
      [:code, 'if condition'],
      [:static, 'Hello'],
      [:code, 'end']
    ]
  end

  it 'should process if with else' do
    expect(@filter.call([:if, 'condition',
      [:static, 'True'],
      [:static, 'False']
    ])).to eq [:multi,
      [:code, 'if condition'],
      [:static, 'True'],
      [:code, 'else'],
      [:static, 'False'],
      [:code, 'end']
    ]
  end

  it 'should create elsif' do
    expect(@filter.call([:if, 'condition1',
      [:static, '1'],
      [:if, 'condition2',
       [:static, '2'],
       [:static, '3']]
    ])).to eq [:multi,
      [:code, 'if condition1'],
      [:static, '1'],
      [:code, 'elsif condition2'],
      [:static, '2'],
      [:code, 'else'],
      [:static, '3'],
      [:code, 'end']
    ]
  end

  it 'should process cond' do
    expect(@filter.call([:cond,
      ['cond1', [:exp1]],
      ['cond2', [:exp2]],
      [:else,   [:exp3]],
    ])).to eq [:multi,
      [:code, 'case'],
      [:code, 'when cond1'],
      [:exp1],
      [:code, 'when cond2'],
      [:exp2],
      [:code, 'else'],
      [:exp3],
      [:code, 'end']
    ]
  end

  it 'should process case' do
    expect(@filter.call([:case, 'var',
      ['Array',  [:exp1]],
      ['String', [:exp2]],
      [:else,    [:exp3]],
    ])).to eq [:multi,
      [:code, 'case (var)'],
      [:code, 'when Array'],
      [:exp1],
      [:code, 'when String'],
      [:exp2],
      [:code, 'else'],
      [:exp3],
      [:code, 'end']
    ]
  end
end