Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b626c9b
extend support for 'rule' sections in features
makmu Mar 18, 2021
cb7c122
Merge branch 'master' into extended-rule-support
makmu Mar 20, 2021
addba1b
merge defineFeature and defineRuleBasedFeature
makmu Mar 21, 2021
467f412
fix steps file naming
makmu Mar 22, 2021
642b88e
restore package json
makmu Mar 22, 2021
8750ef1
renaming
makmu Mar 22, 2021
28d32f8
renaming
makmu Mar 22, 2021
6def68b
renaming
makmu Mar 22, 2021
6079b29
renaming
makmu Mar 22, 2021
0ac395e
renaming
makmu Mar 22, 2021
8c61ded
renaming
makmu Mar 22, 2021
62a0479
renaming & linting
makmu Mar 22, 2021
5688655
renaming & linting
makmu Mar 22, 2021
5c8031d
renaming & linting
makmu Mar 22, 2021
5995023
renaming & linting
makmu Mar 22, 2021
0bec5d0
renaming & linting
makmu Mar 22, 2021
274cc63
renaming & linting
makmu Mar 22, 2021
e8ea580
renaming & linting
makmu Mar 22, 2021
cd7ee3d
renaming & linting
makmu Mar 22, 2021
4d56b21
renaming & linting
makmu Mar 22, 2021
5afb53c
renaming & linting
makmu Mar 22, 2021
6b5d87c
renaming & linting
makmu Mar 22, 2021
287cf39
renaming & linting
makmu Mar 22, 2021
a0e0071
renaming & linting
makmu Mar 22, 2021
735eece
renaming & linting
makmu Mar 22, 2021
064c718
renaming & linting
makmu Mar 22, 2021
fad2b08
renaming & linting
makmu Mar 22, 2021
47a8b27
renaming & linting
makmu Mar 22, 2021
667b829
renaming & linting
makmu Mar 22, 2021
3822d74
renaming & linting
makmu Mar 22, 2021
22fff8d
renaming & linting
makmu Mar 22, 2021
1cb7a1c
renaming & linting
makmu Mar 22, 2021
e82180e
renaming & linting
makmu Mar 22, 2021
c43ff66
renaming & linting
makmu Mar 22, 2021
04113c3
renaming & linting
makmu Mar 22, 2021
0ef2e91
renaming & linting
makmu Mar 22, 2021
19ac265
renaming & linting
makmu Mar 22, 2021
c03da2d
renaming & linting
makmu Mar 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Feature: Vending machine

Rule: Dispenses items if correct amount of money is inserted

Scenario: Selecting a snack
Given the vending machine has "Maltesers" in stock
And I have inserted the correct amount of money
When I select "Maltesers"
Then my "Maltesers" should be dispensed

Scenario Outline: Selecting a beverage
Given the vending machine has "<beverage>" in stock
And I have inserted the correct amount of money
When I select "<beverage>"
Then my "<beverage>" should be dispensed

Examples:
| beverage |
| Cola |
| Ginger ale |

Rule: Returns my money if item is out of stock

Scenario: Selecting a snack
Given the vending machine has no "Maltesers" in stock
And I have inserted the correct amount of money
When I select "Maltesers"
Then my money should be returned

Scenario: Selecting a beverage
Given the vending machine has no "Cola" in stock
And I have inserted the correct amount of money
When I select "Cola"
Then my money should be returned
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Feature: Vending machine

Rule: Dispenses items if correct amount of money is inserted

Scenario: Selecting a snack
Given the vending machine has "Maltesers" in stock
And I have inserted the correct amount of money
When I select "Maltesers"
Then my "Maltesers" should be dispensed

Scenario Outline: Selecting a beverage
Given the vending machine has "<beverage>" in stock
And I have inserted the correct amount of money
When I select "<beverage>"
Then my "<beverage>" should be dispensed

Examples:
| beverage |
| Cola |
| Ginger ale |

Rule: Returns my money if item is out of stock

Scenario: Selecting a snack
Given the vending machine has no "Maltesers" in stock
And I have inserted the correct amount of money
When I select "Maltesers"
Then my money should be returned

Scenario: Selecting a beverage
Given the vending machine has no "Cola" in stock
And I have inserted the correct amount of money
When I select "Cola"
Then my money should be returned
37 changes: 37 additions & 0 deletions examples/typescript/specs/features/tag-filtering.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@included
Feature: Vending machine

Rule: Dispenses items if correct amount of money is inserted

Scenario: Selecting a snack
Given the vending machine has "Maltesers" in stock
And I have inserted the correct amount of money
When I select "Maltesers"
Then my "Maltesers" should be dispensed

@excluded
Scenario Outline: Selecting a beverage
Given the vending machine has "<beverage>" in stock
And I have inserted the correct amount of money
When I select "<beverage>"
Then my "<beverage>" should be dispensed

Examples:
| beverage |
| Cola |
| Ginger ale |

Rule: Returns my money if item is out of stock

@excluded
Scenario: Selecting a snack
Given the vending machine has no "Maltesers" in stock
And I have inserted the correct amount of money
When I select "Maltesers"
Then my money should be returned

Scenario: Selecting a beverage
Given the vending machine has no "Cola" in stock
And I have inserted the correct amount of money
When I select "Cola"
Then my money should be returned
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { StepDefinitions, loadFeature, autoBindSteps} from '../../../../src';
import { VendingMachine } from '../../src/vending-machine';

export const vendingMachineSteps: StepDefinitions = ({ given, and, when, then }) => {
let vendingMachine: VendingMachine;

const myMoney = 0.50;

given(/^the vending machine has "([^"]*)" in stock$/, (itemName: string) => {
vendingMachine = new VendingMachine();
vendingMachine.stockItem(itemName, 1);
});

given(/^the vending machine has no "([^"]*)" in stock$/, (itemName: string) => {
vendingMachine = new VendingMachine();
vendingMachine.stockItem(itemName, 0);
});

and('I have inserted the correct amount of money', () => {
vendingMachine.insertMoney(myMoney);
});

when(/^I select "(.*)"$/, (itemName: string) => {
vendingMachine.dispenseItem(itemName);
});

then(/^my money should be returned$/, () => {
const returnedMoney = vendingMachine.moneyReturnSlot;
expect(returnedMoney).toBe(myMoney);
});

then(/^my "(.*)" should be dispensed$/, (itemName: string) => {
const inventoryAmount = vendingMachine.items[itemName];
expect(inventoryAmount).toBe(0);
});
};

const feature = loadFeature(
'./examples/typescript/specs/features/extended-rules-auto-step-binding.feature', {
collapseRules: false,
});

autoBindSteps([feature], [ vendingMachineSteps ]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { loadFeature, defineFeature} from '../../../../src';
import { DefineStepFunction } from '../../../../src/feature-definition-creation';
import { VendingMachine } from '../../src/vending-machine';

const feature = loadFeature(
'./examples/typescript/specs/features/extended-rules-definition.feature', {
collapseRules: false,
});

defineFeature(feature, ({rule}) => {
let vendingMachine: VendingMachine;

const myMoney = 0.50;

const givenTheVendingMachineHasXInStock = (given: DefineStepFunction) => {
given(/^the vending machine has "([^"]*)" in stock$/, (itemName: string) => {
vendingMachine = new VendingMachine();
vendingMachine.stockItem(itemName, 1);
});
};

const givenTheVendingMachineHasNoXInStock = (given: DefineStepFunction) => {
given(/^the vending machine has no "([^"]*)" in stock$/, (itemName: string) => {
vendingMachine = new VendingMachine();
vendingMachine.stockItem(itemName, 0);
});
};

const givenIHaveInsertedTheCorrectAmountOfMoney = (given: DefineStepFunction) => {
given('I have inserted the correct amount of money', () => {
vendingMachine.insertMoney(myMoney);
});
};

const whenISelectX = (when: DefineStepFunction) => {
when(/^I select "(.*)"$/, (itemName: string) => {
vendingMachine.dispenseItem(itemName);
});
};

const thenXShouldBeDespensed = (then: DefineStepFunction) => {
then(/^my "(.*)" should be dispensed$/, (itemName: string) => {
const inventoryAmount = vendingMachine.items[itemName];
expect(inventoryAmount).toBe(0);
});
};

const thenMyMoneyShouldBeReturned = (then: DefineStepFunction) => {
then(/^my money should be returned$/, () => {
const returnedMoney = vendingMachine.moneyReturnSlot;
expect(returnedMoney).toBe(myMoney);
});
};

rule('Dispenses items if correct amount of money is inserted', (test) => {

test('Selecting a snack', ({ given, when, then }) => {
givenTheVendingMachineHasXInStock(given);
givenIHaveInsertedTheCorrectAmountOfMoney(given);
whenISelectX(when);
thenXShouldBeDespensed(then);
});

test('Selecting a beverage', ({ given, when, then }) => {
givenTheVendingMachineHasXInStock(given);
givenIHaveInsertedTheCorrectAmountOfMoney(given);
whenISelectX(when);
thenXShouldBeDespensed(then);
});
});

rule('Returns my money if item is out of stock', (test) => {

test('Selecting a snack', ({ given, when, then }) => {
givenTheVendingMachineHasNoXInStock(given);
givenIHaveInsertedTheCorrectAmountOfMoney(given);
whenISelectX(when);
thenMyMoneyShouldBeReturned(then);
});

test('Selecting a beverage', ({ given, when, then }) => {
givenTheVendingMachineHasNoXInStock(given);
givenIHaveInsertedTheCorrectAmountOfMoney(given);
whenISelectX(when);
thenMyMoneyShouldBeReturned(then);
});
});
});
74 changes: 74 additions & 0 deletions examples/typescript/specs/step-definitions/tag-filtering.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { loadFeature, defineFeature} from '../../../../src';
import { DefineStepFunction } from '../../../../src/feature-definition-creation';
import { VendingMachine } from '../../src/vending-machine';

const feature = loadFeature('./examples/typescript/specs/features/tag-filtering.feature', {
collapseRules: false,
tagFilter: '@included and not @excluded',
});

defineFeature(feature, ({rule}) => {
let vendingMachine: VendingMachine;

const myMoney = 0.50;

const givenTheVendingMachineHasXInStock = (given: DefineStepFunction) => {
given(/^the vending machine has "([^"]*)" in stock$/, (itemName: string) => {
vendingMachine = new VendingMachine();
vendingMachine.stockItem(itemName, 1);
});
};

const givenTheVendingMachineHasNoXInStock = (given: DefineStepFunction) => {
given(/^the vending machine has no "([^"]*)" in stock$/, (itemName: string) => {
vendingMachine = new VendingMachine();
vendingMachine.stockItem(itemName, 0);
});
};

const givenIHaveInsertedTheCorrectAmountOfMoney = (given: DefineStepFunction) => {
given('I have inserted the correct amount of money', () => {
vendingMachine.insertMoney(myMoney);
});
};

const whenISelectX = (when: DefineStepFunction) => {
when(/^I select "(.*)"$/, (itemName: string) => {
vendingMachine.dispenseItem(itemName);
});
};

const thenXShouldBeDespensed = (then: DefineStepFunction) => {
then(/^my "(.*)" should be dispensed$/, (itemName: string) => {
const inventoryAmount = vendingMachine.items[itemName];
expect(inventoryAmount).toBe(0);
});
};

const thenMyMoneyShouldBeReturned = (then: DefineStepFunction) => {
then(/^my money should be returned$/, () => {
const returnedMoney = vendingMachine.moneyReturnSlot;
expect(returnedMoney).toBe(myMoney);
});
};

rule('Dispenses items if correct amount of money is inserted', (test) => {

test('Selecting a snack', ({ given, when, then }) => {
givenTheVendingMachineHasXInStock(given);
givenIHaveInsertedTheCorrectAmountOfMoney(given);
whenISelectX(when);
thenXShouldBeDespensed(then);
});
});

rule('Returns my money if item is out of stock', (test) => {

test('Selecting a beverage', ({ given, when, then }) => {
givenTheVendingMachineHasNoXInStock(given);
givenIHaveInsertedTheCorrectAmountOfMoney(given);
whenISelectX(when);
thenMyMoneyShouldBeReturned(then);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ defineFeature(feature, (test) => {
};

const thenTheOutputOfXShouldBeDisplayed = (then: DefineStepFunction) => {
then(/^the output of "(\d+)" should be displayed$/, (expectedOutput: string) => {
then(/^the output of "(\d+|undefined)" should be displayed$/, (expectedOutput: string) => {
if (!expectedOutput) {
expect(output).toBeFalsy();
} else {
expect(output).toBe(parseFloat(expectedOutput));
expect(output).toBe(expectedOutput === 'undefined' ? undefined : parseFloat(expectedOutput));
}
});
};
Expand Down
9 changes: 7 additions & 2 deletions examples/typescript/src/vending-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const ITEM_COST = 0.50;
export class VendingMachine {
public balance: number = 0;
public items: { [itemName: string]: number } = {};
public moneyReturnSlot: number = 0;

public stockItem(itemName: string, count: number) {
this.items[itemName] = this.items[itemName] || 0;
Expand All @@ -14,10 +15,14 @@ export class VendingMachine {
}

public dispenseItem(itemName: string) {
if (this.items[itemName] === 0) {
this.moneyReturnSlot = this.balance;
this.balance = 0;
}

if (this.balance >= ITEM_COST && this.items[itemName] > 0) {
this.balance -= ITEM_COST;
this.items[itemName]--;
}

this.items[itemName]--;
}
}
Loading