BOO-999
[boo.git] / tests / Boo.Lang.Useful.Tests / Attributes / SingletonAttributeTestFixture.boo
blobcae59365b4f7d64c9b4a9e637a9d7115159a6afc
1 #region license
2 // Copyright (c) 2004, 2005 Rodrigo B. de Oliveira (rbo@acm.org)
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
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.
16 //
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.
27 #endregion
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
36 [TestFixture]
37 class SingletonAttributeTestFixture(AbstractAttributeTestFixture):
38 """
39 @author Marcus Griep (neoeinstein+boo@gmail.com)
40 @author Sorin Ionescu (sorin.ionescu@gmail.com)
41 @author Rodrigo B. de Oliveira
42 """
43 final static SINGLETON_CLASS_NAME = "SingletonObject"
45 [Test]
46 def DefaultSingleton():
47 code = """
48 import Useful.Attributes
50 [Singleton]
51 transient class ${SINGLETON_CLASS_NAME}:
52 pass
53 """
54 expected = """
55 import Useful.Attributes
57 public final transient class ${SINGLETON_CLASS_NAME}(object):
59 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
60 private def constructor():
61 super()
63 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
64 public static ${CUT.DEFAULT_INSTANCE_PROPERTY_NAME} as ${SINGLETON_CLASS_NAME}:
65 public static get:
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():
75 super()
77 public static final ${CUT.INSTANCE_FIELD_NAME} as ${SINGLETON_CLASS_NAME}
78 """
79 RunTestCase(expected, code)
81 [Test]
82 def Serializable():
83 code = """
84 import Useful.Attributes
86 [Singleton]
87 class ${SINGLETON_CLASS_NAME}:
88 pass
89 """
90 expected = """
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():
97 super()
99 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
100 public static ${CUT.DEFAULT_INSTANCE_PROPERTY_NAME} as ${SINGLETON_CLASS_NAME}:
101 public static get:
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():
111 super()
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():
122 super()
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)
130 [Test]
131 def GivenAccessorName():
132 accessorPropertyName = "Factory"
134 code = """
135 import Useful.Attributes
137 [Singleton(AccessorName: ${accessorPropertyName})]
138 transient class ${SINGLETON_CLASS_NAME}:
139 pass
141 expected = """
142 import Useful.Attributes
144 public final transient class ${SINGLETON_CLASS_NAME}(object):
146 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
147 private def constructor():
148 super()
150 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
151 public static ${accessorPropertyName} as ${SINGLETON_CLASS_NAME}:
152 public static get:
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():
162 super()
164 public static final ${CUT.INSTANCE_FIELD_NAME} as ${SINGLETON_CLASS_NAME}
166 RunTestCase(expected, code)
168 [Test]
169 def ParameterizedConstructor():
170 code = """
171 import Useful.Attributes
173 [Singleton]
174 transient class ${SINGLETON_CLASS_NAME}:
175 public def constructor(o as object):
176 pass
178 expected = """
179 import Useful.Attributes
181 public final transient class ${SINGLETON_CLASS_NAME}(object):
183 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
184 private def constructor():
185 super()
187 [${CUT.COMPILERGENERATEDATTRIBUTE_FULL_NAME}]
188 public static ${CUT.DEFAULT_INSTANCE_PROPERTY_NAME} as ${SINGLETON_CLASS_NAME}:
189 public static get:
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():
199 super()
201 public static final ${CUT.INSTANCE_FIELD_NAME} as ${SINGLETON_CLASS_NAME}
203 RunTestCase(expected, code)
205 [Test]
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)