Skip to content

Commit

Permalink
Revert "Merge pull request #637 from janosch-x/master"
Browse files Browse the repository at this point in the history
This reverts commit b925ba8, reversing
changes made to 9e0b844.
  • Loading branch information
tagliala committed Oct 17, 2015
1 parent b925ba8 commit c96871a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 147 deletions.
1 change: 0 additions & 1 deletion client_side_validations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Gem::Specification.new do |s|

s.add_dependency 'rails', '>= 4.0.0', '< 4.3.0'
s.add_dependency 'jquery-rails', '>= 3.1.2', '< 5.0.0'
s.add_dependency 'js_regex', '~> 1.0'

s.add_development_dependency 'appraisal', '~> 2.0'
s.add_development_dependency 'coveralls', '~> 0.8.1'
Expand Down
16 changes: 13 additions & 3 deletions lib/client_side_validations/core_ext/regexp.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
class Regexp
require 'js_regex'

def as_json(options = nil)
JsRegex.new(self).to_h
str = inspect
.sub('\\A' , '^')
.sub('\\Z' , '$')
.sub('\\z' , '$')
.sub(/^\// , '')
.sub(/\/[a-z]*$/ , '')
.gsub(/\(\?#.+\)/ , '')
.gsub(/\(\?-\w+:/ , '(')
.gsub(/\s/ , '')
opts = []
opts << 'i' if (self.options & Regexp::IGNORECASE) > 0
opts << 'm' if (self.options & Regexp::MULTILINE) > 0
{ source: Regexp.new(str).source, options: opts.join }
end

def to_json(options = nil)
Expand Down
3 changes: 0 additions & 3 deletions test/action_view/cases/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def _routes
resources :comments
end

resources :format_things

root to: 'main#index'
end

Expand Down Expand Up @@ -87,7 +85,6 @@ def setup

@post = Post.new
@comment = Comment.new
@format_thing = FormatThing.new

if defined?(ActionView::OutputFlow)
@view_flow = ActionView::OutputFlow.new
Expand Down
52 changes: 0 additions & 52 deletions test/action_view/cases/test_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- encoding: utf-8 -*-

require 'action_view/cases/helper'

class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
Expand Down Expand Up @@ -640,54 +638,4 @@ def test_number_format_with_locale

assert_dom_equal expected, output_buffer
end

def test_field_with_format_a
assert_field_with_format_has_source(:a, 'a')
end

def test_field_with_format_backslash
assert_field_with_format_has_source(:backslash, '\\\\\\\\')
end

def test_field_with_format_space
# regression test for issue #460
assert_field_with_format_has_source(:space, ' ')
end

def test_field_with_format_escaped_space
assert_field_with_format_has_source(:escaped_space, '\\\\ ')
end

def test_field_with_format_ascii_escape
assert_field_with_format_has_source(:ascii_escape, '\\\\x41')
end

def test_field_with_format_unicode_escape
assert_field_with_format_has_source(:unicode_escape, '\\\\u263A')
end

def test_field_with_format_unicode_literal
assert_field_with_format_has_source(:unicode_literal, '☺')
end

def test_field_with_format_newline_escape
assert_field_with_format_has_source(:newline_escape, '\\\\n')
end

def test_field_with_format_newline_literal
assert_field_with_format_has_source(:newline_literal, '\\\\n')
end

def assert_field_with_format_has_source(field, expected_source)
form_for(@format_thing, validate: true) do |f|
concat f.text_field(field)
end

validators = {"format_thing[#{field}]" => {format: [{message: 'is invalid', with: {source: expected_source, options: 'g'}}]}}
expected = whole_form('/format_things', 'new_format_thing', 'new_format_thing', validators: validators) do
form_field('input', "format_thing_#{field}", "format_thing[#{field}]", 'text')
end

assert_dom_equal expected, output_buffer
end
end
1 change: 0 additions & 1 deletion test/action_view/models.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'active_model'
require 'client_side_validations/active_model'
require 'action_view/models/comment'
require 'action_view/models/format_thing'
require 'action_view/models/post'
39 changes: 0 additions & 39 deletions test/action_view/models/format_thing.rb

This file was deleted.

69 changes: 21 additions & 48 deletions test/core_ext/cases/test_core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,100 +3,73 @@

class CoreExtTest < MiniTest::Test
def test_regexp_replace_A_and_Z
test_regexp = /\A\Z/ # \Z allows optional newline before end
expected_regexp = { source: '^(?=\\\\n?$)', options: 'g' }
test_regexp = /\A\Z/
expected_regexp = { source: '^$', options: '' }
assert_equal expected_regexp, test_regexp.as_json
end

def test_regexp_replace_a_and_z
test_regexp = /\A\z/
expected_regexp = { source: '^$', options: 'g' }
expected_regexp = { source: '^$', options: '' }
assert_equal expected_regexp, test_regexp.as_json
end

def test_regexp_to_json
expected_regexp = { source: '^$', options: 'g' }
assert_equal expected_regexp, /\A\z/.to_json(nil)
expected_regexp = { source: '^$', options: '' }
assert_equal expected_regexp, /\A\Z/.to_json(nil)
end

def test_regexp_in_hash_to_json
expected_regexp = { hello: { source: 'world', options: 'gi' } }
expected_regexp = { hello: { source: 'world', options: 'i' } }
hash = { hello: /world/i }
assert_equal expected_regexp.to_json, hash.to_json
end

def test_regexp_encode_json
assert_equal '//', //.encode_json(nil)
assert_equal "//", //.encode_json(nil)
end

def test_regexp_remove_comment
expected_regexp = { source: '', options: 'g' }
expected_regexp = { source: '', options: '' }
assert_equal expected_regexp, /(?# comment)/.to_json(nil)
end

def test_regexp_remove_group_options
expected_regexp = { source: '(something)', options: 'g' }
expected_regexp = { source: '(something)', options: '' }
assert_equal expected_regexp, /(?-mix:something)/.to_json(nil)
end

def test_regexp_as_json_with_options
expected_regexp = { source: '', options: 'gi' }
expected_regexp = { source: '', options: 'i' }
assert_equal expected_regexp, //i.as_json
end

def test_range_as_json
assert_equal [1, 3], (1..3).as_json
assert_equal [1,3], (1..3).as_json
end

def test_range_to_json
assert_equal '[1, 3]', (1..3).to_json(nil)
end

def test_range_as_json_with_floats
assert_equal [0.5, 5.5], (0.5..5.5).as_json
assert_equal [0.5,5.5], (0.5..5.5).as_json
end

def test_multiline_regexp_as_json
# A regexp with a line break in it WILL match line breaks in Ruby
# if the extended option /x is not set. It WILL also match any
# spaces with which the line that follows the line break is indented.
test_regexp = /
/
expected_regexp = { source: '\\\\n', options: 'g' }
assert_equal expected_regexp, test_regexp.as_json
end

def test_regexp_with_literal_whitespace_as_json
# regression test for issue #460
test_regexp = / /
expected_regexp = { source: ' ', options: 'g' }
assert_equal expected_regexp, test_regexp.as_json
end

def test_regexp_with_unicode_properties_as_json
# regression test for issue #615
# The double backslashes are needed by JS' new RegExp() constructor.
test_regexp = /\p{ASCII}/
expected_regexp = { source: '[\\\\x00-\\\\x7F]', options: 'g' }
assert_equal expected_regexp, test_regexp.as_json
end

def test_extended_mode_regexp_with_escaped_whitespace_as_json
# regression test for issue #625
test_regexp = / [\ a]\ /x
expected_regexp = { source: '[\\\\ a]\\\\ ', options: 'g' }
/
expected_regexp = { source: '', options: '' }
assert_equal expected_regexp, test_regexp.as_json
end

def test_regexp_modifiers_as_json
# - All Ruby regexes are what is considered "global" (/g) in JS.
# - JS has the same /i option as Ruby, so this should be transferred.
# - /m in JS has nothing to do with /m in Ruby. Ruby's /m can only be
# achieved in JS by modifying the source, not by setting /m.
assert_equal(({ source: '', options: 'gi' }), //i.as_json)
assert_equal(({ source: '', options: 'g' }), //m.as_json)
assert_equal(({ source: '', options: 'gi' }), //im.as_json)
assert_equal(({ source: '', options: 'g' }), //x.as_json)
assert_equal(({ source: '', options: 'gi' }), //ix.as_json)
# JS allows /i and /m modifiers, all other lead to error
assert_equal(({ source: '', options: 'i' }), //i.as_json)
assert_equal(({ source: '', options: 'm' }), //m.as_json)
assert_equal(({ source: '', options: 'im' }), //im.as_json)
assert_equal(({ source: '', options: '' }), //x.as_json)
assert_equal(({ source: '', options: 'i' }), //ix.as_json)
end

end

0 comments on commit c96871a

Please sign in to comment.