|
如有这种文档的中文版请发给我!yedi007@163.com
谢谢,一共好象有5个!
This Help module describes the basic elements of the TSL programming language. Click one of the following buttons for more information on a topic in this Help module:
Variables and constants
Operators and expressions
Statements
Control-flow
Arrays
Input/output
Comments
Built-in functions
User-defined functions
External function declarations
Variables and Constants
Variables and constants may be either strings or numbers. Declaration is optional; if variables are not declared, their type is determined at run time according to their context.
Variable names can include English-language letters (a-z and A-Z), digits, and underscores (_). The first character must be a letter or an underscore. TSL is case sensitive; y
and Y are therefore two different characters. Note that names of built-in functions and keywords (such as if, while, switch) cannot be used as variable names.
Click one of the following buttons for more information on a topic in this Help module:
Type (of variable or constant)
Undeclared variables
Variable declarations
Constant declarations
Type ( of variable or constant)
TSL supports two types of constants and variables: numbers and strings. Numbers may be either integer or floating point, and exponential notation is also acceptable. For example, -17, .05, -3e2, and 3E-2 are all legal values.
Strings consist of a sequence of zero or more characters enclosed within double quotes. When a backslash (\) or double-quote (") character appears within a string, it must be preceded by a backslash. Special characters can be incorporated in a string using the appropriate representation:
Backspace \b Vertical tab \v
Carriage return \r Newline \n
Formfeed \f Octal number \ooo
Horizontal tab \t
In the case of octal numbers, the zeroes represent the ASCII code of a character. For example, "\126" is equivalent to the letter "v."
For example, to represent the string "The values are: 12 14 16", type
"\"The values are:\t12\t14\t16\""
At a given moment, the value of a constant or variable can be either a string or a number. The TSL interpreter determines the type according to the operation performed. For example:
x = 123;
s = x & "Hello";
y = x + 1;
Variable x is assigned the value 123. In the second statement, because the operation is concatenation (&), x is treated as a string. The interpreted value of s is therefore 123Hello. In the third line, because the operation is addition, x is treated as a number. Variable y
is therefore assigned the value?24.
In the case of an expression where a mathematical operation is performed on a string, such as
"6RED87" + 0
the numeric value of the string is the first part of the string that can be evaluated to a number. Here, the numeric value of the expression is 6.
Since relational operators are valid for both strings and numbers, a numeric comparison is always performed if both operands can be evaluated to a number. For instance, in the relational expression below
"0.01" == "1e-2"
although both constants are written like strings (enclosed within quotation marks), both expressions are also valid numbers so a numeric comparison is performed. But in the next expression
"0.01" == "1f-2"
the second expression is not a number, so a string comparison is performed.
Undeclared Variables
If a variable is not declared, it is created implicitly when it is assigned or used in an expression. If a variable is not initialized, it is given the string value "" (null) at run time.
All undeclared variables are global, unless they are on the formal Parameter List of a called test. (For more information on parameters, refer to the chapter "Calling Tests" in your User's Guide.)
Variable Declarations
Note that while constant and variable declarations are optional in tests, they are required in user-defined functions. Variable declarations have the following syntax:
class variable [ = init_expression ];
The init_expression assigned to a declared variable can be any valid expression. If an init_expression is not set, the variable is assigned an empty string. The variable class can be any one of the following:
auto: An auto variable can only be declared within a function and is local to that function. It exists only as long as the function is running. A new copy of the variable is created each time the function is called.
static: A static variable is local to the function, test, or compiled module in which it is declared. The variable retains its value until the test is terminated by a Stop command.
public: A public variable can only be declared within a test or module, and is available for all functions, tests, and compiled modules.
extern: An extern declaration indicates a reference to a public variable declared outside of the current test or module.
With the exception of the auto variable, all variables continue to exist until the Stop command is executed. For example, the statement
static a=175, b=get_time( ), c = 2.235;
defines three variables (a, b, and c), and assigns each an initial value. This value is retained between invocations of the test. The following script segment demonstrates how a static variable can be used so that a message is printed only the first time that the test, T_2, is called.
static first = 1;
pause ("first = " & first);
if (first == 1) {
first = 0;
report_msg ("Test T_2 was called.");
}
The following table summarizes the scope, lifetime, and location of the variable declarations for each class:
Declaration Scope Lifetime Declare the variable in...
Auto Local End of function function
Static Local Until stop function, test, or module
Public Global Until stop test or module
Extern Global Until stop function, test, or module variables: declarations
Constant Declarations
The const specifier indicates that the declared value cannot be modified. The syntax of this declaration is:
[ class ] const name [ = expression ];
The class of a constant may be either public or static. (If no class is explicitly declared, the constant is assigned the default class public.) Once a constant is defined, it remains in existence until WinRunner is closed.
For example, defining the constant TMP_DIR using the declaration:
const TMP_DIR = "/tmp";
means that the assigned value /tmp cannot be modified. (This value can be changed only by explicitly making a new constant declaration for TMP_DIR.)
Operators and Expressions
TSL supports six types of operators: arithmetical, concatenation, relational, logical, conditional, and assignment. Operators are used to create expressions by combining basic elements. In TSL, expressions can consist of constants, variables, function calls, and other expressions.
Click one of the following buttons for more information on a topic in this Help module:
Arithmetical operators
Concatenation operator
Relational operators
Logical operators
Conditional operator
Assignment operators
Precedence and associativity of operators
Arithmetical Operators
TSL supports the following arithmetical operators:
+ Addition
- Subtraction (unary)
- Subtraction (binary)
* Multiplication
/ Division
% Modulus
^ or ** Exponent
++ Increment (adds 1 to its operand - unary operator)
-- Decrement (subtracts 1 from its operand - unary operator)
The result of the modulus operation is assigned the sign of the dividend. For example,
7 % -4 = 3
-4.5 % 4 = -0.5
The increment and decrement operators may be placed before the variable (++n), or after (n
++). As a result, the variable is incremented either before or after the value is used. For example:
i = 5;
j = i++;
k = ++i;
print(i & j & k);
prints the values 7, 5, 7. Note that the increment and decrement operators may be applied only to variables, and not to expressions, such as (a + b).
Note: You can increment or decrement an operand by a number other than 1. For example, to increment an operand by 5, use the following statement:
i = i + 5;
Concatenation Operator
The ampersand (&) character is used to concatenate strings. For example, the statement
[ Last edited by yedi007 on 2004-10-12 at 21:45 ] |
|