Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toHaveGeometryCount #48

Closed
M-Scott-Lassiter opened this issue Jun 8, 2022 · 1 comment
Closed

toHaveGeometryCount #48

M-Scott-Lassiter opened this issue Jun 8, 2022 · 1 comment

Comments

@M-Scott-Lassiter
Copy link
Owner

M-Scott-Lassiter commented Jun 8, 2022

Description

A GeoJSON GeometryCollection contains a "geometries" member with an array of GeoJSON geometry objects.

This matcher uses the toBeGeometryCollection functionality to verify the input is a properly formatted GeometryCollection object, and then determine if it has between Range1 and Range2 number of geometry objects in its "geometries". It will throw an error for any of the other geometry types as it is a trivial comparison on those.

Omitting Range2 or setting it equal to the value of Range1 will causes the matcher to check for exactly the number of geometries specified by Range1.

Checking Range2 less than Range1 will throw an error.

Passing a number less than 0 for either will throw an error.

Decimals will get truncated on both Range1 and Range2.

Will throw an error if Range2 is defined and Range1 is not.

If omitting both Range1 and Range2, it passes if at least one geometry object is contained in "geometries".

Example Matcher Usage

const testCollection = {
    "type": "GeometryCollection",
    "geometries": [{
        "type": "Point",
        "coordinates": [100.0, 0.0]
    }, {
        "type": "LineString",
        "coordinates": [
            [101.0, 0.0],
            [102.0, 1.0]
        ]
    }, {
        "type": "Polygon",
        "coordinates": [
            [
                [102.0, 2.0],
                [103.0, 2.0],
                [103.0, 3.0],
                [102.0, 3.0],
                [102.0, 2.0]
            ]
        ]
    }, {
        "type": "Point",
        "coordinates": [150.0, 73.0]
    }]
}
const emptyCollection = {
    "type": "GeometryCollection",
    "geometries": []
}
const polygon = {
    type: 'Polygon',
    coordinates: [
        [
            [100.0, 0.0],
            [101.0, 0.0],
            [101.0, 1.0],
            [100.0, 1.0],
            [100.0, 0.0]
        ]
    ]
}

expect(testCollection).toHaveGeometryCount(1, 8)
expect(testCollection).toHaveGeometryCount(4)

expect(testCollection).not.toHaveGeometryCount(5, 15)
expect(testCollection).not.toHaveGeometryCount(2, 3.99)
expect(testCollection).not.toHaveGeometryCount()
expect(emptyCollection).not.toHaveGeometryCount()
expect(polygon).not.toHaveGeometryCount(1)

Passing Tests

Good GeometryCollection

  • Known good GeometryCollection with a single geometry
    • .toHaveGeometryCount()
    • .toHaveGeometryCount(undefined)
    • .toHaveGeometryCount(1, undefined)
    • .toHaveGeometryCount(undefined, undefined)
    • .toHaveGeometryCount(1)
    • .toHaveGeometryCount(1, 1)
    • .toHaveGeometryCount(1.2)
    • .toHaveGeometryCount(1.2, 2)
    • .toHaveGeometryCount(1, 2)
    • .toHaveGeometryCount(0, 20)
  • Known good GeometryCollection with 4 geometries
    • .toHaveGeometryCount()
    • .toHaveGeometryCount(4)
    • .toHaveGeometryCount(4, 4)
    • .toHaveGeometryCount(4, undefined)
    • .toHaveGeometryCount(3.999, 4)
    • .toHaveGeometryCount(4.999, 8)
    • .toHaveGeometryCount(2, 4)
    • .toHaveGeometryCount(2, 15)
    • .toHaveGeometryCount(0, 20)
  • Stress test: Known good GeometryCollection with 100 geometries
    • .toHaveGeometryCount()
    • .toHaveGeometryCount(100)
    • .toHaveGeometryCount(100, 100)
    • .toHaveGeometryCount(49, 100000)
    • .toHaveGeometryCount(0, Infinity)
  • Empty Geometry
    • .toHaveGeometryCount(0)
    • .toHaveGeometryCount(0.1)
    • .toHaveGeometryCount(0.9)
    • .toHaveGeometryCount(0.9, 1)
    • .toHaveGeometryCount(0, 10)
    • .toHaveGeometryCount(0, undefined)
    • .toHaveGeometryCount(0, 0)

Failing Tests

Invalid Inputs To Matcher

Rejects each of the following:

  • Each of the seven Geometry objects except GeometryCollection
  • An invalid GeometryCollection
  • Feature and FeatureCollection object
  • undefined, null, false, true, 0, NaN
  • { someProp: 'I am not GeoJSON', id: 4 }
  • {}
  • '',
  • 'Random Feature',
  •   JSON.stringify({
          "type": "GeometryCollection",
          "geometries": [{
              "type": "Point",
              "coordinates": [100.0, 0.0]
          }, {
              "type": "LineString",
              "coordinates": [
                  [101.0, 0.0],
                  [102.0, 1.0]
              ]
          }, {
              "type": "Polygon",
              "coordinates": [
                  [
                      [102.0, 2.0],
                      [103.0, 2.0],
                      [103.0, 3.0],
                      [102.0, 3.0],
                      [102.0, 2.0]
                  ]
              ]
          }, {
              "type": "Point",
              "coordinates": [150.0, 73.0]
          }]
      })

Valid GeometryCollection With Range1 and Range2 Problems

  • Good GeometryCollection with valid Range2 less than valid Range1
  • Good GeometryCollection with valid Range1 undefined and valid Range2
  • Good GeometryCollection with Range1 as each of the values in "Invalid Inputs To Matcher" except 0
  • Good GeometryCollection with Range2 as each of the values in "Invalid Inputs To Matcher" except 0
  • Good GeometryCollection with Range1 and Range2 as each of the values in "Invalid Inputs To Matcher" except 0

Valid GeometryCollection With Negative Ranges

  • Known good GeometryCollection with a single geometry
    • .not.toHaveGeometryCount(-1)
    • .not.toHaveGeometryCount(-1, undefined)
    • .not.toHaveGeometryCount(-10)
    • .not.toHaveGeometryCount(-Infinity)
  • Known good GeometryCollection with 4 geometries
    • .not.toHaveGeometryCount(-4)
    • .not.toHaveGeometryCount(-4, undefined)
    • .not.toHaveGeometryCount(-1, 5)
    • .not.toHaveGeometryCount(-2, -1)
    • .not.toHaveGeometryCount(-5, -1)
  • Empty Geometry
    • .not.toHaveGeometryCount(-1)
    • .not.toHaveGeometryCount(-1, 0)
    • .not.toHaveGeometryCount(-1, undefined)
    • .not.toHaveGeometryCount(-1, 2)
    • .not.toHaveGeometryCount(-2, -1)
    • .not.toHaveGeometryCount(-1, -2)
  • Nested Geometry Collection does not count nested geometries, only top level

Valid GeometryCollection With Out of Range Range1 and Range2

  • Known good GeometryCollection with a single geometry
    • .not.toHaveGeometryCount(0)
    • .not.toHaveGeometryCount(2)
  • Known good GeometryCollection with 4 geometries
    • .not.toHaveGeometryCount(0)
    • .not.toHaveGeometryCount(2)
    • .not.toHaveGeometryCount(3.9999)
    • .not.toHaveGeometryCount(0, 3)
    • .not.toHaveGeometryCount(5, 10)
    • .not.toHaveGeometryCount(0, undefined)
  • Stress test: Known good GeometryCollection with 100 geometries
    • .not.toHaveGeometryCount(0)
    • .not.toHaveGeometryCount(50)
    • .not.toHaveGeometryCount(99.5)
    • .not.toHaveGeometryCount(5, 10)
    • .not.toHaveGeometryCount(90, 99.999)
    • .not.toHaveGeometryCount(101, 1000)
    • .not.toHaveGeometryCount(0, undefined)
    • .not.toHaveGeometryCount(87, undefined)
  • Empty Geometry
    • .not.toHaveGeometryCount(1)
    • .not.toHaveGeometryCount(1, undefined)
    • .not.toHaveGeometryCount()
    • .not.toHaveGeometryCount(undefined)
    • .not.toHaveGeometryCount(1, 5)
@M-Scott-Lassiter M-Scott-Lassiter added the new matcher proposal Proposal for a new GeoJSON matcher label Jun 8, 2022
@M-Scott-Lassiter M-Scott-Lassiter self-assigned this Jun 8, 2022
github-actions bot pushed a commit that referenced this issue Jun 11, 2022
## [1.5.0](v1.4.0...v1.5.0) (2022-06-11)

### 🎁 Feature Changes

* **toHaveGeometryCount:** add new matcher ([b4bff1c](b4bff1c)), closes [#48](#48)
@M-Scott-Lassiter
Copy link
Owner Author

🎉 This issue has been resolved in version 1.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@M-Scott-Lassiter M-Scott-Lassiter added released matchers/geometries and removed new matcher proposal Proposal for a new GeoJSON matcher labels Jun 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant