2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/person"
|
|
|
|
require "models/topic"
|
|
|
|
require "pp"
|
|
|
|
require "active_support/core_ext/string/strip"
|
2013-05-21 13:19:50 -04:00
|
|
|
|
|
|
|
class NonExistentTable < ActiveRecord::Base; end
|
|
|
|
|
|
|
|
class CoreTest < ActiveRecord::TestCase
|
|
|
|
fixtures :topics
|
|
|
|
|
|
|
|
def test_inspect_class
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "ActiveRecord::Base", ActiveRecord::Base.inspect
|
|
|
|
assert_equal "LoosePerson(abstract)", LoosePerson.inspect
|
2013-05-21 13:19:50 -04:00
|
|
|
assert_match(/^Topic\(id: integer, title: string/, Topic.inspect)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect_instance
|
|
|
|
topic = topics(:first)
|
|
|
|
assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_s(:db)}", bonus_time: "#{topic.bonus_time.to_s(:db)}", last_read: "#{topic.last_read.to_s(:db)}", content: "Have a nice day", important: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "#{topic.created_at.to_s(:db)}", updated_at: "#{topic.updated_at.to_s(:db)}">), topic.inspect
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect_new_instance
|
|
|
|
assert_match(/Topic id: nil/, Topic.new.inspect)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect_limited_select_instance
|
2016-08-06 13:37:57 -04:00
|
|
|
assert_equal %(#<Topic id: 1>), Topic.all.merge!(select: "id", where: "id = 1").first.inspect
|
|
|
|
assert_equal %(#<Topic id: 1, title: "The First Topic">), Topic.all.merge!(select: "id, title", where: "id = 1").first.inspect
|
2013-05-21 13:19:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect_class_without_table
|
|
|
|
assert_equal "NonExistentTable(Table doesn't exist)", NonExistentTable.inspect
|
|
|
|
end
|
2014-05-18 17:27:02 -04:00
|
|
|
|
|
|
|
def test_pretty_print_new
|
|
|
|
topic = Topic.new
|
2017-06-20 05:43:21 -04:00
|
|
|
actual = "".dup
|
2014-05-18 17:27:02 -04:00
|
|
|
PP.pp(topic, StringIO.new(actual))
|
|
|
|
expected = <<-PRETTY.strip_heredoc
|
|
|
|
#<Topic:0xXXXXXX
|
|
|
|
id: nil,
|
|
|
|
title: nil,
|
|
|
|
author_name: nil,
|
|
|
|
author_email_address: "test@test.com",
|
|
|
|
written_on: nil,
|
|
|
|
bonus_time: nil,
|
|
|
|
last_read: nil,
|
|
|
|
content: nil,
|
|
|
|
important: nil,
|
|
|
|
approved: true,
|
|
|
|
replies_count: 0,
|
|
|
|
unique_replies_count: 0,
|
|
|
|
parent_id: nil,
|
|
|
|
parent_title: nil,
|
|
|
|
type: nil,
|
|
|
|
group: nil,
|
|
|
|
created_at: nil,
|
|
|
|
updated_at: nil>
|
|
|
|
PRETTY
|
2016-08-06 12:26:20 -04:00
|
|
|
assert actual.start_with?(expected.split("XXXXXX").first)
|
|
|
|
assert actual.end_with?(expected.split("XXXXXX").last)
|
2014-05-18 17:27:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pretty_print_persisted
|
|
|
|
topic = topics(:first)
|
2017-06-20 05:43:21 -04:00
|
|
|
actual = "".dup
|
2014-05-18 17:27:02 -04:00
|
|
|
PP.pp(topic, StringIO.new(actual))
|
|
|
|
expected = <<-PRETTY.strip_heredoc
|
|
|
|
#<Topic:0x\\w+
|
|
|
|
id: 1,
|
|
|
|
title: "The First Topic",
|
|
|
|
author_name: "David",
|
|
|
|
author_email_address: "david@loudthinking.com",
|
|
|
|
written_on: 2003-07-16 14:28:11 UTC,
|
|
|
|
bonus_time: 2000-01-01 14:28:00 UTC,
|
|
|
|
last_read: Thu, 15 Apr 2004,
|
|
|
|
content: "Have a nice day",
|
|
|
|
important: nil,
|
|
|
|
approved: false,
|
|
|
|
replies_count: 1,
|
|
|
|
unique_replies_count: 0,
|
|
|
|
parent_id: nil,
|
|
|
|
parent_title: nil,
|
|
|
|
type: nil,
|
|
|
|
group: nil,
|
|
|
|
created_at: [^,]+,
|
|
|
|
updated_at: [^,>]+>
|
|
|
|
PRETTY
|
|
|
|
assert_match(/\A#{expected}\z/, actual)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pretty_print_uninitialized
|
|
|
|
topic = Topic.allocate
|
2017-06-20 05:43:21 -04:00
|
|
|
actual = "".dup
|
2014-05-18 17:27:02 -04:00
|
|
|
PP.pp(topic, StringIO.new(actual))
|
|
|
|
expected = "#<Topic:XXXXXX not initialized>\n"
|
2016-08-06 12:26:20 -04:00
|
|
|
assert actual.start_with?(expected.split("XXXXXX").first)
|
|
|
|
assert actual.end_with?(expected.split("XXXXXX").last)
|
2014-05-18 17:27:02 -04:00
|
|
|
end
|
2015-01-12 18:11:23 -05:00
|
|
|
|
|
|
|
def test_pretty_print_overridden_by_inspect
|
|
|
|
subtopic = Class.new(Topic) do
|
|
|
|
def inspect
|
|
|
|
"inspecting topic"
|
|
|
|
end
|
|
|
|
end
|
2017-06-20 05:43:21 -04:00
|
|
|
actual = "".dup
|
2015-01-12 18:11:23 -05:00
|
|
|
PP.pp(subtopic.new, StringIO.new(actual))
|
|
|
|
assert_equal "inspecting topic\n", actual
|
|
|
|
end
|
2013-05-21 13:19:50 -04:00
|
|
|
end
|