fitnesse - Variables and Symbols

fitnesse - Variables and Symbols

2017-09-30html

目录express

1 Variables(静态变量)
  1.1 定义及使用
  1.2 Variable做用域
    1.2.1 Variable在层次结构中的做用域
    1.2.2 include对Variable做用域的影响
2 表中的Symbol(动态变量)
  2.1  定义及使用
  2.2 Symbol的做用域
3 Variable和Symbol的区别ide

1 Variables(静态变量)


 返回post

1.1 定义及使用

Variables初始化有三种方式:测试

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

Variables使用:ui

${VariableName}

示例1

页面脚本以下:spa

!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

页面显示以下:code

图1 示例1展现结果htm

1.2 Variable做用域

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

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

示例:

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)

页面脚本以下:

 1 !define TEST_SYSTEM {slim}
 2 
 3 !path D:\fitnesseUtil\bin
 4 
 5 |import            |
 6 |fitnesse.slim.test|
 7 |util|
 8 
 9 |script|Add|1|2.2|
10 |$result=|calc|
11 |check|calc|$result|
12 
13 |script|Add|$result|2.2|
14 |$secondResult=|calc|
15 |check|calc|$secondResult|
16 |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页面脚本以下:

 1 !define TEST_SYSTEM {slim}
 2 
 3 !path D:\fitnesseUtil\bin
 4 
 5 |import            |
 6 |fitnesse.slim.test|
 7 |util|
 8 
 9 |script|Add|1|2.2|
10 |check|calc|$result|
11 
12 !include .Demo.variableIncludeTest.grandfa
13 
14 |script|Add|1|2.2|
15 |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:

页面脚本以下:

 1 !define TEST_SYSTEM {slim}
 2 
 3 !path D:\fitnesseUtil\bin
 4 
 5 |import            |
 6 |fitnesse.slim.test|
 7 |util|
 8 
 9 |script|Add|1|2.2|
10 |$result=|calc|
11 
12 !define vResult {$result}
13 vResult is: ${vResult}
14 
15 |script|Add|1|2.2|
16 |check|calc|$result|
17 |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

相关文章
相关标签/搜索