aboutsummaryrefslogtreecommitdiff
path: root/src/test/kotlin/com/replaymod/gradle/remap/mapper/TestMixinInjections.kt
blob: fd4c1cb85f8b0bd964c6d304c36daddd03f3693b (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.replaymod.gradle.remap.mapper

import com.replaymod.gradle.remap.util.TestData
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import org.junit.jupiter.api.Test

class TestMixinInjections {
    private fun remaps(annotation: String) {
        TestData.remap("""
            @org.spongepowered.asm.mixin.Mixin(a.pkg.A.class)
            class MixinA {
                @$annotation(method = "aMethod")
                private void test() {}
                @$annotation(method = "aInterfaceMethod")
                private void testInterface() {}
            }
        """.trimIndent()) shouldBe """
            @org.spongepowered.asm.mixin.Mixin(b.pkg.B.class)
            class MixinA {
                @$annotation(method = "bMethod")
                private void test() {}
                @$annotation(method = "bInterfaceMethod")
                private void testInterface() {}
            }
        """.trimIndent()
    }

    @Test
    fun `remaps @Inject method`() = remaps("org.spongepowered.asm.mixin.injection.Inject")

    @Test
    fun `remaps @ModifyArg method`() = remaps("org.spongepowered.asm.mixin.injection.ModifyArg")

    @Test
    fun `remaps @ModifyArgs method`() = remaps("org.spongepowered.asm.mixin.injection.ModifyArgs")

    @Test
    fun `remaps @ModifyConstant method`() = remaps("org.spongepowered.asm.mixin.injection.ModifyConstant")

    @Test
    fun `remaps @ModifyVariable method`() = remaps("org.spongepowered.asm.mixin.injection.ModifyVariable")

    @Test
    fun `remaps @Redirect method`() = remaps("org.spongepowered.asm.mixin.injection.Redirect")

    @Test
    fun `throws when injecting into ambiguous method`() {
        val (_, errors) = TestData.remapWithErrors("""
            @org.spongepowered.asm.mixin.Mixin(a.pkg.A.class)
            class MixinA {
                @org.spongepowered.asm.mixin.injection.Inject(method = "aOverloaded")
                private void test() {}
            }
        """.trimIndent())
        errors shouldHaveSize 1
        val (line, error) = errors[0]
        line shouldBe 2
        error shouldContain "aOverloaded"
        error shouldContain "(I)V"
        error shouldContain "(Z)V"
    }

    @Test
    fun `remaps qualified method`() {
        TestData.remap("""
            @org.spongepowered.asm.mixin.Mixin(a.pkg.A.class)
            class MixinA {
                @org.spongepowered.asm.mixin.injection.Inject(method = "aOverloaded()V")
                private void test() {}
                @org.spongepowered.asm.mixin.injection.Inject(method = "aOverloaded(I)V")
                private void testArg() {}
            }
        """.trimIndent()) shouldBe """
            @org.spongepowered.asm.mixin.Mixin(b.pkg.B.class)
            class MixinA {
                @org.spongepowered.asm.mixin.injection.Inject(method = "bOverloaded()V")
                private void test() {}
                @org.spongepowered.asm.mixin.injection.Inject(method = "bOverloaded(I)V")
                private void testArg() {}
            }
        """.trimIndent()
    }

    @Test
    fun `remaps qualified method argument`() {
        TestData.remap("""
            @org.spongepowered.asm.mixin.Mixin(a.pkg.A.class)
            class MixinA {
                @org.spongepowered.asm.mixin.injection.Inject(method = "commonOverloaded(La/pkg/A;)V")
                private void test() {}
            }
        """.trimIndent()) shouldBe """
            @org.spongepowered.asm.mixin.Mixin(b.pkg.B.class)
            class MixinA {
                @org.spongepowered.asm.mixin.injection.Inject(method = "commonOverloaded(Lb/pkg/B;)V")
                private void test() {}
            }
        """.trimIndent()
    }

    @Test
    fun `remaps qualified method argument without mappings for target`() {
        TestData.remap("""
            @org.spongepowered.asm.mixin.Mixin(a.pkg.A.class)
            class MixinA {
                @org.spongepowered.asm.mixin.injection.Inject(method = "unmappedOverloaded(La/pkg/A;)V")
                private void test() {}
            }
        """.trimIndent()) shouldBe """
            @org.spongepowered.asm.mixin.Mixin(b.pkg.B.class)
            class MixinA {
                @org.spongepowered.asm.mixin.injection.Inject(method = "unmappedOverloaded(Lb/pkg/B;)V")
                private void test() {}
            }
        """.trimIndent()
    }

    @Test
    fun `remaps constructor target`() {
        TestData.remap("""
            @org.spongepowered.asm.mixin.Mixin(a.pkg.A.class)
            class MixinA {
                @org.spongepowered.asm.mixin.injection.Inject(method = "<init>()V")
                private void test() {}
                @org.spongepowered.asm.mixin.injection.Inject(method = "<init>(La/pkg/A;)V")
                private void testArg() {}
            }
        """.trimIndent()) shouldBe """
            @org.spongepowered.asm.mixin.Mixin(b.pkg.B.class)
            class MixinA {
                @org.spongepowered.asm.mixin.injection.Inject(method = "<init>()V")
                private void test() {}
                @org.spongepowered.asm.mixin.injection.Inject(method = "<init>(Lb/pkg/B;)V")
                private void testArg() {}
            }
        """.trimIndent()
    }

    @Test
    fun `remaps @At target`() {
        TestData.remap("""
            import org.spongepowered.asm.mixin.injection.At;
            import org.spongepowered.asm.mixin.injection.Inject;
            @org.spongepowered.asm.mixin.Mixin(a.pkg.A.class)
            class MixinA {
                @Inject(method = "aMethod", at = @At(target = "La/pkg/A;aInterfaceMethod()V"))
                private void test() {}
            }
        """.trimIndent()) shouldBe """
            import org.spongepowered.asm.mixin.injection.At;
            import org.spongepowered.asm.mixin.injection.Inject;
            @org.spongepowered.asm.mixin.Mixin(b.pkg.B.class)
            class MixinA {
                @Inject(method = "bMethod", at = @At(target = "Lb/pkg/B;bInterfaceMethod()V"))
                private void test() {}
            }
        """.trimIndent()
    }

    @Test
    fun `remaps @At target without mappings for target`() {
        TestData.remap("""
            import org.spongepowered.asm.mixin.injection.At;
            import org.spongepowered.asm.mixin.injection.Inject;
            @org.spongepowered.asm.mixin.Mixin(a.pkg.A.class)
            class MixinA {
                @Inject(method = "aMethod", at = @At(target = "La/pkg/A;unmappedOverloaded(La/pkg/A;)V"))
                private void test() {}
            }
        """.trimIndent()) shouldBe """
            import org.spongepowered.asm.mixin.injection.At;
            import org.spongepowered.asm.mixin.injection.Inject;
            @org.spongepowered.asm.mixin.Mixin(b.pkg.B.class)
            class MixinA {
                @Inject(method = "bMethod", at = @At(target = "Lb/pkg/B;unmappedOverloaded(Lb/pkg/B;)V"))
                private void test() {}
            }
        """.trimIndent()
    }

    @Test
    fun `remaps @At target in constant`() {
        TestData.remap("""
            import org.spongepowered.asm.mixin.injection.At;
            import org.spongepowered.asm.mixin.injection.Inject;
            @org.spongepowered.asm.mixin.Mixin(a.pkg.A.class)
            class MixinA {
                private static final String TARGET = "La/pkg/A;aInterfaceMethod()V";
                @Inject(method = "aMethod", at = @At(target = TARGET))
                private void test1() {}
                @Inject(method = "aMethod", at = @At(target = TARGET))
                private void test2() {}
            }
        """.trimIndent()) shouldBe """
            import org.spongepowered.asm.mixin.injection.At;
            import org.spongepowered.asm.mixin.injection.Inject;
            @org.spongepowered.asm.mixin.Mixin(b.pkg.B.class)
            class MixinA {
                private static final String TARGET = "Lb/pkg/B;bInterfaceMethod()V";
                @Inject(method = "bMethod", at = @At(target = TARGET))
                private void test1() {}
                @Inject(method = "bMethod", at = @At(target = TARGET))
                private void test2() {}
            }
        """.trimIndent()
    }
}