|
1 |
| -module ActiveModel::Serializer::Lint |
2 |
| - # == Active \Model \Serializer \Lint \Tests |
3 |
| - # |
4 |
| - # You can test whether an object is compliant with the Active \Model \Serializers |
5 |
| - # API by including <tt>ActiveModel::Serializer::Lint::Tests</tt> in your TestCase. |
6 |
| - # It will include tests that tell you whether your object is fully compliant, |
7 |
| - # or if not, which aspects of the API are not implemented. |
8 |
| - # |
9 |
| - # Note an object is not required to implement all APIs in order to work |
10 |
| - # with Active \Model \Serializers. This module only intends to provide guidance in case |
11 |
| - # you want all features out of the box. |
12 |
| - # |
13 |
| - # These tests do not attempt to determine the semantic correctness of the |
14 |
| - # returned values. For instance, you could implement <tt>serializable_hash</tt> to |
15 |
| - # always return +{}+, and the tests would pass. It is up to you to ensure |
16 |
| - # that the values are semantically meaningful. |
17 |
| - module Tests |
18 |
| - # Passes if the object responds to <tt>serializable_hash</tt> and if it takes |
19 |
| - # zero or one arguments. |
20 |
| - # Fails otherwise. |
21 |
| - # |
22 |
| - # <tt>serializable_hash</tt> returns a hash representation of a object's attributes. |
23 |
| - # Typically, it is implemented by including ActiveModel::Serialization. |
24 |
| - def test_serializable_hash |
25 |
| - assert_respond_to resource, :serializable_hash, 'The resource should respond to serializable_hash' |
26 |
| - resource.serializable_hash |
27 |
| - resource.serializable_hash(nil) |
28 |
| - end |
| 1 | +module ActiveModel |
| 2 | + class Serializer |
| 3 | + module Lint |
| 4 | + # == Active \Model \Serializer \Lint \Tests |
| 5 | + # |
| 6 | + # You can test whether an object is compliant with the Active \Model \Serializers |
| 7 | + # API by including <tt>ActiveModel::Serializer::Lint::Tests</tt> in your TestCase. |
| 8 | + # It will include tests that tell you whether your object is fully compliant, |
| 9 | + # or if not, which aspects of the API are not implemented. |
| 10 | + # |
| 11 | + # Note an object is not required to implement all APIs in order to work |
| 12 | + # with Active \Model \Serializers. This module only intends to provide guidance in case |
| 13 | + # you want all features out of the box. |
| 14 | + # |
| 15 | + # These tests do not attempt to determine the semantic correctness of the |
| 16 | + # returned values. For instance, you could implement <tt>serializable_hash</tt> to |
| 17 | + # always return +{}+, and the tests would pass. It is up to you to ensure |
| 18 | + # that the values are semantically meaningful. |
| 19 | + module Tests |
| 20 | + # Passes if the object responds to <tt>serializable_hash</tt> and if it takes |
| 21 | + # zero or one arguments. |
| 22 | + # Fails otherwise. |
| 23 | + # |
| 24 | + # <tt>serializable_hash</tt> returns a hash representation of a object's attributes. |
| 25 | + # Typically, it is implemented by including ActiveModel::Serialization. |
| 26 | + def test_serializable_hash |
| 27 | + assert_respond_to resource, :serializable_hash, 'The resource should respond to serializable_hash' |
| 28 | + resource.serializable_hash |
| 29 | + resource.serializable_hash(nil) |
| 30 | + end |
29 | 31 |
|
30 |
| - # Passes if the object responds to <tt>read_attribute_for_serialization</tt> |
31 |
| - # and if it requires one argument (the attribute to be read). |
32 |
| - # Fails otherwise. |
33 |
| - # |
34 |
| - # <tt>read_attribute_for_serialization</tt> gets the attribute value for serialization |
35 |
| - # Typically, it is implemented by including ActiveModel::Serialization. |
36 |
| - def test_read_attribute_for_serialization |
37 |
| - assert_respond_to resource, :read_attribute_for_serialization, 'The resource should respond to read_attribute_for_serialization' |
38 |
| - actual_arity = resource.method(:read_attribute_for_serialization).arity |
39 |
| - # using absolute value since arity is: |
40 |
| - # 1 for def read_attribute_for_serialization(name); end |
41 |
| - # -1 for alias :read_attribute_for_serialization :send |
42 |
| - assert_equal 1, actual_arity.abs, "expected #{actual_arity.inspect}.abs to be 1 or -1" |
43 |
| - end |
| 32 | + # Passes if the object responds to <tt>read_attribute_for_serialization</tt> |
| 33 | + # and if it requires one argument (the attribute to be read). |
| 34 | + # Fails otherwise. |
| 35 | + # |
| 36 | + # <tt>read_attribute_for_serialization</tt> gets the attribute value for serialization |
| 37 | + # Typically, it is implemented by including ActiveModel::Serialization. |
| 38 | + def test_read_attribute_for_serialization |
| 39 | + assert_respond_to resource, :read_attribute_for_serialization, 'The resource should respond to read_attribute_for_serialization' |
| 40 | + actual_arity = resource.method(:read_attribute_for_serialization).arity |
| 41 | + # using absolute value since arity is: |
| 42 | + # 1 for def read_attribute_for_serialization(name); end |
| 43 | + # -1 for alias :read_attribute_for_serialization :send |
| 44 | + assert_equal 1, actual_arity.abs, "expected #{actual_arity.inspect}.abs to be 1 or -1" |
| 45 | + end |
44 | 46 |
|
45 |
| - # Passes if the object responds to <tt>as_json</tt> and if it takes |
46 |
| - # zero or one arguments. |
47 |
| - # Fails otherwise. |
48 |
| - # |
49 |
| - # <tt>as_json</tt> returns a hash representation of a serialized object. |
50 |
| - # It may delegate to <tt>serializable_hash</tt> |
51 |
| - # Typically, it is implemented either by including ActiveModel::Serialization |
52 |
| - # which includes ActiveModel::Serializers::JSON. |
53 |
| - # or by the JSON gem when required. |
54 |
| - def test_as_json |
55 |
| - assert_respond_to resource, :as_json |
56 |
| - resource.as_json |
57 |
| - resource.as_json(nil) |
58 |
| - end |
| 47 | + # Passes if the object responds to <tt>as_json</tt> and if it takes |
| 48 | + # zero or one arguments. |
| 49 | + # Fails otherwise. |
| 50 | + # |
| 51 | + # <tt>as_json</tt> returns a hash representation of a serialized object. |
| 52 | + # It may delegate to <tt>serializable_hash</tt> |
| 53 | + # Typically, it is implemented either by including ActiveModel::Serialization |
| 54 | + # which includes ActiveModel::Serializers::JSON. |
| 55 | + # or by the JSON gem when required. |
| 56 | + def test_as_json |
| 57 | + assert_respond_to resource, :as_json |
| 58 | + resource.as_json |
| 59 | + resource.as_json(nil) |
| 60 | + end |
59 | 61 |
|
60 |
| - # Passes if the object responds to <tt>to_json</tt> and if it takes |
61 |
| - # zero or one arguments. |
62 |
| - # Fails otherwise. |
63 |
| - # |
64 |
| - # <tt>to_json</tt> returns a string representation (JSON) of a serialized object. |
65 |
| - # It may be called on the result of <tt>as_json</tt>. |
66 |
| - # Typically, it is implemented on all objects when the JSON gem is required. |
67 |
| - def test_to_json |
68 |
| - assert_respond_to resource, :to_json |
69 |
| - resource.to_json |
70 |
| - resource.to_json(nil) |
71 |
| - end |
| 62 | + # Passes if the object responds to <tt>to_json</tt> and if it takes |
| 63 | + # zero or one arguments. |
| 64 | + # Fails otherwise. |
| 65 | + # |
| 66 | + # <tt>to_json</tt> returns a string representation (JSON) of a serialized object. |
| 67 | + # It may be called on the result of <tt>as_json</tt>. |
| 68 | + # Typically, it is implemented on all objects when the JSON gem is required. |
| 69 | + def test_to_json |
| 70 | + assert_respond_to resource, :to_json |
| 71 | + resource.to_json |
| 72 | + resource.to_json(nil) |
| 73 | + end |
72 | 74 |
|
73 |
| - # Passes if the object responds to <tt>cache_key</tt> |
74 |
| - # Fails otherwise. |
75 |
| - # |
76 |
| - # <tt>cache_key</tt> returns a (self-expiring) unique key for the object, |
77 |
| - # and is part of the (self-expiring) cache_key, which is used by the |
78 |
| - # adapter. It is not required unless caching is enabled. |
79 |
| - def test_cache_key |
80 |
| - assert_respond_to resource, :cache_key |
81 |
| - actual_arity = resource.method(:cache_key).arity |
82 |
| - assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1" |
83 |
| - end |
| 75 | + # Passes if the object responds to <tt>cache_key</tt> |
| 76 | + # Fails otherwise. |
| 77 | + # |
| 78 | + # <tt>cache_key</tt> returns a (self-expiring) unique key for the object, |
| 79 | + # and is part of the (self-expiring) cache_key, which is used by the |
| 80 | + # adapter. It is not required unless caching is enabled. |
| 81 | + def test_cache_key |
| 82 | + assert_respond_to resource, :cache_key |
| 83 | + actual_arity = resource.method(:cache_key).arity |
| 84 | + assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1" |
| 85 | + end |
84 | 86 |
|
85 |
| - # Passes if the object responds to <tt>updated_at</tt> and if it takes no |
86 |
| - # arguments. |
87 |
| - # Fails otherwise. |
88 |
| - # |
89 |
| - # <tt>updated_at</tt> returns a Time object or iso8601 string and |
90 |
| - # is part of the (self-expiring) cache_key, which is used by the adapter. |
91 |
| - # It is not required unless caching is enabled. |
92 |
| - def test_updated_at |
93 |
| - assert_respond_to resource, :updated_at |
94 |
| - actual_arity = resource.method(:updated_at).arity |
95 |
| - assert_equal 0, actual_arity |
96 |
| - end |
| 87 | + # Passes if the object responds to <tt>updated_at</tt> and if it takes no |
| 88 | + # arguments. |
| 89 | + # Fails otherwise. |
| 90 | + # |
| 91 | + # <tt>updated_at</tt> returns a Time object or iso8601 string and |
| 92 | + # is part of the (self-expiring) cache_key, which is used by the adapter. |
| 93 | + # It is not required unless caching is enabled. |
| 94 | + def test_updated_at |
| 95 | + assert_respond_to resource, :updated_at |
| 96 | + actual_arity = resource.method(:updated_at).arity |
| 97 | + assert_equal 0, actual_arity |
| 98 | + end |
97 | 99 |
|
98 |
| - # Passes if the object responds to <tt>id</tt> and if it takes no |
99 |
| - # arguments. |
100 |
| - # Fails otherwise. |
101 |
| - # |
102 |
| - # <tt>id</tt> returns a unique identifier for the object. |
103 |
| - # It is not required unless caching is enabled. |
104 |
| - def test_id |
105 |
| - assert_respond_to resource, :id |
106 |
| - assert_equal 0, resource.method(:id).arity |
107 |
| - end |
| 100 | + # Passes if the object responds to <tt>id</tt> and if it takes no |
| 101 | + # arguments. |
| 102 | + # Fails otherwise. |
| 103 | + # |
| 104 | + # <tt>id</tt> returns a unique identifier for the object. |
| 105 | + # It is not required unless caching is enabled. |
| 106 | + def test_id |
| 107 | + assert_respond_to resource, :id |
| 108 | + assert_equal 0, resource.method(:id).arity |
| 109 | + end |
108 | 110 |
|
109 |
| - # Passes if the object's class responds to <tt>model_name</tt> and if it |
110 |
| - # is in an instance of +ActiveModel::Name+. |
111 |
| - # Fails otherwise. |
112 |
| - # |
113 |
| - # <tt>model_name</tt> returns an ActiveModel::Name instance. |
114 |
| - # It is used by the serializer to identify the object's type. |
115 |
| - # It is not required unless caching is enabled. |
116 |
| - def test_model_name |
117 |
| - resource_class = resource.class |
118 |
| - assert_respond_to resource_class, :model_name |
119 |
| - assert_instance_of resource_class.model_name, ActiveModel::Name |
120 |
| - end |
| 111 | + # Passes if the object's class responds to <tt>model_name</tt> and if it |
| 112 | + # is in an instance of +ActiveModel::Name+. |
| 113 | + # Fails otherwise. |
| 114 | + # |
| 115 | + # <tt>model_name</tt> returns an ActiveModel::Name instance. |
| 116 | + # It is used by the serializer to identify the object's type. |
| 117 | + # It is not required unless caching is enabled. |
| 118 | + def test_model_name |
| 119 | + resource_class = resource.class |
| 120 | + assert_respond_to resource_class, :model_name |
| 121 | + assert_instance_of resource_class.model_name, ActiveModel::Name |
| 122 | + end |
121 | 123 |
|
122 |
| - def test_active_model_errors |
123 |
| - assert_respond_to resource, :errors |
124 |
| - end |
| 124 | + def test_active_model_errors |
| 125 | + assert_respond_to resource, :errors |
| 126 | + end |
125 | 127 |
|
126 |
| - def test_active_model_errors_human_attribute_name |
127 |
| - assert_respond_to resource.class, :human_attribute_name |
128 |
| - assert_equal(-2, resource.class.method(:human_attribute_name).arity) |
129 |
| - end |
| 128 | + def test_active_model_errors_human_attribute_name |
| 129 | + assert_respond_to resource.class, :human_attribute_name |
| 130 | + assert_equal(-2, resource.class.method(:human_attribute_name).arity) |
| 131 | + end |
130 | 132 |
|
131 |
| - def test_active_model_errors_lookup_ancestors |
132 |
| - assert_respond_to resource.class, :lookup_ancestors |
133 |
| - assert_equal 0, resource.class.method(:lookup_ancestors).arity |
134 |
| - end |
| 133 | + def test_active_model_errors_lookup_ancestors |
| 134 | + assert_respond_to resource.class, :lookup_ancestors |
| 135 | + assert_equal 0, resource.class.method(:lookup_ancestors).arity |
| 136 | + end |
135 | 137 |
|
136 |
| - private |
| 138 | + private |
137 | 139 |
|
138 |
| - def resource |
139 |
| - @resource or fail "'@resource' must be set as the linted object" |
140 |
| - end |
| 140 | + def resource |
| 141 | + @resource or fail "'@resource' must be set as the linted object" |
| 142 | + end |
141 | 143 |
|
142 |
| - def assert_instance_of(result, name) |
143 |
| - assert result.instance_of?(name), "#{result} should be an instance of #{name}" |
| 144 | + def assert_instance_of(result, name) |
| 145 | + assert result.instance_of?(name), "#{result} should be an instance of #{name}" |
| 146 | + end |
| 147 | + end |
144 | 148 | end
|
145 | 149 | end
|
146 | 150 | end
|
0 commit comments