Skip to content

Commit

Permalink
Add unit tests for comparators
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauer committed Mar 14, 2016
1 parent a2b1df2 commit eb0d8b9
Showing 1 changed file with 54 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,104 +1,86 @@
package com.github.vbauer.jackdaw

import com.github.vbauer.jackdaw.CompanyComparators.*
import com.github.vbauer.jackdaw.base.BaseTest
import org.hamcrest.Matchers.equalTo
import org.junit.Assert.assertThat
import org.junit.Test
import java.util.*

/**
* @author Vladislav Bauer
*/

class JComparatorTest : BaseTest() {

@Test
fun testCompanyComparatorsConstructor() {
checkConstructor(CompanyComparators::class)
}

@Test
fun testCompanyComparatorsId() {
// a = b
assertThat(
CompanyComparators.ID.compare(null, null),
equalTo(0)
)
assertThat(
CompanyComparators.ID.compare(
Company().apply { id = 0 },
Company().apply { id = 0 }
),
equalTo(0)
)
checkComparator(ID, null, null, 0)
checkComparator(ID, company { id = 0 }, company { id = 0 }, 0)

// a < b (but reverse)
assertThat(
CompanyComparators.ID.compare(null, Company()),
equalTo(1)
)
assertThat(
CompanyComparators.ID.compare(
Company().apply { id = 0 },
Company().apply { id = 1 }
),
equalTo(1)
)
checkComparator(ID, null, Company(), 1)
checkComparator(ID, company { id = 0 }, company { id = 1 }, 1)

// a > b (but reverse)
assertThat(
CompanyComparators.ID.compare(Company(), null),
equalTo(-1)
)
assertThat(
CompanyComparators.ID.compare(
Company().apply { id = 1 },
Company().apply { id = 0 }
),
equalTo(-1)
)
checkComparator(ID, Company(), null, -1)
checkComparator(ID, company { id = 1 }, company { id = 0 }, -1)
}

@Test
fun testCompanyComparatorsName() {
// a = b
assertThat(
CompanyComparators.NAME.compare(null, null),
equalTo(0)
)
assertThat(
CompanyComparators.NAME.compare(
Company().apply { name = "a" },
Company().apply { name = "a" }
),
equalTo(0)
)
checkComparator(NAME, null, null, 0)
checkComparator(NAME, company { name = "a" }, company { name = "a" }, 0)

// a < b
checkComparator(NAME, null, company { name = "a" }, -1)
checkComparator(NAME, company { name = "a" }, company { name = "b" }, -1)

// a > b
checkComparator(NAME, company { name = "a" }, null, 1)
checkComparator(NAME, company { name = "b" }, company { name = "a" }, 1)
}

@Test
fun testCompanyComparatorsLlp() {
// a = b
checkComparator(LLP, company { llp = false }, company { llp = false }, 0)
checkComparator(LLP, company { llp = true }, company { llp = true }, 0)

// a < b
assertThat(
CompanyComparators.NAME.compare(
null,
Company().apply { name = "a" }
),
equalTo(-1)
)
assertThat(
CompanyComparators.NAME.compare(
Company().apply { name = "a" },
Company().apply { name = "b" }
),
equalTo(-1)
)
checkComparator(LLP, company { llp = false }, company { llp = true }, -1)

// a > b
assertThat(
CompanyComparators.NAME.compare(
Company().apply { name = "a" },
null
),
equalTo(1)
)
assertThat(
CompanyComparators.NAME.compare(
Company().apply { name = "b" },
Company().apply { name = "a" }
),
equalTo(1)
)
checkComparator(LLP, company { llp = true }, company { llp = false }, 1)
}

@Test
fun testCompanyComparatorsRevenue() {
// a = b
checkComparator(REVENUE, null, null, 0)
checkComparator(REVENUE, company { revenue = 1 }, company { revenue = 1 }, 0)

// a < b
checkComparator(REVENUE, null, company { revenue = 1 }, -1)
checkComparator(REVENUE, company { revenue = 1 }, company { revenue = 2 }, -1)

// a > b
checkComparator(REVENUE, company { revenue = 1 }, null, 1)
checkComparator(REVENUE, company { revenue = 2 }, company { revenue = 1 }, 1)
}


private fun checkComparator(
comparator: Comparator<Company>, a: Company?, b: Company?, expected: Int
) = assertThat(comparator.compare(a, b), equalTo(expected))

private fun company(init: Company.() -> Unit): Company = Company().apply(init)

}

0 comments on commit eb0d8b9

Please sign in to comment.