Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use assertFailsWith instead of expected #5202

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ package de.westnordost.streetcomplete.data

import org.junit.Assert.assertEquals
import org.junit.Test
import kotlin.test.assertFailsWith

internal class ObjectTypeRegistryTest {

@Test(expected = IllegalArgumentException::class)
@Test
fun `throws when one class is added twice`() {
ObjectTypeRegistry<Any>(listOf(1 to A, 2 to A))
assertFailsWith<IllegalArgumentException> {
ObjectTypeRegistry<Any>(listOf(1 to A, 2 to A))
}
}

@Test(expected = IllegalArgumentException::class)
@Test
fun `throws when one ordinal is added twice`() {
ObjectTypeRegistry(listOf(1 to A, 1 to B))
assertFailsWith<IllegalArgumentException> {
ObjectTypeRegistry(listOf(1 to A, 1 to B))
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package de.westnordost.streetcomplete.data.elementfilter

import org.junit.Assert.assertEquals
import org.junit.Test
import kotlin.test.assertFailsWith

class BooleanExpressionBuilderTest {
@Test fun leaf() { check("a") }
Expand Down Expand Up @@ -55,23 +56,47 @@ class BooleanExpressionBuilderTest {
@Test fun flatten1() { check("((a*b)*c)*d*(e*f)", "a*b*c*d*e*f") }
@Test fun flatten2() { check("(a+b*(c+d)+e)*f", "(a+b*(c+d)+e)*f") }

@Test(expected = IllegalStateException::class)
fun `closed too many brackets 1`() { TestBooleanExpressionParser.parse("a+b)") }
@Test
fun `closed too many brackets 1`() {
assertFailsWith<IllegalStateException> {
TestBooleanExpressionParser.parse("a+b)")
}
}

@Test(expected = IllegalStateException::class)
fun `closed too many brackets 2`() { TestBooleanExpressionParser.parse("(a+b))") }
@Test
fun `closed too many brackets 2`() {
assertFailsWith<IllegalStateException> {
TestBooleanExpressionParser.parse("(a+b))")
}
}

@Test(expected = IllegalStateException::class)
fun `closed too many brackets 3`() { TestBooleanExpressionParser.parse("((b+c)*a)+d)") }
@Test
fun `closed too many brackets 3`() {
assertFailsWith<IllegalStateException> {
TestBooleanExpressionParser.parse("((b+c)*a)+d)")
}
}

@Test(expected = IllegalStateException::class)
fun `closed too few brackets 1`() { TestBooleanExpressionParser.parse("(a+b") }
@Test
fun `closed too few brackets 1`() {
assertFailsWith<IllegalStateException> {
TestBooleanExpressionParser.parse("(a+b")
}
}

@Test(expected = IllegalStateException::class)
fun `closed too few brackets 2`() { TestBooleanExpressionParser.parse("((a+b)") }
@Test
fun `closed too few brackets 2`() {
assertFailsWith<IllegalStateException> {
TestBooleanExpressionParser.parse("((a+b)")
}
}

@Test(expected = IllegalStateException::class)
fun `closed too few brackets 3`() { TestBooleanExpressionParser.parse("((a*(b+c))") }
@Test
fun `closed too few brackets 3`() {
assertFailsWith<IllegalStateException> {
TestBooleanExpressionParser.parse("((a*(b+c))")
}
}

private fun check(input: String, expected: String = input) {
val tree = TestBooleanExpressionParser.parse(input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import kotlin.test.assertFailsWith

internal class CreateNodeActionTest {
private lateinit var repos: MapDataRepository
Expand Down Expand Up @@ -78,7 +79,7 @@ internal class CreateNodeActionTest {
assertEquals(listOf<Long>(1, -123, 2), updatedWay.nodeIds)
}

@Test(expected = ConflictException::class)
@Test
fun `conflict if way the node should be inserted into does not exist`() {
val way = way(id = 1, nodes = listOf(1, 2))
val pos1 = LatLon(0.0, 1.0)
Expand All @@ -91,10 +92,12 @@ internal class CreateNodeActionTest {
val position = LatLon(0.0, 1.5)
val action = CreateNodeAction(position, tags, listOf(InsertIntoWayAt(way.id, pos1, pos2)))

action.createUpdates(repos, provider)
assertFailsWith<ConflictException> {
action.createUpdates(repos, provider)
}
}

@Test(expected = ConflictException::class)
@Test
fun `conflict if node 2 is not successive to node 1`() {
val way = way(id = 1, nodes = listOf(1, 2, 3))
val pos1 = LatLon(0.0, 1.0)
Expand All @@ -111,7 +114,9 @@ internal class CreateNodeActionTest {
val position = LatLon(0.0, 1.5)
val action = CreateNodeAction(position, tags, listOf(InsertIntoWayAt(way.id, pos1, pos3)))

action.createUpdates(repos, provider)
assertFailsWith<ConflictException> {
action.createUpdates(repos, provider)
}
}

@Test
Expand All @@ -135,7 +140,7 @@ internal class CreateNodeActionTest {
action.createUpdates(repos, provider)
}

@Test(expected = ConflictException::class)
@Test
fun `conflict if node 1 has been moved`() {
val way = way(id = 1, nodes = listOf(1, 2))
val oldPos1 = LatLon(0.0, 0.0)
Expand All @@ -152,10 +157,12 @@ internal class CreateNodeActionTest {
val action =
CreateNodeAction(position, tags, listOf(InsertIntoWayAt(way.id, oldPos1, pos2)))

action.createUpdates(repos, provider)
assertFailsWith<ConflictException> {
action.createUpdates(repos, provider)
}
}

@Test(expected = ConflictException::class)
@Test
fun `conflict if node 2 has been moved`() {
val way = way(id = 1, nodes = listOf(1, 2))
val oldPos2 = LatLon(0.0, 0.0)
Expand All @@ -172,7 +179,9 @@ internal class CreateNodeActionTest {
val action =
CreateNodeAction(position, tags, listOf(InsertIntoWayAt(way.id, pos1, oldPos2)))

action.createUpdates(repos, provider)
assertFailsWith<ConflictException> {
action.createUpdates(repos, provider)
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import de.westnordost.streetcomplete.util.math.translate
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import kotlin.test.assertFailsWith

internal class CreateNodeFromVertexActionTest {
private lateinit var repos: MapDataRepository
Expand All @@ -28,25 +29,29 @@ internal class CreateNodeFromVertexActionTest {
provider = mock()
}

@Test(expected = ConflictException::class)
@Test
fun `conflict when node position changed`() {
val n = node()
val n2 = n.copy(position = n.position.translate(1.0, 0.0)) // moved by 1 meter
on(repos.getNode(n.id)).thenReturn(n2)
on(repos.getWaysForNode(n.id)).thenReturn(listOf())

CreateNodeFromVertexAction(n, StringMapChanges(listOf()), listOf())
.createUpdates(repos, provider)
assertFailsWith<ConflictException> {
CreateNodeFromVertexAction(n, StringMapChanges(listOf()), listOf())
.createUpdates(repos, provider)
}
}

@Test(expected = ConflictException::class)
@Test
fun `conflict when node is not part of exactly the same ways as before`() {
val n = node()
on(repos.getNode(n.id)).thenReturn(n)
on(repos.getWaysForNode(n.id)).thenReturn(listOf(way(1), way(2)))

CreateNodeFromVertexAction(n, StringMapChanges(listOf()), listOf(1L))
.createUpdates(repos, provider)
assertFailsWith<ConflictException> {
CreateNodeFromVertexAction(n, StringMapChanges(listOf()), listOf(1L))
.createUpdates(repos, provider)
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import kotlin.test.assertFailsWith

class RevertCreateNodeActionTest {
private lateinit var repos: MapDataRepository
Expand All @@ -43,35 +44,42 @@ class RevertCreateNodeActionTest {
assertEquals(node, deletedNode)
}

@Test(expected = ConflictException::class)
@Test
fun `conflict when node already deleted`() {
on(repos.getNode(1)).thenReturn(null)
RevertCreateNodeAction(node(1), listOf()).createUpdates(repos, provider)

assertFailsWith<ConflictException> {
RevertCreateNodeAction(node(1), listOf()).createUpdates(repos, provider)
}
}

@Test(expected = ConflictException::class)
@Test
fun `conflict when node is now member of a relation`() {
val node = node(1)

on(repos.getNode(node.id)).thenReturn(node)
on(repos.getWaysForNode(1)).thenReturn(emptyList())
on(repos.getRelationsForNode(1)).thenReturn(listOf(rel()))

RevertCreateNodeAction(node, listOf()).createUpdates(repos, provider)
assertFailsWith<ConflictException> {
RevertCreateNodeAction(node, listOf()).createUpdates(repos, provider)
}
}

@Test(expected = ConflictException::class)
@Test
fun `conflict when node is part of more ways than initially`() {
val node = node(1)

on(repos.getNode(node.id)).thenReturn(node)
on(repos.getWaysForNode(1)).thenReturn(listOf(way(1), way(2), way(3)))
on(repos.getRelationsForNode(1)).thenReturn(emptyList())

RevertCreateNodeAction(node, listOf(1, 2)).createUpdates(repos, provider)
assertFailsWith<ConflictException> {
RevertCreateNodeAction(node, listOf(1, 2)).createUpdates(repos, provider)
}
}

@Test(expected = ConflictException::class)
@Test
fun `conflict when node was moved at all`() {
val node = node(1)
val movedNode = node.copy(position = node.position.translate(10.0, 0.0))
Expand All @@ -80,18 +88,22 @@ class RevertCreateNodeActionTest {
on(repos.getWaysForNode(1)).thenReturn(emptyList())
on(repos.getRelationsForNode(1)).thenReturn(emptyList())

RevertCreateNodeAction(node).createUpdates(repos, provider)
assertFailsWith<ConflictException> {
RevertCreateNodeAction(node).createUpdates(repos, provider)
}
}

@Test(expected = ConflictException::class)
@Test
fun `conflict when tags changed on node at all`() {
val node = node(1)

on(repos.getNode(1)).thenReturn(node.copy(tags = mapOf("different" to "tags")))
on(repos.getWaysForNode(1)).thenReturn(emptyList())
on(repos.getRelationsForNode(1)).thenReturn(emptyList())

RevertCreateNodeAction(node).createUpdates(repos, provider)
assertFailsWith<ConflictException> {
RevertCreateNodeAction(node).createUpdates(repos, provider)
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import kotlin.test.assertFailsWith

class DeletePoiNodeActionTest {

Expand Down Expand Up @@ -46,10 +47,13 @@ class DeletePoiNodeActionTest {
assertTrue(data.modifications.single().tags.isEmpty())
}

@Test(expected = ConflictException::class)
@Test
fun `moved element creates conflict`() {
on(repos.getNode(e.id)).thenReturn(e.copy(position = p(1.0, 1.0)))
DeletePoiNodeAction(e).createUpdates(repos, provider)

assertFailsWith<ConflictException> {
DeletePoiNodeAction(e).createUpdates(repos, provider)
}
}

@Test fun idsUpdatesApplied() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import de.westnordost.streetcomplete.util.ktx.copy
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import kotlin.test.assertFailsWith

class RevertDeletePoiNodeActionTest {

Expand Down Expand Up @@ -51,11 +52,14 @@ class RevertDeletePoiNodeActionTest {
)
}

@Test(expected = ConflictException::class)
@Test
fun `conflict if there is already a newer version`() {
on(repos.getNode(1)).thenReturn(e.copy(version = 4))
// version 3 would be the deletion
RevertDeletePoiNodeAction(e).createUpdates(repos, provider)

assertFailsWith<ConflictException> {
// version 3 would be the deletion
RevertDeletePoiNodeAction(e).createUpdates(repos, provider)
}
}

@Test fun idsUpdatesApplied() {
Expand Down
Loading