Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady committed May 28, 2020
1 parent 5fc26ef commit 7a55556
Show file tree
Hide file tree
Showing 2 changed files with 274 additions and 0 deletions.
18 changes: 18 additions & 0 deletions parser/match_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@ func comparisonMatch(
for _, amountMatch := range descriptions.EqualAmounts {
ops := []*types.Operation{}
for _, reqIndex := range amountMatch {
if reqIndex >= len(matches) {
return fmt.Errorf(
"equal amounts comparison index %d out of range",
reqIndex,
)
}
ops = append(ops, matches[reqIndex].Operations...)
}

Expand All @@ -359,6 +365,18 @@ func comparisonMatch(
}

// compare all possible pairs
if amountMatch[0] >= len(matches) {
return fmt.Errorf(
"opposite amounts comparison index %d out of range",
amountMatch[0],
)
}
if amountMatch[1] >= len(matches) {
return fmt.Errorf(
"opposite amounts comparison index %d out of range",
amountMatch[1],
)
}
for _, op := range matches[amountMatch[0]].Operations {
for _, otherOp := range matches[amountMatch[1]].Operations {
if err := oppositeAmounts(
Expand Down
256 changes: 256 additions & 0 deletions parser/match_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,77 @@ func TestMatchOperations(t *testing.T) {
},
err: false,
},
"simple transfer (check type)": {
operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr2",
},
Amount: &types.Amount{
Value: "100",
},
Type: "output",
},
{}, // extra op ignored
{
Account: &types.AccountIdentifier{
Address: "addr1",
},
Amount: &types.Amount{
Value: "-100",
},
Type: "input",
},
},
descriptions: &Descriptions{
OppositeAmounts: [][]int{{0, 1}},
OperationDescriptions: []*OperationDescription{
{
Account: &AccountDescription{
Exists: true,
},
Type: "input",
},
{
Account: &AccountDescription{
Exists: true,
},
Type: "output",
},
},
},
matches: []*Match{
{
Operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr1",
},
Amount: &types.Amount{
Value: "-100",
},
Type: "input",
},
},
Amounts: []*big.Int{big.NewInt(-100)},
},
{
Operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr2",
},
Amount: &types.Amount{
Value: "100",
},
Type: "output",
},
},
Amounts: []*big.Int{big.NewInt(100)},
},
},
err: false,
},
"simple transfer (reject extra op)": {
operations: []*types.Operation{
{
Expand Down Expand Up @@ -898,6 +969,191 @@ func TestMatchOperations(t *testing.T) {
matches: nil,
err: true,
},
"simple repeated op": {
operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr2",
},
Amount: &types.Amount{
Value: "200",
},
},
{}, // extra op ignored
{
Account: &types.AccountIdentifier{
Address: "addr1",
},
Amount: &types.Amount{
Value: "100",
},
},
},
descriptions: &Descriptions{
OperationDescriptions: []*OperationDescription{
{
Account: &AccountDescription{
Exists: true,
},
Amount: &AmountDescription{
Exists: true,
Sign: PositiveAmountSign,
},
AllowRepeats: true,
},
},
},
matches: []*Match{
{
Operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr2",
},
Amount: &types.Amount{
Value: "200",
},
},
{
Account: &types.AccountIdentifier{
Address: "addr1",
},
Amount: &types.Amount{
Value: "100",
},
},
},
Amounts: []*big.Int{
big.NewInt(200),
big.NewInt(100),
},
},
},
err: false,
},
"simple repeated op (no extra ops allowed)": {
operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr2",
},
Amount: &types.Amount{
Value: "200",
},
},
{}, // extra op ignored
{
Account: &types.AccountIdentifier{
Address: "addr1",
},
Amount: &types.Amount{
Value: "100",
},
},
},
descriptions: &Descriptions{
OperationDescriptions: []*OperationDescription{
{
Account: &AccountDescription{
Exists: true,
},
Amount: &AmountDescription{
Exists: true,
Sign: PositiveAmountSign,
},
AllowRepeats: true,
},
},
ErrUnmatched: true,
},
matches: nil,
err: true,
},
"simple repeated op (with invalid comparison indexes)": {
operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr2",
},
Amount: &types.Amount{
Value: "200",
},
},
{}, // extra op ignored
{
Account: &types.AccountIdentifier{
Address: "addr1",
},
Amount: &types.Amount{
Value: "100",
},
},
},
descriptions: &Descriptions{
OppositeAmounts: [][]int{{0, 1}},
OperationDescriptions: []*OperationDescription{
{
Account: &AccountDescription{
Exists: true,
},
Amount: &AmountDescription{
Exists: true,
Sign: PositiveAmountSign,
},
AllowRepeats: true,
},
},
},
matches: nil,
err: true,
},
"simple repeated op (with overlapping, repeated descriptions)": {
operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr2",
},
Amount: &types.Amount{
Value: "200",
},
},
{}, // extra op ignored
{
Account: &types.AccountIdentifier{
Address: "addr1",
},
Amount: &types.Amount{
Value: "100",
},
},
},
descriptions: &Descriptions{
OperationDescriptions: []*OperationDescription{
{
Account: &AccountDescription{
Exists: true,
},
Amount: &AmountDescription{
Exists: true,
Sign: PositiveAmountSign,
},
AllowRepeats: true,
},
{ // will never be possible to meet this description
Account: &AccountDescription{
Exists: true,
},
Amount: &AmountDescription{
Exists: true,
Sign: PositiveAmountSign,
},
AllowRepeats: true,
},
},
},
matches: nil,
err: true,
},
}

for name, test := range tests {
Expand Down

0 comments on commit 7a55556

Please sign in to comment.