Skip to content

Instantly share code, notes, and snippets.

@li2
Created August 18, 2019 03:04
Show Gist options
  • Save li2/dfc1cf0adbc43d37c91cd3b386ae5ef4 to your computer and use it in GitHub Desktop.
Save li2/dfc1cf0adbc43d37c91cd3b386ae5ef4 to your computer and use it in GitHub Desktop.
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
class ViewTest {
private lateinit var firstColour: Colour
private lateinit var secondColour: Colour
private lateinit var overlappedColour: Colour
private lateinit var emptyView: View
private lateinit var view1WithOneChart: View
private lateinit var view2WithNonOverlappedCharts: View
private lateinit var view3WithOverlappedCharts: View
private lateinit var pointNotInView3: Coordinate
private lateinit var pointInView3FirstChart: Coordinate
private lateinit var pointInView3SecondChart: Coordinate
private lateinit var pointInView3OverlappedChart: Coordinate
@Before
fun setup() {
val rectangle1 = Rectangle(Coordinate(0, 0), Coordinate(50, 50))
val rectangle2 = Rectangle(Coordinate(25, 25), Coordinate(75, 75))
val rectangle3 = Rectangle(Coordinate(80, 80), Coordinate(100, 100))
firstColour = Colour(255, 0, 0)
secondColour = Colour(0, 0, 255)
overlappedColour = Colour(128, 0, 128)
emptyView = View(emptyList())
view1WithOneChart = View(listOf(Chart(rectangle1, firstColour)))
view2WithNonOverlappedCharts =
View(listOf(Chart(rectangle1, firstColour), Chart(rectangle3, secondColour)))
view3WithOverlappedCharts =
View(listOf(Chart(rectangle1, firstColour), Chart(rectangle2, secondColour)))
pointNotInView3 = Coordinate(80, 80)
pointInView3FirstChart = Coordinate(0, 0)
pointInView3SecondChart = Coordinate(60, 60)
pointInView3OverlappedChart = Coordinate(35, 35)
}
@Test
fun `View doChartsOverlap should return false if contains zero or one chart`() {
assertFalse(emptyView.doChartsOverlap())
assertFalse(view1WithOneChart.doChartsOverlap())
}
@Test
fun `View doChartsOverlap should return false if contains two non-overlapped charts`() {
assertFalse(view2WithNonOverlappedCharts.doChartsOverlap())
}
@Test
fun `View doChartsOverlap should return true if contains two overlapped charts`() {
assertTrue(view3WithOverlappedCharts.doChartsOverlap())
}
@Test(expected = IllegalArgumentException::class)
fun `View with more than 2 chars should throw exception`() {
val chart = Chart(Rectangle(Coordinate(0, 0), Coordinate(50, 50)), Colour(255, 0, 0))
View(listOf(chart, chart, chart))
}
@Test
fun `View getColour should return null if contains 0 chart`() {
assertNull(emptyView.getColour(0, 0))
}
@Test
fun `View getColour should return null if the given point is not in charts`() {
with (pointNotInView3) {
assertNull(view3WithOverlappedCharts.getColour(x, y))
}
}
@Test
fun `View getColour should return chart colour if the given point is found in only this chart`() {
with (pointInView3FirstChart) {
assertEquals(firstColour, view3WithOverlappedCharts.getColour(x, y))
}
with (pointInView3SecondChart) {
assertEquals(secondColour, view3WithOverlappedCharts.getColour(x, y))
}
}
@Test
fun `View getColour should return overlapped colour if the given point is found in overlapped area`() {
with (pointInView3OverlappedChart) {
assertEquals(overlappedColour, view3WithOverlappedCharts.getColour(x, y))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment