1 // 2 // Copyright (c) 2010-2019 Antmicro 3 // 4 // This file is licensed under the MIT License. 5 // Full license text is available in 'licenses/MIT.txt'. 6 // 7 using System; 8 using NUnit.Framework; 9 using System.Reflection; 10 using Antmicro.Renode.Utilities.Collections; 11 using System.Linq; 12 using System.Collections.Generic; 13 14 namespace Antmicro.Renode.Utilities 15 { 16 [TestFixture] 17 public class TypeExtensionsCacheTests 18 { 19 [SetUp] Init()20 public void Init() 21 { 22 var cacheField = typeof(TypeExtensions).GetField("cache", BindingFlags.Static | BindingFlags.NonPublic); 23 var cache = cacheField.GetValue(null); 24 Cache = (SimpleCache)cache; 25 } 26 27 [TestCaseSource("testCases")] 28 [Test] TypeExtensionsCacheTest(Tuple<int, Action> tuple)29 public void TypeExtensionsCacheTest(Tuple<int, Action> tuple) 30 { 31 Cache.ClearCache(); 32 var shouldMiss = tuple.Item1; 33 var action = tuple.Item2; 34 action(); 35 action(); 36 Assert.AreEqual(Cache.CacheMisses, shouldMiss); 37 } 38 39 [Test] AreAllPublicMethodTested()40 public void AreAllPublicMethodTested() 41 { 42 var methodsCounts = typeof(TypeExtensions).GetMethods(BindingFlags.Public | BindingFlags.Static).Count(); 43 Assert.AreEqual(methodsCounts, testCases.Count); 44 } 45 46 private static readonly List<Tuple<int, Action>> testCases = new List<Tuple<int, Action>> 47 { 48 new Tuple<int, Action>(1, () => methodInfo.IsStatic()), 49 new Tuple<int, Action>(2, () => methodInfo.IsCallable()), //first condition in method is false, so it does not check IsBaseCallable -> so the score is 2 not 3 50 new Tuple<int, Action>(3, () => propertyInfo.IsCallable()), 51 new Tuple<int, Action>(4, () => fieldInfo.IsCallable()), //recursive call of IsTypeConvertible inside this method, so it's 4 instead 3 52 new Tuple<int, Action>(2, () => propertyInfo.IsCallableIndexer()), //first condition in method is false, so it does not check IsBaseCallable -> so the score is 2 not 3 53 new Tuple<int, Action>(2, () => methodInfo.IsExtensionCallable()), //first condition in method is false, so it does not check IsTypeConvertible -> so the score is 2 not 3 54 new Tuple<int, Action>(1, () => propertyInfo.IsCurrentlyGettable(BindingFlags.Default)), 55 new Tuple<int, Action>(1, () => propertyInfo.IsCurrentlySettable(BindingFlags.Default)), 56 new Tuple<int, Action>(1, () => methodInfo.IsExtension()), 57 }; 58 59 private static MethodInfo methodInfo = typeof(String).GetMethods().First(x => x.Name == "Equals"); 60 private static PropertyInfo propertyInfo = typeof(String).GetProperties().First(x => x.Name == "Length"); 61 private static FieldInfo fieldInfo = typeof(String).GetFields().First(x => x.Name == "Empty"); 62 private static SimpleCache Cache; 63 } 64 } 65 66