aboutsummaryrefslogtreecommitdiffstats
path: root/lib/strip_attributes/test/strip_attributes_test.rb
blob: 8158dc664509431a11c64e2113b720aff72420a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require "#{File.dirname(__FILE__)}/test_helper"

module MockAttributes
  def self.included(base)
    base.column :foo, :string
    base.column :bar, :string
    base.column :biz, :string
    base.column :baz, :string
  end
end

class StripAllMockRecord < ActiveRecord::Base
  include MockAttributes
  strip_attributes!
end

class StripOnlyOneMockRecord < ActiveRecord::Base
  include MockAttributes
  strip_attributes! :only => :foo
end

class StripOnlyThreeMockRecord < ActiveRecord::Base
  include MockAttributes
  strip_attributes! :only => [:foo, :bar, :biz]
end

class StripExceptOneMockRecord < ActiveRecord::Base
  include MockAttributes
  strip_attributes! :except => :foo
end

class StripExceptThreeMockRecord < ActiveRecord::Base
  include MockAttributes
  strip_attributes! :except => [:foo, :bar, :biz]
end

class StripAttributesTest < Test::Unit::TestCase
  def setup
    @init_params = { :foo => "\tfoo", :bar => "bar \t ", :biz => "\tbiz ", :baz => "" }
  end

  def test_should_exist
    assert Object.const_defined?(:StripAttributes)
  end

  def test_should_strip_all_fields
    record = StripAllMockRecord.new(@init_params)
    record.valid?
    assert_equal "foo", record.foo
    assert_equal "bar", record.bar
    assert_equal "biz", record.biz
    assert_equal "",    record.baz
  end

  def test_should_strip_only_one_field
    record = StripOnlyOneMockRecord.new(@init_params)
    record.valid?
    assert_equal "foo",     record.foo
    assert_equal "bar \t ", record.bar
    assert_equal "\tbiz ",  record.biz
    assert_equal "",        record.baz
  end

  def test_should_strip_only_three_fields
    record = StripOnlyThreeMockRecord.new(@init_params)
    record.valid?
    assert_equal "foo", record.foo
    assert_equal "bar", record.bar
    assert_equal "biz", record.biz
    assert_equal "",    record.baz
  end

  def test_should_strip_all_except_one_field
    record = StripExceptOneMockRecord.new(@init_params)
    record.valid?
    assert_equal "\tfoo", record.foo
    assert_equal "bar",   record.bar
    assert_equal "biz",   record.biz
    assert_equal "",      record.baz
  end

  def test_should_strip_all_except_three_fields
    record = StripExceptThreeMockRecord.new(@init_params)
    record.valid?
    assert_equal "\tfoo",   record.foo
    assert_equal "bar \t ", record.bar
    assert_equal "\tbiz ",  record.biz
    assert_equal "",        record.baz
  end
end