Class: Mumukit::Templates::MulangExpectationsHook

Inherits:
Hook
  • Object
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(HasTooShortBindings HasWrongCaseBindings HasMisspelledBindings)
GENERIC_SMELLS =
%w(IsLongCode HasCodeDuplication HasRedundantLambda HasRedundantIf DoesTypeTest HasRedundantBooleanComparison HasEmptyIfBranches)

Instance Attribute Summary

Attributes inherited from Hook

#config

Class Method Summary collapse

Instance Method Summary collapse

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



93
94
95
96
97
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 93

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

Instance Method Details

#compile(request) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 15

def compile(request)
  expectations, exceptions = compile_expectations_and_exceptions request
  mulang_code(request).analysis(
    expectations: expectations,
    smellsSet: {
      tag: 'AllSmells',
      exclude: (exceptions + default_smell_exceptions)
    },
    domainLanguage: domain_language)
end

#compile_content(content) ⇒ Object



71
72
73
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 71

def compile_content(content)
  content
end

#compile_expectation(expectation) ⇒ Object



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

def compile_expectation(expectation)
  Mumukit::Inspection::Expectation.parse(expectation).as_v2.to_h
end

#compile_expectations_and_exceptions(request) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 53

def compile_expectations_and_exceptions(request)
  expectations = []
  exceptions = []
  request[:expectations].each do |it|
    fill_expectations_and_excetions it.deep_symbolize_keys, expectations, exceptions
  end
  [expectations, exceptions]
end

#default_smell_exceptionsObject



41
42
43
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 41

def default_smell_exceptions
  []
end

#domain_languageObject



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

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

#fill_expectations_and_excetions(expectation, expectations, exceptions) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 62

def fill_expectations_and_excetions(expectation, expectations, exceptions)
  inspection = expectation[:inspection]
  if inspection&.start_with? 'Except:'
    exceptions << inspection[7..-1]
  else
    expectations << compile_expectation(expectation)
  end
end

#mulang_code(request) ⇒ Object



45
46
47
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 45

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

#mulang_languageObject



49
50
51
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 49

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

#parse_expectation(expectation) ⇒ Object



89
90
91
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 89

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

#parse_response(response) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 79

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!(analysis) ⇒ Object



26
27
28
29
30
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 26

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