Module: EvalExpectationsOnAST

Included in:
Mumukit::Inspection
Defined in:
lib/expectations_hook.rb

Instance Method Summary collapse

Instance Method Details

#check_repeat_of(target) ⇒ Object



28
29
30
# File 'lib/expectations_hook.rb', line 28

def check_repeat_of(target)
  use(/AST\(repeat\s*#{target}/)
end

#eval_in_gobstones(binding, ast) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/expectations_hook.rb', line 8

def eval_in_gobstones(binding, ast)
  pattern_generator = expectations[type]

  result = pattern_generator ? !!(ast =~ pattern_generator[binding]) : true

  negated ? !result : result
end

#expectationsObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/expectations_hook.rb', line 32

def expectations
  {
    'HasBinding' => lambda { |binding| subject_for(binding).ast_regexp },
    'HasForeach' => use(/AST\(foreach/),
    'HasRepeat' => check_repeat_of('.+'),
    'HasVariable' => use(/AST\(assignVarName/),
    'HasWhile' => use(/AST\(while/),
    'HasArity' => lambda { |binding| /#{subject_for(binding).ast_regexp}\s*AST\((\s*\w+){#{target}}\)/ },
    'HasRepeatOf' => check_repeat_of("AST\\(literal\\s*#{target}\\)"),
    'HasUsage' => use(/AST\((proc|func)Call\s*#{target}$/)
  }
end

#subject_for(binding) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/expectations_hook.rb', line 20

def subject_for(binding)
  if binding == 'program'
    StonesSpec::Subject::Program
  else
    StonesSpec::Subject.from(binding)
  end
end

#use(regexp) ⇒ Object



16
17
18
# File 'lib/expectations_hook.rb', line 16

def use(regexp)
  lambda { |_| regexp }
end