public void Tags_must_belong_to_the_same_context() { Action a = () => CKTraitContext.Create( null ); a.Should().Throw<ArgumentException>(); a = () => CKTraitContext.Create( " " ); a.Should().Throw<ArgumentException>(); CKTraitContext c1 = CKTraitContext.Create( "C1" ); CKTraitContext c2 = CKTraitContext.Create( "C2" ); var t1 = c1.FindOrCreate( "T1" ); var t2 = c2.FindOrCreate( "T2" ); t1.Should().NotBeSameAs( t2 ); t1.Invoking( sut => sut.Union( t2 ) ).Should().Throw<InvalidOperationException>(); t1.Invoking( sut => sut.Intersect( t2 ) ).Should().Throw<InvalidOperationException>(); t1.Invoking( sut => sut.Except( t2 ) ).Should().Throw<InvalidOperationException>(); t1.Invoking( sut => sut.SymmetricExcept( t2 ) ).Should().Throw<InvalidOperationException>(); t1.Invoking( sut => sut.Overlaps( t2 ) ).Should().Throw<InvalidOperationException>(); t1.Invoking( sut => sut.IsSupersetOf( t2 ) ).Should().Throw<InvalidOperationException>(); t1.Invoking( sut => sut.Union( null ) ).Should().Throw<ArgumentNullException>(); t1.Invoking( sut => sut.Intersect( null ) ).Should().Throw<ArgumentNullException>(); t1.Invoking( sut => sut.Except( null ) ).Should().Throw<ArgumentNullException>(); t1.Invoking( sut => sut.SymmetricExcept( null ) ).Should().Throw<ArgumentNullException>(); t1.Invoking( sut => sut.Overlaps( null ) ).Should().Throw<ArgumentNullException>(); t1.Invoking( sut => sut.IsSupersetOf( null ) ).Should().Throw<ArgumentNullException>(); }
public void tag_separator_can_be_changed_from_the_default_pipe() { var c = CKTraitContext.Create( "SemiColonContext", ';' ); CKTrait m = c.FindOrCreate( "Beta;Alpha;Fridge;Combo" ); c.EmptyTrait.IsSupersetOf( c.EmptyTrait ).Should().BeTrue( "Empty is contained by definition in itself." ); m.IsSupersetOf( c.EmptyTrait ).Should().BeTrue( "Empty is contained by definition." ); m.IsSupersetOf( c.FindOrCreate( "Fridge;Alpha" ) ).Should().BeTrue(); m.IsSupersetOf( c.FindOrCreate( "Fridge" ) ).Should().BeTrue(); m.IsSupersetOf( c.FindOrCreate( "Fridge;Alpha;Combo" ) ).Should().BeTrue(); m.IsSupersetOf( c.FindOrCreate( "Fridge;Alpha;Beta;Combo" ) ).Should().BeTrue(); m.IsSupersetOf( c.FindOrCreate( "Fridge;Lol" ) ).Should().BeFalse(); m.IsSupersetOf( c.FindOrCreate( "Murfn" ) ).Should().BeFalse(); m.IsSupersetOf( c.FindOrCreate( "Fridge;Alpha;Combo+Lol" ) ).Should().BeFalse(); m.IsSupersetOf( c.FindOrCreate( "Lol;Fridge;Alpha;Beta;Combo" ) ).Should().BeFalse(); m.Overlaps( c.FindOrCreate( "Fridge;Alpha" ) ).Should().BeTrue(); m.Overlaps( c.FindOrCreate( "Nimp;Fridge;Mourfn" ) ).Should().BeTrue(); m.Overlaps( c.FindOrCreate( "Fridge;Alpha;Combo;Albert" ) ).Should().BeTrue(); m.Overlaps( c.FindOrCreate( "ZZF;AAlp;BBeBe;Combo" ) ).Should().BeTrue(); m.Overlaps( c.FindOrCreate( "AFridge;ALol" ) ).Should().BeFalse(); m.Overlaps( c.FindOrCreate( "Murfn" ) ).Should().BeFalse(); m.Overlaps( c.FindOrCreate( "QF;QA;QC;QL" ) ).Should().BeFalse(); m.Overlaps( c.EmptyTrait ).Should().BeFalse( "Empty is NOT contained 'ONE' since EmptyTag.AtomicTags.Count == 0..." ); c.EmptyTrait.Overlaps( c.EmptyTrait ).Should().BeFalse( "Empty is NOT contained 'ONE' in itself." ); }
public void binary_operators_work() { var ctx = CKTraitContext.Create( "Indep", ',', shared: false ); var a = ctx.FindOrCreate( "A" ); var b = ctx.FindOrCreate( "B" ); var c = ctx.FindOrCreate( "C" ); var d = ctx.FindOrCreate( "D" ); var ab = ctx.FindOrCreate( "A,B" ); var cd = ctx.FindOrCreate( "C,D" ); var abc = ctx.FindOrCreate( "A,B,C" ); var abcd = ctx.FindOrCreate( "A,B,C,D" ); (ab + cd).Should() .BeSameAs( abcd ) .And.BeSameAs( c + d + b + a ) .And.BeSameAs( cd + ab ) .And.BeSameAs( abc | d ) .And.BeSameAs( a | b | c | d ); (ab & cd).Should().BeSameAs( ctx.EmptyTrait ); (abc & cd).Should().BeSameAs( c ); (abcd & cd).Should().BeSameAs( cd ); (ab ^ cd).Should().BeSameAs( abcd ); (abc ^ cd).Should().BeSameAs( a | b | d ); ((a|b|d) ^ cd).Should().BeSameAs( abc ); (abcd ^ cd).Should().BeSameAs( ab ); CKTrait v = a; v += b; v.Should().BeSameAs( ab ); v += d; v.Should().BeSameAs( a | b | d ); v -= c; v.Should().BeSameAs( a | b | d ); v -= abcd; v.IsEmpty.Should().BeTrue(); v |= abcd; v &= cd; v.Should().BeSameAs( cd ); v &= d; v.Should().BeSameAs( d ); v |= abcd; v.Should().BeSameAs( abcd ); v ^= b; v.Should().BeSameAs( a | cd ); v ^= ab; v.Should().BeSameAs( b | cd ); v &= ctx.EmptyTrait; v.IsEmpty.Should().BeTrue(); }
public void FindIfAllExist_tests() { var c = CKTraitContext.Create( "Indep", '+', shared: false ); CKTrait m = c.FindOrCreate( "Alpha+Beta+Combo+Fridge" ); c.FindIfAllExist( "" ).Should().Be( c.EmptyTrait ); c.FindIfAllExist( "bo" ).Should().BeNull(); var alpha = c.FindOrCreate( "Alpha" ); c.FindIfAllExist( "Alpha" ).Should().Be( alpha ); c.FindIfAllExist( "bo+pha" ).Should().BeNull(); c.FindIfAllExist( "Fridge+Combo+Alpha+Beta" ).Should().BeSameAs( m ); }
public void comparison_operators_work_cross_contexts() { var c = CKTraitContext.Create( "Indep", '+', shared: false ); var cAfter = CKTraitContext.Create( "Indep+", '+', shared: false ); var t1 = cAfter.FindOrCreate( "B" ); var t2 = c.FindOrCreate( "B" ); // (t1 > t2).Should().BeTrue( "Tag with same name relies on context's separator and then name ('greater' separator and then name are better)." ); (t1 >= t2).Should().BeTrue(); (t1 != t2).Should().BeTrue(); (t1 == t2).Should().BeFalse(); (t1 < t2).Should().BeFalse(); (t1 <= t2).Should().BeFalse(); }
public void independent_or_shared_contexts() { var shared = CKTraitContext.Create( "Shared", '+' ); Action notPossible = () => CKTraitContext.Create( "Shared", '-' ); notPossible.Should().Throw<InvalidOperationException>(); var here = shared.FindOrCreate( "Here!" ); var independent1 = CKTraitContext.Create( "Shared", '-', shared: false ); independent1.FindOnlyExisting( "Here!" ).Should().BeNull(); independent1.FindOrCreate( "In Independent n°1" ); var independent2 = CKTraitContext.Create( "Shared", '-', shared: false ); independent2.FindOnlyExisting( "Here!" ).Should().BeNull(); independent2.FindOnlyExisting( "In Independent n°1" ).Should().BeNull(); var anotherShared = CKTraitContext.Create( "Shared", '+' ); anotherShared.FindOnlyExisting( "Here!" ).Should().BeSameAs( here ); (anotherShared == shared).Should().BeTrue(); (anotherShared != shared).Should().BeFalse(); anotherShared.Equals( shared ).Should().BeTrue(); anotherShared.CompareTo( shared ).Should().Be( 0 ); var here1 = independent1.FindOrCreate( "Here!" ); var here2 = independent2.FindOrCreate( "Here!" ); here.Should().NotBeSameAs( here1 ); here.Should().NotBeSameAs( here2 ); here1.Should().NotBeSameAs( here2 ); // Comparisons across contexts: a unique index "sorts" the independent contexts. // This index is positive for independent contexts: shared context's tags come first. here.CompareTo( here1 ).Should().BeNegative(); here1.CompareTo( here2 ).Should().BeNegative(); here.CompareTo( here2 ).Should().BeNegative(); here1.CompareTo( here ).Should().BePositive(); here2.CompareTo( here1 ).Should().BePositive(); here2.CompareTo( here ).Should().BePositive(); }
public void Comparing_tags() { CKTraitContext c1 = CKTraitContext.Create( "C1" ); CKTraitContext c2 = CKTraitContext.Create( "C2" ); c1.CompareTo( c1 ).Should().Be( 0 ); c1.CompareTo( c2 ).Should().BeLessThan( 0 ); var tAc1 = c1.FindOrCreate( "A" ); var tBc1 = c1.FindOrCreate( "B" ); var tABc1 = c1.FindOrCreate( "A|B" ); var tAc2 = c2.FindOrCreate( "A" ); tAc1.CompareTo( tAc1 ).Should().Be( 0 ); tAc1.CompareTo( tBc1 ).Should().BeGreaterThan( 0, "In the same context, A is stronger than B." ); tABc1.CompareTo( tBc1 ).Should().BeGreaterThan( 0, "In the same context, A|B is stronger than B." ); tAc1.CompareTo( tAc2 ).Should().BeLessThan( 0, "Between different contexts, the context ordering drives the ordering." ); tABc1.CompareTo( tAc2 ).Should().BeLessThan( 0, "Between different contexts, the context ordering drives the ordering." ); }
public void comparison_operators_work() { var c = CKTraitContext.Create( "Indep", '+', shared: false ); var t1 = c.FindOrCreate( "A" ); var t2 = c.FindOrCreate( "B" ); // (t1 > t2).Should().BeTrue( "A is better than B." ); (t1 >= t2).Should().BeTrue(); (t1 != t2).Should().BeTrue(); (t1 == t2).Should().BeFalse(); (t1 < t2).Should().BeFalse(); (t1 <= t2).Should().BeFalse(); // t2 = null; (t1 > t2).Should().BeTrue( "Any tag is better than null." ); (t1 >= t2).Should().BeTrue(); (t1 != t2).Should().BeTrue(); (t1 == t2).Should().BeFalse(); (t1 < t2).Should().BeFalse(); (t1 <= t2).Should().BeFalse(); // t1 = null; (t1 > t2).Should().BeFalse( "null is the same as null." ); (t1 < t2).Should().BeFalse(); (t1 >= t2).Should().BeTrue( "null is the same as null." ); (t1 <= t2).Should().BeTrue(); (t1 == t2).Should().BeTrue(); (t1 != t2).Should().BeFalse(); // t2 = c.FindOrCreate( "B" ); (t1 > t2).Should().BeFalse( "null is smaller that any tag." ); (t1 >= t2).Should().BeFalse(); (t1 != t2).Should().BeTrue(); (t1 == t2).Should().BeFalse(); (t1 < t2).Should().BeTrue(); (t1 <= t2).Should().BeTrue(); }
CKTraitContext ContextWithPlusSeparator() => CKTraitContext.Create( "Test", '+' );