Class: Test::Unit::Assertions::AssertRjs

Inherits:
Object
  • Object
show all
Defined in:
lib/assert2/rjs.rb

Direct Known Subclasses

ALERT, CALL, INSERT_HTML, REMOVE, REPLACE_HTML, TOGGLE

Defined Under Namespace

Classes: ALERT, CALL, INSERT_HTML, REMOVE, REPLACE, REPLACE_HTML, TOGGLE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(js, command, scope) ⇒ AssertRjs

Returns a new instance of AssertRjs.



12
13
14
# File 'lib/assert2/rjs.rb', line 12

def initialize(js, command, scope)
  @js, @command, @scope = js, command, scope
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



16
17
18
# File 'lib/assert2/rjs.rb', line 16

def command
  @command
end

#failure_messageObject (readonly)

Returns the value of attribute failure_message.



16
17
18
# File 'lib/assert2/rjs.rb', line 16

def failure_message
  @failure_message
end

#jsObject (readonly)

Returns the value of attribute js.



16
17
18
# File 'lib/assert2/rjs.rb', line 16

def js
  @js
end

#scopeObject (readonly)

Returns the value of attribute scope.



16
17
18
# File 'lib/assert2/rjs.rb', line 16

def scope
  @scope
end

Instance Method Details

#assert_xhtml(why, &block) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/assert2/rjs.rb', line 51

def assert_xhtml(why, &block)
    #  scope.assert_xhtml @text, complain(why), &block      
  matcher = BeHtmlWith.new(self, &block)
  matcher.message = complain(why)
  matcher.matches?(@text, &block)
  @failure_message = matcher.failure_message
end

#complain(about) ⇒ Object

TODO implement assert_no_rjs by upgrading scope to UnScope



30
31
32
# File 'lib/assert2/rjs.rb', line 30

def complain(about)
  "#{ command } #{ about }\n#{ js }"
end

#flunk(about) ⇒ Object



34
35
36
# File 'lib/assert2/rjs.rb', line 34

def flunk(about)
  @failure_message ||= complain(about)
end

#hash_match(sample, reference) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/assert2/rjs.rb', line 107

def hash_match(sample, reference)
  reference.each do |key, value|
    sample[key] == value or
      value.kind_of?(Regexp) && sample[key] =~ value or 
        return false
  end
  
  return true
end

#match(kode) ⇒ Object

TODO rename js to sample



19
20
21
22
23
24
25
26
# File 'lib/assert2/rjs.rb', line 19

def match(kode)
  RKelly.parse(js).pointcut(kode).
      matches.each do |updater|
    updater.grep(RKelly::Nodes::ArgumentsNode).each do |thang|
      yield thang
    end
  end
end

#match_or_flunk(why) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/assert2/rjs.rb', line 38

def match_or_flunk(why)  
  @text = @text.to_s
  @matcher = @matcher.to_s if @matcher.kind_of?(Symbol)
  return if Regexp.new(@matcher) =~ @text or @text.index(@matcher)
  @failure_message = scope.build_message(complain(why),
                              "<?> expected to be =~\n<?>.", @text, @matcher)
end

#props_to_hash(props) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/assert2/rjs.rb', line 91

def props_to_hash(props)
  case props
    when RKelly::Nodes::ObjectLiteralNode
      hash = {}
      
      props.value.each do |thang|
        hash[thang.name.to_sym] = eval(thang.value.value)
      end

      return hash

    else
      return nil
  end
end

#pwn_call(*args, &block) ⇒ Object

TODO use or reject the block



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/assert2/rjs.rb', line 59

def pwn_call *args, &block  #  TODO  use or reject the block
  target, matchers_backup = args[0], args[1..-1]
  
  match "#{target}()" do |thang|
    matchers = matchers_backup.dup
    
    thang.value.each do |arg|
#         p arg
      @matcher = matchers.first # or return @text
      
      if @matcher.kind_of?(Hash) and
         hash = props_to_hash(arg) 
        hash_match(hash, @matcher) or break  #  TODO  rename to match_hash
      else
        @text = eval(arg.value)
        @matcher.to_s == @text or /#{ @matcher }/ =~ @text or break
      end
      
      matchers.shift
    end

    matchers.empty? and 
      matchers_backup.length == thang.value.length and 
      return @text 
  end
  
  matchers = matchers_backup.inspect

  flunk("#{ command } to #{ target } with arguments #{ 
                    matchers } not found in #{ js }")
end

#wrap_expectation(whatever) ⇒ Object

ERGO blog about how bottom-up TDD decouples

ERGO  assert_no_rjs_ ...without! ... oh the humanity!


49
# File 'lib/assert2/rjs.rb', line 49

def wrap_expectation whatever;  yield;  end