aboutsummaryrefslogtreecommitdiffstats
path: root/tests/Test_Utils/Test_LazyFactory.Get.cs
blob: 26b29e3a688de97f965a04bd33e6b2db1f982fad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using Qt.DotNet.Utils;

namespace Test_Utils
{
    public partial class Test_LazyFactory
    {
        // 1. Instance property default behavior
        [TestMethod]
        public void Get_InstanceProperty_DefaultsToDefaultValue_PerInstance()
        {
            var factory = new LazyFactory();
            var p1 = new Person { Age = 99 };
            Assert.AreEqual(0, factory.Get(() => p1.Age));

            var person = new Person { Age = 50 };
            Assert.AreEqual(0, factory.Get(() => person.Age));
        }

        // 2. Instance property with inline initFunc
        [TestMethod]
        public void Get_InstanceProperty_WithInitFunc_IsolatedPerInstance()
        {
            var factory = new LazyFactory();
            var person = new Person();
            Assert.AreEqual("Alice", factory.Get(() => person.Name, () => "Alice"));
            // Subsequent initFunc ignored
            Assert.AreEqual("Alice", factory.Get(() => person.Name, () => "Bob"));

            var person2 = new Person();
            Assert.IsNull(factory.Get(() => person2.Name));
            Assert.AreEqual("Bob", factory.Get(() => person2.Name, () => "Bob"));
        }

        // 3. Static property default behavior
        [TestMethod]
        public void Get_StaticProperty_DefaultsToDefaultValue()
        {
            var factory = new LazyFactory();
            Assert.IsFalse(factory.Get(() => AppSettings.IsFeatureEnabled));
            Assert.IsFalse(factory.Get(() => AppSettings.IsFeatureEnabled));
        }

        // 4. Static property with initFunc
        [TestMethod]
        public void Get_StaticProperty_WithInitFunc_IgnoredAfterFirstCall()
        {
            var factory = new LazyFactory();
            Assert.AreEqual(0.75, factory.Get(() => AppSettings.Threshold, () => 0.75));
            Assert.AreEqual(0.75, factory.Get(() => AppSettings.Threshold, () => 0.25));
        }

        // 5. Nested instance property behavior
        [TestMethod]
        public void Get_NestedInstanceProperty_IsolatedPerInstance()
        {
            var factory = new LazyFactory();
            var order1 = new Order
            {
                Customer = new Customer
                {
                    Address = new Address
                    {
                        City = "X"
                    }
                }
            };
            Assert.IsNull(factory.Get(() => order1.Customer.Address.City));
            Assert.AreEqual("XX", factory.Get(() => order1.Customer.Address.City, () => "XX"));

            var order2 = new Order
            {
                Customer = new Customer
                {
                    Address = new Address
                    {
                        City = "Y"
                    }
                }
            };
            Assert.IsNull(factory.Get(() => order2.Customer.Address.City));
            Assert.AreEqual("YY", factory.Get(() => order2.Customer.Address.City, () => "YY"));
        }

        // 6. Method-group initFunc
        [TestMethod]
        public void Get_MethodGroupInitFunc_WorksPerInstance()
        {
            var factory = new LazyFactory();
            var person = new Person();
            var expected = new DateTime(2025, 1, 1);
            Assert.AreEqual(expected, factory.Get(() => person.BirthDate, FetchReleaseDate));

            var person2 = new Person();
            Assert.AreEqual(expected, factory.Get(() => person2.BirthDate, FetchReleaseDate));
        }

        // 7. Explicit generic type parameter
        [TestMethod]
        public void Get_ExplicitGenericParameter_IsolatedPerInstance()
        {
            var factory = new LazyFactory();
            var p = new Person();
            Assert.AreEqual(42, factory.Get(() => p.Score, () => 42));

            var pB = new Person();
            Assert.AreEqual(0, factory.Get(() => pB.Score));
        }

        // 8. Capturing local variable in initFunc
        [TestMethod]
        public void Get_CapturedLocalVariable_IsolatedPerInstance()
        {
            var factory = new LazyFactory();
            const int x = 10;
            var person = new Person();
            Assert.AreEqual(30, factory.Get(() => person.Score, () => x * 3));

            var parson2 = new Person();
            Assert.AreEqual(0, factory.Get(() => parson2.Score));
        }

        // 9. Fallback via MethodCall -> compile & invoke
        [TestMethod]
        public void Get_FallbackMethodCall_IsolatedPerInstance()
        {
            var factory = new LazyFactory();

            valueHolder = new ValueHolder { Value = 123 };
            Assert.AreEqual(123, factory.Get(() => GetHolder()!.Value, () => 999));

            valueHolder = new ValueHolder { Value = 456 };
            Assert.AreEqual(456, factory.Get(() => GetHolder()!.Value, () => 888));
        }
    }
}