1+ /*
2+ * Code hard forked from:
3+ * https://github.com/revanced/revanced-library/tree/06733072045c8016a75f232dec76505c0ba2e1cd
4+ */
5+
6+ package app.morphe.patcher.patch
7+
8+ import kotlin.test.Test
9+ import kotlin.test.assertEquals
10+
11+ internal class MostCommonCompatibleVersionsTest {
12+ private val patches =
13+ arrayOf(
14+ newPatch(" some.package" , setOf (" a" )) { stringOption(" string" , " value" ) },
15+ newPatch(" some.package" , setOf (" a" , " b" ), use = false ),
16+ newPatch(" some.package" , setOf (" a" , " b" , " c" ), use = false ),
17+ newPatch(" some.other.package" , setOf (" b" ), use = false ),
18+ newPatch(" some.other.package" , setOf (" b" , " c" )) { booleanOption(" bool" , true ) },
19+ newPatch(" some.other.package" , setOf (" b" , " c" , " d" )),
20+ newPatch(" some.other.other.package" ) { intsOption(" intArray" , listOf (1 , 2 , 3 )) },
21+ newPatch(" some.other.other.package" , setOf (" a" )),
22+ newPatch(" some.other.other.package" , setOf (" b" )),
23+ newPatch(" some.other.other.other.package" , use = false ),
24+ newPatch(" some.other.other.other.package" , use = false ),
25+ ).toSet()
26+
27+ @Test
28+ fun `empty because package is incompatible with any version` () {
29+ assertEqualsVersions(
30+ expected = emptyMap(),
31+ patches = setOf (newPatch(" some.package" , emptySet(), use = true )),
32+ compatiblePackageNames = setOf (" some.package" ),
33+ )
34+ }
35+
36+ @Test
37+ fun `empty list of versions because package is unconstrained to any version` () {
38+ assertEqualsVersions(
39+ expected = mapOf (" some.package" to linkedMapOf()),
40+ patches = setOf (newPatch(" some.package" )),
41+ compatiblePackageNames = setOf (" some.package" ),
42+ countUnusedPatches = true ,
43+ )
44+ }
45+
46+ @Test
47+ fun `empty because no known package was supplied` () {
48+ assertEqualsVersions(
49+ expected = emptyMap(),
50+ patches,
51+ compatiblePackageNames = setOf (" unknown.package" ),
52+ )
53+ }
54+
55+ @Test
56+ fun `common versions correctly ordered for each package` () {
57+ fun assertEqualsExpected (compatiblePackageNames : Set <String >? ) =
58+ assertEqualsVersions(
59+ expected =
60+ mapOf (
61+ " some.package" to linkedMapOf(" a" to 3 , " b" to 2 , " c" to 1 ),
62+ " some.other.package" to linkedMapOf(" b" to 3 , " c" to 2 , " d" to 1 ),
63+ " some.other.other.package" to linkedMapOf(" a" to 1 , " b" to 1 ),
64+ " some.other.other.other.package" to linkedMapOf(),
65+ ),
66+ patches,
67+ compatiblePackageNames,
68+ countUnusedPatches = true ,
69+ )
70+
71+ assertEqualsExpected(
72+ compatiblePackageNames =
73+ setOf (
74+ " some.package" ,
75+ " some.other.package" ,
76+ " some.other.other.package" ,
77+ " some.other.other.other.package" ,
78+ ),
79+ )
80+
81+ assertEqualsExpected(
82+ compatiblePackageNames = null ,
83+ )
84+ }
85+
86+ @Test
87+ fun `common versions correctly ordered for each package without counting unused patches` () {
88+ assertEqualsVersions(
89+ expected =
90+ mapOf (
91+ " some.package" to linkedMapOf(" a" to 1 ),
92+ " some.other.package" to linkedMapOf(" b" to 2 , " c" to 2 , " d" to 1 ),
93+ " some.other.other.package" to linkedMapOf(" a" to 1 , " b" to 1 ),
94+ ),
95+ patches,
96+ compatiblePackageNames =
97+ setOf (
98+ " some.package" ,
99+ " some.other.package" ,
100+ " some.other.other.package" ,
101+ " some.other.other.other.package" ,
102+ ),
103+ countUnusedPatches = false ,
104+ )
105+ }
106+
107+ @Test
108+ fun `return 'a' because it is the most common version` () {
109+ val patches =
110+ arrayOf(" a" , " a" , " c" , " d" , " a" , " b" , " c" , " d" , " a" , " b" , " c" , " d" )
111+ .map { version -> newPatch(" some.package" , setOf (version)) }
112+ .toSet()
113+
114+ assertEqualsVersion(" a" , patches, " some.package" )
115+ }
116+
117+ @Test
118+ fun `return null because no patches were supplied` () {
119+ assertEqualsVersion(null , emptySet<BytecodePatch >(), " some.package" )
120+ }
121+
122+ @Test
123+ fun `return null because no patch is compatible with the supplied package name` () {
124+ val patches = setOf (newPatch(" some.package" , setOf (" a" )))
125+
126+ assertEqualsVersion(null , patches, " other.package" )
127+ }
128+
129+ @Test
130+ fun `return null because no compatible package is constrained to a version` () {
131+ val patches =
132+ setOf (
133+ newPatch(" other.package" ),
134+ newPatch(" other.package" ),
135+ )
136+
137+ assertEqualsVersion(null , patches, " other.package" )
138+ }
139+
140+ private fun assertEqualsVersions (
141+ expected : PackageNameMap ,
142+ patches : Set <Patch <* >>,
143+ compatiblePackageNames : Set <String >? ,
144+ countUnusedPatches : Boolean = false,
145+ ) = assertEquals(
146+ expected,
147+ patches.mostCommonCompatibleVersions(compatiblePackageNames, countUnusedPatches),
148+ )
149+
150+ private fun assertEqualsVersion (
151+ expected : String? ,
152+ patches : Set <Patch <* >>,
153+ compatiblePackageName : String ,
154+ ) {
155+ assertEquals(
156+ expected,
157+ patches.mostCommonCompatibleVersions(setOf (compatiblePackageName))
158+ .entries.firstOrNull()?.value?.keys?.firstOrNull(),
159+ )
160+ }
161+
162+ private fun newPatch (
163+ packageName : String ,
164+ versions : Set <String >? = null,
165+ use : Boolean = true,
166+ options : PatchBuilder <* >.() -> Unit = {},
167+ ) = bytecodePatch(
168+ name = " test" ,
169+ use = use,
170+ ) {
171+ if (versions == null ) {
172+ compatibleWith(packageName)
173+ } else {
174+ compatibleWith(
175+ if (versions.isEmpty()) {
176+ packageName()
177+ } else {
178+ packageName(* versions.toTypedArray())
179+ },
180+ )
181+ }
182+
183+ options()
184+ }
185+ }
0 commit comments