VFP 类可以在运行时获取它自己的方法的集合吗?

发布于 2024-10-13 05:47:01 字数 744 浏览 4 评论 0原文

这就是我想要在 Visual FoxPro 中完成的任务。我想编写一个父 UnitTest 类,可以对其进行子类化以创建单独的单元测试。我希望父 UnitTest 可以有一个 MainMethod 来检查自身,然后查找并执行它自己的所有以“test_”开头的方法。

这样我就可以在单元测试中继续编写适当命名的函数,并且父级将知道如何运行它们,而无需我提供任何额外的输入。但是,如果子类没有明确定义方法名称的集合或与此相关的内容(这正是我希望避免的),我无法找到任何方法在运行时从 VFP 获取此信息。

这是一个基本的存根:

define class UnitTest as custom  && would be abstract if VFP supported that

    procedure MainMethod()
        && run all methods that begin with test_
    endproc

enddefine

define class AUnitTest as UnitTest

    procedure test_thingA()
        ...
    endproc

    procedure test_thingB()
        ...
    endproc

enddefine

喜欢任何人关于如何从父级获取子方法的想法。 (也对更好的实现想法持开放态度,如果我的想法是错误的,我认为我想要的基本想法是明确的)。

非常感谢!

Here's what I'm trying to accomplish, in Visual FoxPro. I want to write a parent UnitTest class, that can be subclassed to create individual unit tests. I am hoping that the parent UnitTest can have a MainMethod which examines itself, and then finds and executes all of it's own methods which begin with "test_".

This way I can go forward writing appropriately named functions in my unit tests, and the parent will know how to run them without any additional input from me. But, I can't locate any way to get this information from VFP at run-time, without having the child class explicitly define a collection of method names or something to that regard (which is exactly what I'm hoping to avoid).

Here's is a basic stub:

define class UnitTest as custom  && would be abstract if VFP supported that

    procedure MainMethod()
        && run all methods that begin with test_
    endproc

enddefine

define class AUnitTest as UnitTest

    procedure test_thingA()
        ...
    endproc

    procedure test_thingB()
        ...
    endproc

enddefine

Would love anyone's ideas on how to get at the child methods from the parent. (Also open to a better implementation idea, if I'm going about this wrong, I think the basic idea of what I'm going for is clear).

Thanks so much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤城病女 2024-10-20 05:47:01

AMEMBERS() 函数可用于获取方法列表。

loX = CREATEOBJECT("AUnitTest")
loX.MainMethod()
STORE .NULL. TO loX
RETURN


define class UnitTest as custom

    procedure MainMethod()
        LOCAL lcCommand, lcName, lcType, lnMemberNo, lnTotalMembers
        LOCAL ARRAY laMembers(1)

        m.lnTotalMembers = AMEMBERS(laMembers, THIS, 1, "U")
        FOR m.lnMemberNo = 1 TO m.lnTotalMembers
            m.lcName = m.laMembers[m.lnMemberNo,1]
            m.lcType = m.laMembers[m.lnMemberNo,2]

            IF (m.lcType == "Method") AND (LEFT(m.lcName, 5) == "TEST_")
                m.lcCommand = "THIS." + m.lcName + "()"
                &lcCommand
            ELSE
                * do nothing
            ENDIF
        ENDFOR

    endproc

enddefine

define class AUnitTest as UnitTest

    procedure test_thingA()
        WAIT WINDOW "Testing Thing A"
    endproc

    procedure test_thingB()
        WAIT WINDOW "Testing Thing B"
    endproc

    procedure NotATest()
        WAIT WINDOW "Not A Test"
    endproc

enddefine

The AMEMBERS() function could be used to get a list of methods.

loX = CREATEOBJECT("AUnitTest")
loX.MainMethod()
STORE .NULL. TO loX
RETURN


define class UnitTest as custom

    procedure MainMethod()
        LOCAL lcCommand, lcName, lcType, lnMemberNo, lnTotalMembers
        LOCAL ARRAY laMembers(1)

        m.lnTotalMembers = AMEMBERS(laMembers, THIS, 1, "U")
        FOR m.lnMemberNo = 1 TO m.lnTotalMembers
            m.lcName = m.laMembers[m.lnMemberNo,1]
            m.lcType = m.laMembers[m.lnMemberNo,2]

            IF (m.lcType == "Method") AND (LEFT(m.lcName, 5) == "TEST_")
                m.lcCommand = "THIS." + m.lcName + "()"
                &lcCommand
            ELSE
                * do nothing
            ENDIF
        ENDFOR

    endproc

enddefine

define class AUnitTest as UnitTest

    procedure test_thingA()
        WAIT WINDOW "Testing Thing A"
    endproc

    procedure test_thingB()
        WAIT WINDOW "Testing Thing B"
    endproc

    procedure NotATest()
        WAIT WINDOW "Not A Test"
    endproc

enddefine
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文