Module: SpecWatchr::EmacsConnection

Included in:
SpecWatchr
Defined in:
lib/rspec-rails-watchr-emacs.rb

Instance Method Summary collapse

Instance Method Details

#alist(hash) ⇒ Object



35
36
37
# File 'lib/rspec-rails-watchr-emacs.rb', line 35

def alist (hash)
  hash.merge(:magic_convert_to => :alist)
end

#elispify_symbol(symbol) ⇒ Object



44
45
46
# File 'lib/rspec-rails-watchr-emacs.rb', line 44

def elispify_symbol(symbol)
  symbol.to_s.gsub(/_/,'-') 
end

#eregisterObject



131
132
133
# File 'lib/rspec-rails-watchr-emacs.rb', line 131

def eregister
  esend :register => @enotify_slot_id, :handler_fn => :enotify_rspec_result_message_handler
end

#esend(object) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/rspec-rails-watchr-emacs.rb', line 82

def esend (object)
  msg = object_to_esexp object
  @sock.puts("|#{msg.length}|#{msg}")
  # @sock.print("|#{msg.length}|")
  # sleep 2
  # @sock.puts msg
end

#esend_results(results) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rspec-rails-watchr-emacs.rb', line 137

def esend_results(results)
  summ = extract_rspec_summary(results)
  status = summ[:status]
  message = { :id => @enotify_slot_id,
    :notification => {
      :text => @notification_message[status],
      :face => @notification_face[status],
      :help => format_help(summ),
      :mouse_1 => :enotify_rspec_mouse_1_handler},
    :data => results,
  }
  esend message
end

#extract_rspec_counts(results, line) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/rspec-rails-watchr-emacs.rb', line 100

def extract_rspec_counts(results, line)
  err_line = results.split("\n")[line]
  err_regex = /^(\d*)\sexamples?,\s(\d*)\s(errors?|failures?)[^\d]*((\d*)\spending)?/
  _, examples, errors, _, pending = (err_line.match err_regex).to_a
  summ = { :examples => examples.to_i, :errors => errors.to_i, :pending => pending.to_i }
  summ.merge(:status => rspec_status(summ))
end

#extract_rspec_summary(results) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rspec-rails-watchr-emacs.rb', line 108

def extract_rspec_summary(results)
  case @custom_extract_summary_proc
  when Proc
    @custom_extract_summary_proc.call(results)
  else
    begin
      extract_rspec_counts(results, @error_count_line)
    rescue
      puts "--- Error while matching error counts.".red
      print "--- Summary line number: ".yellow
      @error_count_line = STDIN.gets.to_i
      extract_rspec_summary results
    end
  end
end

#flatten(hash) ⇒ Object



38
39
40
# File 'lib/rspec-rails-watchr-emacs.rb', line 38

def flatten (hash)
  hash.merge(:magic_convert_to => :flat)
end

#format_help(summary) ⇒ Object



124
125
126
127
128
# File 'lib/rspec-rails-watchr-emacs.rb', line 124

def format_help(summary)
  h = "#{summary[:errors]} errors\n"
  h << ("#{summary[:pending]} pending\n" if summary[:pending]>0).to_s
  h << "\nmouse-1: switch to result buffer"
end

#hash_to_esexp(hash) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rspec-rails-watchr-emacs.rb', line 47

def hash_to_esexp (hash)
  h = hash.clone
  h.delete(:magic_convert_to)
  case hash[:magic_convert_to]
  when :alist
    res = h.map { |k, v| "(#{object_to_esexp k} . #{object_to_esexp v})" }
    "(#{res.join(' ')})"
  when :flat
    res = h.map { |k, v| "#{object_to_esexp k} #{object_to_esexp v}" }
    "(#{res.join(' ')})"
  else
    if hash.keys.reduce(true) { |base, el| base && Symbol === el }
      res = h.map { |k, v| "#{object_to_esexp keyword(k)} #{object_to_esexp v}" }
      "(#{res.join(' ')})"
    else
      h[:magic_convert_to] = :alist
      hash_to_esexp h
    end
  end
end

#keyword(symbol) ⇒ Object



41
42
43
# File 'lib/rspec-rails-watchr-emacs.rb', line 41

def keyword (symbol)
  :"#{symbol.inspect}"
end

#object_to_esexp(object) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rspec-rails-watchr-emacs.rb', line 67

def object_to_esexp (object)
  case object
  when String
    object.inspect
  when Array
    res = object.map { |el| object_to_esexp(el) }
    "(#{res.join(' ')})"
  when Symbol
    elispify_symbol(object)
  when Hash
    hash_to_esexp object
  else
    object.to_s
  end
end

#rspec_status(err_cnt) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/rspec-rails-watchr-emacs.rb', line 90

def rspec_status(err_cnt)
  if err_cnt[:errors] > 0
    :failure
  elsif err_cnt[:pending] > 0
    :pending
  else
    :success
  end
end