Class: Mumukit::Templates::MulangExpectationsHook

Inherits:
ExpectationsHook show all
Defined in:
lib/mumukit/templates/mulang_expectations_hook.rb

Constant Summary collapse

LOGIC_SMELLS =
%w(UsesCut UsesFail UsesUnificationOperator HasRedundantReduction)
FUNCTIONAL_SMELLS =
%w(HasRedundantParameter HasRedundantGuards)
OBJECT_ORIENTED_SMELLS =
%w(DoesNullTest ReturnsNull)
IMPERATIVE_SMELLS =
%w(HasRedundantLocalVariableReturn HasAssignmentReturn)
EXPRESSIVENESS_SMELLS =
%w(HasTooShortIdentifiers HasWrongCaseIdentifiers HasMisspelledIdentifiers)
GENERIC_SMELLS =
%w(IsLongCode HasCodeDuplication HasRedundantLambda HasRedundantIf DoesTypeTest HasRedundantBooleanComparison HasEmptyIfBranches)

Constants inherited from ExpectationsHook

ExpectationsHook::SOURCE_EXPECTATIONS, ExpectationsHook::SOURCE_EXPECTATION_EVALUATORS

Instance Attribute Summary

Attributes inherited from Hook

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ExpectationsHook

#compile

Methods inherited from Hook

#env, #initialize, #logger, #method_missing, #should_forward_to_config?, stateful_through, #t

Methods included from WithContentType

#content_type

Constructor Details

This class inherits a constructor from Mumukit::Hook

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mumukit::Hook

Class Method Details

.include_smells(value = true) ⇒ Object



73
74
75
76
77
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 73

def self.include_smells(value=true)
  if value
    include Mumukit::Templates::WithCodeSmells
  end
end

Instance Method Details

#compile_content(content) ⇒ Object



55
56
57
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 55

def compile_content(content)
  content
end

#compile_mulang_analysis(request, expectations) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 18

def compile_mulang_analysis(request, expectations)
  mulang_code(request).analysis(
    expectations: expectations[:ast],
    smellsSet: {
      tag: 'AllSmells',
      exclude: (expectations[:exceptions] + default_smell_exceptions)
    },
    domainLanguage: domain_language)
end

#default_smell_exceptionsObject



43
44
45
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 43

def default_smell_exceptions
  []
end

#domain_languageObject



35
36
37
38
39
40
41
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 35

def domain_language
  {
    caseStyle: "CamelCase",
    minimumIdentifierSize: 3,
    jargon: []
  }
end

#mulang_code(request) ⇒ Object



47
48
49
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 47

def mulang_code(request)
  Mulang::Code.new(mulang_language, compile_content(request[:content]))
end

#mulang_languageObject



51
52
53
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 51

def mulang_language
  language == 'Mulang' ? Mulang::Language::External.new :  Mulang::Language::Native.new(language)
end

#parse_expectation(expectation) ⇒ Object



69
70
71
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 69

def parse_expectation(expectation)
  expectation.deep_symbolize_keys.except(:tag)
end

#parse_response(response) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 59

def parse_response(response)
  if response['tag'] == 'AnalysisFailed'
    raise Mumukit::CompilationError, response['reason']
  end
  response['expectationResults'].map do |it|
    {result: it['result'],
     expectation: parse_expectation(it['expectation'])}
  end
end

#run!(spec) ⇒ Object



14
15
16
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 14

def run!(spec)
  super(spec) + run_mulang_analysis(compile_mulang_analysis(spec[:request], spec[:expectations]))
end

#run_mulang_analysis(analysis) ⇒ Object



28
29
30
31
32
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 28

def run_mulang_analysis(analysis)
  parse_response Mulang.analyse(analysis)
rescue JSON::ParserError
  raise Mumukit::CompilationError, "Can not handle mulang results for analysis #{analysis}"
end