Skip to content

Commit 5962c8b

Browse files
Xml - ObjectWithAttributesGene - add unsafecopy override and tests
1 parent 9daba4f commit 5962c8b

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

core/src/main/kotlin/org/evomaster/core/search/gene/ObjectWithAttributesGene.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ObjectWithAttributesGene(
3636
* The special name `"#text"` is reserved for element content and therefore cannot be an
3737
* attribute — an [IllegalArgumentException] is also thrown if it is present.
3838
*/
39-
val attributeNames: Set<String> = emptySet()
39+
var attributeNames: Set<String> = emptySet()
4040
) : ObjectGene(name, fixedFields, refType, isFixed, template, additionalFields) {
4141

4242
init {
@@ -73,6 +73,19 @@ class ObjectWithAttributesGene(
7373
)
7474
}
7575

76+
override fun unsafeCopyValueFrom(other: Gene): Boolean {
77+
val ok = super.unsafeCopyValueFrom(other)
78+
if (!ok) return false
79+
80+
attributeNames = if (other is ObjectWithAttributesGene) {
81+
other.attributeNames
82+
} else {
83+
emptySet()
84+
}
85+
86+
return true
87+
}
88+
7689
override fun containsSameValueAs(other: Gene): Boolean {
7790

7891
if (other !is ObjectGene) {

core/src/test/kotlin/org/evomaster/core/search/gene/ObjectWithAttributesGeneTest.kt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,74 @@ class ObjectWithAttributesGeneTest {
467467
assertFalse(withAttrs.containsSameValueAs(plain))
468468
}
469469

470+
// --- unsafeCopyValueFrom tests ---
471+
472+
@Test
473+
fun testUnsafeCopyValueFrom_copiesAttributeNames() {
474+
val gene1 = ObjectWithAttributesGene(
475+
name = "node",
476+
fixedFields = listOf(StringGene("id", "1"), StringGene("name", "Alice")),
477+
isFixed = true,
478+
attributeNames = setOf("id")
479+
)
480+
481+
val gene2 = ObjectWithAttributesGene(
482+
name = "node",
483+
fixedFields = listOf(StringGene("id", "2"), StringGene("name", "Bob")),
484+
isFixed = true,
485+
attributeNames = setOf("name")
486+
)
487+
488+
// Before copy, they have different attribute names
489+
assertFalse(gene1.containsSameValueAs(gene2))
490+
491+
val result = gene1.unsafeCopyValueFrom(gene2)
492+
493+
assertTrue(result)
494+
// Now gene1 should have the same attribute names as gene2 (and same values)
495+
assertTrue(gene1.containsSameValueAs(gene2))
496+
}
497+
498+
@Test
499+
fun testUnsafeCopyValueFrom_fromPlainObjectGeneClearsAttributes() {
500+
val gene1 = ObjectWithAttributesGene(
501+
name = "node",
502+
fixedFields = listOf(StringGene("id", "1"), StringGene("name", "Alice")),
503+
isFixed = true,
504+
attributeNames = setOf("id")
505+
)
506+
507+
val plain = ObjectGene("node", listOf(StringGene("id", "3"), StringGene("name", "Charlie")))
508+
509+
val result = gene1.unsafeCopyValueFrom(plain)
510+
511+
assertTrue(result)
512+
// gene1 should now have empty attributeNames and match plain ObjectGene
513+
assertTrue(gene1.containsSameValueAs(plain))
514+
}
515+
516+
@Test
517+
fun testUnsafeCopyValueFrom_sameAttributesPreserved() {
518+
val gene1 = ObjectWithAttributesGene(
519+
name = "node",
520+
fixedFields = listOf(StringGene("id", "1"), StringGene("name", "Alice")),
521+
isFixed = true,
522+
attributeNames = setOf("id")
523+
)
524+
525+
val gene2 = ObjectWithAttributesGene(
526+
name = "node",
527+
fixedFields = listOf(StringGene("id", "2"), StringGene("name", "Bob")),
528+
isFixed = true,
529+
attributeNames = setOf("id")
530+
)
531+
532+
val result = gene1.unsafeCopyValueFrom(gene2)
533+
534+
assertTrue(result)
535+
assertTrue(gene1.containsSameValueAs(gene2))
536+
}
537+
470538
@Test
471539
fun testContainsSameValueAs_emptyAttributesVsPlainObjectGene() {
472540
// When attributeNames is empty, ObjectWithAttributesGene produces the same XML as a

0 commit comments

Comments
 (0)