2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2016-08-06 12:26:20 -04:00
|
|
|
require "models/topic"
|
2010-11-23 14:12:46 -05:00
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
class CloneTest < ActiveRecord::TestCase
|
|
|
|
fixtures :topics
|
|
|
|
|
|
|
|
def test_persisted
|
|
|
|
topic = Topic.first
|
|
|
|
cloned = topic.clone
|
2016-08-06 12:26:20 -04:00
|
|
|
assert topic.persisted?, "topic persisted"
|
|
|
|
assert cloned.persisted?, "topic persisted"
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not cloned.new_record?, "topic is not new"
|
2019-05-03 12:37:37 -04:00
|
|
|
assert_not cloned.previously_new_record?, "topic was not previously new"
|
2021-06-04 07:02:54 -04:00
|
|
|
assert_not cloned.previously_persisted?, "topic was not previously persisted"
|
2010-11-23 14:12:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_stays_frozen
|
|
|
|
topic = Topic.first
|
|
|
|
topic.freeze
|
|
|
|
|
|
|
|
cloned = topic.clone
|
2016-08-06 12:26:20 -04:00
|
|
|
assert cloned.persisted?, "topic persisted"
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not cloned.new_record?, "topic is not new"
|
2016-08-06 12:26:20 -04:00
|
|
|
assert cloned.frozen?, "topic should be frozen"
|
2020-01-28 21:23:41 -05:00
|
|
|
assert_raise(FrozenError) { cloned.author_name = "Aaron" }
|
2010-11-23 14:12:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_shallow
|
|
|
|
topic = Topic.first
|
|
|
|
cloned = topic.clone
|
2016-08-06 12:26:20 -04:00
|
|
|
topic.author_name = "Aaron"
|
|
|
|
assert_equal "Aaron", cloned.author_name
|
2010-11-23 14:12:46 -05:00
|
|
|
end
|
2013-04-13 00:11:04 -04:00
|
|
|
|
|
|
|
def test_freezing_a_cloned_model_does_not_freeze_clone
|
|
|
|
cloned = Topic.new
|
|
|
|
clone = cloned.clone
|
|
|
|
cloned.freeze
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate clone, :frozen?
|
2020-01-28 21:23:41 -05:00
|
|
|
assert_raise(FrozenError) { cloned.author_name = "Aaron" }
|
2013-04-13 00:11:04 -04:00
|
|
|
end
|
2010-11-23 14:12:46 -05:00
|
|
|
end
|
|
|
|
end
|