opal-activesupport 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +47 -0
- data/README.md +4 -1
- data/Rakefile +22 -6
- data/lib/opal/activesupport/version.rb +1 -1
- data/opal-activesupport.gemspec +1 -0
- data/opal/active_support/concern.rb +152 -0
- data/opal/active_support/core_ext/string.rb +1 -38
- data/opal/active_support/core_ext/string/filters.rb +146 -0
- data/opal/active_support/core_ext/string/inflections.rb +66 -4
- data/opal/active_support/inflections.rb +2 -0
- data/opal/active_support/inflector.rb +5 -2
- data/opal/active_support/inflector/inflections.rb +64 -66
- data/opal/active_support/inflector/methods.rb +198 -0
- data/test/abstract_unit.rb +22 -5
- data/test/concern_test.rb +144 -0
- data/test/constantize_test_cases.rb +121 -0
- data/test/core_ext/numeric_ext_test.rb +1 -1
- data/test/core_ext/string_ext_test.rb +675 -304
- data/test/inflector_test.rb +578 -0
- data/test/inflector_test_cases.rb +91 -31
- metadata +27 -4
data/test/abstract_unit.rb
CHANGED
@@ -26,6 +26,9 @@
|
|
26
26
|
# # Show backtraces for deprecated behavior for quicker cleanup.
|
27
27
|
# ActiveSupport::Deprecation.debug = true
|
28
28
|
|
29
|
+
# To help opal-minitest print errors.
|
30
|
+
Encoding.default_external = Encoding::UTF_8
|
31
|
+
|
29
32
|
require 'minitest/autorun'
|
30
33
|
require 'empty_bool'
|
31
34
|
require 'active_support'
|
@@ -35,12 +38,26 @@ class ActiveSupport::TestCase < Minitest::Test
|
|
35
38
|
define_method "test_#{name.gsub(/[\W-]+/, '_')}", &block
|
36
39
|
end
|
37
40
|
|
38
|
-
def assert_raise
|
41
|
+
def assert_raise error_class, &block
|
39
42
|
block.call
|
40
|
-
assert false, "Expected to see #{
|
41
|
-
rescue error
|
42
|
-
|
43
|
+
assert false, "Expected to see #{error_class.inspect}, but no exception was raised"
|
44
|
+
rescue error_class => error
|
45
|
+
error
|
43
46
|
rescue => actual_error
|
44
|
-
assert false, "Expected to see #{
|
47
|
+
assert false, "Expected to see #{error_class.inspect}, but got #{actual_error.inspect}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def assert_nothing_raised(&block)
|
51
|
+
block.call
|
52
|
+
rescue => e
|
53
|
+
assert false, "Expected no error, but got #{e.inspect}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def assert_not_empty(object)
|
57
|
+
assert !object.empty?, "Expected not empty object, but got: #{object.inspect}"
|
58
|
+
end
|
59
|
+
|
60
|
+
def assert_not_same(obj1, obj2)
|
61
|
+
assert !obj1.equal?(obj2), "Excepted #{obj1.inspect} and #{obj2.inspect} to be different objects"
|
45
62
|
end
|
46
63
|
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "abstract_unit"
|
4
|
+
require "active_support/concern"
|
5
|
+
|
6
|
+
class ConcernTest < ActiveSupport::TestCase
|
7
|
+
module Baz
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
class_methods do
|
11
|
+
def baz
|
12
|
+
"baz"
|
13
|
+
end
|
14
|
+
|
15
|
+
def included_ran=(value)
|
16
|
+
@@included_ran = value
|
17
|
+
end
|
18
|
+
|
19
|
+
def included_ran
|
20
|
+
@@included_ran
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
included do
|
25
|
+
self.included_ran = true
|
26
|
+
end
|
27
|
+
|
28
|
+
def baz
|
29
|
+
"baz"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module Bar
|
34
|
+
extend ActiveSupport::Concern
|
35
|
+
|
36
|
+
include Baz
|
37
|
+
|
38
|
+
module ClassMethods
|
39
|
+
def baz
|
40
|
+
"bar's baz + " + super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def bar
|
45
|
+
"bar"
|
46
|
+
end
|
47
|
+
|
48
|
+
def baz
|
49
|
+
"bar+" + super
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module Foo
|
54
|
+
extend ActiveSupport::Concern
|
55
|
+
|
56
|
+
include Bar, Baz
|
57
|
+
end
|
58
|
+
|
59
|
+
module Qux
|
60
|
+
module ClassMethods
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def setup
|
65
|
+
@klass = Class.new
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_module_is_included_normally
|
69
|
+
@klass.include(Baz)
|
70
|
+
assert_equal "baz", @klass.new.baz
|
71
|
+
assert_includes @klass.included_modules, ConcernTest::Baz
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_class_methods_are_extended
|
75
|
+
@klass.include(Baz)
|
76
|
+
assert_equal "baz", @klass.baz
|
77
|
+
assert_equal ConcernTest::Baz::ClassMethods, (class << @klass; included_modules; end)[0]
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_class_methods_are_extended_only_on_expected_objects
|
81
|
+
::Object.include(Qux)
|
82
|
+
Object.extend(Qux::ClassMethods)
|
83
|
+
# module needs to be created after Qux is included in Object or bug won't
|
84
|
+
# be triggered
|
85
|
+
test_module = Module.new do
|
86
|
+
extend ActiveSupport::Concern
|
87
|
+
|
88
|
+
class_methods do
|
89
|
+
def test
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
@klass.include test_module
|
94
|
+
# NOTE: Opal minitest doesn't have assert_not_respond_to
|
95
|
+
# assert_not_respond_to Object, :test
|
96
|
+
assert_equal false, Object.respond_to?(:test)
|
97
|
+
Qux.class_eval do
|
98
|
+
remove_const :ClassMethods
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_included_block_is_ran
|
103
|
+
@klass.include(Baz)
|
104
|
+
assert_equal true, @klass.included_ran
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_modules_dependencies_are_met
|
108
|
+
@klass.include(Bar)
|
109
|
+
assert_equal "bar", @klass.new.bar
|
110
|
+
assert_equal "bar+baz", @klass.new.baz
|
111
|
+
assert_equal "bar's baz + baz", @klass.baz
|
112
|
+
assert_includes @klass.included_modules, ConcernTest::Bar
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_dependencies_with_multiple_modules
|
116
|
+
@klass.include(Foo)
|
117
|
+
# FIXME: This is how the test was originally written but it throws a weird error:
|
118
|
+
# ArgumentError: unknown encoding name -
|
119
|
+
# at singleton_class_alloc.$$find [as $find]
|
120
|
+
#
|
121
|
+
# assert_equal [ConcernTest::Foo, ConcernTest::Bar, ConcernTest::Baz], @klass.included_modules[0..2]
|
122
|
+
#
|
123
|
+
# Also the order of the indluded_modules is backwards in Opal.
|
124
|
+
# So I've rewritten like so.
|
125
|
+
assert_equal 3, @klass.included_modules[0..2].size
|
126
|
+
@klass.included_modules[0..2].each do |mod|
|
127
|
+
assert_includes [ConcernTest::Foo, ConcernTest::Bar, ConcernTest::Baz], mod
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_raise_on_multiple_included_calls
|
132
|
+
assert_raises(ActiveSupport::Concern::MultipleIncludedBlocks) do
|
133
|
+
Module.new do
|
134
|
+
extend ActiveSupport::Concern
|
135
|
+
|
136
|
+
included do
|
137
|
+
end
|
138
|
+
|
139
|
+
included do
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module Ace
|
2
|
+
module Base
|
3
|
+
class Case
|
4
|
+
class Dice
|
5
|
+
end
|
6
|
+
end
|
7
|
+
class Fase < Case
|
8
|
+
end
|
9
|
+
end
|
10
|
+
class Gas
|
11
|
+
include Base
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Object
|
16
|
+
module AddtlGlobalConstants
|
17
|
+
class Case
|
18
|
+
class Dice
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
include AddtlGlobalConstants
|
23
|
+
end
|
24
|
+
|
25
|
+
module ConstantizeTestCases
|
26
|
+
def run_constantize_tests_on
|
27
|
+
assert_equal Ace::Base::Case, yield("Ace::Base::Case")
|
28
|
+
assert_equal Ace::Base::Case, yield("::Ace::Base::Case")
|
29
|
+
assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice")
|
30
|
+
assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Fase::Dice")
|
31
|
+
assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice")
|
32
|
+
|
33
|
+
assert_equal Ace::Gas::Case, yield("Ace::Gas::Case")
|
34
|
+
assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice")
|
35
|
+
assert_equal Ace::Base::Case::Dice, yield("Ace::Gas::Case::Dice")
|
36
|
+
|
37
|
+
assert_equal Case::Dice, yield("Case::Dice")
|
38
|
+
assert_equal AddtlGlobalConstants::Case::Dice, yield("Case::Dice")
|
39
|
+
assert_equal Object::AddtlGlobalConstants::Case::Dice, yield("Case::Dice")
|
40
|
+
|
41
|
+
assert_equal Case::Dice, yield("Object::Case::Dice")
|
42
|
+
assert_equal AddtlGlobalConstants::Case::Dice, yield("Object::Case::Dice")
|
43
|
+
assert_equal Object::AddtlGlobalConstants::Case::Dice, yield("Case::Dice")
|
44
|
+
|
45
|
+
assert_equal ConstantizeTestCases, yield("ConstantizeTestCases")
|
46
|
+
assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases")
|
47
|
+
|
48
|
+
assert_raises(NameError) { yield("UnknownClass") }
|
49
|
+
assert_raises(NameError) { yield("UnknownClass::Ace") }
|
50
|
+
assert_raises(NameError) { yield("UnknownClass::Ace::Base") }
|
51
|
+
assert_raises(NameError) { yield("An invalid string") }
|
52
|
+
assert_raises(NameError) { yield("InvalidClass\n") }
|
53
|
+
assert_raises(NameError) { yield("Ace::ConstantizeTestCases") }
|
54
|
+
assert_raises(NameError) { yield("Ace::Base::ConstantizeTestCases") }
|
55
|
+
assert_raises(NameError) { yield("Ace::Gas::Base") }
|
56
|
+
assert_raises(NameError) { yield("Ace::Gas::ConstantizeTestCases") }
|
57
|
+
assert_raises(NameError) { yield("") }
|
58
|
+
assert_raises(NameError) { yield("::") }
|
59
|
+
assert_raises(NameError) { yield("Ace::gas") }
|
60
|
+
|
61
|
+
# assert_raises(NameError) do
|
62
|
+
# with_autoloading_fixtures do
|
63
|
+
# yield("RaisesNameError")
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
|
67
|
+
# assert_raises(NoMethodError) do
|
68
|
+
# with_autoloading_fixtures do
|
69
|
+
# yield("RaisesNoMethodError")
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
|
73
|
+
# with_autoloading_fixtures do
|
74
|
+
# yield("Prepend::SubClassConflict")
|
75
|
+
# assert_equal "constant", defined?(Prepend::SubClassConflict)
|
76
|
+
# end
|
77
|
+
end
|
78
|
+
|
79
|
+
def run_safe_constantize_tests_on
|
80
|
+
assert_equal Ace::Base::Case, yield("Ace::Base::Case")
|
81
|
+
assert_equal Ace::Base::Case, yield("::Ace::Base::Case")
|
82
|
+
assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice")
|
83
|
+
assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice")
|
84
|
+
assert_equal Ace::Gas::Case, yield("Ace::Gas::Case")
|
85
|
+
assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice")
|
86
|
+
assert_equal Case::Dice, yield("Case::Dice")
|
87
|
+
assert_equal Case::Dice, yield("Object::Case::Dice")
|
88
|
+
assert_equal ConstantizeTestCases, yield("ConstantizeTestCases")
|
89
|
+
assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases")
|
90
|
+
assert_nil yield("")
|
91
|
+
assert_nil yield("::")
|
92
|
+
assert_nil yield("UnknownClass")
|
93
|
+
assert_nil yield("UnknownClass::Ace")
|
94
|
+
assert_nil yield("UnknownClass::Ace::Base")
|
95
|
+
assert_nil yield("An invalid string")
|
96
|
+
assert_nil yield("InvalidClass\n")
|
97
|
+
assert_nil yield("blargle")
|
98
|
+
assert_nil yield("Ace::ConstantizeTestCases")
|
99
|
+
assert_nil yield("Ace::Base::ConstantizeTestCases")
|
100
|
+
assert_nil yield("Ace::Gas::Base")
|
101
|
+
assert_nil yield("Ace::Gas::ConstantizeTestCases")
|
102
|
+
assert_nil yield("#<Class:0x7b8b718b>::Nested_1")
|
103
|
+
assert_nil yield("Ace::gas")
|
104
|
+
assert_nil yield("Object::ABC")
|
105
|
+
assert_nil yield("Object::Object::Object::ABC")
|
106
|
+
assert_nil yield("A::Object::B")
|
107
|
+
assert_nil yield("A::Object::Object::Object::B")
|
108
|
+
|
109
|
+
# assert_raises(NameError) do
|
110
|
+
# with_autoloading_fixtures do
|
111
|
+
# yield("RaisesNameError")
|
112
|
+
# end
|
113
|
+
# end
|
114
|
+
|
115
|
+
# assert_raises(NoMethodError) do
|
116
|
+
# with_autoloading_fixtures do
|
117
|
+
# yield("RaisesNoMethodError")
|
118
|
+
# end
|
119
|
+
# end
|
120
|
+
end
|
121
|
+
end
|
@@ -9,7 +9,7 @@ end unless defined? DateTime
|
|
9
9
|
class NumericExtTimeAndDateTimeTest < ActiveSupport::TestCase
|
10
10
|
def setup
|
11
11
|
@now = Time.local(2005,2,10,15,30,45)
|
12
|
-
@dtnow = DateTime.civil(2005,2,10,15,30,45)
|
12
|
+
# @dtnow = DateTime.civil(2005,2,10,15,30,45)
|
13
13
|
@seconds = {
|
14
14
|
1.minute => 60,
|
15
15
|
10.minutes => 600,
|
@@ -1,40 +1,39 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
# require
|
6
|
-
|
7
|
-
require
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# require
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# require "date"
|
4
|
+
require "abstract_unit"
|
5
|
+
# require "timeout"
|
6
|
+
require "inflector_test_cases"
|
7
|
+
require "constantize_test_cases"
|
8
|
+
|
9
|
+
require "active_support/inflector"
|
10
|
+
require "active_support/core_ext/string"
|
11
|
+
# require "active_support/time"
|
12
|
+
# require "active_support/core_ext/string/strip"
|
13
|
+
# require "active_support/core_ext/string/output_safety"
|
14
|
+
# require "active_support/core_ext/string/indent"
|
15
|
+
# require "time_zone_test_helpers"
|
16
|
+
# require "yaml"
|
19
17
|
|
20
18
|
class StringInflectionsTest < ActiveSupport::TestCase
|
21
19
|
include InflectorTestCases
|
22
|
-
|
20
|
+
include ConstantizeTestCases
|
21
|
+
# include TimeZoneTestHelpers
|
23
22
|
|
24
23
|
# def test_strip_heredoc_on_an_empty_string
|
25
|
-
# assert_equal
|
24
|
+
# assert_equal "", "".strip_heredoc
|
26
25
|
# end
|
27
|
-
|
26
|
+
|
28
27
|
# def test_strip_heredoc_on_a_string_with_no_lines
|
29
|
-
# assert_equal
|
30
|
-
# assert_equal
|
28
|
+
# assert_equal "x", "x".strip_heredoc
|
29
|
+
# assert_equal "x", " x".strip_heredoc
|
31
30
|
# end
|
32
|
-
|
31
|
+
|
33
32
|
# def test_strip_heredoc_on_a_heredoc_with_no_margin
|
34
33
|
# assert_equal "foo\nbar", "foo\nbar".strip_heredoc
|
35
34
|
# assert_equal "foo\n bar", "foo\n bar".strip_heredoc
|
36
35
|
# end
|
37
|
-
|
36
|
+
|
38
37
|
# def test_strip_heredoc_on_a_regular_indented_heredoc
|
39
38
|
# assert_equal "foo\n bar\nbaz\n", <<-EOS.strip_heredoc
|
40
39
|
# foo
|
@@ -42,12 +41,12 @@ class StringInflectionsTest < ActiveSupport::TestCase
|
|
42
41
|
# baz
|
43
42
|
# EOS
|
44
43
|
# end
|
45
|
-
|
44
|
+
|
46
45
|
# def test_strip_heredoc_on_a_regular_indented_heredoc_with_blank_lines
|
47
46
|
# assert_equal "foo\n bar\n\nbaz\n", <<-EOS.strip_heredoc
|
48
47
|
# foo
|
49
48
|
# bar
|
50
|
-
|
49
|
+
|
51
50
|
# baz
|
52
51
|
# EOS
|
53
52
|
# end
|
@@ -60,22 +59,49 @@ class StringInflectionsTest < ActiveSupport::TestCase
|
|
60
59
|
assert_equal("plurals", "plurals".pluralize)
|
61
60
|
|
62
61
|
assert_equal("blargles", "blargle".pluralize(0))
|
63
|
-
|
62
|
+
assert_equal("blargle", "blargle".pluralize(1))
|
64
63
|
assert_equal("blargles", "blargle".pluralize(2))
|
65
64
|
end
|
66
65
|
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
# end
|
71
|
-
# end
|
66
|
+
# BROKEN: this spec can't pass for Opal
|
67
|
+
# because String#equal? returns true for different objects representing the same string
|
68
|
+
# i.e. string.equal?(string.dup) => true
|
72
69
|
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
# end
|
70
|
+
# test "pluralize with count = 1 still returns new string" do
|
71
|
+
# name = "Kuldeep"
|
72
|
+
# assert_not_same name.pluralize(1), name
|
77
73
|
# end
|
78
74
|
|
75
|
+
def test_singularize
|
76
|
+
SingularToPlural.each do |singular, plural|
|
77
|
+
assert_equal(singular, plural.singularize)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_titleize
|
82
|
+
MixtureToTitleCase.each do |before, titleized|
|
83
|
+
assert_equal(titleized, before.titleize)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_titleize_with_keep_id_suffix
|
88
|
+
MixtureToTitleCaseWithKeepIdSuffix.each do |before, titleized|
|
89
|
+
assert_equal(titleized, before.titleize(keep_id_suffix: true))
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_upcase_first
|
94
|
+
assert_equal "What a Lovely Day", "what a Lovely Day".upcase_first
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_upcase_first_with_one_char
|
98
|
+
assert_equal "W", "w".upcase_first
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_upcase_first_with_empty_string
|
102
|
+
assert_equal "", "".upcase_first
|
103
|
+
end
|
104
|
+
|
79
105
|
def test_camelize
|
80
106
|
CamelToUnderscore.each do |camel, underscore|
|
81
107
|
assert_equal(camel, underscore.camelize)
|
@@ -83,7 +109,14 @@ class StringInflectionsTest < ActiveSupport::TestCase
|
|
83
109
|
end
|
84
110
|
|
85
111
|
def test_camelize_lower
|
86
|
-
assert_equal(
|
112
|
+
assert_equal("capital", "Capital".camelize(:lower))
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_camelize_invalid_option
|
116
|
+
e = assert_raise ArgumentError do
|
117
|
+
"Capital".camelize(nil)
|
118
|
+
end
|
119
|
+
assert_equal("Invalid option, use either :upper or :lower.", e.message)
|
87
120
|
end
|
88
121
|
|
89
122
|
def test_dasherize
|
@@ -111,186 +144,380 @@ class StringInflectionsTest < ActiveSupport::TestCase
|
|
111
144
|
assert_equal "Account", "MyApplication::Billing::Account".demodulize
|
112
145
|
end
|
113
146
|
|
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
|
-
|
147
|
+
def test_deconstantize
|
148
|
+
assert_equal "MyApplication::Billing", "MyApplication::Billing::Account".deconstantize
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_foreign_key
|
152
|
+
ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
|
153
|
+
assert_equal(foreign_key, klass.foreign_key)
|
154
|
+
end
|
155
|
+
|
156
|
+
ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
|
157
|
+
assert_equal(foreign_key, klass.foreign_key(false))
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_tableize
|
162
|
+
ClassNameToTableName.each do |class_name, table_name|
|
163
|
+
assert_equal(table_name, class_name.tableize)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_classify
|
168
|
+
ClassNameToTableName.each do |class_name, table_name|
|
169
|
+
assert_equal(class_name, table_name.classify)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
140
173
|
# def test_string_parameterized_normal
|
141
174
|
# StringToParameterized.each do |normal, slugged|
|
142
|
-
# assert_equal(normal.parameterize
|
175
|
+
# assert_equal(slugged, normal.parameterize)
|
143
176
|
# end
|
144
177
|
# end
|
145
|
-
|
178
|
+
|
179
|
+
# def test_string_parameterized_normal_preserve_case
|
180
|
+
# StringToParameterizedPreserveCase.each do |normal, slugged|
|
181
|
+
# assert_equal(slugged, normal.parameterize(preserve_case: true))
|
182
|
+
# end
|
183
|
+
# end
|
184
|
+
|
146
185
|
# def test_string_parameterized_no_separator
|
147
186
|
# StringToParameterizeWithNoSeparator.each do |normal, slugged|
|
148
|
-
# assert_equal(normal.parameterize(
|
187
|
+
# assert_equal(slugged, normal.parameterize(separator: ""))
|
149
188
|
# end
|
150
189
|
# end
|
151
|
-
|
190
|
+
|
191
|
+
# def test_string_parameterized_no_separator_preserve_case
|
192
|
+
# StringToParameterizePreserveCaseWithNoSeparator.each do |normal, slugged|
|
193
|
+
# assert_equal(slugged, normal.parameterize(separator: "", preserve_case: true))
|
194
|
+
# end
|
195
|
+
# end
|
196
|
+
|
152
197
|
# def test_string_parameterized_underscore
|
153
198
|
# StringToParameterizeWithUnderscore.each do |normal, slugged|
|
154
|
-
# assert_equal(normal.parameterize(
|
199
|
+
# assert_equal(slugged, normal.parameterize(separator: "_"))
|
155
200
|
# end
|
156
201
|
# end
|
157
|
-
|
158
|
-
# def
|
159
|
-
#
|
160
|
-
# assert_equal(
|
202
|
+
|
203
|
+
# def test_string_parameterized_underscore_preserve_case
|
204
|
+
# StringToParameterizePreserveCaseWithUnderscore.each do |normal, slugged|
|
205
|
+
# assert_equal(slugged, normal.parameterize(separator: "_", preserve_case: true))
|
161
206
|
# end
|
162
207
|
# end
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
#
|
183
|
-
# assert_equal "",
|
184
|
-
#
|
185
|
-
# assert_equal 'x', 'x'.first
|
186
|
-
# assert_equal 'x', 'x'.first(4)
|
187
|
-
#
|
188
|
-
# assert_equal 'x', 'x'.last
|
189
|
-
# assert_equal 'x', 'x'.last(4)
|
208
|
+
|
209
|
+
def test_humanize
|
210
|
+
UnderscoreToHuman.each do |underscore, human|
|
211
|
+
assert_equal(human, underscore.humanize)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_humanize_without_capitalize
|
216
|
+
UnderscoreToHumanWithoutCapitalize.each do |underscore, human|
|
217
|
+
assert_equal(human, underscore.humanize(capitalize: false))
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_humanize_with_keep_id_suffix
|
222
|
+
UnderscoreToHumanWithKeepIdSuffix.each do |underscore, human|
|
223
|
+
assert_equal(human, underscore.humanize(keep_id_suffix: true))
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
# def test_humanize_with_html_escape
|
228
|
+
# assert_equal "Hello", ERB::Util.html_escape("hello").humanize
|
190
229
|
# end
|
191
|
-
|
192
|
-
# def
|
193
|
-
#
|
194
|
-
#
|
195
|
-
# hash["hello123".at(0)] = true
|
196
|
-
# assert_equal %w(h), hash.keys
|
197
|
-
#
|
198
|
-
# hash = {}
|
199
|
-
# hash["llo"] = true
|
200
|
-
# hash["hello".from(2)] = true
|
201
|
-
# assert_equal %w(llo), hash.keys
|
202
|
-
#
|
203
|
-
# hash = {}
|
204
|
-
# hash["hel"] = true
|
205
|
-
# hash["hello".to(2)] = true
|
206
|
-
# assert_equal %w(hel), hash.keys
|
207
|
-
#
|
208
|
-
# hash = {}
|
209
|
-
# hash["hello"] = true
|
210
|
-
# hash["123hello".last(5)] = true
|
211
|
-
# assert_equal %w(hello), hash.keys
|
212
|
-
#
|
213
|
-
# hash = {}
|
214
|
-
# hash["hello"] = true
|
215
|
-
# hash["hello123".first(5)] = true
|
216
|
-
# assert_equal %w(hello), hash.keys
|
230
|
+
|
231
|
+
# def test_ord
|
232
|
+
# assert_equal 97, "a".ord
|
233
|
+
# assert_equal 97, "abc".ord
|
217
234
|
# end
|
218
|
-
|
235
|
+
|
219
236
|
# def test_starts_ends_with_alias
|
220
237
|
# s = "hello"
|
221
|
-
# assert s.starts_with?(
|
222
|
-
# assert s.starts_with?(
|
223
|
-
# assert !s.starts_with?(
|
224
|
-
|
225
|
-
# assert s.ends_with?(
|
226
|
-
# assert s.ends_with?(
|
227
|
-
# assert !s.ends_with?(
|
238
|
+
# assert s.starts_with?("h")
|
239
|
+
# assert s.starts_with?("hel")
|
240
|
+
# assert !s.starts_with?("el")
|
241
|
+
|
242
|
+
# assert s.ends_with?("o")
|
243
|
+
# assert s.ends_with?("lo")
|
244
|
+
# assert !s.ends_with?("el")
|
228
245
|
# end
|
229
|
-
|
246
|
+
|
230
247
|
# def test_string_squish
|
231
|
-
# original = %{\
|
232
|
-
# with tabs(\t\t), newlines(\n\n), unicode nextlines(\u0085\u0085) and many spaces( ). \
|
233
|
-
|
234
|
-
# expected = "A string surrounded by unicode
|
248
|
+
# original = %{\u205f\u3000 A string surrounded by various unicode spaces,
|
249
|
+
# with tabs(\t\t), newlines(\n\n), unicode nextlines(\u0085\u0085) and many spaces( ). \u00a0\u2007}.dup
|
250
|
+
|
251
|
+
# expected = "A string surrounded by various unicode spaces, " \
|
235
252
|
# "with tabs( ), newlines( ), unicode nextlines( ) and many spaces( )."
|
236
|
-
|
253
|
+
|
237
254
|
# # Make sure squish returns what we expect:
|
238
|
-
# assert_equal original.squish
|
255
|
+
# assert_equal expected, original.squish
|
239
256
|
# # But doesn't modify the original string:
|
240
|
-
# assert_not_equal
|
241
|
-
|
257
|
+
# assert_not_equal expected, original
|
258
|
+
|
242
259
|
# # Make sure squish! returns what we expect:
|
243
|
-
# assert_equal original.squish
|
260
|
+
# assert_equal expected, original.squish!
|
244
261
|
# # And changes the original string:
|
245
|
-
# assert_equal
|
262
|
+
# assert_equal expected, original
|
246
263
|
# end
|
247
|
-
|
264
|
+
|
248
265
|
# def test_string_inquiry
|
249
|
-
#
|
250
|
-
#
|
266
|
+
# assert_predicate "production".inquiry, :production?
|
267
|
+
# assert_not_predicate "production".inquiry, :development?
|
251
268
|
# end
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
269
|
+
|
270
|
+
def test_truncate
|
271
|
+
assert_equal "Hello World!", "Hello World!".truncate(12)
|
272
|
+
assert_equal "Hello Wor...", "Hello World!!".truncate(12)
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_truncate_with_omission_and_separator
|
276
|
+
assert_equal "Hello[...]", "Hello World!".truncate(10, omission: "[...]")
|
277
|
+
assert_equal "Hello[...]", "Hello Big World!".truncate(13, omission: "[...]", separator: " ")
|
278
|
+
assert_equal "Hello Big[...]", "Hello Big World!".truncate(14, omission: "[...]", separator: " ")
|
279
|
+
assert_equal "Hello Big[...]", "Hello Big World!".truncate(15, omission: "[...]", separator: " ")
|
280
|
+
end
|
281
|
+
|
282
|
+
def test_truncate_with_omission_and_regexp_separator
|
283
|
+
assert_equal "Hello[...]", "Hello Big World!".truncate(13, omission: "[...]", separator: /\s/)
|
284
|
+
assert_equal "Hello Big[...]", "Hello Big World!".truncate(14, omission: "[...]", separator: /\s/)
|
285
|
+
assert_equal "Hello Big[...]", "Hello Big World!".truncate(15, omission: "[...]", separator: /\s/)
|
286
|
+
end
|
287
|
+
|
288
|
+
# def test_truncate_bytes
|
289
|
+
# assert_equal "👍👍👍👍", "👍👍👍👍".truncate_bytes(16)
|
290
|
+
# assert_equal "👍👍👍👍", "👍👍👍👍".truncate_bytes(16, omission: nil)
|
291
|
+
# assert_equal "👍👍👍👍", "👍👍👍👍".truncate_bytes(16, omission: " ")
|
292
|
+
# assert_equal "👍👍👍👍", "👍👍👍👍".truncate_bytes(16, omission: "🖖")
|
293
|
+
#
|
294
|
+
# assert_equal "👍👍👍…", "👍👍👍👍".truncate_bytes(15)
|
295
|
+
# assert_equal "👍👍👍", "👍👍👍👍".truncate_bytes(15, omission: nil)
|
296
|
+
# assert_equal "👍👍👍 ", "👍👍👍👍".truncate_bytes(15, omission: " ")
|
297
|
+
# assert_equal "👍👍🖖", "👍👍👍👍".truncate_bytes(15, omission: "🖖")
|
298
|
+
#
|
299
|
+
# assert_equal "…", "👍👍👍👍".truncate_bytes(5)
|
300
|
+
# assert_equal "👍", "👍👍👍👍".truncate_bytes(5, omission: nil)
|
301
|
+
# assert_equal "👍 ", "👍👍👍👍".truncate_bytes(5, omission: " ")
|
302
|
+
# assert_equal "🖖", "👍👍👍👍".truncate_bytes(5, omission: "🖖")
|
303
|
+
#
|
304
|
+
# assert_equal "…", "👍👍👍👍".truncate_bytes(4)
|
305
|
+
# assert_equal "👍", "👍👍👍👍".truncate_bytes(4, omission: nil)
|
306
|
+
# assert_equal " ", "👍👍👍👍".truncate_bytes(4, omission: " ")
|
307
|
+
# assert_equal "🖖", "👍👍👍👍".truncate_bytes(4, omission: "🖖")
|
308
|
+
#
|
309
|
+
# assert_raise ArgumentError do
|
310
|
+
# "👍👍👍👍".truncate_bytes(3, omission: "🖖")
|
311
|
+
# end
|
256
312
|
# end
|
257
313
|
#
|
258
|
-
# def
|
259
|
-
# assert_equal "
|
260
|
-
# assert_equal "
|
261
|
-
# assert_equal "
|
262
|
-
# assert_equal "
|
314
|
+
# def test_truncate_bytes_preserves_codepoints
|
315
|
+
# assert_equal "👍👍👍👍", "👍👍👍👍".truncate_bytes(16)
|
316
|
+
# assert_equal "👍👍👍👍", "👍👍👍👍".truncate_bytes(16, omission: nil)
|
317
|
+
# assert_equal "👍👍👍👍", "👍👍👍👍".truncate_bytes(16, omission: " ")
|
318
|
+
# assert_equal "👍👍👍👍", "👍👍👍👍".truncate_bytes(16, omission: "🖖")
|
319
|
+
#
|
320
|
+
# assert_equal "👍👍👍…", "👍👍👍👍".truncate_bytes(15)
|
321
|
+
# assert_equal "👍👍👍", "👍👍👍👍".truncate_bytes(15, omission: nil)
|
322
|
+
# assert_equal "👍👍👍 ", "👍👍👍👍".truncate_bytes(15, omission: " ")
|
323
|
+
# assert_equal "👍👍🖖", "👍👍👍👍".truncate_bytes(15, omission: "🖖")
|
324
|
+
#
|
325
|
+
# assert_equal "…", "👍👍👍👍".truncate_bytes(5)
|
326
|
+
# assert_equal "👍", "👍👍👍👍".truncate_bytes(5, omission: nil)
|
327
|
+
# assert_equal "👍 ", "👍👍👍👍".truncate_bytes(5, omission: " ")
|
328
|
+
# assert_equal "🖖", "👍👍👍👍".truncate_bytes(5, omission: "🖖")
|
329
|
+
#
|
330
|
+
# assert_equal "…", "👍👍👍👍".truncate_bytes(4)
|
331
|
+
# assert_equal "👍", "👍👍👍👍".truncate_bytes(4, omission: nil)
|
332
|
+
# assert_equal " ", "👍👍👍👍".truncate_bytes(4, omission: " ")
|
333
|
+
# assert_equal "🖖", "👍👍👍👍".truncate_bytes(4, omission: "🖖")
|
334
|
+
#
|
335
|
+
# assert_raise ArgumentError do
|
336
|
+
# "👍👍👍👍".truncate_bytes(3, omission: "🖖")
|
337
|
+
# end
|
263
338
|
# end
|
264
339
|
#
|
265
|
-
# def
|
266
|
-
# assert_equal "
|
267
|
-
# assert_equal "
|
268
|
-
# assert_equal "
|
340
|
+
# def test_truncates_bytes_preserves_grapheme_clusters
|
341
|
+
# assert_equal "a ", "a ❤️ b".truncate_bytes(2, omission: nil)
|
342
|
+
# assert_equal "a ", "a ❤️ b".truncate_bytes(3, omission: nil)
|
343
|
+
# assert_equal "a ", "a ❤️ b".truncate_bytes(7, omission: nil)
|
344
|
+
# assert_equal "a ❤️", "a ❤️ b".truncate_bytes(8, omission: nil)
|
345
|
+
#
|
346
|
+
# assert_equal "a ", "a 👩❤️👩".truncate_bytes(13, omission: nil)
|
347
|
+
# assert_equal "", "👩❤️👩".truncate_bytes(13, omission: nil)
|
269
348
|
# end
|
270
349
|
#
|
350
|
+
# def test_truncate_words
|
351
|
+
# assert_equal "Hello Big World!", "Hello Big World!".truncate_words(3)
|
352
|
+
# assert_equal "Hello Big...", "Hello Big World!".truncate_words(2)
|
353
|
+
# end
|
354
|
+
|
355
|
+
# def test_truncate_words_with_omission
|
356
|
+
# assert_equal "Hello Big World!", "Hello Big World!".truncate_words(3, omission: "[...]")
|
357
|
+
# assert_equal "Hello Big[...]", "Hello Big World!".truncate_words(2, omission: "[...]")
|
358
|
+
# end
|
359
|
+
|
360
|
+
# def test_truncate_words_with_separator
|
361
|
+
# assert_equal "Hello<br>Big<br>World!...", "Hello<br>Big<br>World!<br>".truncate_words(3, separator: "<br>")
|
362
|
+
# assert_equal "Hello<br>Big<br>World!", "Hello<br>Big<br>World!".truncate_words(3, separator: "<br>")
|
363
|
+
# assert_equal "Hello\n<br>Big...", "Hello\n<br>Big<br>Wide<br>World!".truncate_words(2, separator: "<br>")
|
364
|
+
# end
|
365
|
+
|
366
|
+
# def test_truncate_words_with_separator_and_omission
|
367
|
+
# assert_equal "Hello<br>Big<br>World![...]", "Hello<br>Big<br>World!<br>".truncate_words(3, omission: "[...]", separator: "<br>")
|
368
|
+
# assert_equal "Hello<br>Big<br>World!", "Hello<br>Big<br>World!".truncate_words(3, omission: "[...]", separator: "<br>")
|
369
|
+
# end
|
370
|
+
|
371
|
+
# def test_truncate_words_with_complex_string
|
372
|
+
# Timeout.timeout(10) do
|
373
|
+
# complex_string = "aa aa aaa aa aaa aaa aaa aa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaaa aaaaa aaaaa aaaaaa aa aa aa aaa aa aaa aa aa aa aa a aaa aaa \n a aaa <<s"
|
374
|
+
# assert_equal complex_string, complex_string.truncate_words(80)
|
375
|
+
# end
|
376
|
+
# rescue Timeout::Error
|
377
|
+
# assert false
|
378
|
+
# end
|
379
|
+
|
271
380
|
# def test_truncate_multibyte
|
272
|
-
# assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding(Encoding::UTF_8),
|
273
|
-
# "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding(Encoding::UTF_8).truncate(10)
|
381
|
+
# assert_equal (+"\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...").force_encoding(Encoding::UTF_8),
|
382
|
+
# (+"\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244").force_encoding(Encoding::UTF_8).truncate(10)
|
274
383
|
# end
|
275
|
-
|
384
|
+
|
276
385
|
# def test_truncate_should_not_be_html_safe
|
277
|
-
#
|
386
|
+
# assert_not_predicate "Hello World!".truncate(12), :html_safe?
|
278
387
|
# end
|
279
|
-
|
280
|
-
# def
|
281
|
-
#
|
282
|
-
#
|
283
|
-
#
|
388
|
+
|
389
|
+
# def test_remove
|
390
|
+
# original = "This is a good day to die"
|
391
|
+
# assert_equal "This is a good day", original.remove(" to die")
|
392
|
+
# assert_equal "This is a good day", original.remove(" to ", /die/)
|
393
|
+
# assert_equal "This is a good day to die", original
|
284
394
|
# end
|
285
|
-
|
286
|
-
# def
|
287
|
-
#
|
288
|
-
#
|
289
|
-
#
|
395
|
+
|
396
|
+
# def test_remove_for_multiple_occurrences
|
397
|
+
# original = "This is a good day to die to die"
|
398
|
+
# assert_equal "This is a good day", original.remove(" to die")
|
399
|
+
# assert_equal "This is a good day to die to die", original
|
400
|
+
# end
|
401
|
+
|
402
|
+
# def test_remove!
|
403
|
+
# original = "This is a very good day to die".dup
|
404
|
+
# assert_equal "This is a good day to die", original.remove!(" very")
|
405
|
+
# assert_equal "This is a good day to die", original
|
406
|
+
# assert_equal "This is a good day", original.remove!(" to ", /die/)
|
407
|
+
# assert_equal "This is a good day", original
|
290
408
|
# end
|
409
|
+
|
410
|
+
def test_constantize
|
411
|
+
run_constantize_tests_on(&:constantize)
|
412
|
+
end
|
413
|
+
|
414
|
+
def test_safe_constantize
|
415
|
+
run_safe_constantize_tests_on(&:safe_constantize)
|
416
|
+
end
|
291
417
|
end
|
292
418
|
|
419
|
+
# class StringAccessTest < ActiveSupport::TestCase
|
420
|
+
# test "#at with Integer, returns a substring of one character at that position" do
|
421
|
+
# assert_equal "h", "hello".at(0)
|
422
|
+
# end
|
423
|
+
|
424
|
+
# test "#at with Range, returns a substring containing characters at offsets" do
|
425
|
+
# assert_equal "lo", "hello".at(-2..-1)
|
426
|
+
# end
|
427
|
+
|
428
|
+
# test "#at with Regex, returns the matching portion of the string" do
|
429
|
+
# assert_equal "lo", "hello".at(/lo/)
|
430
|
+
# assert_nil "hello".at(/nonexisting/)
|
431
|
+
# end
|
432
|
+
|
433
|
+
# test "#from with positive Integer, returns substring from the given position to the end" do
|
434
|
+
# assert_equal "llo", "hello".from(2)
|
435
|
+
# end
|
436
|
+
|
437
|
+
# test "#from with negative Integer, position is counted from the end" do
|
438
|
+
# assert_equal "lo", "hello".from(-2)
|
439
|
+
# end
|
440
|
+
|
441
|
+
# test "#to with positive Integer, substring from the beginning to the given position" do
|
442
|
+
# assert_equal "hel", "hello".to(2)
|
443
|
+
# end
|
444
|
+
|
445
|
+
# test "#to with negative Integer, position is counted from the end" do
|
446
|
+
# assert_equal "hell", "hello".to(-2)
|
447
|
+
# end
|
448
|
+
|
449
|
+
# test "#from and #to can be combined" do
|
450
|
+
# assert_equal "hello", "hello".from(0).to(-1)
|
451
|
+
# assert_equal "ell", "hello".from(1).to(-2)
|
452
|
+
# end
|
453
|
+
|
454
|
+
# test "#first returns the first character" do
|
455
|
+
# assert_equal "h", "hello".first
|
456
|
+
# assert_equal "x", "x".first
|
457
|
+
# end
|
458
|
+
|
459
|
+
# test "#first with Integer, returns a substring from the beginning to position" do
|
460
|
+
# assert_equal "he", "hello".first(2)
|
461
|
+
# assert_equal "", "hello".first(0)
|
462
|
+
# assert_equal "hello", "hello".first(10)
|
463
|
+
# assert_equal "x", "x".first(4)
|
464
|
+
# end
|
465
|
+
|
466
|
+
# test "#first with Integer >= string length still returns a new string" do
|
467
|
+
# string = "hello"
|
468
|
+
# different_string = string.first(5)
|
469
|
+
# assert_not_same different_string, string
|
470
|
+
# end
|
471
|
+
|
472
|
+
# test "#last returns the last character" do
|
473
|
+
# assert_equal "o", "hello".last
|
474
|
+
# assert_equal "x", "x".last
|
475
|
+
# end
|
476
|
+
|
477
|
+
# test "#last with Integer, returns a substring from the end to position" do
|
478
|
+
# assert_equal "llo", "hello".last(3)
|
479
|
+
# assert_equal "hello", "hello".last(10)
|
480
|
+
# assert_equal "", "hello".last(0)
|
481
|
+
# assert_equal "x", "x".last(4)
|
482
|
+
# end
|
483
|
+
|
484
|
+
# test "#last with Integer >= string length still returns a new string" do
|
485
|
+
# string = "hello"
|
486
|
+
# different_string = string.last(5)
|
487
|
+
# assert_not_same different_string, string
|
488
|
+
# end
|
489
|
+
|
490
|
+
# test "access returns a real string" do
|
491
|
+
# hash = {}
|
492
|
+
# hash["h"] = true
|
493
|
+
# hash["hello123".at(0)] = true
|
494
|
+
# assert_equal %w(h), hash.keys
|
495
|
+
|
496
|
+
# hash = {}
|
497
|
+
# hash["llo"] = true
|
498
|
+
# hash["hello".from(2)] = true
|
499
|
+
# assert_equal %w(llo), hash.keys
|
500
|
+
|
501
|
+
# hash = {}
|
502
|
+
# hash["hel"] = true
|
503
|
+
# hash["hello".to(2)] = true
|
504
|
+
# assert_equal %w(hel), hash.keys
|
505
|
+
|
506
|
+
# hash = {}
|
507
|
+
# hash["hello"] = true
|
508
|
+
# hash["123hello".last(5)] = true
|
509
|
+
# assert_equal %w(hello), hash.keys
|
510
|
+
|
511
|
+
# hash = {}
|
512
|
+
# hash["hello"] = true
|
513
|
+
# hash["hello123".first(5)] = true
|
514
|
+
# assert_equal %w(hello), hash.keys
|
515
|
+
# end
|
516
|
+
# end
|
517
|
+
|
293
518
|
# class StringConversionsTest < ActiveSupport::TestCase
|
519
|
+
# include TimeZoneTestHelpers
|
520
|
+
|
294
521
|
# def test_string_to_time
|
295
522
|
# with_env_tz "Europe/Moscow" do
|
296
523
|
# assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:utc)
|
@@ -299,32 +526,162 @@ end
|
|
299
526
|
# assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time
|
300
527
|
# assert_equal Time.utc(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:utc)
|
301
528
|
# assert_equal Time.local(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
|
302
|
-
# assert_equal Time.local(2011, 2, 27,
|
529
|
+
# assert_equal Time.local(2011, 2, 27, 17, 50), "2011-02-27 13:50 -0100".to_time
|
303
530
|
# assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time(:utc)
|
304
|
-
# assert_equal Time.local(2005, 2, 27,
|
531
|
+
# assert_equal Time.local(2005, 2, 27, 22, 50), "2005-02-27 14:50 -0500".to_time
|
532
|
+
# assert_nil "010".to_time
|
305
533
|
# assert_nil "".to_time
|
306
534
|
# end
|
307
535
|
# end
|
308
|
-
|
536
|
+
|
309
537
|
# def test_string_to_time_utc_offset
|
310
538
|
# with_env_tz "US/Eastern" do
|
311
|
-
#
|
312
|
-
#
|
313
|
-
#
|
314
|
-
#
|
539
|
+
# if ActiveSupport.to_time_preserves_timezone
|
540
|
+
# assert_equal 0, "2005-02-27 23:50".to_time(:utc).utc_offset
|
541
|
+
# assert_equal(-18000, "2005-02-27 23:50".to_time.utc_offset)
|
542
|
+
# assert_equal 0, "2005-02-27 22:50 -0100".to_time(:utc).utc_offset
|
543
|
+
# assert_equal(-3600, "2005-02-27 22:50 -0100".to_time.utc_offset)
|
544
|
+
# else
|
545
|
+
# assert_equal 0, "2005-02-27 23:50".to_time(:utc).utc_offset
|
546
|
+
# assert_equal(-18000, "2005-02-27 23:50".to_time.utc_offset)
|
547
|
+
# assert_equal 0, "2005-02-27 22:50 -0100".to_time(:utc).utc_offset
|
548
|
+
# assert_equal(-18000, "2005-02-27 22:50 -0100".to_time.utc_offset)
|
549
|
+
# end
|
315
550
|
# end
|
316
551
|
# end
|
317
|
-
|
552
|
+
|
318
553
|
# def test_partial_string_to_time
|
319
|
-
# with_env_tz "Europe/Moscow" do
|
554
|
+
# with_env_tz "Europe/Moscow" do # use timezone which does not observe DST.
|
320
555
|
# now = Time.now
|
321
556
|
# assert_equal Time.local(now.year, now.month, now.day, 23, 50), "23:50".to_time
|
322
557
|
# assert_equal Time.utc(now.year, now.month, now.day, 23, 50), "23:50".to_time(:utc)
|
323
|
-
# assert_equal Time.local(now.year, now.month, now.day,
|
558
|
+
# assert_equal Time.local(now.year, now.month, now.day, 17, 50), "13:50 -0100".to_time
|
324
559
|
# assert_equal Time.utc(now.year, now.month, now.day, 23, 50), "22:50 -0100".to_time(:utc)
|
325
560
|
# end
|
326
561
|
# end
|
327
|
-
|
562
|
+
|
563
|
+
# def test_standard_time_string_to_time_when_current_time_is_standard_time
|
564
|
+
# with_env_tz "US/Eastern" do
|
565
|
+
# Time.stub(:now, Time.local(2012, 1, 1)) do
|
566
|
+
# assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time
|
567
|
+
# assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time(:utc)
|
568
|
+
# assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 -0800".to_time
|
569
|
+
# assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 -0800".to_time(:utc)
|
570
|
+
# assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 -0500".to_time
|
571
|
+
# assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 -0500".to_time(:utc)
|
572
|
+
# assert_equal Time.local(2012, 1, 1, 5, 0), "2012-01-01 10:00 UTC".to_time
|
573
|
+
# assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00 UTC".to_time(:utc)
|
574
|
+
# assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 PST".to_time
|
575
|
+
# assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 PST".to_time(:utc)
|
576
|
+
# assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 EST".to_time
|
577
|
+
# assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 EST".to_time(:utc)
|
578
|
+
# end
|
579
|
+
# end
|
580
|
+
# end
|
581
|
+
|
582
|
+
# def test_standard_time_string_to_time_when_current_time_is_daylight_savings
|
583
|
+
# with_env_tz "US/Eastern" do
|
584
|
+
# Time.stub(:now, Time.local(2012, 7, 1)) do
|
585
|
+
# assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time
|
586
|
+
# assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time(:utc)
|
587
|
+
# assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 -0800".to_time
|
588
|
+
# assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 -0800".to_time(:utc)
|
589
|
+
# assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 -0500".to_time
|
590
|
+
# assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 -0500".to_time(:utc)
|
591
|
+
# assert_equal Time.local(2012, 1, 1, 5, 0), "2012-01-01 10:00 UTC".to_time
|
592
|
+
# assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00 UTC".to_time(:utc)
|
593
|
+
# assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 PST".to_time
|
594
|
+
# assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 PST".to_time(:utc)
|
595
|
+
# assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 EST".to_time
|
596
|
+
# assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 EST".to_time(:utc)
|
597
|
+
# end
|
598
|
+
# end
|
599
|
+
# end
|
600
|
+
|
601
|
+
# def test_daylight_savings_string_to_time_when_current_time_is_standard_time
|
602
|
+
# with_env_tz "US/Eastern" do
|
603
|
+
# Time.stub(:now, Time.local(2012, 1, 1)) do
|
604
|
+
# assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time
|
605
|
+
# assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time(:utc)
|
606
|
+
# assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 -0700".to_time
|
607
|
+
# assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 -0700".to_time(:utc)
|
608
|
+
# assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 -0400".to_time
|
609
|
+
# assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 -0400".to_time(:utc)
|
610
|
+
# assert_equal Time.local(2012, 7, 1, 6, 0), "2012-07-01 10:00 UTC".to_time
|
611
|
+
# assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00 UTC".to_time(:utc)
|
612
|
+
# assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 PDT".to_time
|
613
|
+
# assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 PDT".to_time(:utc)
|
614
|
+
# assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 EDT".to_time
|
615
|
+
# assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 EDT".to_time(:utc)
|
616
|
+
# end
|
617
|
+
# end
|
618
|
+
# end
|
619
|
+
|
620
|
+
# def test_daylight_savings_string_to_time_when_current_time_is_daylight_savings
|
621
|
+
# with_env_tz "US/Eastern" do
|
622
|
+
# Time.stub(:now, Time.local(2012, 7, 1)) do
|
623
|
+
# assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time
|
624
|
+
# assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time(:utc)
|
625
|
+
# assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 -0700".to_time
|
626
|
+
# assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 -0700".to_time(:utc)
|
627
|
+
# assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 -0400".to_time
|
628
|
+
# assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 -0400".to_time(:utc)
|
629
|
+
# assert_equal Time.local(2012, 7, 1, 6, 0), "2012-07-01 10:00 UTC".to_time
|
630
|
+
# assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00 UTC".to_time(:utc)
|
631
|
+
# assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 PDT".to_time
|
632
|
+
# assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 PDT".to_time(:utc)
|
633
|
+
# assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 EDT".to_time
|
634
|
+
# assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 EDT".to_time(:utc)
|
635
|
+
# end
|
636
|
+
# end
|
637
|
+
# end
|
638
|
+
|
639
|
+
# def test_partial_string_to_time_when_current_time_is_standard_time
|
640
|
+
# with_env_tz "US/Eastern" do
|
641
|
+
# Time.stub(:now, Time.local(2012, 1, 1)) do
|
642
|
+
# assert_equal Time.local(2012, 1, 1, 10, 0), "10:00".to_time
|
643
|
+
# assert_equal Time.utc(2012, 1, 1, 10, 0), "10:00".to_time(:utc)
|
644
|
+
# assert_equal Time.local(2012, 1, 1, 6, 0), "10:00 -0100".to_time
|
645
|
+
# assert_equal Time.utc(2012, 1, 1, 11, 0), "10:00 -0100".to_time(:utc)
|
646
|
+
# assert_equal Time.local(2012, 1, 1, 10, 0), "10:00 -0500".to_time
|
647
|
+
# assert_equal Time.utc(2012, 1, 1, 15, 0), "10:00 -0500".to_time(:utc)
|
648
|
+
# assert_equal Time.local(2012, 1, 1, 5, 0), "10:00 UTC".to_time
|
649
|
+
# assert_equal Time.utc(2012, 1, 1, 10, 0), "10:00 UTC".to_time(:utc)
|
650
|
+
# assert_equal Time.local(2012, 1, 1, 13, 0), "10:00 PST".to_time
|
651
|
+
# assert_equal Time.utc(2012, 1, 1, 18, 0), "10:00 PST".to_time(:utc)
|
652
|
+
# assert_equal Time.local(2012, 1, 1, 12, 0), "10:00 PDT".to_time
|
653
|
+
# assert_equal Time.utc(2012, 1, 1, 17, 0), "10:00 PDT".to_time(:utc)
|
654
|
+
# assert_equal Time.local(2012, 1, 1, 10, 0), "10:00 EST".to_time
|
655
|
+
# assert_equal Time.utc(2012, 1, 1, 15, 0), "10:00 EST".to_time(:utc)
|
656
|
+
# assert_equal Time.local(2012, 1, 1, 9, 0), "10:00 EDT".to_time
|
657
|
+
# assert_equal Time.utc(2012, 1, 1, 14, 0), "10:00 EDT".to_time(:utc)
|
658
|
+
# end
|
659
|
+
# end
|
660
|
+
# end
|
661
|
+
|
662
|
+
# def test_partial_string_to_time_when_current_time_is_daylight_savings
|
663
|
+
# with_env_tz "US/Eastern" do
|
664
|
+
# Time.stub(:now, Time.local(2012, 7, 1)) do
|
665
|
+
# assert_equal Time.local(2012, 7, 1, 10, 0), "10:00".to_time
|
666
|
+
# assert_equal Time.utc(2012, 7, 1, 10, 0), "10:00".to_time(:utc)
|
667
|
+
# assert_equal Time.local(2012, 7, 1, 7, 0), "10:00 -0100".to_time
|
668
|
+
# assert_equal Time.utc(2012, 7, 1, 11, 0), "10:00 -0100".to_time(:utc)
|
669
|
+
# assert_equal Time.local(2012, 7, 1, 11, 0), "10:00 -0500".to_time
|
670
|
+
# assert_equal Time.utc(2012, 7, 1, 15, 0), "10:00 -0500".to_time(:utc)
|
671
|
+
# assert_equal Time.local(2012, 7, 1, 6, 0), "10:00 UTC".to_time
|
672
|
+
# assert_equal Time.utc(2012, 7, 1, 10, 0), "10:00 UTC".to_time(:utc)
|
673
|
+
# assert_equal Time.local(2012, 7, 1, 14, 0), "10:00 PST".to_time
|
674
|
+
# assert_equal Time.utc(2012, 7, 1, 18, 0), "10:00 PST".to_time(:utc)
|
675
|
+
# assert_equal Time.local(2012, 7, 1, 13, 0), "10:00 PDT".to_time
|
676
|
+
# assert_equal Time.utc(2012, 7, 1, 17, 0), "10:00 PDT".to_time(:utc)
|
677
|
+
# assert_equal Time.local(2012, 7, 1, 11, 0), "10:00 EST".to_time
|
678
|
+
# assert_equal Time.utc(2012, 7, 1, 15, 0), "10:00 EST".to_time(:utc)
|
679
|
+
# assert_equal Time.local(2012, 7, 1, 10, 0), "10:00 EDT".to_time
|
680
|
+
# assert_equal Time.utc(2012, 7, 1, 14, 0), "10:00 EDT".to_time(:utc)
|
681
|
+
# end
|
682
|
+
# end
|
683
|
+
# end
|
684
|
+
|
328
685
|
# def test_string_to_datetime
|
329
686
|
# assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime
|
330
687
|
# assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset
|
@@ -332,271 +689,285 @@ end
|
|
332
689
|
# assert_equal DateTime.civil(2039, 2, 27, 23, 50, 19 + Rational(275038, 1000000), "-04:00"), "2039-02-27T23:50:19.275038-04:00".to_datetime
|
333
690
|
# assert_nil "".to_datetime
|
334
691
|
# end
|
335
|
-
|
692
|
+
|
336
693
|
# def test_partial_string_to_datetime
|
337
694
|
# now = DateTime.now
|
338
695
|
# assert_equal DateTime.civil(now.year, now.month, now.day, 23, 50), "23:50".to_datetime
|
339
696
|
# assert_equal DateTime.civil(now.year, now.month, now.day, 23, 50, 0, "-04:00"), "23:50 -0400".to_datetime
|
340
697
|
# end
|
341
|
-
|
698
|
+
|
342
699
|
# def test_string_to_date
|
343
700
|
# assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date
|
344
701
|
# assert_nil "".to_date
|
345
702
|
# assert_equal Date.new(Date.today.year, 2, 3), "Feb 3rd".to_date
|
346
703
|
# end
|
347
|
-
#
|
348
|
-
# protected
|
349
|
-
# def with_env_tz(new_tz = 'US/Eastern')
|
350
|
-
# old_tz, ENV['TZ'] = ENV['TZ'], new_tz
|
351
|
-
# yield
|
352
|
-
# ensure
|
353
|
-
# old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
|
354
|
-
# end
|
355
704
|
# end
|
356
|
-
|
705
|
+
|
357
706
|
# class StringBehaviourTest < ActiveSupport::TestCase
|
358
707
|
# def test_acts_like_string
|
359
|
-
#
|
708
|
+
# assert_predicate "Bambi", :acts_like_string?
|
360
709
|
# end
|
361
710
|
# end
|
362
|
-
|
711
|
+
|
363
712
|
# class CoreExtStringMultibyteTest < ActiveSupport::TestCase
|
364
|
-
# UTF8_STRING =
|
365
|
-
# ASCII_STRING =
|
366
|
-
# EUC_JP_STRING =
|
713
|
+
# UTF8_STRING = "こにちわ"
|
714
|
+
# ASCII_STRING = "ohayo".encode("US-ASCII")
|
715
|
+
# EUC_JP_STRING = "さよなら".encode("EUC-JP")
|
367
716
|
# INVALID_UTF8_STRING = "\270\236\010\210\245"
|
368
|
-
|
717
|
+
|
369
718
|
# def test_core_ext_adds_mb_chars
|
370
719
|
# assert_respond_to UTF8_STRING, :mb_chars
|
371
720
|
# end
|
372
|
-
|
721
|
+
|
373
722
|
# def test_string_should_recognize_utf8_strings
|
374
|
-
#
|
375
|
-
#
|
376
|
-
#
|
377
|
-
#
|
723
|
+
# assert_predicate UTF8_STRING, :is_utf8?
|
724
|
+
# assert_predicate ASCII_STRING, :is_utf8?
|
725
|
+
# assert_not_predicate EUC_JP_STRING, :is_utf8?
|
726
|
+
# assert_not_predicate INVALID_UTF8_STRING, :is_utf8?
|
378
727
|
# end
|
379
|
-
|
728
|
+
|
380
729
|
# def test_mb_chars_returns_instance_of_proxy_class
|
381
730
|
# assert_kind_of ActiveSupport::Multibyte.proxy_class, UTF8_STRING.mb_chars
|
382
731
|
# end
|
383
732
|
# end
|
384
|
-
|
733
|
+
|
385
734
|
# class OutputSafetyTest < ActiveSupport::TestCase
|
386
735
|
# def setup
|
387
|
-
# @string = "hello"
|
736
|
+
# @string = "hello".dup
|
388
737
|
# @object = Class.new(Object) do
|
389
738
|
# def to_s
|
390
739
|
# "other"
|
391
740
|
# end
|
392
741
|
# end.new
|
393
742
|
# end
|
394
|
-
|
743
|
+
|
395
744
|
# test "A string is unsafe by default" do
|
396
|
-
#
|
745
|
+
# assert_not_predicate @string, :html_safe?
|
397
746
|
# end
|
398
|
-
|
747
|
+
|
399
748
|
# test "A string can be marked safe" do
|
400
749
|
# string = @string.html_safe
|
401
|
-
#
|
750
|
+
# assert_predicate string, :html_safe?
|
402
751
|
# end
|
403
|
-
|
752
|
+
|
404
753
|
# test "Marking a string safe returns the string" do
|
405
754
|
# assert_equal @string, @string.html_safe
|
406
755
|
# end
|
407
|
-
|
408
|
-
# test "
|
409
|
-
#
|
756
|
+
|
757
|
+
# test "An integer is safe by default" do
|
758
|
+
# assert_predicate 5, :html_safe?
|
410
759
|
# end
|
411
|
-
|
760
|
+
|
412
761
|
# test "a float is safe by default" do
|
413
|
-
#
|
762
|
+
# assert_predicate 5.7, :html_safe?
|
414
763
|
# end
|
415
|
-
|
764
|
+
|
416
765
|
# test "An object is unsafe by default" do
|
417
|
-
#
|
766
|
+
# assert_not_predicate @object, :html_safe?
|
418
767
|
# end
|
419
|
-
|
768
|
+
|
420
769
|
# test "Adding an object to a safe string returns a safe string" do
|
421
770
|
# string = @string.html_safe
|
422
771
|
# string << @object
|
423
|
-
|
772
|
+
|
424
773
|
# assert_equal "helloother", string
|
425
|
-
#
|
774
|
+
# assert_predicate string, :html_safe?
|
426
775
|
# end
|
427
|
-
|
776
|
+
|
428
777
|
# test "Adding a safe string to another safe string returns a safe string" do
|
429
778
|
# @other_string = "other".html_safe
|
430
779
|
# string = @string.html_safe
|
431
780
|
# @combination = @other_string + string
|
432
|
-
|
781
|
+
|
433
782
|
# assert_equal "otherhello", @combination
|
434
|
-
#
|
783
|
+
# assert_predicate @combination, :html_safe?
|
435
784
|
# end
|
436
|
-
|
785
|
+
|
437
786
|
# test "Adding an unsafe string to a safe string escapes it and returns a safe string" do
|
438
787
|
# @other_string = "other".html_safe
|
439
788
|
# @combination = @other_string + "<foo>"
|
440
789
|
# @other_combination = @string + "<foo>"
|
441
|
-
|
790
|
+
|
442
791
|
# assert_equal "other<foo>", @combination
|
443
792
|
# assert_equal "hello<foo>", @other_combination
|
444
|
-
|
445
|
-
#
|
446
|
-
#
|
793
|
+
|
794
|
+
# assert_predicate @combination, :html_safe?
|
795
|
+
# assert_not_predicate @other_combination, :html_safe?
|
447
796
|
# end
|
448
|
-
|
797
|
+
|
798
|
+
# test "Prepending safe onto unsafe yields unsafe" do
|
799
|
+
# @string.prepend "other".html_safe
|
800
|
+
# assert_not_predicate @string, :html_safe?
|
801
|
+
# assert_equal "otherhello", @string
|
802
|
+
# end
|
803
|
+
|
804
|
+
# test "Prepending unsafe onto safe yields escaped safe" do
|
805
|
+
# other = "other".html_safe
|
806
|
+
# other.prepend "<foo>"
|
807
|
+
# assert_predicate other, :html_safe?
|
808
|
+
# assert_equal "<foo>other", other
|
809
|
+
# end
|
810
|
+
|
449
811
|
# test "Concatting safe onto unsafe yields unsafe" do
|
450
|
-
# @other_string = "other"
|
451
|
-
|
812
|
+
# @other_string = "other".dup
|
813
|
+
|
452
814
|
# string = @string.html_safe
|
453
815
|
# @other_string.concat(string)
|
454
|
-
#
|
816
|
+
# assert_not_predicate @other_string, :html_safe?
|
455
817
|
# end
|
456
|
-
|
818
|
+
|
457
819
|
# test "Concatting unsafe onto safe yields escaped safe" do
|
458
820
|
# @other_string = "other".html_safe
|
459
821
|
# string = @other_string.concat("<foo>")
|
460
822
|
# assert_equal "other<foo>", string
|
461
|
-
#
|
823
|
+
# assert_predicate string, :html_safe?
|
462
824
|
# end
|
463
|
-
|
825
|
+
|
464
826
|
# test "Concatting safe onto safe yields safe" do
|
465
827
|
# @other_string = "other".html_safe
|
466
828
|
# string = @string.html_safe
|
467
|
-
|
829
|
+
|
468
830
|
# @other_string.concat(string)
|
469
|
-
#
|
831
|
+
# assert_predicate @other_string, :html_safe?
|
470
832
|
# end
|
471
|
-
|
833
|
+
|
472
834
|
# test "Concatting safe onto unsafe with << yields unsafe" do
|
473
|
-
# @other_string = "other"
|
835
|
+
# @other_string = "other".dup
|
474
836
|
# string = @string.html_safe
|
475
|
-
|
837
|
+
|
476
838
|
# @other_string << string
|
477
|
-
#
|
839
|
+
# assert_not_predicate @other_string, :html_safe?
|
478
840
|
# end
|
479
|
-
|
841
|
+
|
480
842
|
# test "Concatting unsafe onto safe with << yields escaped safe" do
|
481
843
|
# @other_string = "other".html_safe
|
482
844
|
# string = @other_string << "<foo>"
|
483
845
|
# assert_equal "other<foo>", string
|
484
|
-
#
|
846
|
+
# assert_predicate string, :html_safe?
|
485
847
|
# end
|
486
|
-
|
848
|
+
|
487
849
|
# test "Concatting safe onto safe with << yields safe" do
|
488
850
|
# @other_string = "other".html_safe
|
489
851
|
# string = @string.html_safe
|
490
|
-
|
852
|
+
|
491
853
|
# @other_string << string
|
492
|
-
#
|
854
|
+
# assert_predicate @other_string, :html_safe?
|
493
855
|
# end
|
494
|
-
|
856
|
+
|
495
857
|
# test "Concatting safe onto unsafe with % yields unsafe" do
|
496
858
|
# @other_string = "other%s"
|
497
859
|
# string = @string.html_safe
|
498
|
-
|
860
|
+
|
499
861
|
# @other_string = @other_string % string
|
500
|
-
#
|
862
|
+
# assert_not_predicate @other_string, :html_safe?
|
501
863
|
# end
|
502
|
-
|
864
|
+
|
503
865
|
# test "Concatting unsafe onto safe with % yields escaped safe" do
|
504
866
|
# @other_string = "other%s".html_safe
|
505
867
|
# string = @other_string % "<foo>"
|
506
|
-
|
868
|
+
|
507
869
|
# assert_equal "other<foo>", string
|
508
|
-
#
|
870
|
+
# assert_predicate string, :html_safe?
|
509
871
|
# end
|
510
|
-
|
872
|
+
|
511
873
|
# test "Concatting safe onto safe with % yields safe" do
|
512
874
|
# @other_string = "other%s".html_safe
|
513
875
|
# string = @string.html_safe
|
514
|
-
|
876
|
+
|
515
877
|
# @other_string = @other_string % string
|
516
|
-
#
|
878
|
+
# assert_predicate @other_string, :html_safe?
|
517
879
|
# end
|
518
|
-
|
880
|
+
|
519
881
|
# test "Concatting with % doesn't modify a string" do
|
520
882
|
# @other_string = ["<p>", "<b>", "<h1>"]
|
521
883
|
# _ = "%s %s %s".html_safe % @other_string
|
522
|
-
|
884
|
+
|
523
885
|
# assert_equal ["<p>", "<b>", "<h1>"], @other_string
|
524
886
|
# end
|
525
|
-
|
526
|
-
# test "Concatting
|
887
|
+
|
888
|
+
# test "Concatting an integer to safe always yields safe" do
|
527
889
|
# string = @string.html_safe
|
528
890
|
# string = string.concat(13)
|
529
|
-
# assert_equal "hello".concat(13), string
|
530
|
-
#
|
891
|
+
# assert_equal "hello".dup.concat(13), string
|
892
|
+
# assert_predicate string, :html_safe?
|
531
893
|
# end
|
532
|
-
|
533
|
-
# test
|
534
|
-
# assert_equal
|
535
|
-
# end
|
536
|
-
#
|
537
|
-
# test 'knows whether it is encoding aware' do
|
538
|
-
# assert_deprecated do
|
539
|
-
# assert 'ruby'.encoding_aware?
|
540
|
-
# end
|
894
|
+
|
895
|
+
# test "emits normal string yaml" do
|
896
|
+
# assert_equal "foo".to_yaml, "foo".html_safe.to_yaml(foo: 1)
|
541
897
|
# end
|
542
|
-
|
898
|
+
|
543
899
|
# test "call to_param returns a normal string" do
|
544
900
|
# string = @string.html_safe
|
545
|
-
#
|
546
|
-
#
|
901
|
+
# assert_predicate string, :html_safe?
|
902
|
+
# assert_not_predicate string.to_param, :html_safe?
|
547
903
|
# end
|
548
|
-
|
904
|
+
|
549
905
|
# test "ERB::Util.html_escape should escape unsafe characters" do
|
550
906
|
# string = '<>&"\''
|
551
|
-
# expected =
|
907
|
+
# expected = "<>&"'"
|
552
908
|
# assert_equal expected, ERB::Util.html_escape(string)
|
553
909
|
# end
|
554
|
-
|
910
|
+
|
555
911
|
# test "ERB::Util.html_escape should correctly handle invalid UTF-8 strings" do
|
556
|
-
# string =
|
557
|
-
# expected =
|
912
|
+
# string = "\251 <"
|
913
|
+
# expected = "© <"
|
558
914
|
# assert_equal expected, ERB::Util.html_escape(string)
|
559
915
|
# end
|
560
|
-
|
916
|
+
|
561
917
|
# test "ERB::Util.html_escape should not escape safe strings" do
|
562
918
|
# string = "<b>hello</b>".html_safe
|
563
919
|
# assert_equal string, ERB::Util.html_escape(string)
|
564
920
|
# end
|
921
|
+
|
922
|
+
# test "ERB::Util.html_escape_once only escapes once" do
|
923
|
+
# string = "1 < 2 & 3"
|
924
|
+
# escaped_string = "1 < 2 & 3"
|
925
|
+
|
926
|
+
# assert_equal escaped_string, ERB::Util.html_escape_once(string)
|
927
|
+
# assert_equal escaped_string, ERB::Util.html_escape_once(escaped_string)
|
928
|
+
# end
|
929
|
+
|
930
|
+
# test "ERB::Util.html_escape_once should correctly handle invalid UTF-8 strings" do
|
931
|
+
# string = "\251 <"
|
932
|
+
# expected = "© <"
|
933
|
+
# assert_equal expected, ERB::Util.html_escape_once(string)
|
934
|
+
# end
|
565
935
|
# end
|
566
|
-
|
936
|
+
|
567
937
|
# class StringExcludeTest < ActiveSupport::TestCase
|
568
|
-
# test
|
569
|
-
# assert_equal false,
|
570
|
-
# assert_equal true,
|
938
|
+
# test "inverse of #include" do
|
939
|
+
# assert_equal false, "foo".exclude?("o")
|
940
|
+
# assert_equal true, "foo".exclude?("p")
|
571
941
|
# end
|
572
942
|
# end
|
573
|
-
|
943
|
+
|
574
944
|
# class StringIndentTest < ActiveSupport::TestCase
|
575
|
-
# test
|
576
|
-
# [
|
945
|
+
# test "does not indent strings that only contain newlines (edge cases)" do
|
946
|
+
# ["", "\n", "\n" * 7].each do |string|
|
947
|
+
# str = string.dup
|
577
948
|
# assert_nil str.indent!(8)
|
578
949
|
# assert_equal str, str.indent(8)
|
579
950
|
# assert_equal str, str.indent(1, "\t")
|
580
951
|
# end
|
581
952
|
# end
|
582
|
-
|
953
|
+
|
583
954
|
# test "by default, indents with spaces if the existing indentation uses them" do
|
584
955
|
# assert_equal " foo\n bar", "foo\n bar".indent(4)
|
585
956
|
# end
|
586
|
-
|
957
|
+
|
587
958
|
# test "by default, indents with tabs if the existing indentation uses them" do
|
588
959
|
# assert_equal "\tfoo\n\t\t\bar", "foo\n\t\bar".indent(1)
|
589
960
|
# end
|
590
|
-
|
961
|
+
|
591
962
|
# test "by default, indents with spaces as a fallback if there is no indentation" do
|
592
963
|
# assert_equal " foo\n bar\n baz", "foo\nbar\nbaz".indent(3)
|
593
964
|
# end
|
594
|
-
|
965
|
+
|
595
966
|
# # Nothing is said about existing indentation that mixes spaces and tabs, so
|
596
967
|
# # there is nothing to test.
|
597
|
-
|
598
|
-
# test
|
599
|
-
# assert_equal <<EXPECTED, <<ACTUAL.indent(4,
|
968
|
+
|
969
|
+
# test "uses the indent char if passed" do
|
970
|
+
# assert_equal <<EXPECTED, <<ACTUAL.indent(4, ".")
|
600
971
|
# .... def some_method(x, y)
|
601
972
|
# .... some_code
|
602
973
|
# .... end
|
@@ -605,8 +976,8 @@ end
|
|
605
976
|
# some_code
|
606
977
|
# end
|
607
978
|
# ACTUAL
|
608
|
-
|
609
|
-
# assert_equal <<EXPECTED, <<ACTUAL.indent(2,
|
979
|
+
|
980
|
+
# assert_equal <<EXPECTED, <<ACTUAL.indent(2, " ")
|
610
981
|
# def some_method(x, y)
|
611
982
|
# some_code
|
612
983
|
# end
|
@@ -616,12 +987,12 @@ end
|
|
616
987
|
# end
|
617
988
|
# ACTUAL
|
618
989
|
# end
|
619
|
-
|
990
|
+
|
620
991
|
# test "does not indent blank lines by default" do
|
621
992
|
# assert_equal " foo\n\n bar", "foo\n\nbar".indent(1)
|
622
993
|
# end
|
623
|
-
|
624
|
-
# test
|
994
|
+
|
995
|
+
# test "indents blank lines if told so" do
|
625
996
|
# assert_equal " foo\n \n bar", "foo\n\nbar".indent(1, nil, true)
|
626
997
|
# end
|
627
998
|
# end
|