11// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
22
33using System ;
4+ using System . Collections . Generic ;
45using System . ComponentModel ;
6+ using System . Linq ;
57using System . Reflection ;
68using NUnit . Engine . Extensibility ;
79using NUnit . Framework ;
@@ -14,7 +16,8 @@ public class ExtensionAssemblyTrackerTests
1416 private static readonly string THIS_ASSEMBLY_PATH = THIS_ASSEMBLY . Location ;
1517 private static readonly string THIS_ASSEMBLY_NAME = THIS_ASSEMBLY . GetName ( ) . Name ;
1618 private static readonly Version THIS_ASSEMBLY_VERSION = THIS_ASSEMBLY . GetName ( ) . Version ;
17- private static readonly ExtensionAssembly TEST_EXTENSION_ASSEMBLY =
19+
20+ private static readonly ExtensionAssembly TEST_EXTENSION_ASSEMBLY =
1821 new ExtensionAssembly ( THIS_ASSEMBLY_PATH , false , THIS_ASSEMBLY_NAME , THIS_ASSEMBLY_VERSION ) ;
1922
2023 private ExtensionAssemblyTracker _tracker ;
@@ -28,52 +31,74 @@ public void CreateTracker()
2831 [ Test ]
2932 public void AddToList ( )
3033 {
31- _tracker . Add ( TEST_EXTENSION_ASSEMBLY ) ;
34+ _tracker . AddOrUpdate ( TEST_EXTENSION_ASSEMBLY ) ;
3235
3336 Assert . That ( _tracker . Count , Is . EqualTo ( 1 ) ) ;
34- Assert . That ( _tracker [ 0 ] . FilePath , Is . EqualTo ( THIS_ASSEMBLY_PATH ) ) ;
35- Assert . That ( _tracker [ 0 ] . AssemblyName , Is . EqualTo ( THIS_ASSEMBLY_NAME ) ) ;
36- Assert . That ( _tracker [ 0 ] . AssemblyVersion , Is . EqualTo ( THIS_ASSEMBLY_VERSION ) ) ;
37+ var assembly = _tracker . Single ( ) ;
38+
39+ Assert . That ( assembly . FilePath , Is . EqualTo ( THIS_ASSEMBLY_PATH ) ) ;
40+ Assert . That ( assembly . AssemblyName , Is . EqualTo ( THIS_ASSEMBLY_NAME ) ) ;
41+ Assert . That ( assembly . AssemblyVersion , Is . EqualTo ( THIS_ASSEMBLY_VERSION ) ) ;
3742 }
3843
3944 [ Test ]
40- public void AddUpdatesNameIndex ( )
45+ public void AddUpdatesPathIndex ( )
4146 {
42- _tracker . Add ( TEST_EXTENSION_ASSEMBLY ) ;
47+ _tracker . AddOrUpdate ( TEST_EXTENSION_ASSEMBLY ) ;
4348
44- Assert . That ( _tracker . ByName . ContainsKey ( THIS_ASSEMBLY_NAME ) ) ;
45- Assert . That ( _tracker . ByName [ THIS_ASSEMBLY_NAME ] . AssemblyName , Is . EqualTo ( THIS_ASSEMBLY_NAME ) ) ;
46- Assert . That ( _tracker . ByName [ THIS_ASSEMBLY_NAME ] . FilePath , Is . EqualTo ( THIS_ASSEMBLY_PATH ) ) ;
47- Assert . That ( _tracker . ByName [ THIS_ASSEMBLY_NAME ] . AssemblyVersion , Is . EqualTo ( THIS_ASSEMBLY_VERSION ) ) ;
49+ Assert . That ( _tracker . ContainsPath ( THIS_ASSEMBLY_PATH ) ) ;
4850 }
49- [ Test ]
50- public void AddUpdatesPathIndex ( )
51- {
52- _tracker . Add ( TEST_EXTENSION_ASSEMBLY ) ;
5351
54- Assert . That ( _tracker . ByPath . ContainsKey ( THIS_ASSEMBLY_PATH ) ) ;
55- Assert . That ( _tracker . ByPath [ THIS_ASSEMBLY_PATH ] . AssemblyName , Is . EqualTo ( THIS_ASSEMBLY_NAME ) ) ;
56- Assert . That ( _tracker . ByPath [ THIS_ASSEMBLY_PATH ] . FilePath , Is . EqualTo ( THIS_ASSEMBLY_PATH ) ) ;
57- Assert . That ( _tracker . ByPath [ THIS_ASSEMBLY_PATH ] . AssemblyVersion , Is . EqualTo ( THIS_ASSEMBLY_VERSION ) ) ;
52+ private static IEnumerable < TestCaseData > TestCasesAddNewerAssemblyUpdatesExistingInformation ( )
53+ {
54+ yield return new TestCaseData ( new Version ( THIS_ASSEMBLY_VERSION . Major + 1 , THIS_ASSEMBLY_VERSION . Minor , THIS_ASSEMBLY_VERSION . Build ) ) ;
55+ yield return new TestCaseData ( new Version ( THIS_ASSEMBLY_VERSION . Major , THIS_ASSEMBLY_VERSION . Minor + 1 , THIS_ASSEMBLY_VERSION . Build ) ) ;
56+ yield return new TestCaseData ( new Version ( THIS_ASSEMBLY_VERSION . Major , THIS_ASSEMBLY_VERSION . Minor , THIS_ASSEMBLY_VERSION . Build + 1 ) ) ;
5857 }
5958
60- [ Test ]
61- public void AddDuplicatePathThrowsArgumentException ( )
59+ [ TestCaseSource ( nameof ( TestCasesAddNewerAssemblyUpdatesExistingInformation ) ) ]
60+ public void AddNewerAssemblyUpdatesExistingInformation ( Version newVersion )
6261 {
63- _tracker . Add ( TEST_EXTENSION_ASSEMBLY ) ;
62+ _tracker . AddOrUpdate ( TEST_EXTENSION_ASSEMBLY ) ;
63+
64+ string newAssemblyPath = "/path/to/new/assembly" ;
65+ var newerAssembly = new ExtensionAssembly ( newAssemblyPath , false , THIS_ASSEMBLY_NAME , newVersion ) ;
66+
67+ _tracker . AddOrUpdate ( newerAssembly ) ;
6468
65- Assert . That ( ( ) =>
66- _tracker . Add ( TEST_EXTENSION_ASSEMBLY ) ,
67- Throws . TypeOf < System . ArgumentException > ( ) ) ;
69+ Assert . That ( _tracker . Count , Is . EqualTo ( 1 ) ) ;
70+ Assert . That ( _tracker . ContainsPath ( newAssemblyPath ) ) ;
71+
72+ var assembly = _tracker . Single ( ) ;
73+ Assert . That ( assembly . FilePath , Is . EqualTo ( newAssemblyPath ) ) ;
74+ Assert . That ( assembly . AssemblyName , Is . EqualTo ( THIS_ASSEMBLY_NAME ) ) ;
75+ Assert . That ( assembly . AssemblyVersion , Is . EqualTo ( newVersion ) ) ;
6876 }
6977
70- [ Test ]
71- public void AddDuplicateAssemblyNameThrowsArgumentException ( )
78+ private static IEnumerable < TestCaseData > AddNewerAssemblyUpdatesExistingInformationTestCases ( )
79+ {
80+ yield return new TestCaseData ( new Version ( THIS_ASSEMBLY_VERSION . Major - 1 , THIS_ASSEMBLY_VERSION . Minor , THIS_ASSEMBLY_VERSION . Build ) ) ;
81+ yield return new TestCaseData ( new Version ( THIS_ASSEMBLY_VERSION . Major , THIS_ASSEMBLY_VERSION . Minor - 1 , THIS_ASSEMBLY_VERSION . Build ) ) ;
82+ yield return new TestCaseData ( new Version ( THIS_ASSEMBLY_VERSION . Major , THIS_ASSEMBLY_VERSION . Minor , THIS_ASSEMBLY_VERSION . Build ) ) ;
83+ }
84+
85+ [ TestCaseSource ( nameof ( AddNewerAssemblyUpdatesExistingInformationTestCases ) ) ]
86+ public void AddOlderOrSameAssemblyDoesNotUpdateExistingInformation ( Version newVersion )
7287 {
73- _tracker . Add ( TEST_EXTENSION_ASSEMBLY ) ;
88+ _tracker . AddOrUpdate ( TEST_EXTENSION_ASSEMBLY ) ;
89+
90+ string newAssemblyPath = "/path/to/new/assembly" ;
91+ var newerAssembly = new ExtensionAssembly ( newAssemblyPath , false , THIS_ASSEMBLY_NAME , newVersion ) ;
92+
93+ _tracker . AddOrUpdate ( newerAssembly ) ;
94+
95+ Assert . That ( _tracker . Count , Is . EqualTo ( 1 ) ) ;
96+ Assert . That ( _tracker . ContainsPath ( newAssemblyPath ) ) ;
7497
75- Assert . That ( ( ) => _tracker . Add ( new ExtensionAssembly ( "Some/Other/Path" , false , THIS_ASSEMBLY_NAME , THIS_ASSEMBLY_VERSION ) ) ,
76- Throws . TypeOf < System . ArgumentException > ( ) ) ;
98+ var assembly = _tracker . Single ( ) ;
99+ Assert . That ( assembly . FilePath , Is . EqualTo ( THIS_ASSEMBLY_PATH ) ) ;
100+ Assert . That ( assembly . AssemblyName , Is . EqualTo ( THIS_ASSEMBLY_NAME ) ) ;
101+ Assert . That ( assembly . AssemblyVersion , Is . EqualTo ( THIS_ASSEMBLY_VERSION ) ) ;
77102 }
78103 }
79104}
0 commit comments