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

相關文章
相關標籤/搜索