aboutsummaryrefslogtreecommitdiffstats
path: root/query.h
blob: f041957323c44d103810f04d45c2f3e9ddc50611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  /********************************************************************\
  * BitlBee -- An IRC to other IM-networks gateway                     *
  *                                                                    *
  * Copyright 2002-2004 Wilmer van der Gaast and others                *
  \********************************************************************/

/* Questions to the user (mainly authorization requests from IM)        */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License with
  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  Suite 330, Boston, MA  02111-1307  USA
*/

#ifndef _QUERY_H
#define _QUERY_H

typedef void (*query_callback) ( void *data );

typedef struct query
{
	struct im_connection *ic;
	char *question;
	query_callback yes, no, free;
	void *data;
	struct query *next;
} query_t;

query_t *query_add( irc_t *irc, struct im_connection *ic, char *question,
                    query_callback yes, query_callback no, query_callback free,
                    void *data );
void query_del( irc_t *irc, query_t *q );
void query_del_by_conn( irc_t *irc, struct im_connection *ic );
void query_answer( irc_t *irc, query_t *q, int ans );

#endif
c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
require File.dirname(__FILE__) + '/../../spec_helper'

# assert_select plugins for Rails
#
# Copyright (c) 2006 Assaf Arkin, under Creative Commons Attribution and/or MIT License
# Developed for http://co.mments.com
# Code and documention: http://labnotes.org

class AssertSelectController < ActionController::Base

  def response=(content)
    @content = content
  end

  #NOTE - this is commented because response is implemented in lib/spec/rails/context/controller
  # def response(&block)
  #   @update = block
  # end
  # 
  def html()
    render :text=>@content, :layout=>false, :content_type=>Mime::HTML
    @content = nil
  end

  def rjs()
    update = @update
    render :update do |page|
      update.call page
    end
    @update = nil
  end

  def xml()
    render :text=>@content, :layout=>false, :content_type=>Mime::XML
    @content = nil
  end

  def rescue_action(e)
    raise e
  end

end

class AssertSelectMailer < ActionMailer::Base

  def test(html)
    recipients "test <test@test.host>"
    from "test@test.host"
    subject "Test e-mail"
    part :content_type=>"text/html", :body=>html
  end

end

module AssertSelectSpecHelpers
  def render_html(html)
    @controller.response = html
    get :html
  end

  def render_rjs(&block)
    clear_response
    @controller.response &block
    get :rjs
  end

  def render_xml(xml)
    @controller.response = xml
    get :xml
  end
  
  private
    # necessary for 1.2.1
    def clear_response
      render_html("")
    end
end

unless defined?(SpecFailed)
  SpecFailed = Spec::Expectations::ExpectationNotMetError 
end

describe "should have_tag", :behaviour_type => :controller do
  include AssertSelectSpecHelpers
  controller_name :assert_select
  integrate_views

  it "should find specific numbers of elements" do
    render_html %Q{<div id="1"></div><div id="2"></div>}
    response.should have_tag( "div" )
    response.should have_tag("div", 2)
    lambda { response.should_not have_tag("div") }.should raise_error(SpecFailed, "should not have tag(\"div\"), but did")

    lambda { response.should have_tag("div", 3) }.should raise_error(SpecFailed)
    lambda { response.should have_tag("p") }.should raise_error(SpecFailed)
  end

  it "should expect to find elements when using true" do
    render_html %Q{<div id="1"></div><div id="2"></div>}
    response.should have_tag( "div", true )
    lambda { response.should have_tag( "p", true )}.should raise_error(SpecFailed)
  end

  it "should expect to not find elements when using false" do
    render_html %Q{<div id="1"></div><div id="2"></div>}
    response.should have_tag( "p", false )
    lambda { response.should have_tag( "div", false )}.should raise_error(SpecFailed)
  end


  it "should match submitted text using text or regexp" do
    render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
    response.should have_tag("div", "foo")
    response.should have_tag("div", /(foo|bar)/)
    response.should have_tag("div", :text=>"foo")
    response.should have_tag("div", :text=>/(foo|bar)/)

    lambda { response.should have_tag("div", "bar") }.should raise_error(SpecFailed)
    lambda { response.should have_tag("div", :text=>"bar") }.should raise_error(SpecFailed)
    lambda { response.should have_tag("p", :text=>"foo") }.should raise_error(SpecFailed)

    lambda { response.should have_tag("div", /foobar/) }.should raise_error(SpecFailed)
    lambda { response.should have_tag("div", :text=>/foobar/) }.should raise_error(SpecFailed)
    lambda { response.should have_tag("p", :text=>/foo/) }.should raise_error(SpecFailed)
  end
  
  it "should use submitted message" do
    render_html %Q{nothing here}
    lambda {
      response.should have_tag("div", {}, "custom message")
    }.should raise_error(SpecFailed, /custom message/)
  end

  it "should match submitted html" do
    render_html %Q{<p>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</p>}
    text = "\"This is not a big problem,\" he said."
    html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said."
    response.should have_tag("p", text)
    lambda { response.should have_tag("p", html) }.should raise_error(SpecFailed)
    response.should have_tag("p", :html=>html)
    lambda { response.should have_tag("p", :html=>text) }.should raise_error(SpecFailed)

    # # No stripping for pre.
    render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>}
    text = "\n\"This is not a big problem,\" he said.\n"
    html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n"
    response.should have_tag("pre", text)
    lambda { response.should have_tag("pre", html) }.should raise_error(SpecFailed)
    response.should have_tag("pre", :html=>html)
    lambda { response.should have_tag("pre", :html=>text) }.should raise_error(SpecFailed)
  end

  it "should match number of instances" do
    render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
    response.should have_tag("div", 2)
    lambda { response.should have_tag("div", 3) }.should raise_error(SpecFailed)
    response.should have_tag("div", 1..2)
    lambda { response.should have_tag("div", 3..4) }.should raise_error(SpecFailed)
    response.should have_tag("div", :count=>2)
    lambda { response.should have_tag("div", :count=>3) }.should raise_error(SpecFailed)
    response.should have_tag("div", :minimum=>1)
    response.should have_tag("div", :minimum=>2)
    lambda { response.should have_tag("div", :minimum=>3) }.should raise_error(SpecFailed)
    response.should have_tag("div", :maximum=>2)
    response.should have_tag("div", :maximum=>3)
    lambda { response.should have_tag("div", :maximum=>1) }.should raise_error(SpecFailed)
    response.should have_tag("div", :minimum=>1, :maximum=>2)
    lambda { response.should have_tag("div", :minimum=>3, :maximum=>4) }.should raise_error(SpecFailed)
  end

  it "substitution values" do
    render_html %Q{<div id="1">foo</div><div id="2">foo</div><span id="3"></span>}
    response.should have_tag("div#?", /\d+/) do |elements| #using do/end
      elements.size.should == 2
    end
    response.should have_tag("div#?", /\d+/) { |elements| #using {}
      elements.size.should == 2
    }
    lambda {
      response.should have_tag("div#?", /\d+/) do |elements|
        elements.size.should == 3
      end
    }.should raise_error(SpecFailed, "expected: 3,\n     got: 2 (using ==)")
    
    lambda {
      response.should have_tag("div#?", /\d+/) { |elements|
        elements.size.should == 3
      }
    }.should raise_error(SpecFailed, "expected: 3,\n     got: 2 (using ==)")

    response.should have_tag("div#?", /\d+/) do |elements|
      elements.size.should == 2
      with_tag("#1")
      with_tag("#2")
      without_tag("#3")
    end 
  end
  
  #added for RSpec
  it "nested tags in form" do
    render_html %Q{
      <form action="test">
        <input type="text" name="email">
      </form>
      <form action="other">
        <input type="text" name="other_input">
      </form>
    }
    response.should have_tag("form[action=test]") { |form|
      with_tag("input[type=text][name=email]")
    }
    response.should have_tag("form[action=test]") { |form|
      with_tag("input[type=text][name=email]")
    }
    
    lambda {
      response.should have_tag("form[action=test]") { |form|
        with_tag("input[type=text][name=other_input]")
      }
    }.should raise_error(SpecFailed)
    
    lambda {
      response.should have_tag("form[action=test]") {
        with_tag("input[type=text][name=other_input]")
      }
    }.should raise_error(SpecFailed)
  end
  
  it "should should report the correct line number for a nested failure" do
    pending("bug report: http://rubyforge.org/tracker/index.php?func=detail&aid=11602&group_id=797&atid=3149") do
      render_html %Q{
        <form action="test">
          <input type="text" name="email">
        </form>
      }
      begin
        response.should have_tag("form[action=test]") {
          with_tag("input[type=text][name=other_input]")
        }
      rescue => e
        e.backtrace[3].to_s.should =~ /assert_select_spec.rb:238/
      end
    end
  end
  
  it "beatles" do
    unless defined?(BEATLES)
      BEATLES = [
        ["John", "Guitar"],
        ["George", "Guitar"],
        ["Paul", "Bass"],
        ["Ringo", "Drums"]
      ]
    end

    render_html %Q{
      <div id="beatles">
        <div class="beatle">
          <h2>John</h2><p>Guitar</p>
        </div>
        <div class="beatle">
          <h2>George</h2><p>Guitar</p>
        </div>
        <div class="beatle">
          <h2>Paul</h2><p>Bass</p>
        </div>
        <div class="beatle">
          <h2>Ringo</h2><p>Drums</p>
        </div>
      </div>          
    }
    response.should have_tag("div#beatles>div[class=\"beatle\"]", 4)

    response.should have_tag("div#beatles>div.beatle") {
      BEATLES.each { |name, instrument|
        with_tag("div.beatle>h2", name)
        with_tag("div.beatle>p", instrument)
        without_tag("div.beatle>span")
      }
    }
  end

  it "assert_select_text_match" do
    render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>}
    response.should have_tag("div") do |divs|
      with_tag("div", "foo")
      with_tag("div", "bar")
      with_tag("div", /\w*/)
      with_tag("div", /\w*/, :count=>2)
      without_tag("div", :text=>"foo", :count=>2)
      with_tag("div", :html=>"<span>bar</span>")
      with_tag("div", :html=>"<span>bar</span>")
      with_tag("div", :html=>/\w*/)
      with_tag("div", :html=>/\w*/, :count=>2)
      without_tag("div", :html=>"<span>foo</span>", :count=>2)
    end
  end


  it "assert_select_from_rjs with one item" do
    render_rjs do |page|
      page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
    end
    response.should have_tag("div") { |elements|
      elements.size.should == 2
      with_tag("#1")
      with_tag("#2")
    }
    
    lambda {
      response.should have_tag("div") { |elements|
        elements.size.should == 2
        with_tag("#1")
        with_tag("#3")
      }
    }.should raise_error(SpecFailed)

    lambda {
      response.should have_tag("div") { |elements|
        elements.size.should == 2
        with_tag("#1")
        without_tag("#2")
      }
    }.should raise_error(SpecFailed, "should not have tag(\"#2\"), but did")

    lambda {
      response.should have_tag("div") { |elements|
        elements.size.should == 3
        with_tag("#1")
        with_tag("#2")
      }
    }.should raise_error(SpecFailed)


    response.should have_tag("div#?", /\d+/) { |elements|
      with_tag("#1")
      with_tag("#2")
    }
  end
  
  it "assert_select_from_rjs with multiple items" do
    render_rjs do |page|
      page.replace_html "test", "<div id=\"1\">foo</div>"
      page.replace_html "test2", "<div id=\"2\">foo</div>"
    end
    response.should have_tag("div")
    response.should have_tag("div") { |elements|
      elements.size.should == 2
      with_tag("#1")
      with_tag("#2")
    }

    lambda {
      response.should have_tag("div") { |elements|
        with_tag("#3")
      }
    }.should raise_error(SpecFailed)
  end
end

describe "css_select", :behaviour_type => :controller do
  include AssertSelectSpecHelpers
  controller_name :assert_select
  integrate_views

  it "can select tags from html" do
    render_html %Q{<div id="1"></div><div id="2"></div>}
    css_select("div").size.should == 2
    css_select("p").size.should == 0
  end


  it "can select nested tags from html" do
    render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
    response.should have_tag("div#?", /\d+/) { |elements|
      css_select(elements[0], "div").should have(1).element
      css_select(elements[1], "div").should have(1).element
    }
    response.should have_tag("div") {
      css_select("div").should have(2).elements
      css_select("div").each { |element|
        # Testing as a group is one thing
        css_select("#1,#2").should have(2).elements
        # Testing individually is another
        css_select("#1").should have(1).element
        css_select("#2").should have(1).element
      }
    }
  end

  it "can select nested tags from rjs (one result)" do
    render_rjs do |page|
      page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
    end
    css_select("div").should have(2).elements
    css_select("#1").should have(1).element
    css_select("#2").should have(1).element
  end

  it "can select nested tags from rjs (two results)" do
    render_rjs do |page|
      page.replace_html "test", "<div id=\"1\">foo</div>"
      page.replace_html "test2", "<div id=\"2\">foo</div>"
    end
    css_select("div").should have(2).elements
    css_select("#1").should have(1).element
    css_select("#2").should have(1).element
  end
  
end

describe "have_rjs behaviour", :behaviour_type => :controller do
  include AssertSelectSpecHelpers
  controller_name :assert_select
  integrate_views

  before(:each) do
    render_rjs do |page|
      page.replace "test1", "<div id=\"1\">foo</div>"
      page.replace_html "test2", "<div id=\"2\">bar</div><div id=\"3\">none</div>"
      page.insert_html :top, "test3", "<div id=\"4\">loopy</div>"
      page.hide "test4"
      page["test5"].hide
    end
  end
  
  it "should pass if any rjs exists" do
    response.should have_rjs
  end
  
  it "should fail if no rjs exists" do
    render_rjs do |page|
    end
    lambda do
      response.should have_rjs
    end.should raise_error(SpecFailed)
  end
  
  it "should find all rjs from multiple statements" do
    response.should have_rjs do
      with_tag("#1")
      with_tag("#2")
      with_tag("#3")
      # with_tag("#4")
      # with_tag("#5")
    end
  end

  it "should find by id" do
    response.should have_rjs("test1") { |rjs|
      rjs.size.should == 1
      with_tag("div", 1)
      with_tag("div#1", "foo")
    }
    
    lambda do
      response.should have_rjs("test1") { |rjs|
        rjs.size.should == 1
        without_tag("div#1", "foo")
      }
    end.should raise_error(SpecFailed, "should not have tag(\"div#1\", \"foo\"), but did")

    response.should have_rjs("test2") { |rjs|
      rjs.size.should == 2
      with_tag("div", 2)
      with_tag("div#2", "bar")
      with_tag("div#3", "none")
    }
    # response.should have_rjs("test4")
    # response.should have_rjs("test5")
  end
  
  # specify "should find rjs using :hide" do
  #   response.should have_rjs(:hide)
  #   response.should have_rjs(:hide, "test4")
  #   response.should have_rjs(:hide, "test5")
  #   lambda do
  #     response.should have_rjs(:hide, "test3")
  #   end.should raise_error(SpecFailed)
  # end

  it "should find rjs using :replace" do
    response.should have_rjs(:replace) { |rjs|
      with_tag("div", 1)
      with_tag("div#1", "foo")
    }
    response.should have_rjs(:replace, "test1") { |rjs|
      with_tag("div", 1)
      with_tag("div#1", "foo")
    }
    lambda {
      response.should have_rjs(:replace, "test2")
    }.should raise_error(SpecFailed)

    lambda {
      response.should have_rjs(:replace, "test3")
    }.should raise_error(SpecFailed)
  end

  it "should find rjs using :replace_html" do
    response.should have_rjs(:replace_html) { |rjs|
      with_tag("div", 2)
      with_tag("div#2", "bar")
      with_tag("div#3", "none")
    }

    response.should have_rjs(:replace_html, "test2") { |rjs|
      with_tag("div", 2)
      with_tag("div#2", "bar")
      with_tag("div#3", "none")
    }

    lambda {
      response.should have_rjs(:replace_html, "test1")
    }.should raise_error(SpecFailed)

    lambda {
      response.should have_rjs(:replace_html, "test3")
    }.should raise_error(SpecFailed)
  end
    
  it "should find rjs using :insert_html (non-positioned)" do
    response.should have_rjs(:insert_html) { |rjs|
      with_tag("div", 1)
      with_tag("div#4", "loopy")
    }

    response.should have_rjs(:insert_html, "test3") { |rjs|
      with_tag("div", 1)
      with_tag("div#4", "loopy")
    }

    lambda {
      response.should have_rjs(:insert_html, "test1")
    }.should raise_error(SpecFailed)

    lambda {
      response.should have_rjs(:insert_html, "test2")
    }.should raise_error(SpecFailed)
  end

  it "should find rjs using :insert (positioned)" do
    render_rjs do |page|
      page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
      page.insert_html :bottom, "test2", "<div id=\"2\">bar</div>"
      page.insert_html :before, "test3", "<div id=\"3\">none</div>"
      page.insert_html :after, "test4", "<div id=\"4\">loopy</div>"
    end
    response.should have_rjs(:insert, :top) do
      with_tag("div", 1)
      with_tag("#1")
    end
    response.should have_rjs(:insert, :top, "test1") do
      with_tag("div", 1)
      with_tag("#1")
    end
    lambda {
      response.should have_rjs(:insert, :top, "test2")
    }.should raise_error(SpecFailed)
    response.should have_rjs(:insert, :bottom) {|rjs|
      with_tag("div", 1)
      with_tag("#2")
    }
    response.should have_rjs(:insert, :bottom, "test2") {|rjs|
      with_tag("div", 1)
      with_tag("#2")
    }
    response.should have_rjs(:insert, :before) {|rjs|
      with_tag("div", 1)
      with_tag("#3")
    }
    response.should have_rjs(:insert, :before, "test3") {|rjs|
      with_tag("div", 1)
      with_tag("#3")
    }
    response.should have_rjs(:insert, :after) {|rjs|
      with_tag("div", 1)
      with_tag("#4")
    }
    response.should have_rjs(:insert, :after, "test4") {|rjs|
      with_tag("div", 1)
      with_tag("#4")
    }
  end
end

describe "send_email behaviour", :behaviour_type => :controller do
  include AssertSelectSpecHelpers
  controller_name :assert_select
  integrate_views

  before(:each) do
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
  end

  after(:each) do
    ActionMailer::Base.deliveries.clear
  end

  it "should fail with nothing sent" do
    response.should_not send_email
    lambda {
      response.should send_email{}
    }.should raise_error(SpecFailed, /No e-mail in delivery list./)
  end
  
  it "should pass otherwise" do
    AssertSelectMailer.deliver_test "<div><p>foo</p><p>bar</p></div>"
    response.should send_email
    lambda {
      response.should_not send_email
    }.should raise_error(SpecFailed)
    response.should send_email{}
    response.should send_email {
      with_tag("div:root") {
        with_tag("p:first-child", "foo")
        with_tag("p:last-child", "bar")
      }
    }
    
    lambda {
      response.should_not send_email
    }.should raise_error(SpecFailed, "should not send email, but did")
  end

end

# describe "An rjs call to :visual_effect, a 'should have_rjs' spec with",
#   :behaviour_type => :view do
#     
#   before do
#     render 'rjs_spec/visual_effect'
#   end
# 
#   it "should pass with the correct element name" do
#     response.should have_rjs(:effect, :fade, 'mydiv')
#   end
#   
#   it "should fail the wrong element name" do
#     lambda {
#       response.should have_rjs(:effect, :fade, 'wrongname')
#     }.should raise_error(SpecFailed)
#   end
#   
#   it "should fail with the correct element but the wrong command" do
#     lambda {
#       response.should have_rjs(:effect, :puff, 'mydiv')
#     }.should raise_error(SpecFailed)
#   end
#   
# end
#   
# describe "An rjs call to :visual_effect for a toggle, a 'should have_rjs' spec with",
#   :behaviour_type => :view do
#     
#   before do
#     render 'rjs_spec/visual_toggle_effect'
#   end
#   
#   it "should pass with the correct element name" do
#     response.should have_rjs(:effect, :toggle_blind, 'mydiv')
#   end
#   
#   it "should fail with the wrong element name" do
#     lambda {
#       response.should have_rjs(:effect, :toggle_blind, 'wrongname')
#     }.should raise_error(SpecFailed)
#   end
#   
#   it "should fail the correct element but the wrong command" do
#     lambda {
#       response.should have_rjs(:effect, :puff, 'mydiv')
#     }.should raise_error(SpecFailed)
#   end
#   
# end

describe "string.should have_tag", :behaviour_type => :helper do
  include AssertSelectSpecHelpers

  it "should find root element" do
    "<p>a paragraph</p>".should have_tag("p", "a paragraph")
  end

  it "should not find non-existent element" do
    lambda do
      "<p>a paragraph</p>".should have_tag("p", "wrong text")
    end.should raise_error(SpecFailed)
  end

  it "should find child element" do
    "<div><p>a paragraph</p></div>".should have_tag("p", "a paragraph")
  end

  it "should find nested element" do
    "<div><p>a paragraph</p></div>".should have_tag("div") do
      with_tag("p", "a paragraph")
    end
  end

  it "should not find wrong nested element" do
    lambda do
      "<div><p>a paragraph</p></div>".should have_tag("div") do
        with_tag("p", "wrong text")
      end
    end.should raise_error(SpecFailed)
  end
end

describe "have_tag", :behaviour_type => :controller do
  include AssertSelectSpecHelpers
  controller_name :assert_select
  integrate_views

  it "should work exactly the same as assert_select" do
    render_html %Q{
      <div id="wrapper">foo
        <div class="piece">
          <h3>Text</h3>
        </div>
        <div class="piece">
          <h3>Another</h3>
        </div>      
      </div>
    }

    assert_select "#wrapper .piece h3", :text => "Text"
    assert_select "#wrapper .piece h3", :text => "Another"

    response.should have_tag("#wrapper .piece h3", :text => "Text")
    response.should have_tag("#wrapper .piece h3", :text => "Another")
  end
end