Collection of test code snippets for modules
Posted: Sat Sep 07, 2013 11:21 am
Hi,
in earlier 5.20 beta versions there were several bugs in the implementation of the new module feature, which have been found bit by bit in the course of time. The current version beta 17 still contains module bugs (see below), and I feel that it would be useful if these things were tested more systematically.
So I've written some basic test code snippets for modules. Test #3 shows two bugs that are still present in beta 17. Code snippets like these can also help people ( including myself
) to understand how exactly modules are supposed to work.
Of course the following snippets do not cover the whole topic and can't fetch all possible bugs in this field. But they are a beginning, and I think it would be helpful if other people would post additional test code snippets for modules in this thread.
//edit 2013-09-23:
Tested all 4 code snippets with PB 5.20 final => everything works as expected.
Module test #1
Module test #2
Module test #3
Module test #4
in earlier 5.20 beta versions there were several bugs in the implementation of the new module feature, which have been found bit by bit in the course of time. The current version beta 17 still contains module bugs (see below), and I feel that it would be useful if these things were tested more systematically.
So I've written some basic test code snippets for modules. Test #3 shows two bugs that are still present in beta 17. Code snippets like these can also help people ( including myself
Of course the following snippets do not cover the whole topic and can't fetch all possible bugs in this field. But they are a beginning, and I think it would be helpful if other people would post additional test code snippets for modules in this thread.
//edit 2013-09-23:
- Cosmetic changes in tests #1, #2, and #3
- Added test #4 (based on bug reports by idle and luis, using PB 5.20 beta 17)
Tested all 4 code snippets with PB 5.20 final => everything works as expected.
Module test #1
Code: Select all
; PB 5.20 LTS final
; => Uncommenting any of the commented lines will raise an error.
;
; Conclusions:
; - Variables that are declared in the main scope of the program
; (i.e. outside of a module) can not be accessed in a module.
; - Variables that are declared in the main scope of a program
; can of course be accessed in the main scope. As usual, if
; they are declared outside of a procedure (i.e. not Protected),
; they can be accessed in a procedure only if they are Global
; (or Shared; not tested here).
Define a.i = 1
Dim b.i(1) : b(0) = 2
NewMap c.i() : c("foo") = 3
NewList d.i() : AddElement(d()) : d() = 4
Global e.i = 5
Global Dim f.i(1) : f(0) = 6
Global NewMap g.i() : g("foo") = 7
Global NewList h.i() : AddElement(h()) : h() = 8
DeclareModule MyModule
EnableExplicit
; Debug a
; Debug b(0)
; Debug c()
; Debug d()
; Debug e
; Debug f(0)
; Debug g()
; Debug h()
EndDeclareModule
Module MyModule
Procedure ModuleProc()
; Debug a
; Debug b(0)
; Debug c()
; Debug d()
; Debug e
; Debug f(0)
; Debug g()
; Debug h()
EndProcedure
ModuleProc()
; Debug a
; Debug b(0)
; Debug c()
; Debug d()
; Debug e
; Debug f(0)
; Debug g()
; Debug h()
EndModule
EnableExplicit
Procedure MainProc()
; Debug a
; Debug b(0)
; Debug c()
; Debug d()
Debug e ; writes 5, OK
Debug f(0) ; writes 6, OK
Debug g() ; writes 7, OK
Debug h() ; writes 8, OK
Debug "---"
EndProcedure
MainProc()
Debug a ; writes 1, OK
Debug b(0) ; writes 2, OK
Debug c() ; writes 3, OK
Debug d() ; writes 4, OK
Debug e ; writes 5, OK
Debug f(0) ; writes 6, OK
Debug g() ; writes 7, OK
Debug h() ; writes 8, OKCode: Select all
; PB 5.20 LTS final
; => Uncommenting any of the commented lines will raise an error.
;
; Conclusions:
; - Variables that are declared in the 'Module' part can neither
; be accessed in the 'DeclareModule' part nor in the main scope
; of the program.
; - Variables that are declared in the 'Module' part can of
; course be accessed inside this part. As usual, if they
; are declared outside of a procedure (i.e. not Protected),
; they can be accessed in a procedure only if they are Global
; (or Shared; not tested here).
DeclareModule MyModule
EnableExplicit
; Debug a
; Debug b(0)
; Debug c()
; Debug d()
; Debug e
; Debug f(0)
; Debug g()
; Debug h()
EndDeclareModule
Module MyModule
Define a.i = 1
Dim b.i(1) : b(0) = 2
NewMap c.i() : c("foo") = 3
NewList d.i() : AddElement(d()) : d() = 4
Global e.i = 5
Global Dim f.i(1) : f(0) = 6
Global NewMap g.i() : g("foo") = 7
Global NewList h.i() : AddElement(h()) : h() = 8
Procedure ModuleProc()
; Debug a
; Debug b(0)
; Debug c()
; Debug d()
Debug e ; writes 5, OK
Debug f(0) ; writes 6, OK
Debug g() ; writes 7, OK
Debug h() ; writes 8, OK
Debug "---"
EndProcedure
ModuleProc()
Debug a ; writes 1, OK
Debug b(0) ; writes 2, OK
Debug c() ; writes 3, OK
Debug d() ; writes 4, OK
Debug e ; writes 5, OK
Debug f(0) ; writes 6, OK
Debug g() ; writes 7, OK
Debug h() ; writes 8, OK
EndModule
EnableExplicit
Procedure MainProc()
; Debug a
; Debug b(0)
; Debug c()
; Debug d()
; Debug e
; Debug f(0)
; Debug g()
; Debug h()
EndProcedure
MainProc()
; Debug a
; Debug b(0)
; Debug c()
; Debug d()
; Debug e
; Debug f(0)
; Debug g()
; Debug h()
; Debug MyModule::a
; Debug MyModule::b(0)
; Debug MyModule::c()
; Debug MyModule::d()
; Debug MyModule::e
; Debug MyModule::f(0)
; Debug MyModule::g()
; Debug MyModule::h()
UseModule MyModule
; Debug a
; Debug b(0)
; Debug c()
; Debug d()
; Debug e
; Debug f(0)
; Debug g()
; Debug h()Code: Select all
; PB 5.20 LTS final
; => Uncommenting any of the commented lines will raise an error.
;
; Conclusions:
; - Variables that are declared in the 'DeclarModule' part can
; be accessed in both parts of the module and in the main scope
; of the program.
; - The variable scope *inside* the module is determined by
; keywords such as Define, Global etc.
; - The variable scope *outside* the module is *not* determined by
; these keywords. Public module variables can be accessed
; *everywhere* outside the module by using either "ModuleName::..."
; or "UseModule ...".
DeclareModule MyModule
EnableExplicit
Define a.i = 1
Dim b.i(1) : b(0) = 2
NewMap c.i() : c("foo") = 3
NewList d.i() : AddElement(d()) : d() = 4
Global e.i = 5
Global Dim f.i(1) : f(0) = 6
Global NewMap g.i() : g("foo") = 7
Global NewList h.i() : AddElement(h()) : h() = 8
Debug a ; writes 1, OK
Debug b(0) ; writes 2, OK
Debug c() ; writes 3, OK
Debug d() ; writes 4, OK
Debug e ; writes 5, OK
Debug f(0) ; writes 6, OK
Debug g() ; writes 7, OK
Debug h() ; writes 8, OK
Debug "---"
EndDeclareModule
Module MyModule
Procedure ModuleProc()
; Debug a
; Debug b(0)
; Debug c()
; Debug d()
Debug e ; writes 5, OK
Debug f(0) ; writes 6, OK
Debug g() ; writes 7, OK
Debug h() ; writes 8, OK
Debug "---"
EndProcedure
ModuleProc()
Debug a ; writes 1, OK
Debug b(0) ; writes 2, OK
Debug c() ; writes 3, OK
Debug d() ; writes 4, OK
Debug e ; writes 5, OK
Debug f(0) ; writes 6, OK
Debug g() ; writes 7, OK
Debug h() ; writes 8, OK
Debug "==="
EndModule
EnableExplicit
Procedure MainProc()
Debug MyModule::a ; writes 1, OK
Debug MyModule::b(0) ; writes 2, OK
Debug MyModule::c() ; writes 3, OK
Debug MyModule::d() ; writes 4, OK
Debug MyModule::e ; writes 5, OK
Debug MyModule::f(0) ; writes 6, OK
Debug MyModule::g() ; writes 7, OK
Debug MyModule::h() ; writes 8, OK
Debug "---"
UseModule MyModule
Debug a ; writes 1, OK
Debug b(0) ; writes 2, OK
Debug c() ; writes 3, OK
Debug d() ; writes 4, OK
Debug e ; writes 5, OK
Debug f(0) ; writes 6, OK
Debug g() ; writes 7, OK
Debug h() ; writes 8, OK
Debug "---"
UnuseModule MyModule
EndProcedure
MainProc()
; Debug a
; Debug b(0)
; Debug c()
; Debug d()
; Debug e
; Debug f(0)
; Debug g()
; Debug h()
Debug MyModule::a ; writes 1, OK
Debug MyModule::b(0) ; writes 2, OK
Debug MyModule::c() ; writes 3, OK
Debug MyModule::d() ; writes 4, OK
Debug MyModule::e ; writes 5, OK
Debug MyModule::f(0) ; writes 6, OK
Debug MyModule::g() ; writes 7, OK
Debug MyModule::h() ; writes 8, OK
Debug "---"
UseModule MyModule
Debug a ; writes 1, OK
Debug b(0) ; writes 2, OK
Debug c() ; writes 3, OK
Debug d() ; writes 4, OK
Debug e ; writes 5, OK
Debug f(0) ; writes 6, OK
Debug g() ; writes 7, OK
Debug h() ; writes 8, OKCode: Select all
; PB 5.20 LTS final
; Conclusions:
; - Inside procedures in a module, 'Protected' variables have
; a higher priority than 'Global' variables.
; - Inside procedures in the main scope, 'Protected' variables
; have a higher priority than public module variables.
DeclareModule MyModule
EnableExplicit
Define a.i = 1
Dim b.i(1) : b(0) = 2
NewMap c.i() : c("foo") = 3
NewList d.i() : AddElement(d()) : d() = 4
Global e.i = 5
Global Dim f.i(1) : f(0) = 6
Global NewMap g.i() : g("foo") = 7
Global NewList h.i() : AddElement(h()) : h() = 8
EndDeclareModule
Module MyModule
Procedure ModuleProc()
Protected a.i
Protected Dim b.i(1)
Protected NewMap c.i() : AddMapElement(c(), "foo")
Protected NewList d.i() : AddElement(d())
Protected e.i
Protected Dim f.i(1)
Protected NewMap g.i() : AddMapElement(g(), "foo")
Protected NewList h.i() : AddElement(h())
Debug a ; writes 0, OK
Debug b(0) ; writes 0, OK
Debug c() ; writes 0, OK
Debug d() ; writes 0, OK
Debug e ; writes 0, OK
Debug f(0) ; writes 0, OK
Debug g() ; writes 0, OK
Debug h() ; writes 0, OK
Debug "---"
EndProcedure
ModuleProc()
Debug a ; writes 1, OK
Debug b(0) ; writes 2, OK
Debug c() ; writes 3, OK
Debug d() ; writes 4, OK
Debug e ; writes 5, OK
Debug f(0) ; writes 6, OK
Debug g() ; writes 7, OK
Debug h() ; writes 8, OK
Debug "==="
EndModule
EnableExplicit
UseModule MyModule
Procedure MainProc1()
Protected a.i
Protected Dim b.i(1)
Protected NewMap c.i() : AddMapElement(c(), "foo")
Protected NewList d.i() : AddElement(d())
Protected e.i
Protected Dim f.i(1)
Protected NewMap g.i() : AddMapElement(g(), "foo")
Protected NewList h.i() : AddElement(h())
Debug a ; writes 0, OK
Debug b(0) ; writes 0, OK
Debug c() ; writes 0, OK
Debug d() ; writes 0, OK
Debug e ; writes 0, OK
Debug f(0) ; writes 0, OK
Debug g() ; writes 0, OK
Debug h() ; writes 0, OK
Debug "---"
EndProcedure
Procedure MainProc2()
Protected a.i = 9
Protected Dim b.i(1) : b(0) = 10
Protected NewMap c.i() : : c("foo") = 11
Protected NewList d.i() : AddElement(d()) : d() = 12
Protected e.i = 13
Protected Dim f.i(1) : f(0) = 14
Protected NewMap g.i() : g("foo") = 15
Protected NewList h.i() : AddElement(h()) : h() = 16
Debug a ; writes 9, OK
Debug b(0) ; writes 10, OK
Debug c() ; writes 11, OK
Debug d() ; writes 12, OK
Debug e ; writes 13, OK
Debug f(0) ; writes 14, OK
Debug g() ; writes 15, OK
Debug h() ; writes 16, OK
Debug "---"
EndProcedure
MainProc1()
MainProc2()
Debug a ; writes 1, OK
Debug b(0) ; writes 2, OK
Debug c() ; writes 3, OK
Debug d() ; writes 4, OK
Debug e ; writes 5, OK
Debug f(0) ; writes 6, OK
Debug g() ; writes 7, OK
Debug h() ; writes 8, OK