How to use to_regexp method of Helpers Package

Best Capybara code snippet using Helpers.to_regexp

schema_dumper_spec.rb

Source:schema_dumper_spec.rb Github

copy

Full Screen

...42 class ::Comment < ActiveRecord::Base ; end43 end44 it "should include foreign_key definition" do45 with_foreign_key Post, :user_id, :users, :id do46 expect(dump_posts).to match(to_regexp(%q{t.foreign_key ["user_id"], "users", ["id"]}))47 end48 end49 it "should include foreign_key name" do50 with_foreign_key Post, :user_id, :users, :id, :name => "yippee" do51 expect(dump_posts).to match /foreign_key.*user_id.*users.*id.*:name => "yippee"/52 end53 end54 it "should sort foreign_key definitions" do55 with_foreign_keys Comment, [ [ :post_id, :posts, :id ], [ :commenter_id, :users, :id ]] do56 expect(dump_schema).to match(/foreign_key.+commenter_id.+foreign_key.+post_id/m)57 end58 end59 context "with constraint dependencies" do60 it "should sort in Posts => Comments direction" do61 with_foreign_key Comment, :post_id, :posts, :id do62 expect(dump_schema).to match(%r{create_table "posts".*create_table "comments"}m)63 end64 end65 it "should sort in Comments => Posts direction" do66 with_foreign_key Post, :first_comment_id, :comments, :id do67 expect(dump_schema).to match(%r{create_table "comments".*create_table "posts"}m)68 end69 end70 it "should handle regexp in ignore_tables" do71 with_foreign_key Comment, :post_id, :posts, :id do72 dump = dump_schema(:ignore => /post/)73 expect(dump).to match /create_table "comments"/74 expect(dump).not_to match /create_table "posts"/75 end76 end77 end78 context "with date default" do79 if SchemaPlusHelpers.postgresql?80 it "should dump the default hash expr as now()" do81 with_additional_column Post, :posted_at, :datetime, :default => :now do82 expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr\s*=>)\s*"now\(\)"\s*\}})83 end84 end85 it "should dump the default hash expr as CURRENT_TIMESTAMP" do86 with_additional_column Post, :posted_at, :datetime, :default => {:expr => 'date \'2001-09-28\''} do87 expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*'2001-09-28 00:00:00'})88 end89 end90 it "can dump a complex default expression" do91 with_additional_column Post, :name, :string, :default => {:expr => 'substring(random()::text from 3 for 6)'} do92 expect(dump_posts).to match(%r{t\.string\s+"name",\s*(?:default:|:default\s*=>)\s*{\s*(?:expr:|:expr\s*=>)\s*"\\"substring\\"\(\(random\(\)\)::text, 3, 6\)"\s*}})93 end94 end95 end96 if SchemaPlusHelpers.sqlite3?97 it "should dump the default hash expr as now" do98 with_additional_column Post, :posted_at, :datetime, :default => :now do99 expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr =>)\s*"\(DATETIME\('now'\)\)"\s*\}})100 end101 end102 it "should dump the default hash expr string as now" do103 with_additional_column Post, :posted_at, :datetime, :default => { :expr => "(DATETIME('now'))" } do104 expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr =>)\s*"\(DATETIME\('now'\)\)"\s*\}})105 end106 end107 it "should dump the default value normally" do108 with_additional_column Post, :posted_at, :string, :default => "now" do109 expect(dump_posts).to match(%r{t\.string\s*"posted_at",\s*(?:default:|:default =>)\s*"now"})110 end111 end112 end113 end114 it "should leave out :default when default was changed to null" do115 ActiveRecord::Migration.suppress_messages do116 ActiveRecord::Migration.change_column_default :posts, :string_no_default, nil117 end118 expect(dump_posts).to match(%r{t\.string\s+"string_no_default"\s*$})119 end120 it "should include foreign_key options" do121 with_foreign_key Post, :user_id, :users, :id, :on_update => :cascade, :on_delete => :set_null do122 expect(dump_posts).to match(to_regexp(%q{t.foreign_key ["user_id"], "users", ["id"], :on_update => :cascade, :on_delete => :set_null}))123 end124 end125 it "should include index definition" do126 with_index Post, :user_id do127 expect(dump_posts).to match(to_regexp(%q{t.index ["user_id"]}))128 end129 end130 it "should include index name" do131 with_index Post, :user_id, :name => "custom_name" do132 expect(dump_posts).to match(to_regexp(%q{t.index ["user_id"], :name => "custom_name"}))133 end134 end135 136 it "should define unique index" do137 with_index Post, :user_id, :name => "posts_user_id_index", :unique => true do138 expect(dump_posts).to match(to_regexp(%q{t.index ["user_id"], :name => "posts_user_id_index", :unique => true}))139 end140 end141 it "should sort indexes" do142 with_index Post, :user_id do143 with_index Post, :short_id do144 expect(dump_posts).to match(/on_short_id.+on_user_id/m)145 end146 end147 end148 unless SchemaPlusHelpers.mysql?149 it "should include index order" do150 with_index Post, [:user_id, :first_comment_id, :short_id], :order => { :user_id => :asc, :first_comment_id => :desc } do151 expect(dump_posts).to match(%r{t.index \["user_id", "first_comment_id", "short_id"\],.*:order => {"user_id" => :asc, "first_comment_id" => :desc, "short_id" => :asc}})152 end153 end154 end155 if SchemaPlusHelpers.postgresql?156 it "should define case insensitive index" do157 with_index Post, [:body, :string_no_default], :case_sensitive => false do158 expect(dump_posts).to match(to_regexp(%q{t.index ["body", "string_no_default"], :name => "index_posts_on_body_and_string_no_default", :case_sensitive => false}))159 end160 end161 it "should define index with type cast" do162 with_index Post, [:integer_col], :name => "index_with_type_cast", :expression => "LOWER(integer_col::text)" do163 expect(dump_posts).to match(to_regexp(%q{t.index :name => "index_with_type_cast", :expression => "lower((integer_col)::text)"}))164 end165 end166 it "should define case insensitive index with mixed ids and strings" do167 with_index Post, [:user_id, :str_short, :short_id, :body], :case_sensitive => false do168 expect(dump_posts).to match(to_regexp(%q{t.index ["user_id", "str_short", "short_id", "body"], :name => "index_posts_on_user_id_and_str_short_and_short_id_and_body", :case_sensitive => false}))169 end170 end171 [:integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean].each do |col_type|172 col_name = "#{col_type}_col"173 it "should define case insensitive index that includes an #{col_type}" do174 with_index Post, [:user_id, :str_short, col_name, :body], :case_sensitive => false do175 expect(dump_posts).to match(to_regexp(%Q!t.index ["user_id", "str_short", "#{col_name}", "body"], :name => "index_posts_on_user_id_and_str_short_and_#{col_name}_and_body", :case_sensitive => false!))176 end177 end178 end179 it "should define conditions" do180 with_index Post, :user_id, :name => "posts_user_id_index", :conditions => "user_id IS NOT NULL" do181 expect(dump_posts).to match(to_regexp(%q{t.index ["user_id"], :name => "posts_user_id_index", :conditions => "(user_id IS NOT NULL)"}))182 end183 end184 it "should define expression" do185 with_index Post, :name => "posts_freaky_index", :expression => "USING hash (least(id, user_id))" do186 expect(dump_posts).to match(to_regexp(%q{t.index :name => "posts_freaky_index", :kind => "hash", :expression => "LEAST(id, user_id)"}))187 end188 end189 it "should dump unique: true with expression (Issue #142)" do190 with_index Post, :name => "posts_user_body_index", :unique => true, :expression => "BTRIM(LOWER(body))" do191 expect(dump_posts).to match(%r{#{to_regexp(%q{t.index :name => "posts_user_body_index", :unique => true, :expression => "btrim(lower(body))"})}$})192 end193 end194 it "should not define :case_sensitive => false with non-trivial expression" do195 with_index Post, :name => "posts_user_body_index", :expression => "BTRIM(LOWER(body))" do196 expect(dump_posts).to match(%r{#{to_regexp(%q{t.index :name => "posts_user_body_index", :expression => "btrim(lower(body))"})}$})197 end198 end199 it "should define kind" do200 with_index Post, :name => "posts_body_index", :expression => "USING hash (body)" do201 expect(dump_posts).to match(to_regexp(%q{t.index ["body"], :name => "posts_body_index", :kind => "hash"}))202 end203 end204 it "should not include index order for non-ordered index types" do205 with_index Post, :user_id, :kind => :hash do206 expect(dump_posts).to match(to_regexp(%q{t.index ["user_id"], :name => "index_posts_on_user_id", :kind => "hash"}))207 expect(dump_posts).not_to match(%r{:order})208 end209 end210 end211 unless SchemaPlusHelpers.sqlite3?212 context "with cyclic foreign key constraints" do213 before (:all) do214 ActiveRecord::Base.connection.add_foreign_key(Comment.table_name, :commenter_id, User.table_name, :id)215 ActiveRecord::Base.connection.add_foreign_key(Comment.table_name, :post_id, Post.table_name, :id)216 ActiveRecord::Base.connection.add_foreign_key(Post.table_name, :first_comment_id, Comment.table_name, :id)217 ActiveRecord::Base.connection.add_foreign_key(Post.table_name, :user_id, User.table_name, :id)218 ActiveRecord::Base.connection.add_foreign_key(User.table_name, :first_post_id, Post.table_name, :id)219 end220 it "should not raise an error" do221 expect { dump_schema }.to_not raise_error222 end223 it "should dump constraints after the tables they reference" do224 expect(dump_schema).to match(%r{create_table "comments".*foreign_key.*\["first_comment_id"\], "comments", \["id"\]}m)225 expect(dump_schema).to match(%r{create_table "posts".*foreign_key.*\["first_post_id"\], "posts", \["id"\]}m)226 expect(dump_schema).to match(%r{create_table "posts".*foreign_key.*\["post_id"\], "posts", \["id"\]}m)227 expect(dump_schema).to match(%r{create_table "users".*foreign_key.*\["commenter_id"\], "users", \["id"\]}m)228 expect(dump_schema).to match(%r{create_table "users".*foreign_key.*\["user_id"\], "users", \["id"\]}m)229 end230 end231 end232 protected233 def to_regexp(string)234 Regexp.new(Regexp.escape(string))235 end236 def with_additional_column(model, column_name, column_type, options)237 table_columns = model.columns.reject{|column| column.name == 'id'}238 ActiveRecord::Migration.suppress_messages do239 ActiveRecord::Migration.create_table model.table_name, :force => true do |t|240 table_columns.each do |column|241 t.column column.name, column.type, :default => column.default242 end243 t.column column_name, column_type, options244 end245 end246 yield247 end...

Full Screen

Full Screen

text_query.rb

Source:text_query.rb Github

copy

Full Screen

...10 unless @expected_text.is_a?(Regexp)11 @expected_text = Capybara::Helpers.normalize_whitespace(@expected_text)12 end13 @options ||= {}14 @search_regexp = Capybara::Helpers.to_regexp(@expected_text, nil, exact?)15 assert_valid_keys16 end17 def resolve_for(node)18 @node = node19 @actual_text = text(node, @type)20 @count = @actual_text.scan(@search_regexp).size21 end22 def failure_message23 super << build_message(true)24 end25 def negative_failure_message26 super << build_message(false)27 end28 def description29 if @expected_text.is_a?(Regexp)30 "text matching #{@expected_text.inspect}"31 else32 "#{"exact " if exact?}text #{@expected_text.inspect}"33 end34 end35 private36 def exact?37 options.fetch(:exact, Capybara.exact_text)38 end39 def build_message(report_on_invisible)40 message = String.new()41 unless (COUNT_KEYS & @options.keys).empty?42 message << " but found #{@count} #{Capybara::Helpers.declension('time', 'times', @count)}"43 end44 message << " in #{@actual_text.inspect}"45 details_message = []46 if @node and !@expected_text.is_a? Regexp47 insensitive_regexp = Capybara::Helpers.to_regexp(@expected_text, Regexp::IGNORECASE)48 insensitive_count = @actual_text.scan(insensitive_regexp).size49 if insensitive_count != @count50 details_message << "it was found #{insensitive_count} #{Capybara::Helpers.declension("time", "times", insensitive_count)} using a case insensitive search"51 end52 end53 if @node and check_visible_text? and report_on_invisible54 begin55 invisible_text = text(@node, :all)56 invisible_count = invisible_text.scan(@search_regexp).size57 if invisible_count != @count58 details_message << ". it was found #{invisible_count} #{Capybara::Helpers.declension("time", "times", invisible_count)} including non-visible text"59 end60 rescue61 # An error getting the non-visible text (if element goes out of scope) should not affect the response...

Full Screen

Full Screen

to_regexp

Using AI Code Generation

copy

Full Screen

1puts Helpers.to_regexp('foo', 'bar')2puts Helpers.to_regexp('foo', 'bar', 'baz')3puts Helpers.to_regexp('foo', 'bar')4puts Helpers.to_regexp('foo', 'bar', 'baz')5puts Helpers.to_regexp('foo', 'bar')6puts Helpers.to_regexp('foo', 'bar', 'baz')7puts Helpers.to_regexp('foo', 'bar')8puts Helpers.to_regexp('foo', 'bar', 'baz')9puts Helpers.to_regexp('foo', 'bar')10puts Helpers.to_regexp('foo', 'bar', 'baz')11puts Helpers.to_regexp('foo', 'bar')12puts Helpers.to_regexp('foo', 'bar', 'baz')13puts Helpers.to_regexp('foo', 'bar')14puts Helpers.to_regexp('foo', 'bar', 'baz')15puts Helpers.to_regexp('foo', 'bar')16puts Helpers.to_regexp('foo', 'bar', 'baz')

Full Screen

Full Screen

to_regexp

Using AI Code Generation

copy

Full Screen

1 Regexp.new(self)2 Regexp.new(self)3 Regexp.new(self)

Full Screen

Full Screen

to_regexp

Using AI Code Generation

copy

Full Screen

1 Regexp.new(self)2String.send(:include, Helpers)3 Regexp.new(self)4 Regexp.new(self)5 Regexp.new(self)

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Capybara automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful