Skip to content

Commit

Permalink
pr#32 issue#31-issue#33-marked-composite
Browse files Browse the repository at this point in the history
Marked composite types, functions, and tests.
  • Loading branch information
vkamiansky authored Apr 10, 2018
2 parents fcf713c + 312556a commit 2f88127
Show file tree
Hide file tree
Showing 20 changed files with 526 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
using Xunit;
using Composite.Cs.Tests.Infrastructure;

namespace Composite.Cs.Tests.Enumerables
namespace Composite.Cs.Tests.Composites
{
public class AsEnumerableTests
{
[Fact]
public void AsEnumerableTest()
{
var obj = C.Composite(new[] {
C.Value(new Simple { Number = 1, }),
C.Composite(new [] {
C.Value(new Simple { Number = 2, }),
C.Value(new Simple { Number = 3, }),
C.Composite(new [] {
C.Value(new Simple { Number = 4, }),
C.Value(new Simple { Number = 5, }),
var obj = Composite.Create(new[] {
Composite.CreateValue(new Simple { Number = 1, }),
Composite.Create(new [] {
Composite.CreateValue(new Simple { Number = 2, }),
Composite.CreateValue(new Simple { Number = 3, }),
Composite.Create(new [] {
Composite.CreateValue(new Simple { Number = 4, }),
Composite.CreateValue(new Simple { Number = 5, }),
}.AllowTake(1)),
}),
C.Value(new Simple { Number = 6, }),
Composite.CreateValue(new Simple { Number = 6, }),
}.AllowTake(2));

var result = obj.AsEnumerable().Take(4).ToArray();
Expand Down
51 changes: 51 additions & 0 deletions Composite/Composite.Cs.Tests/Composites/EnsureHasContainerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Linq;

using Xunit;
using Composite.Cs.Tests.Infrastructure;

namespace Composite.Cs.Tests.Composites
{
public class EnsureHasContainerTests
{
[Fact]
public void TransformationTest()
{
var obj = MarkedComposite.Create(string.Empty, new[] {
MarkedComposite.Create("inner composite", new[]{
MarkedComposite.CreateValue("mark", new Simple { Number = 1, }),
MarkedComposite.Create("inner composite level2", new[]{
MarkedComposite.CreateValue("mark2", new Simple { Number = 2, })
})
})
});

var result = obj.EnsureHasContainer(
"inner composite",
"inner composite level2",
(x, y) => x == y);

Assert.Equal("[ ( inner composite )[ ( mark )1, ( inner composite level2 )[ ( mark2 )2 ] ] ]", result.ToStringShort());

result = obj.EnsureHasContainer(
string.Empty,
"inner composite2",
(x, y) => x == y);

Assert.Equal("[ ( inner composite )[ ( mark )1, ( inner composite level2 )[ ( mark2 )2 ] ], ( inner composite2 )[ ] ]", result.ToStringShort());
}

[Fact]
public void TrivialCaseTest()
{
var obj = MarkedComposite.CreateValue("mark", new Simple { Number = 1, });

var result = obj.EnsureHasContainer(
string.Empty,
"mark",
(x, y) => x == y);

Assert.Equal("( mark )1", result.ToStringShort());
}
}
}
36 changes: 18 additions & 18 deletions Composite/Composite.Cs.Tests/Composites/ForkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class ForkTests
[Fact]
public void TransformationTest()
{
var obj = C.Composite(new[] {
C.Value( new Simple { Number = 1, } ),
C.Value( new Simple { Number = 6, } ),
var obj = Composite.Create(new[] {
Composite.CreateValue( new Simple { Number = 1, } ),
Composite.CreateValue( new Simple { Number = 6, } ),
});

var result = obj.Fork(x => x.Number == 1, _ => new[] { new Simple { Number = 4, }, new Simple { Number = 5, } });
Expand All @@ -25,14 +25,14 @@ public void TransformationTest()
[Fact]
public void LazyInputTest()
{
var obj = C.Composite(new[] {
C.Composite(new[]
var obj = Composite.Create(new[] {
Composite.Create(new[]
{
C.Value(new Simple { Number = 1, }),
C.Value(new Simple { Number = 2, }),
C.Value(new Simple { Number = 3, })
Composite.CreateValue(new Simple { Number = 1, }),
Composite.CreateValue(new Simple { Number = 2, }),
Composite.CreateValue(new Simple { Number = 3, })
}.AllowTake(2)),
C.Value( new Simple { Number = 6, } ),
Composite.CreateValue( new Simple { Number = 6, } ),
});

var baseQuery = obj.Fork(
Expand All @@ -56,9 +56,9 @@ public void LazyInputTest()
[Fact]
public void LazyOutputTest()
{
var obj = C.Composite(new[] {
C.Value( new Simple { Number = 1, } ),
C.Value( new Simple { Number = 6, } ),
var obj = Composite.Create(new[] {
Composite.CreateValue( new Simple { Number = 1, } ),
Composite.CreateValue( new Simple { Number = 6, } ),
});

var forked = obj.Fork(
Expand All @@ -78,9 +78,9 @@ public void LazyOutputTest()
[Fact]
public void PredicateCallTest()
{
var obj = C.Composite(new[] {
C.Value( new Simple { Number = 1, } ),
C.Value( new Simple { Number = 6, } ),
var obj = Composite.Create(new[] {
Composite.CreateValue( new Simple { Number = 1, } ),
Composite.CreateValue( new Simple { Number = 6, } ),
});

var forked = obj.Fork(
Expand All @@ -95,9 +95,9 @@ public void PredicateCallTest()
[Fact]
public void InvalidParametersTest()
{
var obj = C.Composite(new[] {
C.Value(new Simple()),
C.Value(new Simple()),
var obj = Composite.Create(new[] {
Composite.CreateValue(new Simple()),
Composite.CreateValue(new Simple()),
});

Assert.Throws<ArgumentNullException>(() => obj.Fork(null, null));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Linq;

using Xunit;

using Composite;
using Composite.Cs.Tests.Infrastructure;

namespace Composite.Cs.Tests.Composites
{
public class MarkedCompositeEmptyTests
{
[Fact]
public void GenerationTest(){
var result = MarkedComposite.Empty<string, Simple>("mark");

Assert.Equal("( mark )[ ]", result.ToStringShort());
}
}
}
28 changes: 14 additions & 14 deletions Composite/Composite.Cs.Tests/Composites/SelectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ public class SelectTests
[Fact]
public void TransformationTest()
{
var obj = C.Composite(new[] {
C.Value( new Simple { Number = 1, } ),
C.Value( new Simple { Number = 6, } ),
C.Composite(new[] {
C.Value( new Simple { Number = 1, } ),
C.Value( new Simple { Number = 7, } ),
var obj = Composite.Create(new[] {
Composite.CreateValue( new Simple { Number = 1, } ),
Composite.CreateValue( new Simple { Number = 6, } ),
Composite.Create(new[] {
Composite.CreateValue( new Simple { Number = 1, } ),
Composite.CreateValue( new Simple { Number = 7, } ),
}),
C.Value( new Simple { Number = 8, } ),
Composite.CreateValue( new Simple { Number = 8, } ),
});

var result = obj.Select(x => x.Number == 1
Expand All @@ -34,13 +34,13 @@ public void TransformationTest()
[Fact]
public void LazyInputTest()
{
var obj = C.Composite(new[] {
C.Value( new Simple { Number = 1, } ),
C.Composite(new[] {
C.Value( new Simple { Number = 6, } ),
C.Value( new Simple { Number = 7, } ),
var obj = Composite.Create(new[] {
Composite.CreateValue( new Simple { Number = 1, } ),
Composite.Create(new[] {
Composite.CreateValue( new Simple { Number = 6, } ),
Composite.CreateValue( new Simple { Number = 7, } ),
}.AllowTake(1)),
C.Value( new Simple { Number = 8, } ),
Composite.CreateValue( new Simple { Number = 8, } ),
});

var baseQuery = obj.Select(x => x.Number == 1 ? 4 : x.Number == 6 ? 5 : x.Number);
Expand All @@ -58,7 +58,7 @@ public void LazyInputTest()
[Fact]
public void InvalidParametersTest()
{
var obj = C.Composite(new[] { C.Value(new Simple()), });
var obj = Composite.Create(new[] { Composite.CreateValue(new Simple()), });

Assert.Throws<ArgumentNullException>(() => obj.Select<Simple, int>(null));
}
Expand Down
51 changes: 51 additions & 0 deletions Composite/Composite.Cs.Tests/Composites/SetValueTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Linq;

using Xunit;
using Composite.Cs.Tests.Infrastructure;

namespace Composite.Cs.Tests.Composites
{
public class SetValueTests
{
[Fact]
public void TransformationTest()
{
var obj = MarkedComposite.Create(string.Empty, new[] {
MarkedComposite.Create("inner composite", new[]{
MarkedComposite.CreateValue("mark", new Simple { Number = 1, })
})
});

var result = obj.SetValue(
"inner composite",
"mark",
new Simple { Number = 2, },
(x, y) => x == y);

Assert.Equal("[ ( inner composite )[ ( mark )2 ] ]", result.ToStringShort());

result = obj.SetValue(
"inner composite",
"mark2",
new Simple { Number = 2, },
(x, y) => x == y);

Assert.Equal("[ ( inner composite )[ ( mark )1, ( mark2 )2 ] ]", result.ToStringShort());
}

[Fact]
public void TrivialCaseTest()
{
var obj = MarkedComposite.CreateValue("mark", new Simple { Number = 1, });

var result = obj.SetValue(
string.Empty,
"mark",
new Simple { Number = 2, },
(x, y) => x == y);

Assert.Equal("( mark )1", result.ToStringShort());
}
}
}
12 changes: 6 additions & 6 deletions Composite/Composite.Cs.Tests/Composites/ToComponentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class ToComponentsTests
{
[Fact]
public void LazyOutputTest(){
var obj = C.Composite (new [] {
C.Value(new Simple { Number = 2, }),
C.Composite(new [] {
C.Value(new Simple { Number = 1, }),
C.Value(new Simple { Number = 6, }),
var obj = Composite.Create (new [] {
Composite.CreateValue(new Simple { Number = 2, }),
Composite.Create(new [] {
Composite.CreateValue(new Simple { Number = 1, }),
Composite.CreateValue(new Simple { Number = 6, }),
}),
C.Value(new Simple { Number = 2, }),
Composite.CreateValue(new Simple { Number = 2, }),
}.AllowTake(2));

obj.ToComponents().Take(2).ToArray();
Expand Down
34 changes: 34 additions & 0 deletions Composite/Composite.Cs.Tests/Infrastructure/AllowTakeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Linq;

using Xunit;

namespace Composite.Cs.Tests.Infrastructure
{
public class AllowTakeTests
{
[Fact]
public void TransformationTest()
{
var limitedEnumerable = Enumerable.Range(1, 5).AllowTake(4);

var result = limitedEnumerable.Take(4).ToArray();
Assert.Equal(new[]{1, 2, 3, 4}, result);

Assert.Throws<InvalidOperationException>(() => limitedEnumerable.Take(5).ToArray());
}

[Fact]
public void TrivialParametersTest()
{
var limitedEnumerable = Enumerable.Empty<int>().AllowTake(0);
Assert.Throws<InvalidOperationException>(() => limitedEnumerable.ToArray());

limitedEnumerable = Enumerable.Range(1, 5).AllowTake(0);
Assert.Throws<InvalidOperationException>(() => limitedEnumerable.Take(1).ToArray());

limitedEnumerable = Enumerable.Empty<int>().AllowTake(1);
limitedEnumerable.ToArray();
}
}
}
19 changes: 19 additions & 0 deletions Composite/Composite.Cs.Tests/Infrastructure/CompositeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.FSharp.Core;

using Composite;

Expand All @@ -18,5 +19,23 @@ public static string ToStringShort<T>(this Composite<T> source)
else
return (source as Composite<T>.Value).Item.ToString();
}

public static string ToStringShort<TMark, TPayload>(this Composite<TMark, TPayload> source)
{
if(source is Composite<TMark, TPayload>.MarkedComposite composite)
{
var innerString = string.Join(", ", composite.Item.Components.Select(x => x.ToStringShort()));
var markString = composite.Item.Mark.ToString();
return string.Format(string.IsNullOrEmpty(markString) ? string.Empty : "( {0} )" , markString) +
string.Format(string.IsNullOrEmpty(innerString) ? "[ ]" : "[ {0} ]", innerString);
}
else if(source is Composite<TMark, TPayload>.MarkedValue value)
{
var markString = value.Item.Mark.ToString();
return string.Format(string.IsNullOrEmpty(markString) ? string.Empty : "( {0} )" , markString) +
value.Item.Value.ToString();
}
else return string.Empty;
}
}
}
Loading

0 comments on commit 2f88127

Please sign in to comment.