Class: Mumukit::Templates::MulangExpectationsHook

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

Constant Summary collapse

LOGIC_SMELLS =
Mulang::Expectation::LOGIC_SMELLS
FUNCTIONAL_SMELLS =
Mulang::Expectation::FUNCTIONAL_SMELLS
OBJECT_ORIENTED_SMELLS =
Mulang::Expectation::OBJECT_ORIENTED_SMELLS
IMPERATIVE_SMELLS =
Mulang::Expectation::IMPERATIVE_SMELLS
EXPRESSIVENESS_SMELLS =
Mulang::Expectation::EXPRESSIVENESS_SMELLS
GENERIC_SMELLS =
Mulang::Expectation::GENERIC_SMELLS

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



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

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

Instance Method Details

#autocorrection_rulesObject



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

def autocorrection_rules
  {}
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_mulang_analysis(request, expectations) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 18

def compile_mulang_analysis(request, expectations)
  mulang_code(request).analysis({
    expectations: expectations[:ast],
    customExpectations: expectations[:custom],
    smellsSet: {
      tag: 'AllSmells',
      exclude: (expectations[:exceptions] + default_smell_exceptions)
    },
    domainLanguage: domain_language
  }.merge({
    originalLanguage: original_language,
    autocorrectionRules: autocorrection_rules.try { |it| positive_and_negative it }.presence,
    normalizationOptions: normalization_options(request).presence
  }.compact))
end

#default_smell_exceptionsObject



59
60
61
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 59

def default_smell_exceptions
  []
end

#domain_languageObject



40
41
42
43
44
45
46
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 40

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

#mulang_code(request) ⇒ Object



63
64
65
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 63

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

#mulang_languageObject



67
68
69
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 67

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

#normalization_options(request) ⇒ Object



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

def normalization_options(request)
  request.dig(:settings, :normalization_options) || {}
end

#original_languageObject



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

def original_language
end

#parse_expectation(expectation) ⇒ Object



85
86
87
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 85

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

#parse_response(response) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/mumukit/templates/mulang_expectations_hook.rb', line 75

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



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

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