From f5e3ffffea8f9b9c2a997a898e42d4b49b4e4e9b Mon Sep 17 00:00:00 2001 From: David Date: Wed, 27 Feb 2019 13:01:53 +0100 Subject: [PATCH] eliminada addEZI(), refactorizado payable con Math.floor(), refactorizados casos test --- src/main/java/enzinium/TokenContract.java | 2 +- src/test/java/enzinium/AddressTest.java | 2 - src/test/java/enzinium/TokenContractTest.java | 48 +++++++------------ 3 files changed, 18 insertions(+), 34 deletions(-) diff --git a/src/main/java/enzinium/TokenContract.java b/src/main/java/enzinium/TokenContract.java index 9b8d75a..e2e8c05 100644 --- a/src/main/java/enzinium/TokenContract.java +++ b/src/main/java/enzinium/TokenContract.java @@ -146,7 +146,7 @@ public int totalTokensSold() { public void payable(PublicKey recipient, Double enziniums) { try { require(enziniums >= this.getTokenPrice()); - Double units = Math.ceil(enziniums / tokenPrice); + Double units = Math.floor(enziniums / tokenPrice); transfer(recipient, units); this.owner.transferEZI(enziniums); } catch (Exception e) { diff --git a/src/test/java/enzinium/AddressTest.java b/src/test/java/enzinium/AddressTest.java index 603d6ae..8661b78 100644 --- a/src/test/java/enzinium/AddressTest.java +++ b/src/test/java/enzinium/AddressTest.java @@ -34,10 +34,8 @@ public void transferEZI_test() { rick.generateKeyPair(); rick.transferEZI(20d); - rick.transferEZI(20d); assertEquals(40d, rick.getBalance(), 0d); } - } \ No newline at end of file diff --git a/src/test/java/enzinium/TokenContractTest.java b/src/test/java/enzinium/TokenContractTest.java index 644a69f..719ba62 100644 --- a/src/test/java/enzinium/TokenContractTest.java +++ b/src/test/java/enzinium/TokenContractTest.java @@ -2,21 +2,31 @@ import static org.junit.Assert.assertEquals; +import org.junit.Before; import org.junit.Test; public class TokenContractTest { - @Test - public void addOwner_test() { + private Address rick = null; + private Address morty = null; + TokenContract ricknillos = null; + + @Before + public void setup_contract() { - Address rick = new Address(); + rick = new Address(); rick.generateKeyPair(); - TokenContract ricknillos = new TokenContract(rick); + ricknillos = new TokenContract(rick); ricknillos.addOwner(rick.getPK(), 100d); assertEquals(1, ricknillos.getBalances().size()); - Address morty = new Address(); + morty = new Address(); morty.generateKeyPair(); + } + + @Test + public void addOwner_test() { + ricknillos.addOwner(morty.getPK(), 0d); assertEquals(2, ricknillos.getBalances().size()); @@ -27,15 +37,6 @@ public void addOwner_test() { @Test public void balanceOf_test() { - - Address rick = new Address(); - rick.generateKeyPair(); - TokenContract ricknillos = new TokenContract(rick); - ricknillos.addOwner(rick.getPK(), 100d); - assertEquals(1, ricknillos.getBalances().size()); - - Address morty = new Address(); - morty.generateKeyPair(); assertEquals(100d, ricknillos.balanceOf(rick.getPK()), 0d); // chequeo getOrDefault(PK, 0d) para direcciones que no existen @@ -45,15 +46,6 @@ public void balanceOf_test() { @Test public void transfer_test() { - Address rick = new Address(); - rick.generateKeyPair(); - TokenContract ricknillos = new TokenContract(rick); - ricknillos.addOwner(rick.getPK(), 100d); - assertEquals(1, ricknillos.getBalances().size()); - - Address morty = new Address(); - morty.generateKeyPair(); - ricknillos.transfer(morty.getPK(), 2d); assertEquals(2d, ricknillos.balanceOf(morty.getPK()), 0d); assertEquals(98d, ricknillos.balanceOf(rick.getPK()), 0d); @@ -66,13 +58,6 @@ public void transfer_test() { @Test public void payable_test() { - Address rick = new Address(); - rick.generateKeyPair(); - TokenContract ricknillos = new TokenContract(rick); - ricknillos.addOwner(rick.getPK(), 100d); - Address morty = new Address(); - morty.generateKeyPair(); - morty.transferEZI(20d); // verifico la transferencia de entradas @@ -86,8 +71,9 @@ public void payable_test() { assertEquals(4d, ricknillos.balanceOf(morty.getPK()), 0d); assertEquals(20d, ricknillos.owner().getBalance(), 0d); + // intento de compra de media entrada ricknillos.payable(morty.getPK(), 8d); assertEquals(5d, ricknillos.balanceOf(morty.getPK()), 0d); - assertEquals(20d, ricknillos.owner().getBalance(), 0d); + assertEquals(28d, ricknillos.owner().getBalance(), 0d); } } \ No newline at end of file