PageRenderTime 36ms CodeModel.GetById 20ms app.highlight 12ms RepoModel.GetById 0ms app.codeStats 0ms

/DLR_Main/Languages/Ruby/IronRuby.Tests/Runtime/DefinedTests.cs

https://bitbucket.org/mdavid/dlr
C# | 346 lines | 317 code | 15 blank | 14 comment | 0 complexity | afe2775acac541aa640432783e571724 MD5 | raw file
  1/* ****************************************************************************
  2 *
  3 * Copyright (c) Microsoft Corporation. 
  4 *
  5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6 * copy of the license can be found in the License.html file at the root of this distribution. If 
  7 * you cannot locate the  Apache License, Version 2.0, please send an email to 
  8 * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9 * by the terms of the Apache License, Version 2.0.
 10 *
 11 * You must not remove this notice, or any other, from this software.
 12 *
 13 *
 14 * ***************************************************************************/
 15
 16namespace IronRuby.Tests {
 17    public partial class Tests {
 18        public void DefinedOperator_Globals1() {
 19            AssertOutput(delegate() {
 20                CompilerTest(@"
 21p defined? $+
 22alias $plus $+
 23p defined? $+
 24p defined? $plus
 25"
 26                    );
 27            }, @"
 28nil
 29nil
 30""global-variable""
 31");
 32        }
 33
 34        public void DefinedOperator_Globals2() {
 35            AssertOutput(delegate() {
 36                CompilerTest(@"
 37alias $foo $bar
 38p defined? $foo
 39p defined? $bar
 40$foo = nil
 41p defined? $foo
 42p defined? $bar
 43$foo = 1
 44p defined? $foo
 45p defined? $bar
 46"
 47                    );
 48            }, @"
 49nil
 50nil
 51""global-variable""
 52""global-variable""
 53""global-variable""
 54""global-variable""
 55");
 56        }
 57
 58        public void DefinedOperator_Methods1() {
 59            CompilerTest(@"
 60module M
 61  def foo; end
 62end
 63
 64module N
 65  def foo_defined?
 66     $o1 = defined? foo                  
 67     undef foo rescue $o2 = 1            
 68  end
 69end
 70
 71class C
 72  include M,N    
 73end
 74
 75C.new.foo_defined?
 76");
 77            object o = Context.GetGlobalVariable("o1");
 78            AssertEquals(o.ToString(), "method");
 79
 80            o = Context.GetGlobalVariable("o2");
 81            AssertEquals(o, 1);
 82        }
 83
 84        public void DefinedOperator_Methods2() {
 85            AssertOutput(() => CompilerTest(@"
 86def foo
 87  puts 'foo'
 88  self
 89end
 90
 91puts defined? 1.+
 92puts defined? 1.method_that_doesnt_exist
 93puts defined? raise.foo
 94puts defined? foo.foo(puts(1))                  # foo is private
 95public :foo
 96puts defined? foo.foo
 97"), @"
 98method
 99nil
100nil
101foo
102nil
103foo
104method
105");
106        }
107
108        public void DefinedOperator_Constants1() {
109            AssertOutput(delegate() {
110                CompilerTest(@"
111W = 0
112
113module M
114  X = 1
115end
116
117module N
118  Y = 2
119  def cs_defined?
120     puts defined? ::W
121     puts defined? ::Undef
122     puts defined? M::X
123     puts defined? M::Undef
124     puts defined? X           
125     puts defined? Y
126     puts defined? Z
127  end
128end
129
130class C
131  include M,N    
132  Z = 3
133end
134
135C.new.cs_defined?
136");
137            }, @"
138constant
139nil
140constant
141nil
142nil
143constant
144nil
145");
146        }
147
148        public void DefinedOperator_Constants2() {
149            TestOutput(@"
150module M
151  C = 1
152end
153
154def foo
155  M
156end
157
158class << Object
159  def const_missing name
160    puts ""missing #{name}""
161    M
162  end
163end
164
165puts defined?(X::C)
166puts defined?(raise::C)
167puts defined?(foo::C)
168", @"
169missing X
170constant
171nil
172constant
173");
174        }
175
176        public void DefinedOperator_Constants3() {
177            TestOutput(@"
178print '1' unless defined? FOO
179print '2' unless defined? FOO
180print '.' unless not (defined? Object and defined? FOO)
181print '.' unless defined? FOO or defined? Object
182print '.' if defined? FOO
183print '.' if defined? FOO
184print '7' if not (defined? Object and defined? FOO)
185print '8' if defined? FOO or defined? Object
186", @"
1871278
188");
189        }
190
191        public void DefinedOperator_Expressions1() {
192            AssertOutput(delegate() {
193                CompilerTest(@"
194puts defined? true
195puts defined? false
196puts defined? self
197
198puts defined? 1
199puts defined? 'foo'
200
201puts defined?((1+3)+1)
202
203puts defined? x = 1
204puts defined? x &= 1
205puts defined? x &&= 1
206");
207            }, @"
208true
209false
210self
211expression
212expression
213method
214assignment
215assignment
216expression
217");
218        }
219
220        public void DefinedOperator_InstanceVariables1() {
221            AssertOutput(delegate() {
222                CompilerTest(@"
223p defined? @x
224@x = nil
225p defined? @x
226@x = 1
227p defined? @x
228");
229            }, @"
230nil
231""instance-variable""
232""instance-variable""
233");
234        }
235
236        public void DefinedOperator_ClassVariables1() {
237            AssertOutput(delegate() {
238                CompilerTest(@"
239module M
240  def foo; @@foo = 1; end
241  def foo_defined_on_M?
242     puts defined? @@foo                  
243  end
244end
245
246module N
247  def foo_defined?
248     puts defined? @@foo                  
249  end
250end
251
252class C
253  include M,N    
254end
255
256c = C.new
257c.foo
258c.foo_defined?
259c.foo_defined_on_M?
260");
261            }, @"
262nil
263class variable
264");
265        }
266
267        public void DefinedOperator_ClassVariables2() {
268            AssertOutput(delegate() {
269                CompilerTest(@"
270@@x = 1
271puts defined? @@x
272");
273            }, @"
274class variable
275");
276        }
277
278        public void DefinedOperator_Yield1() {
279            AssertOutput(delegate() {
280                CompilerTest(@"
281def foo
282  defined? yield
283end
284
285puts foo
286puts foo {}
287");
288            }, @"
289nil
290yield
291");
292        }
293
294        public void DefinedOperator_Locals1() {
295            AssertOutput(delegate() {
296                CompilerTest(@"
297def bob a
298  x = 1
299  1.times { |y|
300    z = 2
301    puts defined? w, defined? x, defined? y, defined? z, defined? a
302  }
303end
304
305bob 1
306");
307            }, @"
308nil
309local-variable
310local-variable
311local-variable
312local-variable
313");
314        }
315
316        public void DefinedOperator_Super1() {
317            AssertOutput(delegate() {
318                CompilerTest(@"
319class C
320  def foo
321    defined?(super(puts(1)))
322  end
323end
324
325class D < C
326  def foo
327    defined?(super(puts(1)))
328  end
329
330  def bar
331    defined?(super(puts(1)))
332  end
333end
334
335puts C.new.foo
336puts D.new.foo
337puts D.new.bar
338");
339            }, @"
340nil
341super
342nil
343");
344        }
345    }
346}