ContainsOnly succeeds if array contains all given elements, in any order, and only them, ignoring duplicates. Before comparison, array and all elements are converted to canonical form. Example: array := NewArray(t, []interface{}{"foo", 123, 123}) array.ContainsOnly(123, "foo") These calls are e
(values ...interface{})
| 1440 | // array.ContainsOnly("a", "b") |
| 1441 | // array.ContainsOnly("b", "a") |
| 1442 | func (a *Array) ContainsOnly(values ...interface{}) *Array { |
| 1443 | opChain := a.chain.enter("ContainsOnly()") |
| 1444 | defer opChain.leave() |
| 1445 | |
| 1446 | if opChain.failed() { |
| 1447 | return a |
| 1448 | } |
| 1449 | |
| 1450 | elements, ok := canonArray(opChain, values) |
| 1451 | if !ok { |
| 1452 | return a |
| 1453 | } |
| 1454 | |
| 1455 | for _, element := range elements { |
| 1456 | if countElement(a.value, element) == 0 { |
| 1457 | opChain.fail(AssertionFailure{ |
| 1458 | Type: AssertContainsElement, |
| 1459 | Actual: &AssertionValue{a.value}, |
| 1460 | Expected: &AssertionValue{element}, |
| 1461 | Reference: &AssertionValue{values}, |
| 1462 | Errors: []error{ |
| 1463 | errors.New("expected: array contains element from reference array"), |
| 1464 | }, |
| 1465 | }) |
| 1466 | return a |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | for _, element := range a.value { |
| 1471 | if countElement(elements, element) == 0 { |
| 1472 | opChain.fail(AssertionFailure{ |
| 1473 | Type: AssertNotContainsElement, |
| 1474 | Actual: &AssertionValue{a.value}, |
| 1475 | Expected: &AssertionValue{element}, |
| 1476 | Reference: &AssertionValue{values}, |
| 1477 | Errors: []error{ |
| 1478 | errors.New("expected: array does not contain elements" + |
| 1479 | " that are not present in reference array"), |
| 1480 | }, |
| 1481 | }) |
| 1482 | return a |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | return a |
| 1487 | } |
| 1488 | |
| 1489 | // NotContainsOnly is opposite to ContainsOnly. |
| 1490 | // |