@@ -16,15 +16,36 @@ void main() {
1616      'plugins.flutter.io/shared_preferences_android' ,
1717    );
1818
19-     const  Map <String , Object > kTestValues  =  < String , Object > {
19+     const  Map <String , Object > flutterTestValues  =  < String , Object > {
2020      'flutter.String' :  'hello world' ,
2121      'flutter.Bool' :  true ,
2222      'flutter.Int' :  42 ,
2323      'flutter.Double' :  3.14159 ,
2424      'flutter.StringList' :  < String > ['foo' , 'bar' ],
2525    };
26-     // Create a dummy in-memory implementation to back the mocked method channel 
27-     // API to simplify validation of the expected calls. 
26+ 
27+     const  Map <String , Object > prefixTestValues =  < String , Object > {
28+       'prefix.String' :  'hello world' ,
29+       'prefix.Bool' :  true ,
30+       'prefix.Int' :  42 ,
31+       'prefix.Double' :  3.14159 ,
32+       'prefix.StringList' :  < String > ['foo' , 'bar' ],
33+     };
34+ 
35+     const  Map <String , Object > nonPrefixTestValues =  < String , Object > {
36+       'String' :  'hello world' ,
37+       'Bool' :  true ,
38+       'Int' :  42 ,
39+       'Double' :  3.14159 ,
40+       'StringList' :  < String > ['foo' , 'bar' ],
41+     };
42+ 
43+     final  Map <String , Object > allTestValues =  < String , Object > {};
44+ 
45+     allTestValues.addAll (flutterTestValues);
46+     allTestValues.addAll (prefixTestValues);
47+     allTestValues.addAll (nonPrefixTestValues);
48+ 
2849    late  InMemorySharedPreferencesStore  testData;
2950
3051    final  List <MethodCall > log =  < MethodCall > [];
@@ -45,6 +66,12 @@ void main() {
4566        if  (methodCall.method ==  'getAll' ) {
4667          return  testData.getAll ();
4768        }
69+         if  (methodCall.method ==  'getAllWithPrefix' ) {
70+           final  Map <String , Object ?> arguments = 
71+               getArgumentDictionary (methodCall);
72+           final  String  prefix =  arguments['prefix' ]!  as  String ;
73+           return  testData.getAllWithPrefix (prefix);
74+         }
4875        if  (methodCall.method ==  'remove' ) {
4976          final  Map <String , Object ?> arguments = 
5077              getArgumentDictionary (methodCall);
@@ -54,6 +81,12 @@ void main() {
5481        if  (methodCall.method ==  'clear' ) {
5582          return  testData.clear ();
5683        }
84+         if  (methodCall.method ==  'clearWithPrefix' ) {
85+           final  Map <String , Object ?> arguments = 
86+               getArgumentDictionary (methodCall);
87+           final  String  prefix =  arguments['prefix' ]!  as  String ;
88+           return  testData.clearWithPrefix (prefix);
89+         }
5790        final  RegExp  setterRegExp =  RegExp (r'set(.*)' );
5891        final  Match ?  match =  setterRegExp.matchAsPrefix (methodCall.method);
5992        if  (match? .groupCount ==  1 ) {
@@ -77,14 +110,21 @@ void main() {
77110
78111    test ('getAll' , () async  {
79112      store =  SharedPreferencesAndroid ();
80-       testData =  InMemorySharedPreferencesStore .withData (kTestValues);
81-       expect (await  store.getAll (), kTestValues);
82-       expect (log.single.method, 'getAll' );
113+       testData =  InMemorySharedPreferencesStore .withData (allTestValues);
114+       expect (await  store.getAll (), flutterTestValues);
115+       expect (log.single.method, 'getAllWithPrefix' );
116+     });
117+ 
118+     test ('getAllWithPrefix' , () async  {
119+       store =  SharedPreferencesAndroid ();
120+       testData =  InMemorySharedPreferencesStore .withData (allTestValues);
121+       expect (await  store.getAllWithPrefix ('prefix.' ), prefixTestValues);
122+       expect (log.single.method, 'getAllWithPrefix' );
83123    });
84124
85125    test ('remove' , () async  {
86126      store =  SharedPreferencesAndroid ();
87-       testData =  InMemorySharedPreferencesStore .withData (kTestValues );
127+       testData =  InMemorySharedPreferencesStore .withData (allTestValues );
88128      expect (await  store.remove ('flutter.String' ), true );
89129      expect (await  store.remove ('flutter.Bool' ), true );
90130      expect (await  store.remove ('flutter.Int' ), true );
@@ -102,13 +142,13 @@ void main() {
102142    test ('setValue' , () async  {
103143      store =  SharedPreferencesAndroid ();
104144      expect (await  testData.getAll (), isEmpty);
105-       for  (final  String  key in  kTestValues .keys) {
106-         final  Object  value =  kTestValues [key]! ;
145+       for  (final  String  key in  allTestValues .keys) {
146+         final  Object  value =  allTestValues [key]! ;
107147        expect (await  store.setValue (key.split ('.' ).last, key, value), true );
108148      }
109-       expect (await  testData.getAll (), kTestValues );
149+       expect (await  testData.getAll (), flutterTestValues );
110150
111-       expect (log, hasLength (5 ));
151+       expect (log, hasLength (15 ));
112152      expect (log[0 ].method, 'setString' );
113153      expect (log[1 ].method, 'setBool' );
114154      expect (log[2 ].method, 'setInt' );
@@ -118,11 +158,36 @@ void main() {
118158
119159    test ('clear' , () async  {
120160      store =  SharedPreferencesAndroid ();
121-       testData =  InMemorySharedPreferencesStore .withData (kTestValues );
161+       testData =  InMemorySharedPreferencesStore .withData (allTestValues );
122162      expect (await  testData.getAll (), isNotEmpty);
123163      expect (await  store.clear (), true );
124164      expect (await  testData.getAll (), isEmpty);
125-       expect (log.single.method, 'clear' );
165+       expect (log.single.method, 'clearWithPrefix' );
166+     });
167+ 
168+     test ('clearWithPrefix' , () async  {
169+       store =  SharedPreferencesAndroid ();
170+       testData =  InMemorySharedPreferencesStore .withData (allTestValues);
171+ 
172+       expect (await  testData.getAllWithPrefix ('prefix.' ), isNotEmpty);
173+       expect (await  store.clearWithPrefix ('prefix.' ), true );
174+       expect (await  testData.getAllWithPrefix ('prefix.' ), isEmpty);
175+     });
176+ 
177+     test ('getAllWithNoPrefix' , () async  {
178+       store =  SharedPreferencesAndroid ();
179+       testData =  InMemorySharedPreferencesStore .withData (allTestValues);
180+ 
181+       expect (await  testData.getAllWithPrefix ('' ), hasLength (15 ));
182+     });
183+ 
184+     test ('clearWithNoPrefix' , () async  {
185+       store =  SharedPreferencesAndroid ();
186+       testData =  InMemorySharedPreferencesStore .withData (allTestValues);
187+ 
188+       expect (await  testData.getAllWithPrefix ('' ), isNotEmpty);
189+       expect (await  store.clearWithPrefix ('' ), true );
190+       expect (await  testData.getAllWithPrefix ('' ), isEmpty);
126191    });
127192  });
128193}
0 commit comments