From 1d9b1ee3f60e1a58c31fe14f8cbe1af81cacddb1 Mon Sep 17 00:00:00 2001
From: Brady James Garvin <bgarvin@cse.unl.edu>
Date: Wed, 30 Oct 2019 14:13:39 -0500
Subject: [PATCH] Updated lab for 2019.

---
 .eslintrc                   |  3 +++
 js/buy.js                   |  4 +---
 js/buy_sell.js              |  6 ++----
 js/sell.js                  |  6 ++----
 unit_tests/test_buy.js      |  6 ++++--
 unit_tests/test_buy_sell.js | 14 ++++++++++++--
 unit_tests/test_sell.js     |  6 ++++--
 unit_tests/unit_tests.html  | 11 +++--------
 8 files changed, 31 insertions(+), 25 deletions(-)

diff --git a/.eslintrc b/.eslintrc
index ec0d68b..15eefba 100755
--- a/.eslintrc
+++ b/.eslintrc
@@ -4,6 +4,9 @@
     "browser": true,
     "jquery": true,
   },
+  "parserOptions": {
+    "sourceType": "module",
+  },
   "rules": {
     "no-await-in-loop": "warn",
     "no-compare-neg-zero": "warn",
diff --git a/js/buy.js b/js/buy.js
index 18f7ca6..d22dd93 100644
--- a/js/buy.js
+++ b/js/buy.js
@@ -1,5 +1,3 @@
-/* exported determineBuyPrice */
-
 function descendsThenAscends(prices) {
   let previous = Infinity;
   let recovering = false;
@@ -16,7 +14,7 @@ function descendsThenAscends(prices) {
   return true;
 }
 
-function determineBuyPrice(forecastPrices) {
+export function determineBuyPrice(forecastPrices) {
   console.assert(forecastPrices.length > 0, 'Tried to find a buy price with an empty forecast.');
   console.assert(descendsThenAscends(forecastPrices), 'Tried to find a buy price with a forecast that does something other than descend and then ascend.');
   if (forecastPrices.length === 1) {
diff --git a/js/buy_sell.js b/js/buy_sell.js
index 6934242..0c9d379 100644
--- a/js/buy_sell.js
+++ b/js/buy_sell.js
@@ -1,6 +1,4 @@
-/* exported BuySellPlan determineBuyAndSellPrices */
-
-class BuySellPlan {
+export class BuySellPlan {
   constructor(buyPrice, sellPrice, minimumPrice, maximumPrice) {
     this.buyPrice = buyPrice;
     this.sellPrice = sellPrice;
@@ -24,7 +22,7 @@ function combineBuySellPlans(firstPlan, secondPlan) {
   return result;
 }
 
-function determineBuyAndSellPrices(forecastPrices) {
+export function determineBuyAndSellPrices(forecastPrices) {
   console.assert(forecastPrices.length > 0, 'Tried to find buy and sell prices with an empty forecast.');
   if (forecastPrices.length === 1) {
     return new BuySellPlan(forecastPrices[0], forecastPrices[0], forecastPrices[0], forecastPrices[0]);
diff --git a/js/sell.js b/js/sell.js
index 03496b2..7277c48 100644
--- a/js/sell.js
+++ b/js/sell.js
@@ -1,6 +1,4 @@
-/* exported SellPlan determineSellChange */
-
-class SellPlan {
+export class SellPlan {
   constructor(sellChange, totalChange) {
     this.sellChange = sellChange;
     this.totalChange = totalChange;
@@ -14,7 +12,7 @@ function combineSellPlans(firstPlan, secondPlan) {
   );
 }
 
-function determineSellChange(forecastChanges) {
+export function determineSellChange(forecastChanges) {
   if (forecastChanges.length === 0) {
     return new SellPlan(0, 0);
   }
diff --git a/unit_tests/test_buy.js b/unit_tests/test_buy.js
index 3b9a1ec..85eb689 100644
--- a/unit_tests/test_buy.js
+++ b/unit_tests/test_buy.js
@@ -1,5 +1,7 @@
-QUnit.module('buy.js');
-/* globals QUnit determineBuyPrice */
+import {determineBuyPrice} from '../js/buy.js';
+
+/* globals QUnit */
+QUnit.module('test_buy.js');
 /* eslint-disable no-magic-numbers */
 
 QUnit.test('find a buy price for a one-element forecast', (assert) => {
diff --git a/unit_tests/test_buy_sell.js b/unit_tests/test_buy_sell.js
index b07a249..89e1251 100644
--- a/unit_tests/test_buy_sell.js
+++ b/unit_tests/test_buy_sell.js
@@ -1,5 +1,7 @@
-QUnit.module('buy_sell.js');
-/* globals QUnit BuySellPlan determineBuyAndSellPrices */
+import {BuySellPlan, determineBuyAndSellPrices} from '../js/buy_sell.js';
+
+/* globals QUnit */
+QUnit.module('test_buy_sell.js');
 /* eslint-disable no-magic-numbers */
 
 QUnit.test('find buy and sell prices for a one-element forecast', (assert) => {
@@ -42,6 +44,14 @@ QUnit.test('find buy and sell prices for a four-element invereted zig-zag foreca
   assert.deepEqual(determineBuyAndSellPrices([102, 103, 100, 101]), new BuySellPlan(102, 103, 100, 103));
 });
 
+QUnit.test('find buy and sell prices for a six-element leveling forecast', (assert) => {
+  assert.deepEqual(determineBuyAndSellPrices([101, 102, 100, 101, 101, 101]), new BuySellPlan(101, 102, 100, 102));
+});
+
+QUnit.test('find buy and sell prices for a six-element rotated leveling forecast', (assert) => {
+  assert.deepEqual(determineBuyAndSellPrices([101, 101, 101, 102, 100, 101]), new BuySellPlan(101, 102, 100, 102));
+});
+
 QUnit.test('find buy and sell prices for random forecast #0', (assert) => {
   assert.deepEqual(determineBuyAndSellPrices([900, 865, 691, 580, 967, 480, 740, 936, 153, 371, 606, 901]), new BuySellPlan(153, 901, 153, 967));
 });
diff --git a/unit_tests/test_sell.js b/unit_tests/test_sell.js
index 3094f4c..4b1eaa1 100644
--- a/unit_tests/test_sell.js
+++ b/unit_tests/test_sell.js
@@ -1,5 +1,7 @@
-QUnit.module('sell.js');
-/* globals QUnit SellPlan determineSellChange */
+import {SellPlan, determineSellChange} from '../js/sell.js';
+
+/* globals QUnit */
+QUnit.module('test_sell.js');
 /* eslint-disable no-magic-numbers */
 
 QUnit.test('find cumulative change at which to sell for a zero-element forecast', (assert) => {
diff --git a/unit_tests/unit_tests.html b/unit_tests/unit_tests.html
index e905e96..271fc01 100644
--- a/unit_tests/unit_tests.html
+++ b/unit_tests/unit_tests.html
@@ -13,14 +13,9 @@
   <script src="../libraries/qunit/qunit.js"></script>
   <script src="overrides.js"></script>
 
-  <script src="../js/sell.js"></script>
-  <script src="test_sell.js"></script>
-
-  <script src="../js/buy_sell.js"></script>
-  <script src="test_buy_sell.js"></script>
-
-  <script src="../js/buy.js"></script>
-  <script src="test_buy.js"></script>
+  <script type="module" src="test_sell.js"></script>
+  <script type="module" src="test_buy_sell.js"></script>
+  <script type="module" src="test_buy.js"></script>
 </body>
 
 </html>
-- 
GitLab