Skip to content

Commit 54827b1

Browse files
authored
Merge pull request DomCR#650 from DomCR/clone-remove-events
clone remove events
2 parents 2aaad73 + 3d711b1 commit 54827b1

File tree

9 files changed

+129
-28
lines changed

9 files changed

+129
-28
lines changed

src/ACadSharp.Tests/CadObjectTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using ACadSharp.Tables;
77
using ACadSharp.XData;
88
using System.Collections.Generic;
9-
using System.Reflection.Emit;
109
using System.Linq;
1110

1211
namespace ACadSharp.Tests
@@ -62,7 +61,7 @@ public void CleanReactorsTest()
6261

6362
[Theory]
6463
[MemberData(nameof(ACadTypes))]
65-
public void Clone(Type t)
64+
public void ExtendedDataCloneTest(Type t)
6665
{
6766
CadObject cadObject = Factory.CreateObject(t);
6867

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ACadSharp.Objects;
1+
using ACadSharp.Extensions;
2+
using ACadSharp.Objects;
23
using System;
34
using Xunit;
45

@@ -7,14 +8,30 @@ namespace ACadSharp.Tests.Objects
78
public abstract class NonGraphicalObjectTests<T>
89
where T : NonGraphicalObject
910
{
11+
[Fact]
12+
public void DetachNameEvent()
13+
{
14+
string name = "custom_name";
15+
T obj = (T)Activator.CreateInstance(typeof(T), name);
16+
17+
CadDictionary dict = new CadDictionary();
18+
dict.Add(obj);
19+
20+
T clone = obj.CloneTyped();
21+
string cloneName = "test-event";
22+
clone.Name = cloneName;
23+
24+
Assert.False(dict.ContainsKey(cloneName));
25+
}
26+
1027
[Fact]
1128
public void InitName()
1229
{
1330
string name = "custom_name";
1431
T obj = (T)Activator.CreateInstance(typeof(T), name);
15-
32+
1633
Assert.NotNull(obj.Name);
1734
Assert.Equal(name, obj.Name);
1835
}
1936
}
20-
}
37+
}

src/ACadSharp.Tests/Tables/TableEntryTests.cs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using ACadSharp.Tables;
1+
using ACadSharp.Extensions;
2+
using ACadSharp.Tables;
23
using ACadSharp.Tests.Common;
34
using System;
5+
using System.Reflection;
46
using Xunit;
57

68
namespace ACadSharp.Tests.Tables
@@ -17,6 +19,52 @@ static TableEntryTests()
1719
}
1820
}
1921

22+
[Fact()]
23+
public void ChangeName()
24+
{
25+
string initialName = "custom_layer";
26+
Layer layer = new Layer(initialName);
27+
28+
CadDocument doc = new CadDocument();
29+
30+
doc.Layers.Add(layer);
31+
32+
layer.Name = "new_name";
33+
34+
Assert.NotNull(doc.Layers[layer.Name]);
35+
Assert.False(doc.Layers.TryGetValue(initialName, out _));
36+
}
37+
38+
[Fact]
39+
public void ChangeNameCloned()
40+
{
41+
string initialName = "custom_layer";
42+
Layer layer = new Layer(initialName);
43+
44+
CadDocument doc = new CadDocument();
45+
46+
doc.Layers.Add(layer);
47+
48+
layer.Name = "new_name";
49+
layer.OnNameChanged += (sender, args) =>
50+
{
51+
throw new Exception();
52+
};
53+
54+
Layer clone = layer.CloneTyped();
55+
clone.Name = "test-event";
56+
57+
Assert.ThrowsAny<Exception>(() => layer.Name = "Hello");
58+
59+
var field = typeof(TableEntry)
60+
.GetField(nameof(TableEntry.OnNameChanged), BindingFlags.Instance | BindingFlags.NonPublic);
61+
62+
var e = field.GetValue(layer) as EventHandler<OnNameChangedArgs>;
63+
Assert.NotEmpty(e.GetInvocationList());
64+
e = field.GetValue(clone) as EventHandler<OnNameChangedArgs>;
65+
Assert.Null(e);
66+
}
67+
2068
[Theory]
2169
[MemberData(nameof(TableEntryTypes))]
2270
public void Clone(Type entryType)
@@ -27,20 +75,20 @@ public void Clone(Type entryType)
2775
CadObjectTestUtils.AssertTableEntryClone(entry, clone);
2876
}
2977

30-
[Fact()]
31-
public void ChangeName()
78+
[Fact]
79+
public void DetachCloneEvents()
3280
{
3381
string initialName = "custom_layer";
3482
Layer layer = new Layer(initialName);
3583

3684
CadDocument doc = new CadDocument();
37-
3885
doc.Layers.Add(layer);
3986

40-
layer.Name = "new_name";
87+
Layer clone = layer.CloneTyped();
88+
string name = "new_name";
89+
clone.Name = name;
4190

42-
Assert.NotNull(doc.Layers[layer.Name]);
43-
Assert.False(doc.Layers.TryGetValue(initialName, out _));
91+
Assert.False(doc.Layers.Contains(name));
4492
}
4593

4694
[Fact()]

src/ACadSharp/ACadSharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<PropertyGroup>
1818
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1919
<PackageReadmeFile>README.md</PackageReadmeFile>
20-
<Version>1.1.18</Version>
20+
<Version>1.1.19</Version>
2121
<PackageOutputPath>../nupkg</PackageOutputPath>
2222
</PropertyGroup>
2323

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ACadSharp.Extensions
8+
{
9+
public static class CadObjectExtensions
10+
{
11+
public static T CloneTyped<T>(this T obj)
12+
where T : CadObject
13+
{
14+
return (T)obj.Clone();
15+
}
16+
}
17+
}

src/ACadSharp/Objects/CadDictionary.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ public bool Remove(string key, out NonGraphicalObject item)
292292
{
293293
item.Owner = null;
294294
OnRemove?.Invoke(this, new CollectionChangedEventArgs(item));
295+
item.OnNameChanged -= this.onEntryNameChanged;
295296
return true;
296297
}
297298

src/ACadSharp/Objects/NonGraphicalObject.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ public abstract class NonGraphicalObject : CadObject, INamedCadObject
1111
{
1212
public event EventHandler<OnNameChangedArgs> OnNameChanged;
1313

14-
/// <inheritdoc/>
15-
public override ObjectType ObjectType => ObjectType.UNLISTED;
16-
1714
/// <inheritdoc/>
1815
/// <remarks>
1916
/// The name of a <see cref="NonGraphicalObject"/> will be used as the name of the entry when the owner is a <see cref="CadDictionary"/>
@@ -29,12 +26,16 @@ public virtual string Name
2926
}
3027
}
3128

29+
/// <inheritdoc/>
30+
public override ObjectType ObjectType => ObjectType.UNLISTED;
31+
3232
private string _name = string.Empty;
3333

3434
/// <summary>
3535
/// Default constructor.
3636
/// </summary>
37-
public NonGraphicalObject() { }
37+
public NonGraphicalObject()
38+
{ }
3839

3940
/// <summary>
4041
/// Initialize a <see cref="NonGraphicalObject"/> with an specific name.
@@ -45,6 +46,14 @@ public NonGraphicalObject(string name)
4546
this._name = name;
4647
}
4748

49+
/// <inheritdoc/>
50+
public override CadObject Clone()
51+
{
52+
NonGraphicalObject clone = (NonGraphicalObject)base.Clone();
53+
clone.OnNameChanged = null;
54+
return clone;
55+
}
56+
4857
/// <inheritdoc/>
4958
public override string ToString()
5059
{
@@ -58,4 +67,4 @@ public override string ToString()
5867
}
5968
}
6069
}
61-
}
70+
}

src/ACadSharp/Tables/Collections/Table.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public T Remove(string key)
107107
{
108108
item.Owner = null;
109109
OnRemove?.Invoke(this, new CollectionChangedEventArgs(item));
110+
item.OnNameChanged -= this.onEntryNameChanged;
110111
return item;
111112
}
112113

src/ACadSharp/Tables/TableEntry.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ public abstract class TableEntry : CadObject, INamedCadObject
1111
/// </summary>
1212
public event EventHandler<OnNameChangedArgs> OnNameChanged;
1313

14-
/// <inheritdoc/>
15-
public override string SubclassMarker => DxfSubclassMarker.TableRecord;
14+
/// <summary>
15+
/// Standard flags
16+
/// </summary>
17+
[DxfCodeValue(70)]
18+
public StandardFlags Flags { get; set; }
1619

1720
/// <summary>
1821
/// Specifies the name of the object
@@ -33,16 +36,11 @@ public virtual string Name
3336
}
3437
}
3538

36-
/// <summary>
37-
/// Standard flags
38-
/// </summary>
39-
[DxfCodeValue(70)]
40-
public StandardFlags Flags { get; set; }
39+
/// <inheritdoc/>
40+
public override string SubclassMarker => DxfSubclassMarker.TableRecord;
4141

4242
protected string name = string.Empty;
4343

44-
internal TableEntry() { }
45-
4644
/// <summary>
4745
/// Default constructor.
4846
/// </summary>
@@ -56,10 +54,21 @@ public TableEntry(string name)
5654
this.Name = name;
5755
}
5856

57+
internal TableEntry()
58+
{ }
59+
60+
/// <inheritdoc/>
61+
public override CadObject Clone()
62+
{
63+
TableEntry clone = (TableEntry)base.Clone();
64+
clone.OnNameChanged = null;
65+
return clone;
66+
}
67+
5968
/// <inheritdoc/>
6069
public override string ToString()
6170
{
6271
return $"{this.ObjectName}:{this.Name}";
6372
}
6473
}
65-
}
74+
}

0 commit comments

Comments
 (0)