Lichen

Changeset

820:be4f3575f605
2018-01-08 Paul Boddie raw files shortlog changelog graph Added tests for attribute initialisation plus testing for duplicate parameters.
inspector.py (file) tests/methods_attr_init.py (file) tests/methods_attr_init_bad.py (file)
     1.1 --- a/inspector.py	Mon Jan 08 00:30:23 2018 +0100
     1.2 +++ b/inspector.py	Mon Jan 08 01:15:11 2018 +0100
     1.3 @@ -604,6 +604,9 @@
     1.4                  argname = argname[1:]
     1.5                  attr_initialisers.append(argname)
     1.6  
     1.7 +            if argname in l:
     1.8 +                raise InspectError("Argument name %s is repeated." % argname, function_name, n)
     1.9 +
    1.10              l.append(argname)
    1.11  
    1.12          argnames = l
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/tests/methods_attr_init.py	Mon Jan 08 01:15:11 2018 +0100
     2.3 @@ -0,0 +1,24 @@
     2.4 +class C:
     2.5 +    def __init__(.x, .y, .z): # no explicit self, attributes initialised
     2.6 +        pass
     2.7 +
     2.8 +    def c():
     2.9 +        return self.x
    2.10 +
    2.11 +class D(C):
    2.12 +    def d():
    2.13 +        return self.y
    2.14 +
    2.15 +class E(D):
    2.16 +    def c():
    2.17 +        return self.z
    2.18 +
    2.19 +c = C(1, 2, 3)
    2.20 +d = D(1, 2, 3)
    2.21 +e = E(1, 2, 3)
    2.22 +
    2.23 +print c.c() # 1
    2.24 +print d.c() # 1
    2.25 +print e.c() # 3
    2.26 +print d.d() # 2
    2.27 +print e.d() # 2
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tests/methods_attr_init_bad.py	Mon Jan 08 01:15:11 2018 +0100
     3.3 @@ -0,0 +1,24 @@
     3.4 +class C:
     3.5 +    def __init__(.x, .y, .z, x): # no explicit self, attributes initialised
     3.6 +        pass
     3.7 +
     3.8 +    def c():
     3.9 +        return self.x
    3.10 +
    3.11 +class D(C):
    3.12 +    def d():
    3.13 +        return self.y
    3.14 +
    3.15 +class E(D):
    3.16 +    def c():
    3.17 +        return self.z
    3.18 +
    3.19 +c = C(1, 2, 3, 4)
    3.20 +d = D(1, 2, 3, 4)
    3.21 +e = E(1, 2, 3, 4)
    3.22 +
    3.23 +print c.c() # 1
    3.24 +print d.c() # 1
    3.25 +print e.c() # 3
    3.26 +print d.d() # 2
    3.27 +print e.d() # 2