2 // Copyright (c) 2004, 2005 Rodrigo B. de Oliveira (rbo@acm.org)
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Rodrigo B. de Oliveira nor the names of its
14 // contributors may be used to endorse or promote products derived from this
15 // software without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 namespace Boo
.Lang
.Useful
.Tests
.Attributes
31 import NUnit
.Framework
32 import System
.Runtime
.Serialization
33 import System
.Runtime
.CompilerServices
34 import Boo
.Lang
.Useful
.Attributes
.SingletonAttribute
as CUT
37 class SingletonAttributeTestFixture(AbstractAttributeTestFixture
):
39 @author Marcus Griep (neoeinstein+boo@gmail.com)
40 @author Sorin Ionescu (sorin.ionescu@gmail.com)
41 @author Rodrigo B. de Oliveira
43 final static SINGLETON_CLASS_NAME
= "SingletonObject"
46 def DefaultSingleton():
48 import Useful.Attributes
51 transient class ${SINGLETON_CLASS_NAME}:
55 import Useful.Attributes
57 public final transient class ${SINGLETON_CLASS_NAME}(object):
59 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
60 private def constructor():
63 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
64 public static ${CUT.DEFAULT_INSTANCE_PROPERTY_NAME} as ${SINGLETON_CLASS_NAME}:
66 return self.${CUT.NESTED_CLASS_NAME}.${CUT.INSTANCE_FIELD_NAME}
68 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
69 private final transient class ${CUT.NESTED_CLASS_NAME}(object):
71 public static def constructor():
72 self.${CUT.INSTANCE_FIELD_NAME} = ${SINGLETON_CLASS_NAME}()
74 private def constructor():
77 public static final ${CUT.INSTANCE_FIELD_NAME} as ${SINGLETON_CLASS_NAME}
79 RunTestCase(expected
, code
)
84 import Useful.Attributes
87 class ${SINGLETON_CLASS_NAME}:
91 import Useful.Attributes
93 public final class ${SINGLETON_CLASS_NAME}(object, ${CUT.ISERIALIZABLE_FULL_NAME}):
95 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
96 private def constructor():
99 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
100 public static ${CUT.DEFAULT_INSTANCE_PROPERTY_NAME} as ${SINGLETON_CLASS_NAME}:
102 return self.${CUT.NESTED_CLASS_NAME}.${CUT.INSTANCE_FIELD_NAME}
104 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
105 private final transient class ${CUT.NESTED_CLASS_NAME}(object):
107 public static def constructor():
108 self.${CUT.INSTANCE_FIELD_NAME} = ${SINGLETON_CLASS_NAME}()
110 private def constructor():
113 public static final ${CUT.INSTANCE_FIELD_NAME} as ${SINGLETON_CLASS_NAME}
115 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
116 private final class ${CUT.SERIALIZATION_OBJECT_CLASS_NAME}(object, ${CUT.IOBJECTREFERENCE_FULL_NAME}):
118 public virtual def GetRealObject(context as ${CUT.STREAMINGCONTEXT_FULL_NAME}) as object:
119 return ${SINGLETON_CLASS_NAME}.${CUT.DEFAULT_INSTANCE_PROPERTY_NAME}
121 public def constructor():
124 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
125 private virtual def ${CUT.ISERIALIZABLE_FULL_NAME}.${CUT.GETOBJECTDATA_METHOD_NAME}(info as ${CUT.SERIALIZATIONINFO_FULL_NAME}, context as ${CUT.STREAMINGCONTEXT_FULL_NAME}) as void:
126 info.SetType(${CUT.SERIALIZATION_OBJECT_CLASS_NAME})
128 RunTestCase(expected
, code
)
131 def GivenAccessorName():
132 accessorPropertyName
= "Factory"
135 import Useful.Attributes
137 [Singleton(AccessorName: ${accessorPropertyName})]
138 transient class ${SINGLETON_CLASS_NAME}:
142 import Useful.Attributes
144 public final transient class ${SINGLETON_CLASS_NAME}(object):
146 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
147 private def constructor():
150 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
151 public static ${accessorPropertyName} as ${SINGLETON_CLASS_NAME}:
153 return self.${CUT.NESTED_CLASS_NAME}.${CUT.INSTANCE_FIELD_NAME}
155 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
156 private final transient class ${CUT.NESTED_CLASS_NAME}(object):
158 public static def constructor():
159 self.${CUT.INSTANCE_FIELD_NAME} = ${SINGLETON_CLASS_NAME}()
161 private def constructor():
164 public static final ${CUT.INSTANCE_FIELD_NAME} as ${SINGLETON_CLASS_NAME}
166 RunTestCase(expected
, code
)
169 def ParameterizedConstructor():
171 import Useful.Attributes
174 transient class ${SINGLETON_CLASS_NAME}:
175 public def constructor(o as object):
179 import Useful.Attributes
181 public final transient class ${SINGLETON_CLASS_NAME}(object):
183 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
184 private def constructor():
187 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
188 public static ${CUT.DEFAULT_INSTANCE_PROPERTY_NAME} as ${SINGLETON_CLASS_NAME}:
190 return self.${CUT.NESTED_CLASS_NAME}.${CUT.INSTANCE_FIELD_NAME}
192 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
193 private final transient class ${CUT.NESTED_CLASS_NAME}(object):
195 public static def constructor():
196 self.${CUT.INSTANCE_FIELD_NAME} = ${SINGLETON_CLASS_NAME}()
198 private def constructor():
201 public static final ${CUT.INSTANCE_FIELD_NAME} as ${SINGLETON_CLASS_NAME}
203 RunTestCase(expected
, code
)
206 def VerifyConstants():
207 Assert
.AreEqual(typeof(ISerializable
).FullName
, CUT
.ISERIALIZABLE_FULL_NAME
)
208 Assert
.AreEqual(typeof(ISerializable
).GetMethods()[0].Name
, CUT
.GETOBJECTDATA_METHOD_NAME
)
209 Assert
.AreEqual(typeof(IObjectReference
).FullName
, CUT
.IOBJECTREFERENCE_FULL_NAME
)
210 Assert
.AreEqual(typeof(SerializationInfo
).FullName
, CUT
.SERIALIZATIONINFO_FULL_NAME
)
211 Assert
.AreEqual(typeof(StreamingContext
).FullName
, CUT
.STREAMINGCONTEXT_FULL_NAME
)
212 Assert
.AreEqual(typeof(CompilerGeneratedAttribute
).FullName
, CUT
.COMPILERGENERATEDATTRIBUTE_FULL_NAME
)