在 Progress 4GL 中实现断言

发布于 2024-07-19 03:41:40 字数 138 浏览 14 评论 0 原文

使用 Progress 4GL 或 WebSpeed 实现断言的最佳方法是什么?

What is the best way of implementing assertions using Progress 4GL or WebSpeed?

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

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

发布评论

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

评论(4

清醇 2024-07-26 03:41:40

经过一番考虑,这是我对问题的解决方案。 它的工作原理基于以下假设:开发环境 propath 与测试和生产环境不同,并且代码始终重新编译以供测试或生产使用:

&IF PROPATH MATCHES '*development*' &THEN 
&SCOPED-DEFINE ASSERTION   {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} ~
{11} {12} {13} {14} {15} {16} {17} {18} {19} {20} ~
{21} {22} {23} {24} {25} {26} {27} {28} {29} {30} ~
{31} {32} {33} {34} {35} {36} {37} {38} {39} {40} ~
{41} {42} {43} {44} {45} {46} {47} {48} {49} {50} ~
{51} {52} {53} {54} {55} {56} {57} {58} {59} {60} ~
{61} {62} {63} {64} {65} {66} {67} {68} {69} {70} ~
{71} {72} {73} {74} {75} {76} {77} {78} {79} {80} 


 IF NOT ({&ASSERTION}) THEN 
     MESSAGE "Failed assertion {&ASSERTION} in" PROGRAM-NAME(1).

 IF ({&ASSERTION}) = ? THEN 
     MESSAGE "Unknown value as a result of assertion {&ASSERTION} in" 
              PROGRAM-NAME(1).

&ENDIF

代码旨在避免任何副作用,并且在任何执行环境(GUI 或ChUI、WebSpeed、AppServer、批处理等)。

1)将代码保存为名为“assert”的文件(不带任何扩展名)。

2) 将文件放入PROPATH 指向的目录中。

3)示例用法:

{assert valid-handle(hProc)}
{assert i > 0 and i <= 100}
{assert cExtra begins ‘opt’}  /* note the single quotes */
{assert dtEnd > = dtStart}

作为一种变体,可以通过在测试和生产环境中仅使用一个空包含文件来完全避免依赖 propath,开发版本将变得只是:

&SCOPED-DEFINE ASSERTION   {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} ~
{11} {12} {13} {14} {15} {16} {17} {18} {19} {20} ~
{21} {22} {23} {24} {25} {26} {27} {28} {29} {30} ~
{31} {32} {33} {34} {35} {36} {37} {38} {39} {40} ~
{41} {42} {43} {44} {45} {46} {47} {48} {49} {50} ~
{51} {52} {53} {54} {55} {56} {57} {58} {59} {60} ~
{61} {62} {63} {64} {65} {66} {67} {68} {69} {70} ~
{71} {72} {73} {74} {75} {76} {77} {78} {79} {80} 


 IF NOT ({&ASSERTION}) THEN 
     MESSAGE "Failed assertion {&ASSERTION} in" PROGRAM-NAME(1).

 IF ({&ASSERTION}) = ? THEN 
     MESSAGE "Unknown value as a result of assertion {&ASSERTION} in" 
              PROGRAM-NAME(1).

一个额外的提示是向编辑器添加自动文本宏选择将自动扩展为 {assert }。

After some consideration here is my solution to the problem. It works based on the assumption that development environment propath is different from test and production environments and code is always re-compiled for test or production use:

&IF PROPATH MATCHES '*development*' &THEN 
&SCOPED-DEFINE ASSERTION   {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} ~
{11} {12} {13} {14} {15} {16} {17} {18} {19} {20} ~
{21} {22} {23} {24} {25} {26} {27} {28} {29} {30} ~
{31} {32} {33} {34} {35} {36} {37} {38} {39} {40} ~
{41} {42} {43} {44} {45} {46} {47} {48} {49} {50} ~
{51} {52} {53} {54} {55} {56} {57} {58} {59} {60} ~
{61} {62} {63} {64} {65} {66} {67} {68} {69} {70} ~
{71} {72} {73} {74} {75} {76} {77} {78} {79} {80} 


 IF NOT ({&ASSERTION}) THEN 
     MESSAGE "Failed assertion {&ASSERTION} in" PROGRAM-NAME(1).

 IF ({&ASSERTION}) = ? THEN 
     MESSAGE "Unknown value as a result of assertion {&ASSERTION} in" 
              PROGRAM-NAME(1).

&ENDIF

The code is designed to avoid any side effects and works equally well in any execution environment (GUI or ChUI, WebSpeed, AppServer, batch and so on).

1) Save the code as a file called “assert” (without any extension).

2) Place the file into a directory pointed to by PROPATH.

3) Sample usage:

{assert valid-handle(hProc)}
{assert i > 0 and i <= 100}
{assert cExtra begins ‘opt’}  /* note the single quotes */
{assert dtEnd > = dtStart}

As a variation it’s possible to avoid relying on propath altogether by having just an empty include file in test and production environment, the development version will become just:

&SCOPED-DEFINE ASSERTION   {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} ~
{11} {12} {13} {14} {15} {16} {17} {18} {19} {20} ~
{21} {22} {23} {24} {25} {26} {27} {28} {29} {30} ~
{31} {32} {33} {34} {35} {36} {37} {38} {39} {40} ~
{41} {42} {43} {44} {45} {46} {47} {48} {49} {50} ~
{51} {52} {53} {54} {55} {56} {57} {58} {59} {60} ~
{61} {62} {63} {64} {65} {66} {67} {68} {69} {70} ~
{71} {72} {73} {74} {75} {76} {77} {78} {79} {80} 


 IF NOT ({&ASSERTION}) THEN 
     MESSAGE "Failed assertion {&ASSERTION} in" PROGRAM-NAME(1).

 IF ({&ASSERTION}) = ? THEN 
     MESSAGE "Unknown value as a result of assertion {&ASSERTION} in" 
              PROGRAM-NAME(1).

An extra tip is to add an auto-text macro to your editor of choice that will automatically expand into {assert }.

初见 2024-07-26 03:41:40

由于 Progress 没有对断言的本机处理,但我想出的最好方法是:

IF NOT <assertion> THEN
RUN assertionFailed.p.

assertionFailed.p 可以向程序员发送电子邮件,或记录条件并优雅退出。

Since Progress doesn't have native handling for assertions, but best I've come up with is:

IF NOT <assertion> THEN
RUN assertionFailed.p.

assertionFailed.p can email the programmer, or log the conditions and also exit gracefully.

暖心男生 2024-07-26 03:41:40

鉴于最终代码中通常会省略断言,我建议使用预处理器路线。 您可以执行如下操作,将其设置为两个包含文件。 当您将其编译到生产环境时,请确保 debugalert.i 为空。 可以编辑assert.i来执行任何您喜欢的消息、停止、电子邮件等...

要设置断言,您只需遵循格式 {assert.i &condition=}


/* assert.i */
{debugalert.i}

&IF DEFINED(DEBUGALERT) <> 0 &THEN

IF NOT {&条件}
然后做:

MESSAGE THIS-PROCEDURE:FILENAME "ERROR...{&CONDITION}" 
    VIEW-AS ALERT-BOX.
/* add code to email message etc.. or stop */

结束。

&ENDIF


/* debugalert.i 在测试或开发环境中
要关闭断言,请删除此语句 */

&GLOBAL-DEFINE DEBUGALERT


/* 在您的测试代码中,您只需执行以下操作:/
/
测试断言 */

DEF VAR h_ct AS INT NO-UNDO INIT 10.

{assert.i &CONDITION="h_ct = 8"}

Given that assertions are generally omitted from the final code I'd suggest the preprocessor route. You might do something like the following set it up as two include files. When you are compiling it to production ensure that the debugalert.i is empty. assert.i could be edited to do whatever you like message, stop, email etc...

To setup an assertion you would just follow the format {assert.i &condition=}


/* assert.i */
{debugalert.i}

&IF DEFINED( DEBUGALERT ) <> 0 &THEN

IF NOT {&CONDITION}
THEN DO:

MESSAGE THIS-PROCEDURE:FILENAME "ERROR...{&CONDITION}" 
    VIEW-AS ALERT-BOX.
/* add code to email message etc.. or stop */

END.

&ENDIF


/* debugalert.i on test or development environments
to turn off the assertions remove this statement */

&GLOBAL-DEFINE DEBUGALERT


/* In your test code you would just do the following: /
/
testing assertion */

DEF VAR h_ct AS INT NO-UNDO INIT 10.

{assert.i &CONDITION="h_ct = 8"}

满地尘埃落定 2024-07-26 03:41:40

OpenEdge 11.6 将单元测试引入了 ABL 世界。 它大致基于 JUnit 原则。 因此,断言现在是包的一部分。 更多信息位于文档中: https://documentation.progress.com/output/ua/OpenEdge_latest/index.html#page/pdsoe/overview-of-ablunit-testing-framework.html

OpenEdge 11.6 introduced Unit Testing to the world of ABL. It's loosely based on JUnit principals. Assertions are therefore now part of the package. More info is in the documentation: https://documentation.progress.com/output/ua/OpenEdge_latest/index.html#page/pdsoe/overview-of-ablunit-testing-framework.html

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