-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
72 deletions.
There are no files selected for viewing
126 changes: 54 additions & 72 deletions
126
jackdaw-sample/src/test/kotlin/com/github/vbauer/jackdaw/JComparatorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |