Skip to content

Commit 67b282c

Browse files
CopilotbedaHovorka
andauthored
Convert getter/setter methods to properties, replace .equals() with ==, and use property references in assertions (#36)
* Convert EditingContext getter/setter methods to properties - Convert getCurrentMaxSpeed/setCurrentMaxSpeed to currentMaxSpeed property - Convert getCurrentTrackLength/setCurrentTrackLength to currentTrackLength property - Convert getCurrentNameString/setCurrentNameString to currentNameString property - Update all call sites in source and tests to use property syntax - All tests pass (629/659 active tests) * Replace .equals() calls with == operator for idiomatic Kotlin - Convert Point.equals() to == in DefaultContext.kt (3 occurrences) - Convert Point.equals() to != in Cell.kt (1 occurrence) - All null-safety assertions remain unchanged - All tests pass (629/659 active tests) * Use assertk prop() with property references for more idiomatic Kotlin assertions - Import assertk.assertions.prop in test files - Replace assertThat(object.property) with assertThat(object).prop(Type::property) - More type-safe and idiomatic Kotlin testing pattern - All tests pass (629/659 active tests) --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: bedaHovorka <5263405+bedaHovorka@users.noreply.github.com>
1 parent 53edc28 commit 67b282c

6 files changed

Lines changed: 47 additions & 78 deletions

File tree

src/main/kotlin/cz/vutbr/fit/interlockSim/context/DefaultContext.kt

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,20 @@ abstract class DefaultContext :
9292
/**
9393
* Current maximum speed for path elements
9494
*/
95-
private var currentMaxSpeed: Double = PathElement.COMMON_MAX_SPEED
95+
override var currentMaxSpeed: Double = PathElement.COMMON_MAX_SPEED
9696

9797
/**
9898
* Current track length for new track elements
9999
*/
100-
private var currentTrackLength: Double = Track.COMMON_TRACK_LENGTH
100+
override var currentTrackLength: Double = Track.COMMON_TRACK_LENGTH
101101

102102
/**
103103
* Railway network grid structure
104104
*/
105105
private val railwayNetGrid: DefaultRailWayNetGrid
106106

107107
/**
108-
* Name string for train generation
108+
* Name string for train generation (backing field for currentNameString)
109109
*/
110110
private var nameString: String? = null
111111

@@ -388,13 +388,13 @@ abstract class DefaultContext :
388388
points: MutableList<Point>
389389
): Boolean {
390390
assert(key1 != null && key2 != null && p1 != null && p2 != null)
391-
assert(!key1.equals(p1) && !key2.equals(p2) && !key1.equals(p2) && !key2.equals(p1))
391+
assert(key1 != p1 && key2 != p2 && key1 != p2 && key2 != p1)
392392

393393
// Make mutable copies since we need to modify them for the algorithm
394394
var p1Mut = p1
395395
var p2Mut = p2
396396

397-
if (p1Mut.equals(p2Mut)) {
397+
if (p1Mut == p2Mut) {
398398
points.add(p1Mut) // snad je naklonovany
399399
return false
400400
}
@@ -429,7 +429,7 @@ abstract class DefaultContext :
429429
for (x in p1Mut.x..p2Mut.x) {
430430
val newPoint = if (!swapped) Point(x, y) else Point(y, x)
431431

432-
if (newPoint.equals(key1) || newPoint.equals(key2) || used(newPoint)) {
432+
if (newPoint == key1 || newPoint == key2 || used(newPoint)) {
433433
points.clear()
434434
return b
435435
}
@@ -536,7 +536,7 @@ abstract class DefaultContext :
536536
s1,
537537
p,
538538
s2,
539-
SimpleTrackBlock(nodeCell, nodeCell2, Track.MIN_LENGTH, getCurrentMaxSpeed())
539+
SimpleTrackBlock(nodeCell, nodeCell2, Track.MIN_LENGTH, currentMaxSpeed)
540540
)
541541
}
542542
}
@@ -872,30 +872,6 @@ abstract class DefaultContext :
872872
return null
873873
}
874874

875-
/**
876-
* Get current maximum speed for path elements
877-
*/
878-
override fun getCurrentMaxSpeed(): Double = currentMaxSpeed
879-
880-
/**
881-
* Get current track length for new elements
882-
*/
883-
override fun getCurrentTrackLength(): Double = currentTrackLength
884-
885-
/**
886-
* Set current maximum speed
887-
*/
888-
override fun setCurrentMaxSpeed(speed: Double) {
889-
currentMaxSpeed = speed
890-
}
891-
892-
/**
893-
* Set current track length
894-
*/
895-
override fun setCurrentTrackLength(length: Double) {
896-
currentTrackLength = length
897-
}
898-
899875
/**
900876
* Report simulation events
901877
*/
@@ -948,22 +924,19 @@ abstract class DefaultContext :
948924
}
949925

950926
/**
951-
* Get current name string for train generation
927+
* Current name string for train generation
952928
*/
953-
override fun getCurrentNameString(): String = nameString ?: randomString()
929+
override var currentNameString: String
930+
get() = nameString ?: randomString()
931+
set(value) {
932+
nameString = value
933+
}
954934

955935
/**
956936
* Generate random name string (single character A-T)
957937
*/
958938
private fun randomString(): String = String(Character.toChars(65 + random.nextInt(20)))
959939

960-
/**
961-
* Set current name string for train generation
962-
*/
963-
override fun setCurrentNameString(name: String) {
964-
this.nameString = name
965-
}
966-
967940
/**
968941
* Get the worker for an entry/exit point
969942
*/

src/main/kotlin/cz/vutbr/fit/interlockSim/context/EditingContext.kt

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,14 @@ interface EditingContext : Context {
5050
fun removeLine(block: TrackBlock)
5151

5252
/**
53-
* @return current maximal speed, which is setting in elements
53+
* Current maximal speed, which is setting in elements
5454
*/
55-
fun getCurrentMaxSpeed(): Double
55+
var currentMaxSpeed: Double
5656

5757
/**
58-
* @param speed maximal speed, which is setting in elements
58+
* Current track length, which is setting in elements
5959
*/
60-
fun setCurrentMaxSpeed(speed: Double)
61-
62-
/**
63-
* @return current track length, which is setting in elements
64-
*/
65-
fun getCurrentTrackLength(): Double
66-
67-
/**
68-
* @param length track length, which is setting in elements
69-
*/
70-
fun setCurrentTrackLength(length: Double)
60+
var currentTrackLength: Double
7161

7262
/**
7363
* Create relation between nodes. In specified places, must be {@link NodeCell}
@@ -82,12 +72,7 @@ interface EditingContext : Context {
8272
)
8373

8474
/**
85-
* @param name which is setting in elements
86-
*/
87-
fun setCurrentNameString(name: String)
88-
89-
/**
90-
* @return name which is setting in elements
75+
* Name which is setting in elements
9176
*/
92-
fun getCurrentNameString(): String
77+
var currentNameString: String
9378
}

src/main/kotlin/cz/vutbr/fit/interlockSim/gui/RailwayNetGridCanvas.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class RailwayNetGridCanvas :
120120
*(toolbarArgs!! as Array<Any>)
121121
) as NodeCell
122122
if (newCell is InOut) {
123-
newCell.setName(editingContext.getCurrentNameString())
123+
newCell.setName(editingContext.currentNameString)
124124
}
125125
editingContext.putCell(clickKey, newCell)
126126
// Clear selection after creating a cell to prevent auto-joining
@@ -143,8 +143,8 @@ class RailwayNetGridCanvas :
143143
SimpleTrackBlock(
144144
selectedCell,
145145
cellAtClick,
146-
editingContext.getCurrentTrackLength(),
147-
editingContext.getCurrentMaxSpeed()
146+
editingContext.currentTrackLength,
147+
editingContext.currentMaxSpeed
148148
)
149149
editingContext.joinCells(selectedPoint, clickKey, trackBlock)
150150
repaint()

src/main/kotlin/cz/vutbr/fit/interlockSim/objects/cells/Cell.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ interface Cell {
8383
fun transform(from: Point): Point {
8484
assert(from != null)
8585
val tr = Point(from.x + dx, from.y + dy)
86-
assert(!from.equals(tr)) { this }
86+
assert(from != tr) { this }
8787
return tr
8888
}
8989

src/test/kotlin/cz/vutbr/fit/interlockSim/context/ContextInitializationTest.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import assertk.assertions.isEqualTo
1515
import assertk.assertions.isGreaterThan
1616
import assertk.assertions.isInstanceOf
1717
import assertk.assertions.isNotNull
18+
import assertk.assertions.prop
1819
import cz.vutbr.fit.interlockSim.objects.cells.InOut
1920
import cz.vutbr.fit.interlockSim.testutil.TestContextBuilder
2021
import cz.vutbr.fit.interlockSim.testutil.withMessage
@@ -345,7 +346,7 @@ class ContextInitializationTest {
345346
@DisplayName("context preserves name from XML initialization")
346347
fun context_preservesName() {
347348
// Arrange & Act
348-
val contextName = linearTrackContext.getCurrentNameString()
349+
val contextName = linearTrackContext.currentNameString
349350

350351
// Assert
351352
assertThat(contextName)
@@ -363,8 +364,8 @@ class ContextInitializationTest {
363364
@DisplayName("context initialized with valid configuration settings")
364365
fun context_hasValidSettings() {
365366
// Arrange & Act
366-
val maxSpeed = linearTrackContext.getCurrentMaxSpeed()
367-
val trackLength = linearTrackContext.getCurrentTrackLength()
367+
val maxSpeed = linearTrackContext.currentMaxSpeed
368+
val trackLength = linearTrackContext.currentTrackLength
368369

369370
// Assert
370371
assertThat(maxSpeed)
@@ -398,7 +399,8 @@ class ContextInitializationTest {
398399
.isNotNull()
399400

400401
// Railway context: Default settings should be applicable
401-
assertThat(emptyContext.getCurrentMaxSpeed())
402+
assertThat(emptyContext)
403+
.prop(EditingContext::currentMaxSpeed)
402404
.withMessage("Empty context should have max speed setting")
403405
.isGreaterThan(0.0)
404406
}
@@ -470,13 +472,15 @@ class ContextInitializationTest {
470472
val context2 = factory.createEmptyContext()
471473

472474
// Modify first context
473-
context1.setCurrentNameString("Network A")
475+
context1.currentNameString = "Network A"
474476

475477
// Assert
476-
assertThat(context1.getCurrentNameString())
478+
assertThat(context1)
479+
.prop(EditingContext::currentNameString)
477480
.withMessage("First context should have modified name")
478481
.isEqualTo("Network A")
479-
assertThat(context2.getCurrentNameString())
482+
assertThat(context2)
483+
.prop(EditingContext::currentNameString)
480484
.withMessage("Second context should have different name (independent)")
481485
.isNotNull()
482486
}

src/test/kotlin/cz/vutbr/fit/interlockSim/context/DefaultContextTest.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import assertk.assertions.isNull
2020
import assertk.assertions.isSameInstanceAs
2121
import assertk.assertions.isNotSameInstanceAs
2222
import assertk.assertions.isTrue
23+
import assertk.assertions.prop
2324
import cz.vutbr.fit.interlockSim.objects.cells.Cell.SpatialType
2425
import cz.vutbr.fit.interlockSim.objects.cells.InOut
2526
import cz.vutbr.fit.interlockSim.objects.cells.RailSemaphore
@@ -319,10 +320,12 @@ class DefaultContextTest {
319320
val newSpeed = 120.0
320321

321322
// Act
322-
context.setCurrentMaxSpeed(newSpeed)
323+
context.currentMaxSpeed = newSpeed
323324

324325
// Assert
325-
assertThat(context.getCurrentMaxSpeed()).isEqualTo(newSpeed)
326+
assertThat(context)
327+
.prop(EditingContext::currentMaxSpeed)
328+
.isEqualTo(newSpeed)
326329
}
327330

328331
@Test
@@ -332,10 +335,12 @@ class DefaultContextTest {
332335
val newLength = 500.0
333336

334337
// Act
335-
context.setCurrentTrackLength(newLength)
338+
context.currentTrackLength = newLength
336339

337340
// Assert
338-
assertThat(context.getCurrentTrackLength()).isEqualTo(newLength)
341+
assertThat(context)
342+
.prop(EditingContext::currentTrackLength)
343+
.isEqualTo(newLength)
339344
}
340345

341346
@Test
@@ -345,10 +350,12 @@ class DefaultContextTest {
345350
val name = "Test Network"
346351

347352
// Act
348-
context.setCurrentNameString(name)
353+
context.currentNameString = name
349354

350355
// Assert
351-
assertThat(context.getCurrentNameString()).isEqualTo(name)
356+
assertThat(context)
357+
.prop(EditingContext::currentNameString)
358+
.isEqualTo(name)
352359
}
353360
}
354361

0 commit comments

Comments
 (0)