|
| 1 | +package org.stellar.sdk |
| 2 | + |
| 3 | +import io.kotest.core.spec.style.FunSpec |
| 4 | +import io.kotest.datatest.withData |
| 5 | +import io.kotest.matchers.shouldBe |
| 6 | + |
| 7 | +data class PriceTestCase( |
| 8 | + val input: String, |
| 9 | + val expectedNumerator: Int, |
| 10 | + val expectedDenominator: Int, |
| 11 | +) |
| 12 | + |
| 13 | +class PriceTest : |
| 14 | + FunSpec({ |
| 15 | + context("Price.fromString()") { |
| 16 | + withData( |
| 17 | + nameFn = { |
| 18 | + "should parse '${it.input}' to ${it.expectedNumerator}/${it.expectedDenominator}" |
| 19 | + }, |
| 20 | + PriceTestCase("0", 0, 1), |
| 21 | + PriceTestCase("0.1", 1, 10), |
| 22 | + PriceTestCase("0.01", 1, 100), |
| 23 | + PriceTestCase("0.001", 1, 1000), |
| 24 | + PriceTestCase("543.01793", 54301793, 100000), |
| 25 | + PriceTestCase("319.69983", 31969983, 100000), |
| 26 | + PriceTestCase("0.93", 93, 100), |
| 27 | + PriceTestCase("0.5", 1, 2), |
| 28 | + PriceTestCase("1.730", 173, 100), |
| 29 | + PriceTestCase("0.85334384", 5333399, 6250000), |
| 30 | + PriceTestCase("5.5", 11, 2), |
| 31 | + PriceTestCase("2.72783", 272783, 100000), |
| 32 | + PriceTestCase("638082.0", 638082, 1), |
| 33 | + PriceTestCase("2.93850088", 36731261, 12500000), |
| 34 | + PriceTestCase("58.04", 1451, 25), |
| 35 | + PriceTestCase("41.265", 8253, 200), |
| 36 | + PriceTestCase("5.1476", 12869, 2500), |
| 37 | + PriceTestCase("95.14", 4757, 50), |
| 38 | + PriceTestCase("0.74580", 3729, 5000), |
| 39 | + PriceTestCase("4119.0", 4119, 1), |
| 40 | + PriceTestCase("1073742464.5", 1073742464, 1), |
| 41 | + PriceTestCase("1635962526.2", 1635962526, 1), |
| 42 | + PriceTestCase("2147483647", 2147483647, 1), |
| 43 | + ) { testCase -> |
| 44 | + val price = Price.fromString(testCase.input) |
| 45 | + price.numerator shouldBe testCase.expectedNumerator |
| 46 | + price.denominator shouldBe testCase.expectedDenominator |
| 47 | + Price.fromXdr(price.toXdr()) shouldBe price |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + context("Price constructor") { |
| 52 | + test("should create price with numerator and denominator") { |
| 53 | + val price = Price(3, 4) |
| 54 | + price.numerator shouldBe 3 |
| 55 | + price.denominator shouldBe 4 |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + context("Price.toString()") { |
| 60 | + test("should return decimal representation") { |
| 61 | + Price(1, 2).toString() shouldBe "0.5" |
| 62 | + Price(3, 4).toString() shouldBe "0.75" |
| 63 | + Price(5, 1).toString() shouldBe "5" |
| 64 | + Price(0, 1).toString() shouldBe "0" |
| 65 | + } |
| 66 | + } |
| 67 | + }) |
0 commit comments