Skip to content

Commit

Permalink
Rename DeepCopyDynamicProperties -> DeepCopyProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
kMutagene committed Dec 17, 2024
1 parent b216d77 commit 32e5064
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/DynamicObj/DynamicObj.fs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ type DynamicObj() =
|> Seq.map (fun kv -> kv.Key)

/// <summary>
/// Copies all dynamic members of the source DynamicObj to the target DynamicObj.
/// Copies all dynamic properties to a target `DynamicObj` instance without trying to prevent reference equality.
///
/// Note that this function does not attempt to do any deep copying.
/// The dynamic properties of the source will be copied as references to the target.
Expand All @@ -245,7 +245,7 @@ type DynamicObj() =
)

/// <summary>
/// Returns a new DynamicObj with only the dynamic properties of the original DynamicObj (sans instance properties).
/// Copies all dynamic properties to a new `DynamicObj` instance without trying to prevent reference equality.
///
/// Note that this function does not attempt to do any deep copying.
/// The dynamic properties of the source will be copied as references to the target.
Expand All @@ -257,7 +257,7 @@ type DynamicObj() =
target

/// <summary>
/// Attempts to deep copy the properties of the DynamicObj onto the target.
/// Recursively deep copies **all** (static and dynamic) properties to a **target** `DynamicObj` instance (or derived class). Reinstantiation - and therefore prevention of reference equality - is possible for `DynamicObj`, `array|list|ResizeArray&lt;DynamicObj&gt;`, and classes implementing `System.Icloneable`
///
/// As many properties as possible are re-instantiated as new objects, meaning the
/// copy has as little reference equal properties as possible.
Expand Down Expand Up @@ -285,7 +285,7 @@ type DynamicObj() =
/// </summary>
/// <param name="target">The target object to copy dynamic members to</param>
/// <param name="overWrite">Whether existing properties on the target object will be overwritten</param>
member this.DeepCopyDynamicPropertiesTo(target:#DynamicObj, ?overWrite) =
member this.DeepCopyPropertiesTo(target:#DynamicObj, ?overWrite) =
let overWrite = defaultArg overWrite false

this.GetProperties(true)
Expand All @@ -297,7 +297,7 @@ type DynamicObj() =
)

/// <summary>

Check warning on line 299 in src/DynamicObj/DynamicObj.fs

View workflow job for this annotation

GitHub Actions / build-and-deploy-docs

This XML comment is invalid: unknown parameter 'target'

Check warning on line 299 in src/DynamicObj/DynamicObj.fs

View workflow job for this annotation

GitHub Actions / build-and-deploy-docs

This XML comment is invalid: unknown parameter 'overWrite'

Check warning on line 299 in src/DynamicObj/DynamicObj.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This XML comment is invalid: unknown parameter 'target'

Check warning on line 299 in src/DynamicObj/DynamicObj.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This XML comment is invalid: unknown parameter 'overWrite'

Check warning on line 299 in src/DynamicObj/DynamicObj.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This XML comment is invalid: unknown parameter 'target'

Check warning on line 299 in src/DynamicObj/DynamicObj.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This XML comment is invalid: unknown parameter 'overWrite'
/// Attempts to perform a deep copy of the DynamicObj.
/// Recursively deep copy a `DynamicObj` instance (or derived class) with **all** (static and dynamic) properties. Reinstantiation - and therefore prevention of reference equality - is possible for `DynamicObj`, `array|list|ResizeArray&lt;DynamicObj&gt;`, and classes implementing `System.Icloneable`
///
/// On the deep copy, as many properties as possible are re-instantiated as new objects, meaning the
/// copy has as little reference equal properties as possible.
Expand Down Expand Up @@ -325,7 +325,7 @@ type DynamicObj() =
/// </summary>
/// <param name="target">The target object to copy dynamic members to</param>
/// <param name="overWrite">Whether existing properties on the target object will be overwritten</param>
member this.DeepCopyDynamicProperties() = CopyUtils.tryDeepCopyObj this
member this.DeepCopyProperties() = CopyUtils.tryDeepCopyObj this

#if !FABLE_COMPILER
// Some necessary overrides for methods inherited from System.Dynamic.DynamicObject()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module DeepCopyDynamicProperties
module DeepCopyProperties

open Fable.Pyxpecto
open DynamicObj
open TestUtils

let tests_DeepCopyDynamicProperties = testList "DeepCopyDynamicProperties" [
let tests_DeepCopyProperties = testList "DeepCopyProperties" [

testList "DynamicObj" [
testList "Cloneable dynamic properties" [
Expand Down Expand Up @@ -130,31 +130,31 @@ let tests_DeepCopyDynamicProperties = testList "DeepCopyDynamicProperties" [
testCase "can unbox copy as DerivedClassCloneable" <| fun _ ->
Expect.pass (
let original = DerivedClassCloneable(stat = "stat", dyn = "dyn")
let clone = original.DeepCopyDynamicProperties() |> unbox<DerivedClassCloneable>
let clone = original.DeepCopyProperties() |> unbox<DerivedClassCloneable>
()
)
testCase "copy is of type DerivedClassCloneable" <| fun _ ->
let original = DerivedClassCloneable(stat = "stat", dyn = "dyn")
let clone = original.DeepCopyDynamicProperties() |> unbox<DerivedClassCloneable>
let clone = original.DeepCopyProperties() |> unbox<DerivedClassCloneable>
Expect.equal (clone.GetType()) typeof<DerivedClassCloneable> "Clone is of type DerivedClassCloneable"
ptestCase "copy has NO instance prop as dynamic prop" <| fun _ ->
let original = DerivedClassCloneable(stat = "stat", dyn = "dyn")
let clone = original.DeepCopyDynamicProperties() |> unbox<DerivedClassCloneable>
let clone = original.DeepCopyProperties() |> unbox<DerivedClassCloneable>
let clonedProps = clone.GetProperties(false) |> Seq.map (fun p -> p.Key, p.Value)
Expect.sequenceEqual clonedProps ["dyn", "dyn"] "Clone should have no dynamic properties"
testCase "copy has static and dynamic props of original" <| fun _ ->
let original = DerivedClassCloneable(stat = "stat", dyn = "dyn")
let clone = original.DeepCopyDynamicProperties() |> unbox<DerivedClassCloneable>
let clone = original.DeepCopyProperties() |> unbox<DerivedClassCloneable>
Expect.equal clone original "Clone and original should be equal"
Expect.equal (clone.stat) (original.stat) "Clone should have static prop from derived class"
Expect.equal (clone |> DynObj.getNestedPropAs<string> ["dyn"]) (original |> DynObj.getNestedPropAs<string> ["dyn"]) "Clone should have dynamic prop from derived class"
testCase "can use instance method on copied derived class" <| fun _ ->
let original = DerivedClassCloneable(stat = "stat", dyn = "dyn")
let clone = original.DeepCopyDynamicProperties() |> unbox<DerivedClassCloneable>
let clone = original.DeepCopyProperties() |> unbox<DerivedClassCloneable>
Expect.pass (clone.PrintStat())
testCase "instance method on copied derived class returns correct value" <| fun _ ->
let original = DerivedClassCloneable(stat = "stat", dyn = "dyn")
let clone = original.DeepCopyDynamicProperties() |> unbox<DerivedClassCloneable>
let clone = original.DeepCopyProperties() |> unbox<DerivedClassCloneable>
Expect.equal (clone.FormatStat()) "stat: stat" "instance method should return correct value"
]
]
Expand All @@ -165,21 +165,21 @@ let tests_DeepCopyDynamicProperties = testList "DeepCopyDynamicProperties" [
// this test is transpiled as Expect_throws(() => {} and can never fail, so let's just test it in F# for now
testCase "Cannot unbox clone as original type" <| fun _ ->
let original = DerivedClass(stat = "stat", dyn = "dyn")
let clone = original.DeepCopyDynamicProperties()
let clone = original.DeepCopyProperties()
let unboxMaybe() = clone |> unbox<DerivedClass> |> ignore
Expect.throws unboxMaybe "Clone cannot be unboxed as DerivedClass"
#endif

testCase "copy has instance prop as dynamic prop" <| fun _ ->
let original = DerivedClass(stat = "stat", dyn = "dyn")
let clone = original.DeepCopyDynamicProperties() |> unbox<DynamicObj>
let clone = original.DeepCopyProperties() |> unbox<DynamicObj>
let clonedProps = clone.GetProperties(false) |> Seq.map (fun p -> p.Key, p.Value)
Expect.containsAll clonedProps ["stat","stat"] "Clone should have static prop from derived class as dynamic prop"
testCase "mutable instance prop is reference equal on clone" <| fun _ ->
let original = DerivedClass(stat = "stat", dyn = "dyn")
let mut = MutableClass("initial")
original.SetProperty("mutable", mut)
let clone = original.DeepCopyDynamicProperties() |> unbox<DynamicObj>
let clone = original.DeepCopyProperties() |> unbox<DynamicObj>
mut.stat <- "mutated"
let originalProp = original |> DynObj.getNestedPropAs<MutableClass>["mutable"]
let clonedProp = clone |> DynObj.getNestedPropAs<MutableClass> ["mutable"]
Expand Down Expand Up @@ -207,7 +207,7 @@ let tests_DeepCopyDynamicProperties = testList "DeepCopyDynamicProperties" [
let original = DerivedClass(stat = "stat", dyn = "dyn")
bulkMutate originalProps original

let clone = original.DeepCopyDynamicProperties() |> unbox<DynamicObj>
let clone = original.DeepCopyProperties() |> unbox<DynamicObj>
let mutatedProps = [
"int", box 2
"float", box 2.0
Expand Down Expand Up @@ -258,7 +258,7 @@ let tests_DeepCopyDynamicProperties = testList "DeepCopyDynamicProperties" [
let inner = DynamicObj() |> DynObj.withProperty "inner int" 2
let original = DerivedClass(stat = "stat", dyn = "dyn")
original.SetProperty("inner", inner)
let clone = original.DeepCopyDynamicProperties() |> unbox<DynamicObj>
let clone = original.DeepCopyProperties() |> unbox<DynamicObj>
inner.SetProperty("inner int", 1)

Expect.equal (original |> DynObj.getNestedPropAs<int> ["inner";"inner int"]) 1 "Original should have mutated properties"
Expand All @@ -271,7 +271,7 @@ let tests_DeepCopyDynamicProperties = testList "DeepCopyDynamicProperties" [
let arr = [|item1; item2; item3|]
let original = DerivedClass(stat = "stat", dyn = "dyn")
original.SetProperty("arr", arr)
let clone = original.DeepCopyDynamicProperties() |> unbox<DynamicObj>
let clone = original.DeepCopyProperties() |> unbox<DynamicObj>
item1.SetProperty("item", -1)
item2.SetProperty("item", -1)
item3.SetProperty("item", -1)
Expand All @@ -287,7 +287,7 @@ let tests_DeepCopyDynamicProperties = testList "DeepCopyDynamicProperties" [
let l = [item1; item2; item3]
let original = DerivedClass(stat = "stat", dyn = "dyn")
original.SetProperty("list", l)
let clone = original.DeepCopyDynamicProperties() |> unbox<DynamicObj>
let clone = original.DeepCopyProperties() |> unbox<DynamicObj>
item1.SetProperty("item", -1)
item2.SetProperty("item", -1)
item3.SetProperty("item", -1)
Expand All @@ -303,7 +303,7 @@ let tests_DeepCopyDynamicProperties = testList "DeepCopyDynamicProperties" [
let r = ResizeArray([item1; item2; item3])
let original = DerivedClass(stat = "stat", dyn = "dyn")
original.SetProperty("resizeArr", r)
let clone = original.DeepCopyDynamicProperties() |> unbox<DynamicObj>
let clone = original.DeepCopyProperties() |> unbox<DynamicObj>
item1.SetProperty("item", -1)
item2.SetProperty("item", -1)
item3.SetProperty("item", -1)
Expand All @@ -317,7 +317,7 @@ let tests_DeepCopyDynamicProperties = testList "DeepCopyDynamicProperties" [
let item = MutableClass("initial")
let original = DerivedClass(stat = "stat", dyn = "dyn")
original.SetProperty("item", item)
let clone = original.DeepCopyDynamicProperties() |> unbox<DynamicObj>
let clone = original.DeepCopyProperties() |> unbox<DynamicObj>
item.stat <- "mutated"
let originalProp = original |> DynObj.getNestedPropAs<MutableClass>["item"]
let clonedProp = clone |> DynObj.getNestedPropAs<MutableClass> ["item"]
Expand Down
2 changes: 1 addition & 1 deletion tests/DynamicObject.Tests/DynamicObj/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ let main = testList "DynamicObj (Class)" [

ShallowCopyDynamicPropertiesTo.tests_ShallowCopyDynamicPropertiesTo
ShallowCopyDynamicProperties.tests_ShallowCopyDynamicProperties
DeepCopyDynamicProperties.tests_DeepCopyDynamicProperties
DeepCopyProperties.tests_DeepCopyProperties
]
2 changes: 1 addition & 1 deletion tests/DynamicObject.Tests/DynamicObject.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<Compile Include="DynamicObj\GetProperties.fs" />
<Compile Include="DynamicObj\ShallowCopyDynamicPropertiesTo.fs" />
<Compile Include="DynamicObj\ShallowCopyDynamicProperties.fs" />
<Compile Include="DynamicObj\DeepCopyDynamicProperties.fs" />
<Compile Include="DynamicObj\DeepCopyProperties.fs" />
<Compile Include="DynamicObj\Main.fs" />
<Compile Include="ReflectionUtils.fs" />
<Compile Include="Inheritance.fs" />
Expand Down
2 changes: 1 addition & 1 deletion tests/DynamicObject.Tests/TestUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let constructDeepCopiedClone<'T> (props: seq<string*obj>) =
let original = DynamicObj()
props
|> Seq.iter (fun (propertyName, propertyValue) -> original.SetProperty(propertyName, propertyValue))
let clone = original.DeepCopyDynamicProperties()
let clone = original.DeepCopyProperties()
original, clone |> unbox<'T>

let bulkMutate (props: seq<string*obj>) (dyn: #DynamicObj) =
Expand Down

0 comments on commit 32e5064

Please sign in to comment.