From 470b26dc8e88d78a1df41f9cd496bb47886f1822 Mon Sep 17 00:00:00 2001
From: Yuriy Maksymets <iurii.maksymets@gmail.com>
Date: Sun, 10 Nov 2019 15:46:49 +0000
Subject: [PATCH] More line detection tests

---
 __tests__/shape.test.js | 110 +++++++++++++++++++++++------
 package-lock.json       | 148 ++++++++++++++++++++++++++++++++++++++++
 src/shapes.js           |   1 +
 3 files changed, 239 insertions(+), 20 deletions(-)

diff --git a/__tests__/shape.test.js b/__tests__/shape.test.js
index 93e9991..c8a2db9 100644
--- a/__tests__/shape.test.js
+++ b/__tests__/shape.test.js
@@ -1,27 +1,97 @@
 import recognizeFromPoints, { Shapes } from "../src/shapes"
 
 describe("shape recognition", () => {
-  test("should return a shape description object", () => {
-    const points = [[0, 0]]
-    const result = recognizeFromPoints(points)
-    expect(result).not.toBeNull()
+  describe("general", () => {
+    test("should return a shape description object", () => {
+      const points = [[0, 0]]
+      const result = recognizeFromPoints(points)
+      expect(result).not.toBeNull()
+    })
   })
-  test("should recognize simple rectangle", () => {
-    const points = [[0, 0], [10, 0], [10, 10], [0, 10]]
-    const result = recognizeFromPoints(points)
-    expect(result.shape).toBe(Shapes.rectangle)
+
+  describe("lines", () => {
+    test("should recognize a simple horizontal line", () => {
+      const points = [[0, 0], [100, 0]]
+      const result = recognizeFromPoints(points)
+      expect(result).toBe(Shapes.line)
+    })
+
+    test("should recognize a simple vertical line", () => {
+      const points = [[0, 50], [0, -100]]
+      const result = recognizeFromPoints(points)
+      expect(result).toBe(Shapes.line)
+    })
+
+    test("should recognize a slightly curve horizontal line", () => {
+      const points = [[0, 0], [30, 10], [100, 2]]
+      const result = recognizeFromPoints(points)
+      expect(result).toBe(Shapes.line)
+    })
+
+    test("should not recognize a really curved horizontal line", () => {
+      const points = [[0, 0], [30, 30], [100, -4]]
+      const result = recognizeFromPoints(points)
+      expect(result).not.toBe(Shapes.line)
+    })
   })
-  test("should recognize rectangle with many points", () => {
-    const points = [
-      [0, 0],
-      [5, 1],
-      [10, 0],
-      [10, 6],
-      [10, 10],
-      [6, 10],
-      [0, 10],
-    ]
-    const result = recognizeFromPoints(points)
-    expect(result.shape).toBe(Shapes.rectangle)
+
+  describe("rectangles", () => {
+    test("should recognize simple rectangle", () => {
+      const points = [[-10, -10], [10, -10], [10, 10], [-10, 10]]
+      const result = recognizeFromPoints(points)
+      expect(result.shape).toBe(Shapes.rectangle)
+    })
+
+    test("should recognize rectangle with many points", () => {
+      const points = [
+        [0, 0],
+        [5, 0],
+        [10, 0],
+        [10, 5],
+        [10, 10],
+        [5, 10],
+        [0, 10],
+      ]
+      const result = recognizeFromPoints(points)
+      expect(result.shape).toBe(Shapes.rectangle)
+    })
+
+    test("should recognize rectangle with varying points", () => {
+      const points = [
+        [0, 0],
+        [5, 1],
+        [10, 0],
+        [10, 6],
+        [10, 10],
+        [6, 10],
+        [0, 10],
+      ]
+      const result = recognizeFromPoints(points)
+      expect(result.shape).toBe(Shapes.rectangle)
+    })
+
+    test("should not recognize rectangle with non-rectangular points", () => {
+      const points = [[5, 1], [23, 0], [10, 54], [0, 10]]
+      const result = recognizeFromPoints(points)
+      expect(result.shape).not.toBe(Shapes.rectangle)
+    })
+
+    test("should not recognize unclosed rectangle", () => {
+      const points = [[-10, -10], [10, -10], [10, 10]]
+      const result = recognizeFromPoints(points)
+      expect(result.shape).not.toBe(Shapes.rectangle)
+    })
+
+    test("should recognize almost-closed rectangle", () => {
+      const points = [[-10, -10], [10, -10], [10, 10], [-9, -9]]
+      const result = recognizeFromPoints(points)
+      expect(result.shape).toBe(Shapes.rectangle)
+    })
+
+    test("should not recognize half-closed rectangle", () => {
+      const points = [[-10, -10], [10, -10], [10, 10], [-1, -9]]
+      const result = recognizeFromPoints(points)
+      expect(result.shape).not.toBe(Shapes.rectangle)
+    })
   })
 })
diff --git a/package-lock.json b/package-lock.json
index ede7bee..2b56a1d 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -104,12 +104,127 @@
         "@babel/types": "^7.0.0"
       }
     },
+    "@babel/helper-module-imports": {
+      "version": "7.7.0",
+      "resolved": "https://registry.npmjs.org/@babel/helper-module-imports/-/helper-module-imports-7.7.0.tgz",
+      "integrity": "sha512-Dv3hLKIC1jyfTkClvyEkYP2OlkzNvWs5+Q8WgPbxM5LMeorons7iPP91JM+DU7tRbhqA1ZeooPaMFvQrn23RHw==",
+      "dev": true,
+      "requires": {
+        "@babel/types": "^7.7.0"
+      },
+      "dependencies": {
+        "@babel/types": {
+          "version": "7.7.2",
+          "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.7.2.tgz",
+          "integrity": "sha512-YTf6PXoh3+eZgRCBzzP25Bugd2ngmpQVrk7kXX0i5N9BO7TFBtIgZYs7WtxtOGs8e6A4ZI7ECkbBCEHeXocvOA==",
+          "dev": true,
+          "requires": {
+            "esutils": "^2.0.2",
+            "lodash": "^4.17.13",
+            "to-fast-properties": "^2.0.0"
+          }
+        }
+      }
+    },
+    "@babel/helper-module-transforms": {
+      "version": "7.7.0",
+      "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.7.0.tgz",
+      "integrity": "sha512-rXEefBuheUYQyX4WjV19tuknrJFwyKw0HgzRwbkyTbB+Dshlq7eqkWbyjzToLrMZk/5wKVKdWFluiAsVkHXvuQ==",
+      "dev": true,
+      "requires": {
+        "@babel/helper-module-imports": "^7.7.0",
+        "@babel/helper-simple-access": "^7.7.0",
+        "@babel/helper-split-export-declaration": "^7.7.0",
+        "@babel/template": "^7.7.0",
+        "@babel/types": "^7.7.0",
+        "lodash": "^4.17.13"
+      },
+      "dependencies": {
+        "@babel/helper-split-export-declaration": {
+          "version": "7.7.0",
+          "resolved": "https://registry.npmjs.org/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.7.0.tgz",
+          "integrity": "sha512-HgYSI8rH08neWlAH3CcdkFg9qX9YsZysZI5GD8LjhQib/mM0jGOZOVkoUiiV2Hu978fRtjtsGsW6w0pKHUWtqA==",
+          "dev": true,
+          "requires": {
+            "@babel/types": "^7.7.0"
+          }
+        },
+        "@babel/parser": {
+          "version": "7.7.2",
+          "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.7.2.tgz",
+          "integrity": "sha512-DDaR5e0g4ZTb9aP7cpSZLkACEBdoLGwJDWgHtBhrGX7Q1RjhdoMOfexICj5cqTAtpowjGQWfcvfnQG7G2kAB5w==",
+          "dev": true
+        },
+        "@babel/template": {
+          "version": "7.7.0",
+          "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.7.0.tgz",
+          "integrity": "sha512-OKcwSYOW1mhWbnTBgQY5lvg1Fxg+VyfQGjcBduZFljfc044J5iDlnDSfhQ867O17XHiSCxYHUxHg2b7ryitbUQ==",
+          "dev": true,
+          "requires": {
+            "@babel/code-frame": "^7.0.0",
+            "@babel/parser": "^7.7.0",
+            "@babel/types": "^7.7.0"
+          }
+        },
+        "@babel/types": {
+          "version": "7.7.2",
+          "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.7.2.tgz",
+          "integrity": "sha512-YTf6PXoh3+eZgRCBzzP25Bugd2ngmpQVrk7kXX0i5N9BO7TFBtIgZYs7WtxtOGs8e6A4ZI7ECkbBCEHeXocvOA==",
+          "dev": true,
+          "requires": {
+            "esutils": "^2.0.2",
+            "lodash": "^4.17.13",
+            "to-fast-properties": "^2.0.0"
+          }
+        }
+      }
+    },
     "@babel/helper-plugin-utils": {
       "version": "7.0.0",
       "resolved": "https://registry.npmjs.org/@babel/helper-plugin-utils/-/helper-plugin-utils-7.0.0.tgz",
       "integrity": "sha512-CYAOUCARwExnEixLdB6sDm2dIJ/YgEAKDM1MOeMeZu9Ld/bDgVo8aiWrXwcY7OBh+1Ea2uUcVRcxKk0GJvW7QA==",
       "dev": true
     },
+    "@babel/helper-simple-access": {
+      "version": "7.7.0",
+      "resolved": "https://registry.npmjs.org/@babel/helper-simple-access/-/helper-simple-access-7.7.0.tgz",
+      "integrity": "sha512-AJ7IZD7Eem3zZRuj5JtzFAptBw7pMlS3y8Qv09vaBWoFsle0d1kAn5Wq6Q9MyBXITPOKnxwkZKoAm4bopmv26g==",
+      "dev": true,
+      "requires": {
+        "@babel/template": "^7.7.0",
+        "@babel/types": "^7.7.0"
+      },
+      "dependencies": {
+        "@babel/parser": {
+          "version": "7.7.2",
+          "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.7.2.tgz",
+          "integrity": "sha512-DDaR5e0g4ZTb9aP7cpSZLkACEBdoLGwJDWgHtBhrGX7Q1RjhdoMOfexICj5cqTAtpowjGQWfcvfnQG7G2kAB5w==",
+          "dev": true
+        },
+        "@babel/template": {
+          "version": "7.7.0",
+          "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.7.0.tgz",
+          "integrity": "sha512-OKcwSYOW1mhWbnTBgQY5lvg1Fxg+VyfQGjcBduZFljfc044J5iDlnDSfhQ867O17XHiSCxYHUxHg2b7ryitbUQ==",
+          "dev": true,
+          "requires": {
+            "@babel/code-frame": "^7.0.0",
+            "@babel/parser": "^7.7.0",
+            "@babel/types": "^7.7.0"
+          }
+        },
+        "@babel/types": {
+          "version": "7.7.2",
+          "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.7.2.tgz",
+          "integrity": "sha512-YTf6PXoh3+eZgRCBzzP25Bugd2ngmpQVrk7kXX0i5N9BO7TFBtIgZYs7WtxtOGs8e6A4ZI7ECkbBCEHeXocvOA==",
+          "dev": true,
+          "requires": {
+            "esutils": "^2.0.2",
+            "lodash": "^4.17.13",
+            "to-fast-properties": "^2.0.0"
+          }
+        }
+      }
+    },
     "@babel/helper-split-export-declaration": {
       "version": "7.4.4",
       "resolved": "https://registry.npmjs.org/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.4.4.tgz",
@@ -156,6 +271,18 @@
         "@babel/helper-plugin-utils": "^7.0.0"
       }
     },
+    "@babel/plugin-transform-modules-commonjs": {
+      "version": "7.7.0",
+      "resolved": "https://registry.npmjs.org/@babel/plugin-transform-modules-commonjs/-/plugin-transform-modules-commonjs-7.7.0.tgz",
+      "integrity": "sha512-KEMyWNNWnjOom8vR/1+d+Ocz/mILZG/eyHHO06OuBQ2aNhxT62fr4y6fGOplRx+CxCSp3IFwesL8WdINfY/3kg==",
+      "dev": true,
+      "requires": {
+        "@babel/helper-module-transforms": "^7.7.0",
+        "@babel/helper-plugin-utils": "^7.0.0",
+        "@babel/helper-simple-access": "^7.7.0",
+        "babel-plugin-dynamic-import-node": "^2.3.0"
+      }
+    },
     "@babel/template": {
       "version": "7.6.0",
       "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.6.0.tgz",
@@ -980,6 +1107,15 @@
         "slash": "^2.0.0"
       }
     },
+    "babel-plugin-dynamic-import-node": {
+      "version": "2.3.0",
+      "resolved": "https://registry.npmjs.org/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.3.0.tgz",
+      "integrity": "sha512-o6qFkpeQEBxcqt0XYlWzAVxNCSCZdUgcR8IRlhD/8DylxjjO4foPcvTW0GGKa/cVt3rvxZ7o5ippJ+/0nvLhlQ==",
+      "dev": true,
+      "requires": {
+        "object.assign": "^4.1.0"
+      }
+    },
     "babel-plugin-istanbul": {
       "version": "5.2.0",
       "resolved": "https://registry.npmjs.org/babel-plugin-istanbul/-/babel-plugin-istanbul-5.2.0.tgz",
@@ -5508,6 +5644,18 @@
         "isobject": "^3.0.0"
       }
     },
+    "object.assign": {
+      "version": "4.1.0",
+      "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.0.tgz",
+      "integrity": "sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==",
+      "dev": true,
+      "requires": {
+        "define-properties": "^1.1.2",
+        "function-bind": "^1.1.1",
+        "has-symbols": "^1.0.0",
+        "object-keys": "^1.0.11"
+      }
+    },
     "object.getownpropertydescriptors": {
       "version": "2.0.3",
       "resolved": "https://registry.npmjs.org/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.0.3.tgz",
diff --git a/src/shapes.js b/src/shapes.js
index c978a19..2c827fd 100644
--- a/src/shapes.js
+++ b/src/shapes.js
@@ -7,6 +7,7 @@ function recognizeFromPoints(points) {
 
 export const Shapes = {
   rectangle: "rect",
+  line: "line",
 }
 
 export default recognizeFromPoints
-- 
GitLab