Class: GraphicalTestsForRails::UserIntention

Inherits:
Object
  • Object
show all
Includes:
GraphicalTestsForRails, Names
Defined in:
lib/graphical_tests_for_rails/user-intention.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Names Classes: Applier

Constant Summary collapse

DISAMBIGUATE_BY_WORD_COUNT =
proc { |a, b| 
  # The 'compact' is needed to get rid of non-matching alternatives
  # in something like /(foo)|(bar)/.
  words = proc { | x | - x.raw_reason.compact.length }
  words[a] <=> words[b]
}
LINE_LABEL_REGEXP =
/\b(line)\s+(labels?)\b/
NOTE_REGEXP =
/\b(notes?)|(annotations?)\b/
CONTENT_REGEXP =
/\b(contents?)\b/
LINE_REGEXP =
/\b(lines?)\b/
SHAPED_GRAPHIC_REGEXP =
/\b(shaped\s+graphics?)|(shapes?)\b/
PLACE =
{
  [LINE_LABEL_REGEXP, NOTE_REGEXP] => USER_MEANS_LINE_LABEL_NOTE,
  [LINE_LABEL_REGEXP, CONTENT_REGEXP] => USER_MEANS_LINE_LABEL_CONTENT,
  [LINE_REGEXP, NOTE_REGEXP] => USER_MEANS_LINE_NOTE,
  [SHAPED_GRAPHIC_REGEXP, NOTE_REGEXP] => USER_MEANS_SHAPED_GRAPHIC_NOTE,
  [SHAPED_GRAPHIC_REGEXP, CONTENT_REGEXP] => USER_MEANS_SHAPED_GRAPHIC_CONTENT,
  [CONTENT_REGEXP] => USER_MEANS_ANY_CONTENT,
  [NOTE_REGEXP] => USER_MEANS_ANY_NOTE,
}
PLACER =
JumbledStringCanonicalizer.new(PLACE) {
  disambiguate &DISAMBIGUATE_BY_WORD_COUNT
  no_match_message { | source | "'#{source}' does not refer to any graphic."}
}
TEXTS =
{
  [/\b(ignore)\b/] => USER_MEANS_CONTAINS_IGNORE,
}
TEXTER =
JumbledStringCanonicalizer.new(TEXTS) {
  default nil
}
QUOTED_ARGS_REGEXPS =
[/\b(quote\w*)\b/, /\b(arg\w*)\b/]
ACTIONS =
{
  [/\b(skip)|(ignore)\b/] => USER_MEANS_DO_NOTHING,
  [/\b(user)\s+(actions?)\b/] + QUOTED_ARGS_REGEXPS => USER_MEANS_ACTOR_ACTION_WITH_QUOTE_ARGS,
  [/\b(claims?)\b/] + QUOTED_ARGS_REGEXPS => USER_MEANS_CLAIM_WITH_QUOTE_ARGS,
  [/\b(names?)\b/, /\b(pages?)\b/] => USER_MEANS_PAGE_NAME,
}
ACTIONER =
JumbledStringCanonicalizer.new(ACTIONS) {
  no_match_message { | source | "'#{source}' doesn't say what should happen in the test."}
  multiple_match_message { | source, pretty_reasons, raw_reasons | 
    pretty_reasons = pretty_reasons.sort_by { |r| r.length }
    "'#{source}' implies conflicting instructions:\n" + 
    "One is from these words: #{pretty_reasons[0]}\n" + 
    pretty_reasons[1..-1].collect { |r| "Another is from these: #{r}" }.join("\n")
  }
  
}
OTHER_KIND_REGEXP =
'(\b(other)\s+(kinds?)\s+(of)\b)'
OTHERWISE_REGEXP =
'(\botherwise\b)'
LINE_GUARDS =
{
  [/\b(with)\s+.*\b(one)\s+(word)\b/] => USER_MEANS_LINES_WITH_ONE_WORD,
  [/\b(ellipses)\b/] => USER_MEANS_LINE_ENDS_WITH_ELLIPSES,
  [/\b(text\.\.\.*)/] => USER_MEANS_LINE_ENDS_WITH_ELLIPSES,
  [/#{OTHERWISE_REGEXP}|#{OTHER_KIND_REGEXP}/] => USER_MEANS_REMAINING_LINES,
}
GUARDER =
JumbledStringCanonicalizer.new(LINE_GUARDS) {
  default nil
}

Constants included from Names

Names::USER_MEANS_ACTOR_ACTION_WITH_QUOTE_ARGS, Names::USER_MEANS_ANY_CONTENT, Names::USER_MEANS_ANY_NOTE, Names::USER_MEANS_CLAIM_WITH_QUOTE_ARGS, Names::USER_MEANS_CONTAINS_IGNORE, Names::USER_MEANS_DO_NOTHING, Names::USER_MEANS_LINES_WITH_ONE_WORD, Names::USER_MEANS_LINE_ENDS_WITH_ELLIPSES, Names::USER_MEANS_LINE_LABEL_CONTENT, Names::USER_MEANS_LINE_LABEL_NOTE, Names::USER_MEANS_LINE_NOTE, Names::USER_MEANS_PAGE_NAME, Names::USER_MEANS_REMAINING_LINES, Names::USER_MEANS_SHAPED_GRAPHIC_CONTENT, Names::USER_MEANS_SHAPED_GRAPHIC_NOTE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



102
103
104
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 102

def action
  @action
end

#line_guardObject (readonly)

Returns the value of attribute line_guard.



102
103
104
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 102

def line_guard
  @line_guard
end

#placeObject (readonly)

Returns the value of attribute place.



102
103
104
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 102

def place
  @place
end

#sourceObject (readonly)

Returns the value of attribute source.



102
103
104
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 102

def source
  @source
end

#textObject (readonly)

Returns the value of attribute text.



102
103
104
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 102

def text
  @text
end

Class Method Details

.understand(instruction) ⇒ Object



113
114
115
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 113

def self.understand(instruction)
  prog1(new) { | i | i.understand(instruction) }
end

Instance Method Details

#applies_to_graphic?(graphic) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 141

def applies_to_graphic?(graphic)
  graphic.send('has_' + place.value + '?')
end

#applies_to_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 160

def applies_to_line?(line)
  return true if line_guard.value.nil?
  send(line_guard.value + '_acceptable?', line)
end

#applies_to_text?(lines) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 150

def applies_to_text?(lines)
  return true if text.value.nil?
  send(text.value + '?', lines)
end

#apply(string, target, log = []) ⇒ Object



177
178
179
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 177

def apply(string, target, log = [])
  Applier.new(target, log).send("apply_" + action.value, string)
end

#catch_all_for_lines_acceptable?(line) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 173

def catch_all_for_lines_acceptable?(line)
  true
end

#contains_ignore?(lines) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 156

def contains_ignore?(lines)
  lines.any? { |l| l.strip.ends_with?("ignore") }
end

#ends_with_ellipses_acceptable?(line) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 169

def ends_with_ellipses_acceptable?(line)
  line.strip =~ /\.\.+$/
end

#inspectObject



111
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 111

def inspect; to_s; end

#line_with_one_word_acceptable?(line) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 165

def line_with_one_word_acceptable?(line)
  line.split(' ').length == 1
end

#lines_from(graphic) ⇒ Object



145
146
147
148
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 145

def lines_from(graphic)
  lines = graphic.send(place.value + '_lines')
  lines.collect { |l| l.strip }.find_all { |l| l != '' }
end

#to_sObject



104
105
106
107
108
109
110
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 104

def to_s
  retval = "'#{source}' produces:\n"
  retval += "\t#{action.value} if #{place.value}"
  retval += " && #{text.value}" if text.value 
  retval += " && #{line_guard.value}" if line_guard.value 
  retval
end

#understand(instruction) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 117

def understand(instruction)
  @source = instruction
  understand_place(instruction)
  understand_text(instruction)
  understand_action(instruction)
  understand_line_guard(instruction)
end

#understand_action(instruction) ⇒ Object



133
134
135
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 133

def understand_action(instruction)
  @action = ACTIONER.canonicalize(instruction)
end

#understand_line_guard(instruction) ⇒ Object



137
138
139
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 137

def understand_line_guard(instruction)
  @line_guard = GUARDER.canonicalize(instruction)
end

#understand_place(instruction) ⇒ Object



125
126
127
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 125

def understand_place(instruction)
  @place = PLACER.canonicalize(instruction)
end

#understand_text(instruction) ⇒ Object



129
130
131
# File 'lib/graphical_tests_for_rails/user-intention.rb', line 129

def understand_text(instruction)
  @text = TEXTER.canonicalize(instruction)
end