27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Advanced Topics 251<br />

This example shows the basics of maintaining a field map. There is an action<br />

CreateEmployee that instantiates new objects. The action DeleteEmployee removes<br />

a previously created employee from the state. The SetSalary action changes the<br />

salary of a given employee (Override is a method on Map).<br />

Trace 1 Trace 2<br />

CreateEmployee(Employee(1)) CreateEmployee(Employee(1))<br />

SetSalary(Employee(1), 200) CreateEmployee(Employee(2))<br />

CreateEmployee(Employee(2)) DeleteEmployee(Employee(1))<br />

SetSalary(Employee(2), 400)<br />

DeleteEmployee(Employee(1))<br />

SetSalary(Employee(2), 400)<br />

Trace 1 and Trace 2 produce the same end state. In this state the values of the<br />

state variables are<br />

nextId = 3<br />

allEmployees = Set(Employee(2))<br />

salary = Map(Employee(2), 400)<br />

This example shows that you can model instances using only the features introduced<br />

in the previous chapters.<br />

Now, let’s show this same example using C# instance fields. This is an example<br />

of how to do object-oriented programming in N<strong>Model</strong>.<br />

namespace Payroll2<br />

{<br />

class Employee : LabeledInstance<br />

{<br />

static Set allEmployees = Set.EmptySet;<br />

int salary;<br />

[Action]<br />

static void CreateEmployee([Domain("new")] Employee emp)<br />

{ allEmployees = allEmployees.Add(emp); }<br />

[Action]<br />

[Domain("allEmployees")]<br />

void DeleteEmployee()<br />

{ allEmployees = allEmployees.Remove(this); }<br />

[Action]<br />

[Domain("allEmployees")]<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!