Best Active_mocker_ruby code snippet using Sort.not
form_helper_spec.rb
Source:form_helper_spec.rb
...3 module Helpers4 describe FormHelper do5 router = ActionDispatch::Routing::RouteSet.new6 router.draw do7 resources :people, :notes8 namespace :admin do9 resources :comments10 end11 end12 include router.url_helpers13 # FIXME: figure out a cleaner way to get this behavior14 before do15 @controller = ActionView::TestCase::TestController.new16 @controller.instance_variable_set(:@_routes, router)17 @controller.class_eval { include router.url_helpers }18 @controller.view_context_class.class_eval { include router.url_helpers }19 end20 describe '#sort_link with default search_key' do21 subject { @controller.view_context22 .sort_link(23 [:main_app, Person.ransack(sorts: ['name desc'])],24 :name,25 controller: 'people'26 )27 }28 it { should match /people\?q(%5B|\[)s(%5D|\])=name\+asc/ }29 it { should match /sort_link desc/ }30 it { should match /Full Name ▼/ }31 end32 describe '#sort_url with default search_key' do33 subject { @controller.view_context34 .sort_url(35 [:main_app, Person.ransack(sorts: ['name desc'])],36 :name,37 controller: 'people'38 )39 }40 it { should match /people\?q(%5B|\[)s(%5D|\])=name\+asc/ }41 end42 describe '#sort_link with default search_key defined as symbol' do43 subject { @controller.view_context44 .sort_link(45 Person.ransack({ sorts: ['name desc'] }, search_key: :people_search),46 :name, controller: 'people'47 )48 }49 it { should match /people\?people_search(%5B|\[)s(%5D|\])=name\+asc/ }50 end51 describe '#sort_url with default search_key defined as symbol' do52 subject { @controller.view_context53 .sort_url(54 Person.ransack({ sorts: ['name desc'] }, search_key: :people_search),55 :name, controller: 'people'56 )57 }58 it { should match /people\?people_search(%5B|\[)s(%5D|\])=name\+asc/ }59 end60 describe '#sort_link desc through association table defined as symbol' do61 subject { @controller.view_context62 .sort_link(63 Person.ransack({ sorts: 'comments_body asc' }),64 :comments_body,65 controller: 'people'66 )67 }68 it { should match /people\?q(%5B|\[)s(%5D|\])=comments.body\+desc/ }69 it { should match /sort_link asc/ }70 it { should match /Body ▲/ }71 end72 describe '#sort_url desc through association table defined as symbol' do73 subject { @controller.view_context74 .sort_url(75 Person.ransack({ sorts: 'comments_body asc' }),76 :comments_body,77 controller: 'people'78 )79 }80 it { should match /people\?q(%5B|\[)s(%5D|\])=comments.body\+desc/ }81 end82 describe '#sort_link through association table defined as a string' do83 subject { @controller.view_context84 .sort_link(85 Person.ransack({ sorts: 'comments.body desc' }),86 'comments.body',87 controller: 'people'88 )89 }90 it { should match /people\?q(%5B|\[)s(%5D|\])=comments.body\+asc/ }91 it { should match /sort_link desc/ }92 it { should match /Comments.body ▼/ }93 end94 describe '#sort_url through association table defined as a string' do95 subject { @controller.view_context96 .sort_url(97 Person.ransack({ sorts: 'comments.body desc' }),98 'comments.body',99 controller: 'people'100 )101 }102 it { should match /people\?q(%5B|\[)s(%5D|\])=comments.body\+asc/ }103 end104 describe '#sort_link works even if search params are a blank string' do105 before { @controller.view_context.params[:q] = '' }106 specify {107 expect { @controller.view_context108 .sort_link(109 Person.ransack(@controller.view_context.params[:q]),110 :name,111 controller: 'people'112 )113 }.not_to raise_error114 }115 end116 describe '#sort_url works even if search params are a blank string' do117 before { @controller.view_context.params[:q] = '' }118 specify {119 expect { @controller.view_context120 .sort_url(121 Person.ransack(@controller.view_context.params[:q]),122 :name,123 controller: 'people'124 )125 }.not_to raise_error126 }127 end128 describe '#sort_link with search_key defined as a string' do129 subject { @controller.view_context130 .sort_link(131 Person.ransack(132 { sorts: ['name desc'] }, search_key: 'people_search'133 ),134 :name,135 controller: 'people'136 )137 }138 it { should match /people\?people_search(%5B|\[)s(%5D|\])=name\+asc/ }139 end140 describe '#sort_link with default_order defined with a string key' do141 subject { @controller.view_context142 .sort_link(143 [:main_app, Person.ransack()],144 :name,145 controller: 'people',146 default_order: 'desc'147 )148 }149 it { should_not match /default_order/ }150 end151 describe '#sort_url with default_order defined with a string key' do152 subject { @controller.view_context153 .sort_url(154 [:main_app, Person.ransack()],155 :name,156 controller: 'people',157 default_order: 'desc'158 )159 }160 it { should_not match /default_order/ }161 end162 describe '#sort_link with multiple search_keys defined as an array' do163 subject { @controller.view_context164 .sort_link(165 [:main_app, Person.ransack(sorts: ['name desc', 'email asc'])],166 :name, [:name, 'email DESC'],167 controller: 'people'168 )169 }170 it {171 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/172 )173 }174 it { should match /sort_link desc/ }175 it { should match /Full Name ▼/ }176 end177 describe '#sort_url with multiple search_keys defined as an array' do178 subject { @controller.view_context179 .sort_url(180 [:main_app, Person.ransack(sorts: ['name desc', 'email asc'])],181 :name, [:name, 'email DESC'],182 controller: 'people'183 )184 }185 it {186 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/187 )188 }189 end190 describe '#sort_link with multiple search_keys does not break on nil values & ignores them' do191 subject { @controller.view_context192 .sort_link(193 [:main_app, Person.ransack(sorts: ['name desc', nil, 'email', nil])],194 :name, [nil, :name, nil, 'email DESC', nil],195 controller: 'people'196 )197 }198 it {199 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/200 )201 }202 it { should match /sort_link desc/ }203 it { should match /Full Name ▼/ }204 end205 describe '#sort_url with multiple search_keys does not break on nil values & ignores them' do206 subject { @controller.view_context207 .sort_url(208 [:main_app, Person.ransack(sorts: ['name desc', nil, 'email', nil])],209 :name, [nil, :name, nil, 'email DESC', nil],210 controller: 'people'211 )212 }213 it {214 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/215 )216 }217 end218 describe '#sort_link with multiple search_keys should allow a label to be specified' do219 subject { @controller.view_context220 .sort_link(221 [:main_app, Person.ransack(sorts: ['name desc', 'email asc'])],222 :name, [:name, 'email DESC'],223 'Property Name',224 controller: 'people'225 )226 }227 it { should match /Property Name ▼/ }228 end229 describe '#sort_link with multiple search_keys should flip multiple fields specified without a direction' do230 subject { @controller.view_context231 .sort_link(232 [:main_app, Person.ransack(sorts: ['name desc', 'email asc'])],233 :name, [:name, :email],234 controller: 'people'235 )236 }237 it {238 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/239 )240 }241 it { should match /sort_link desc/ }242 it { should match /Full Name ▼/ }243 end244 describe '#sort_url with multiple search_keys should flip multiple fields specified without a direction' do245 subject { @controller.view_context246 .sort_url(247 [:main_app, Person.ransack(sorts: ['name desc', 'email asc'])],248 :name, [:name, :email],249 controller: 'people'250 )251 }252 it {253 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/254 )255 }256 end257 describe '#sort_link with multiple search_keys and default_order specified as a string' do258 subject { @controller.view_context259 .sort_link(260 [:main_app, Person.ransack()],261 :name, [:name, :email],262 controller: 'people',263 default_order: 'desc'264 )265 }266 it {267 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/268 )269 }270 it { should match /sort_link/ }271 it { should match /Full Name/ }272 end273 describe '#sort_url with multiple search_keys and default_order specified as a string' do274 subject { @controller.view_context275 .sort_url(276 [:main_app, Person.ransack()],277 :name, [:name, :email],278 controller: 'people',279 default_order: 'desc'280 )281 }282 it {283 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/284 )285 }286 end287 describe '#sort_link with multiple search_keys and default_order specified as a symbol' do288 subject { @controller.view_context289 .sort_link(290 [:main_app, Person.ransack()],291 :name, [:name, :email],292 controller: 'people',293 default_order: :desc294 )295 }296 it {297 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/298 )299 }300 it { should match /sort_link/ }301 it { should match /Full Name/ }302 end303 describe '#sort_url with multiple search_keys and default_order specified as a symbol' do304 subject { @controller.view_context305 .sort_url(306 [:main_app, Person.ransack],307 :name, [:name, :email],308 controller: 'people',309 default_order: :desc310 )311 }312 it {313 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/314 )315 }316 end317 describe '#sort_link with multiple search_keys should allow multiple default_orders to be specified' do318 subject { @controller.view_context319 .sort_link(320 [:main_app, Person.ransack],321 :name, [:name, :email],322 controller: 'people',323 default_order: { name: 'desc', email: 'asc' }324 )325 }326 it {327 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+asc/328 )329 }330 it { should match /sort_link/ }331 it { should match /Full Name/ }332 end333 describe '#sort_url with multiple search_keys should allow multiple default_orders to be specified' do334 subject { @controller.view_context335 .sort_url(336 [:main_app, Person.ransack],337 :name, [:name, :email],338 controller: 'people',339 default_order: { name: 'desc', email: 'asc' }340 )341 }342 it {343 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+asc/344 )345 }346 end347 describe '#sort_link with multiple search_keys with multiple default_orders should not override a specified order' do348 subject { @controller.view_context349 .sort_link(350 [:main_app, Person.ransack],351 :name, [:name, 'email desc'],352 controller: 'people',353 default_order: { name: 'desc', email: 'asc' }354 )355 }356 it {357 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/358 )359 }360 it { should match /sort_link/ }361 it { should match /Full Name/ }362 end363 describe '#sort_url with multiple search_keys with multiple default_orders should not override a specified order' do364 subject { @controller.view_context365 .sort_url(366 [:main_app, Person.ransack],367 :name, [:name, 'email desc'],368 controller: 'people',369 default_order: { name: 'desc', email: 'asc' }370 )371 }372 it {373 should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/374 )375 }376 end377 describe "#sort_link on polymorphic association should preserve association model name case" do378 subject { @controller.view_context379 .sort_link(380 [:main_app, Note.ransack],381 :notable_of_Person_type_name, "Notable",382 controller: 'notes'383 )384 }385 it { should match /notes\?q(%5B|\[)s(%5D|\])=notable_of_Person_type_name\+asc/ }386 it { should match /sort_link/ }387 it { should match /Notable/ }388 end389 describe "#sort_url on polymorphic association should preserve association model name case" do390 subject { @controller.view_context391 .sort_link(392 [:main_app, Note.ransack],393 :notable_of_Person_type_name, "Notable",394 controller: 'notes'395 )396 }397 it { should match /notes\?q(%5B|\[)s(%5D|\])=notable_of_Person_type_name\+asc/ }398 end399 context 'view has existing parameters' do400 describe '#sort_link should not remove existing params' do401 before { @controller.view_context.params[:exist] = 'existing' }402 subject {403 @controller.view_context.sort_link(404 Person.ransack(405 { sorts: ['name desc'] },406 search_key: 'people_search'407 ),408 :name,409 controller: 'people'410 )411 }412 it { should match /exist\=existing/ }413 end414 describe '#sort_url should not remove existing params' do415 before { @controller.view_context.params[:exist] = 'existing' }416 subject {417 @controller.view_context.sort_url(418 Person.ransack(419 { sorts: ['name desc'] },420 search_key: 'people_search'421 ),422 :name,423 controller: 'people'424 )425 }426 it { should match /exist\=existing/ }427 end428 context 'using a real ActionController::Parameter object' do429 describe 'with symbol q:, #sort_link should include search params' do430 subject { @controller.view_context.sort_link(Person.ransack, :name) }431 let(:params) { ActionController::Parameters.new(432 { :q => { name_eq: 'TEST' }, controller: 'people' }433 ) }434 before { @controller.instance_variable_set(:@params, params) }435 it {436 should match(437 /people\?q(%5B|\[)name_eq(%5D|\])=TEST&q(%5B|\[)s(%5D|\])438 =name\+asc/x,439 )440 }441 end442 describe 'with symbol q:, #sort_url should include search params' do443 subject { @controller.view_context.sort_url(Person.ransack, :name) }444 let(:params) { ActionController::Parameters.new(445 { :q => { name_eq: 'TEST' }, controller: 'people' }446 ) }447 before { @controller.instance_variable_set(:@params, params) }448 it {449 should match(450 /people\?q(%5B|\[)name_eq(%5D|\])=TEST&q(%5B|\[)s(%5D|\])451 =name\+asc/x,452 )453 }454 end455 describe "with string 'q', #sort_link should include search params" do456 subject { @controller.view_context.sort_link(Person.ransack, :name) }457 let(:params) {458 ActionController::Parameters.new(459 { 'q' => { name_eq: 'Test2' }, controller: 'people' }460 ) }461 before { @controller.instance_variable_set(:@params, params) }462 it {463 should match(464 /people\?q(%5B|\[)name_eq(%5D|\])=Test2&q(%5B|\[)s(%5D|\])465 =name\+asc/x,466 )467 }468 end469 describe "with string 'q', #sort_url should include search params" do470 subject { @controller.view_context.sort_url(Person.ransack, :name) }471 let(:params) {472 ActionController::Parameters.new(473 { 'q' => { name_eq: 'Test2' }, controller: 'people' }474 ) }475 before { @controller.instance_variable_set(:@params, params) }476 it {477 should match(478 /people\?q(%5B|\[)name_eq(%5D|\])=Test2&q(%5B|\[)s(%5D|\])479 =name\+asc/x,480 )481 }482 end483 end484 end485 describe '#sort_link with hide order indicator set to true' do486 subject { @controller.view_context487 .sort_link(488 [:main_app, Person.ransack(sorts: ['name desc'])],489 :name,490 controller: 'people',491 hide_indicator: true492 )493 }494 it { should match /Full Name/ }495 it { should_not match /▼|▲/ }496 end497 describe '#sort_link with hide order indicator set to false' do498 subject { @controller.view_context499 .sort_link(500 [:main_app, Person.ransack(sorts: ['name desc'])],501 :name,502 controller: 'people',503 hide_indicator: false504 )505 }506 it { should match /Full Name ▼/ }507 end508 describe '#sort_link with config set with custom up_arrow' do509 before do510 Ransack.configure { |c| c.custom_arrows = { up_arrow: "\u{1F446}" } }511 end512 after do513 Ransack.configure { |c| c.custom_arrows = { up_arrow: "▼" } }514 end515 subject { @controller.view_context516 .sort_link(517 [:main_app, Person.ransack(sorts: ['name desc'])],518 :name,519 controller: 'people',520 hide_indicator: false521 )522 }523 it { should match /Full Name \u{1F446}/ }524 end525 describe '#sort_link with config set with custom down_arrow' do526 before do527 Ransack.configure { |c| c.custom_arrows = { down_arrow: "\u{1F447}" } }528 end529 after do530 Ransack.configure { |c| c.custom_arrows = { down_arrow: "▲" } }531 end532 subject { @controller.view_context533 .sort_link(534 [:main_app, Person.ransack(sorts: ['name asc'])],535 :name,536 controller: 'people',537 hide_indicator: false538 )539 }540 it { should match /Full Name \u{1F447}/ }541 end542 describe '#sort_link with config set to hide arrows' do543 before do544 Ransack.configure { |c| c.hide_sort_order_indicators = true }545 end546 after do547 Ransack.configure { |c| c.hide_sort_order_indicators = false }548 end549 subject { @controller.view_context550 .sort_link(551 [:main_app, Person.ransack(sorts: ['name desc'])],552 :name,553 controller: 'people'554 )555 }556 it { should_not match /▼|▲/ }557 end558 describe '#sort_link with config set to show arrows (default setting)' do559 before do560 Ransack.configure { |c| c.hide_sort_order_indicators = false }561 end562 subject { @controller.view_context563 .sort_link(564 [:main_app, Person.ransack(sorts: ['name desc'])],565 :name,566 controller: 'people'567 )568 }569 it { should match /Full Name ▼/ }570 end571 describe '#sort_link with config set to show arrows and a default arrow set' do572 before do573 Ransack.configure do |c|574 c.hide_sort_order_indicators = false575 c.custom_arrows = { default_arrow: "defaultarrow" }576 end577 end578 after do579 Ransack.configure do |c|580 c.custom_arrows = { default_arrow: nil }581 end582 end583 subject { @controller.view_context584 .sort_link(585 [:main_app, Person.ransack],586 :name,587 controller: 'people'588 )589 }590 it { should match /Full Name defaultarrow/ }591 end592 describe '#sort_link w/config to hide arrows + custom arrow, hides all' do593 before do594 Ransack.configure do |c|595 c.hide_sort_order_indicators = true596 c.custom_arrows = { down_arrow: 'down', default_arrow: "defaultarrow" }597 end598 end599 after do600 Ransack.configure do |c|601 c.hide_sort_order_indicators = false602 c.custom_arrows = { down_arrow: '▲' }603 end604 end605 subject { @controller.view_context606 .sort_link(607 [:main_app, Person.ransack(sorts: ['name desc'])],608 :name,609 controller: 'people'610 )611 }612 it { should_not match /▼|down|defaultarrow/ }613 end614 describe '#sort_link with config set to show arrows + custom arrow' do615 before do616 Ransack.configure do |c|617 c.hide_sort_order_indicators = false618 c.custom_arrows = { up_arrow: 'up-value' }619 end620 end621 after do622 Ransack.configure do |c|623 c.hide_sort_order_indicators = false624 c.custom_arrows = { up_arrow: '▼' }625 end626 end627 subject { @controller.view_context628 .sort_link(629 [:main_app, Person.ransack(sorts: ['name desc'])],630 :name,631 controller: 'people'632 )633 }634 it { should match /▲|up-value/ }635 end636 describe '#sort_link with a block' do637 subject { @controller.view_context638 .sort_link(639 [:main_app, Person.ransack(sorts: ['name desc'])],640 :name,641 controller: 'people'642 ) { 'Block label' }643 }644 it { should match /Block label ▼/ }645 end646 describe '#sort_link with class option' do647 subject { @controller.view_context648 .sort_link(649 [:main_app, Person.ransack(sorts: ['name desc'])],650 :name,651 class: 'people', controller: 'people'652 )653 }654 it { should match /class="sort_link desc people"/ }655 it { should_not match /people\?class=people/ }656 end657 describe '#sort_link with class option workaround' do658 it "generates a correct link and prints a deprecation" do659 expect do660 link = @controller.view_context661 .sort_link(662 [:main_app, Person.ransack(sorts: ['name desc'])],663 :name,664 'name',665 { controller: 'people' },666 class: 'people'667 )668 expect(link).to match(/class="sort_link desc people"/)669 expect(link).not_to match(/people\?class=people/)670 end.to output(671 /Passing two trailing hashes to `sort_link` is deprecated, merge the trailing hashes into a single one\. \(called at #{Regexp.escape(__FILE__)}:/672 ).to_stderr673 end674 end675 describe '#sort_link with data option' do676 subject { @controller.view_context677 .sort_link(678 [:main_app, Person.ransack(sorts: ['name desc'])],679 :name,680 data: { turbo_action: :advance }, controller: 'people'681 )682 }683 it { should match /data-turbo-action="advance"/ }684 it { should_not match /people\?data%5Bturbo_action%5D=advance/ }685 end686 describe '#search_form_for with default format' do687 subject { @controller.view_context688 .search_form_for(Person.ransack) {} }689 it { should match /action="\/people"/ }690 end691 describe '#search_form_for with pdf format' do692 subject {693 @controller.view_context694 .search_form_for(Person.ransack, format: :pdf) {}695 }696 it { should match /action="\/people.pdf"/ }697 end698 describe '#search_form_for with json format' do...
sort_spec.rb
Source:sort_spec.rb
...4 it "returns a new array sorted based on comparing elements with <=>" do5 a = [1, -2, 3, 9, 1, 5, -5, 1000, -5, 2, -10, 14, 6, 23, 0]6 a.sort.should == [-10, -5, -5, -2, 0, 1, 1, 2, 3, 5, 6, 9, 14, 23, 1000]7 end8 it "does not affect the original Array" do9 a = [3, 1, 2]10 a.sort.should == [1, 2, 3]11 a.should == [3, 1, 2]12 a = [0, 15, 2, 3, 4, 6, 14, 5, 7, 12, 8, 9, 1, 10, 11, 13]13 b = a.sort14 a.should == [0, 15, 2, 3, 4, 6, 14, 5, 7, 12, 8, 9, 1, 10, 11, 13]15 b.should == (0..15).to_a16 end17 it "sorts already-sorted Arrays" do18 (0..15).to_a.sort.should == (0..15).to_a19 end20 it "sorts reverse-sorted Arrays" do21 (0..15).to_a.reverse.sort.should == (0..15).to_a22 end23 it "sorts Arrays that consist entirely of equal elements" do24 a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]25 a.sort.should == a26 b = Array.new(15).map { ArraySpecs::SortSame.new }27 b.sort.should == b28 end29 it "sorts Arrays that consist mostly of equal elements" do30 a = [1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]31 a.sort.should == [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]32 end33 it "does not return self even if the array would be already sorted" do34 a = [1, 2, 3]35 sorted = a.sort36 sorted.should == a37 sorted.should_not equal(a)38 end39 it "properly handles recursive arrays" do40 empty = ArraySpecs.empty_recursive_array41 empty.sort.should == empty42 array = [[]]; array << array43 array.sort.should == [[], array]44 end45 it "uses #<=> of elements in order to sort" do46 a = ArraySpecs::MockForCompared.new47 b = ArraySpecs::MockForCompared.new48 c = ArraySpecs::MockForCompared.new49 ArraySpecs::MockForCompared.compared?.should == false50 [a, b, c].sort.should == [c, b, a]51 ArraySpecs::MockForCompared.compared?.should == true52 end53 it "does not deal with exceptions raised by unimplemented or incorrect #<=>" do54 o = Object.new55 lambda {56 [o, 1].sort57 }.should raise_error(ArgumentError)58 end59 it "may take a block which is used to determine the order of objects a and b described as -1, 0 or +1" do60 a = [5, 1, 4, 3, 2]61 a.sort.should == [1, 2, 3, 4, 5]62 a.sort {|x, y| y <=> x}.should == [5, 4, 3, 2, 1]63 end64 it "raises an error when a given block returns nil" do65 lambda { [1, 2].sort {} }.should raise_error(ArgumentError)66 end67 it "does not call #<=> on contained objects when invoked with a block" do68 a = Array.new(25)69 (0...25).each {|i| a[i] = ArraySpecs::UFOSceptic.new }70 a.sort { -1 }.should be_an_instance_of(Array)71 end72 it "does not call #<=> on elements when invoked with a block even if Array is large (Rubinius #412)" do73 a = Array.new(1500)74 (0...1500).each {|i| a[i] = ArraySpecs::UFOSceptic.new }75 a.sort { -1 }.should be_an_instance_of(Array)76 end77 it "completes when supplied a block that always returns the same result" do78 a = [2, 3, 5, 1, 4]79 a.sort { 1 }.should be_an_instance_of(Array)80 a.sort { 0 }.should be_an_instance_of(Array)81 a.sort { -1 }.should be_an_instance_of(Array)82 end83 it "does not freezes self during being sorted" do84 a = [1, 2, 3]85 a.sort { |x,y| a.frozen?.should == false; x <=> y }86 end87 it "returns the specified value when it would break in the given block" do88 [1, 2, 3].sort{ break :a }.should == :a89 end90 it "uses the sign of Bignum block results as the sort result" do91 a = [1, 2, 5, 10, 7, -4, 12]92 begin93 class Bignum;94 alias old_spaceship <=>95 def <=>(other)96 raise97 end98 end99 a.sort {|n, m| (n - m) * (2 ** 200)}.should == [-4, 1, 2, 5, 7, 10, 12]100 ensure101 class Bignum102 alias <=> old_spaceship103 end104 end105 end106 it "compares values returned by block with 0" do107 a = [1, 2, 5, 10, 7, -4, 12]108 a.sort { |n, m| n - m }.should == [-4, 1, 2, 5, 7, 10, 12]109 a.sort { |n, m|110 ArraySpecs::ComparableWithFixnum.new(n-m)111 }.should == [-4, 1, 2, 5, 7, 10, 12]112 lambda {113 a.sort { |n, m| (n - m).to_s }114 }.should raise_error(ArgumentError)115 end116 it "sorts an array that has a value shifted off without a block" do117 a = Array.new(20, 1)118 a.shift119 a[0] = 2120 a.sort.last.should == 2121 end122 it "sorts an array that has a value shifted off with a block" do123 a = Array.new(20, 1)124 a.shift125 a[0] = 2126 a.sort {|x, y| x <=> y }.last.should == 2127 end128 it "raises an error if objects can't be compared" do129 a=[ArraySpecs::Uncomparable.new, ArraySpecs::Uncomparable.new]130 lambda {a.sort}.should raise_error(ArgumentError)131 end132 # From a strange Rubinius bug133 it "handles a large array that has been pruned" do134 pruned = ArraySpecs::LargeArray.dup.delete_if { |n| n !~ /^test./ }135 pruned.sort.should == ArraySpecs::LargeTestArraySorted136 end137 it "does not return subclass instance on Array subclasses" do138 ary = ArraySpecs::MyArray[1, 2, 3]139 ary.sort.should be_an_instance_of(Array)140 end141end142describe "Array#sort!" do143 it "sorts array in place using <=>" do144 a = [1, -2, 3, 9, 1, 5, -5, 1000, -5, 2, -10, 14, 6, 23, 0]145 a.sort!146 a.should == [-10, -5, -5, -2, 0, 1, 1, 2, 3, 5, 6, 9, 14, 23, 1000]147 end148 it "sorts array in place using block value if a block given" do149 a = [0, 15, 2, 3, 4, 6, 14, 5, 7, 12, 8, 9, 1, 10, 11, 13]150 a.sort! { |x, y| y <=> x }.should == (0..15).to_a.reverse151 end152 it "returns self if the order of elements changed" do153 a = [6, 7, 2, 3, 7]154 a.sort!.should equal(a)155 a.should == [2, 3, 6, 7, 7]156 end157 it "returns self even if makes no modification" do158 a = [1, 2, 3, 4, 5]159 a.sort!.should equal(a)160 a.should == [1, 2, 3, 4, 5]161 end162 it "properly handles recursive arrays" do163 empty = ArraySpecs.empty_recursive_array164 empty.sort!.should == empty165 array = [[]]; array << array166 array.sort!.should == array167 end168 it "uses #<=> of elements in order to sort" do169 a = ArraySpecs::MockForCompared.new170 b = ArraySpecs::MockForCompared.new171 c = ArraySpecs::MockForCompared.new172 ArraySpecs::MockForCompared.compared?.should == false173 [a, b, c].sort!.should == [c, b, a]174 ArraySpecs::MockForCompared.compared?.should == true175 end176 it "does not call #<=> on contained objects when invoked with a block" do177 a = Array.new(25)178 (0...25).each {|i| a[i] = ArraySpecs::UFOSceptic.new }179 a.sort! { -1 }.should be_an_instance_of(Array)180 end181 it "does not call #<=> on elements when invoked with a block even if Array is large (Rubinius #412)" do182 a = Array.new(1500)183 (0...1500).each {|i| a[i] = ArraySpecs::UFOSceptic.new }184 a.sort! { -1 }.should be_an_instance_of(Array)185 end186 it "completes when supplied a block that always returns the same result" do187 a = [2, 3, 5, 1, 4]188 a.sort!{ 1 }.should be_an_instance_of(Array)189 a.sort!{ 0 }.should be_an_instance_of(Array)190 a.sort!{ -1 }.should be_an_instance_of(Array)191 end192 it "raises a RuntimeError on a frozen array" do193 lambda { ArraySpecs.frozen_array.sort! }.should raise_error(RuntimeError)194 end195 it "returns the specified value when it would break in the given block" do...
not
Using AI Code Generation
1p = Sort.new(a,b,c,d,e)2p = Sort.new(a,b,c,d,e)3p = Sort.new(a,b,c,d,e)4p = Sort.new(a,b,c,d,e)5p = Sort.new(a,b,c,d,e)6p = Sort.new(a,b,c,d,e)7p = Sort.new(a,b,c,d,e)
not
Using AI Code Generation
1 Sort.new(self)2 def initialize(arr)3 Sort.new(self)4 def initialize(arr)5 Sort.new(self)6 def initialize(arr)7 Sort.new(self)8 def initialize(arr)9 Sort.new(self)10 def initialize(arr)11 Sort.new(self)12 def initialize(arr)13 Sort.new(self)14 def initialize(arr)15 Sort.new(self)16 def initialize(arr)
not
Using AI Code Generation
1puts Sort.new([1,2,3,4,5,6,7,8,9,10]).not2puts Sort.new([1,2,3,4,5,6,7,8,9,10]).not.not3puts Sort.new([1,2,3,4,5,6,7,8,9,10]).not.not.not4puts Sort.new([1,2,3,4,5,6,7,8,9,10]).not5puts Sort.new([1,2,3,4,5,6,7,8,9,10]).not.not6puts Sort.new([1,2,3,4,5,6,7,8,9,10]).not.not.not7puts Sort.new([1
not
Using AI Code Generation
1 a.sort! { |x, y| y <=> x }2 a.sort! { |x, y| not x <=> y }3 a.sort! { |x, y| not y <=> x }4 a.sort! { |x, y| not x <=> y }5 a.sort! { |x, y| not y <=> x }
not
Using AI Code Generation
1 def not(array)2 array.sort { |a, b| b <=> a }3array = gets.split.map(&:to_i)4array = sort.not(array)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!