码迷,mamicode.com
首页 > 其他好文 > 详细

fitnesse - Variables and Symbols

时间:2017-10-09 16:45:14      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:定义   表达式   onclick   href   variable   min   upvar   int   show   

fitnesse - Variables and Symbols

2017-09-30

1 Variables(静态变量)

1.1 定义及使用

Variables初始化有三种方式:

  • !define VariableName {VariableValue} - 把大括号内的文本赋值给变量
  • !define VariableName ${OtherVariableName} - 把另一个变量赋值给变量
  • !define VariableName {${= 10 / 2 =}} - 通过表达式赋值给变量

Variables使用:

${VariableName}

示例1:

脚本如下:

技术分享
!define markedUp {This is ‘‘‘bold‘‘‘}
${markedUp} is expressed as: This is bold

!define y {y-value}
!define x {The value of y is ${y}}
${x} is expressed as: The value of y is y-value

!define n {10}
!define q {2} 
!define d {${=${n}/${q}=}}
${d} is : 5

!define q {5} 
${d} is : 2
View Code

页面显示如下:

技术分享

图1 示例1展示结果

1.2 Variable作用域

1.2.1 Variable在层次结构中的作用域

当Variable在页面中被调用,但当前页面没有,fitnesse会去依次去父页面寻找,若祖先页面也没找到,fitnesse会去System.properties寻找

示例:

http://ip:port/ HelloWorld

http://ip:port/ HelloWorld.HelloWorld1

http://ip:port/ HelloWorld.HelloWorld2

  • 假设HelloWorld定义了静态变量!define name {tom}, HelloWorld1和HelloWorld2不用再次定义name变量,直接调用${name},就变量name的值为tom
  • 假设HelloWorld定义了静态变量!define name {tom}, HelloWorld1又重新定义了变量!define name {jack},当HelloWorld1调用${name}后,变量name的值为jack,但不会影响HelloWorld2(HelloWorld2中name的值仍然是tom)

1.2.2 include对Variable作用域的影响 

当变量在included page中时:

  • 如果你把子页面include到主页面,那么你既可以使用主页面定义的变量,也可以用子页面定义的变量。
  • 如果修改子页面的变量,那么主页面使用的变量也会修改。

示例2:

层次结构下:

Contents:

其中:

  • grandfa页面定义变量: !define grandfaMoney {100}
  • relative页面定义变量:!define relativeMoney {150}
  • father页面定义变量:!define relativeMoney {150}
  • mother页面定义变量:!define motherMoney {50}
  • son页面定义变量:!define sonMoney {10}

把其他页面include到father页面,来测试在不同关系中各个变量的使用范围,代码如下:

技术分享
!define fatherMoney {80}

grandfaMoney is: ${grandfaMoney}
relativeMoney is: ${relativeMoney}
fatherMoney is: ${fatherMoney}
motherMoney is: ${motherMoney}
sonMoney is: ${sonMoney}

!include .Demo.variableIncludeTest.relative
!include .Demo.variableIncludeTest.grandfa.mother
!include .Demo.variableIncludeTest.grandfa.father.son

grandfaMoney is: ${grandfaMoney}
relativeMoney is: ${relativeMoney}
fatherMoney is: ${fatherMoney}
motherMoney is: ${motherMoney}
sonMoney is: ${sonMoney}
View Code

页面保存后展示如下图所示:

技术分享

图2 include变量作用域展示结果

在图2中我们可以得知:

  • 未include前,我们可以使用主页面定义的变量和父节点或祖先(直系)页面定义的变量
  • include的页面定义的变量,include后都能使用

2 表中的Symbol(动态变量)

2.1  定义及使用

Symbol是在表中定义(赋值)和使用的

示例3:

源代码见 fitnesse - 一个简单的例子(slim)

wiki page脚本如下:

技术分享
!define TEST_SYSTEM {slim}

!path D:\fitnesseUtil\bin

|import            |
|fitnesse.slim.test|
|util|

|script|Add|1|2.2|
|$result=|calc|
|check|calc|$result|

|script|Add|$result|2.2|
|$secondResult=|calc|
|check|calc|$secondResult|
|check|calc|$result|
View Code

其中:

  • 第10行:$result= 表示声明并将返回的值赋值给声明的Symbol,从下图3可以看到 $result<-[3.2]
  • 第11行:$result 表示使用变量result,从下图3可以看到 $result->[3.2]

执行结果:
技术分享

图3 symbol(动态变量)的定义和使用

 

2.2 Symbol的作用域

Symbol作用域:

  • Symbol定义的当前页面。从图3我们可以看到result定义后,可以跨表使用。
  • 不是当前页面定义,在父节点页面定义,未在当前页面定义,不能使用,如下示例4。
  • include symbol定义页面后可以使用,如下示例4。

示例4:

Contents:

grandfa页面脚本如示例3。father页面脚本如下:

技术分享
!define TEST_SYSTEM {slim}

!path D:\fitnesseUtil\bin

|import            |
|fitnesse.slim.test|
|util|

|script|Add|1|2.2|
|check|calc|$result|

!include .Demo.variableIncludeTest.grandfa

|script|Add|1|2.2|
|check|calc|$result|
View Code

其中:

  • 第10行:$result使用result,但由于在当前页面没有定义,所以没有被slim替换,还是$result,如下图4所示。
  • 第15行:$result使用result,在这段脚本前,已经include father页面,在father页面定义了result,所以没有被slim替换,还是$result,如下图4所示。

执行结果如下:

技术分享

图4 symbol作用域

 3 Variable和Symbol的区别

  • Variable的值在页面提交(页面保存)的时候,测试执行前就已经计算了。所以当父节点或祖先(直系)页面定义后,子节点页面可以使用。
  • Symbol只存在在执行时

示例5:

脚本代码如下:

技术分享
!define TEST_SYSTEM {slim}

!path D:\fitnesseUtil\bin

|import            |
|fitnesse.slim.test|
|util|

|script|Add|1|2.2|
|$result=|calc|

!define vResult {$result}
vResult is: ${vResult}

|script|Add|1|2.2|
|check|calc|$result|
|check|calc|${vResult}|
View Code

其中:

  • 第12行: !define vResult {$result} 把Symbol result 赋值给 Variable vResult
  • 第13行:调用${vResult},见图5,执行前执行后,它的值都是$result
  • 第17行:调用${vResult},见图5,vResult的值$result,$result执行时被slim识别是symbol,执行时被替换

执行结果如下:

技术分享

图5 把Symbol赋值给Variable

 

fitnesse - Variables and Symbols

标签:定义   表达式   onclick   href   variable   min   upvar   int   show   

原文地址:http://www.cnblogs.com/Ming8006/p/7615639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!