fixed build console
[groovy.git] / src / test / gls / scope / MultipleDefinitionOfSameVariableTest.groovy
blob93c2030ed174b9bf259225d5779237102e9c2389
1 package gls.scope
3 import gls.scope.CompilableTestSupport
5 public class MultipleDefinitionOfSameVariableTest extends CompilableTestSupport {
7 public void testInSameBlock() {
8 shouldNotCompile("""
9 def foo = 1
10 def foo = 2
11 """)
13 shouldNotCompile("""
14 class Foo {
15 def foo() {
16 def bar=1
17 def bar=2
20 """)
23 public void testInSubblocks() {
24 shouldNotCompile("""
25 def foo = 1
26 5.times { def foo=2 }
27 """)
29 shouldNotCompile("""
30 def foo = 1
31 label1: { def foo=2 }
32 """)
34 shouldNotCompile("""
35 def foo = 1
36 for (i in []) { def foo=2 }
37 """)
39 shouldNotCompile("""
40 def foo = 1
41 while (true) { def foo=2 }
42 """)
45 public void testInNestedClosure() {
46 shouldNotCompile("""
47 def foo = 1
48 5.times { 6.times {def foo=2 }
49 """)
51 assertScript ("""
52 def foo = 1
53 5.times { 6.times {foo=2 } }
54 assert foo == 2
55 """)
58 public void testBindingHiding() {
59 assertScript("""
60 foo = 1
61 def foo = 3
62 assert foo==3
63 assert this.foo == 1
64 assert binding.foo == 1
65 """)
68 public void testBindingAccessInMethod() {
69 assertScript("""
70 def methodUsingBinding() {
71 try {
72 s = " bbb ";
73 } finally {
74 s = s.trim();
76 assert s == "bbb"
78 methodUsingBinding()
79 assert s == "bbb"
80 """)
83 public void testMultipleOfSameName() {
84 shouldNotCompile("""
85 class DoubleField {
86 def zero = 0
87 public zero = 0
90 """)
92 shouldNotCompile("""
93 class DoubleField {
94 def zero = 0
95 def zero = 0
98 """)