1
exercism fetch csharp pov

PovTest.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using Xunit;
using System.Linq;

public class PovTest
{
    private const string x = "x";

    private static Graph<T> leaf<T>(T v) => Pov.CreateGraph(v, new Graph<T>[0]);

    private static readonly Graph<string> singleton = Pov.CreateGraph(x, new Graph<string>[0]);

    private static readonly Graph<string> flat = Pov.CreateGraph("root", new[] { "a", "b", x, "c" }.Select(leaf));

    private static readonly Graph<string> nested =
        Pov.CreateGraph("level-0", new[] {
            Pov.CreateGraph("level-1", new[] {
                Pov.CreateGraph("level-2", new[] {
                    Pov.CreateGraph("level-3", new[] {
                        Pov.CreateGraph(x, new Graph<string>[0])
                    })
                })
            })
        });

    private static readonly Graph<string> kids =
        Pov.CreateGraph("root", new[] {
            Pov.CreateGraph(x, new[] {
                Pov.CreateGraph("kid-0", new Graph<string>[0]),
                Pov.CreateGraph("kid-1", new Graph<string>[0])
            })
        });

    private static readonly Graph<string> cousins =
        Pov.CreateGraph("grandparent", new[] {
            Pov.CreateGraph("parent", new [] {
                Pov.CreateGraph(x, new [] {
                    leaf("kid-a"),
                    leaf("kid-b")
                }),
                leaf("sibling-0"),
                leaf("sibling-1")
            }),
            Pov.CreateGraph("uncle", new [] {
                leaf("cousin-0"),
                leaf("cousin-1")
            })
        });

    private static readonly Graph<string> singleton_ = singleton;

    private static readonly Graph<string> flat_ = 
        Pov.CreateGraph(x, new[] {
            Pov.CreateGraph("root", new [] { "a", "b", "c" }.Select(leaf))
        });

    private static readonly Graph<string> nested_ = 
        Pov.CreateGraph(x, new[] {
            Pov.CreateGraph("level-3", new [] {
                Pov.CreateGraph("level-2", new [] {
                    Pov.CreateGraph("level-1", new [] {
                        Pov.CreateGraph("level-0", new Graph<string>[0])
                    })
                })
            })
        });

    private static readonly Graph<string> kids_ = 
        Pov.CreateGraph(x, new[] {
            Pov.CreateGraph("kid-0", new Graph<string>[0]),
            Pov.CreateGraph("kid-1", new Graph<string>[0]),
            Pov.CreateGraph("root", new Graph<string>[0])
        });

    private static readonly Graph<string> cousins_ = 
        Pov.CreateGraph(x, new[] {
            leaf("kid-a"),
            leaf("kid-b"),
            Pov.CreateGraph("parent", new [] {
                Pov.CreateGraph("sibling-0", new Graph<string>[0]),
                Pov.CreateGraph("sibling-1", new Graph<string>[0]),
                Pov.CreateGraph("grandparent", new [] {
                    Pov.CreateGraph("uncle", new [] {
                        Pov.CreateGraph("cousin-0", new Graph<string>[0]),
                        Pov.CreateGraph("cousin-1", new Graph<string>[0])
                    })
                })
            })
        });

    [Fact]
    public void Reparent_singleton()
    {
        Assert.Equal(singleton_, Pov.FromPOV(x, singleton));
    }

    [Fact(Skip = "Remove to run test")]
    public void Reparent_flat()
    {
        Assert.Equal(flat_, Pov.FromPOV(x, flat));
    }

    [Fact(Skip = "Remove to run test")]
    public void Reparent_nested()
    {
        Assert.Equal(nested_, Pov.FromPOV(x, nested));
    }

    [Fact(Skip = "Remove to run test")]
    public void Reparent_kids()
    {
        Assert.Equal(kids_, Pov.FromPOV(x, kids));
    }

    [Fact(Skip = "Remove to run test")]
    public void Reparent_cousins()
    {
        Assert.Equal(cousins_, Pov.FromPOV(x, cousins));
    }

    [Fact(Skip = "Remove to run test")]
    public void Reparent_from_POV_of_non_existent_node()
    {
        Assert.Null(Pov.FromPOV(x, leaf("foo")));
    }

    [Fact(Skip = "Remove to run test")]
    public void Should_not_be_able_to_find_a_missing_node()
    {
        var nodes = new[] { singleton, flat, kids, nested, cousins }.Select(graph => Pov.FromPOV("NOT THERE", graph));
    
        Assert.All(nodes, node =>
        {
            Assert.Null(node);
        });
    }

    [Fact(Skip = "Remove to run test")]
    public void Cannot_trace_between_unconnected_nodes()
    {
        Assert.Null(Pov.TracePathBetween(x, "NOT THERE", cousins));
    }

    [Fact(Skip = "Remove to run test")]
    public void Can_trace_a_path_from_x_to_cousin()
    {
        Assert.Equal(new[] { "x", "parent", "grandparent", "uncle", "cousin-1" }, Pov.TracePathBetween(x, "cousin-1", cousins));
    }

    [Fact(Skip = "Remove to run test")]
    public void Can_trace_from_a_leaf_to_a_leaf()
    {
        Assert.Equal(new[] { "kid-a", "x", "parent", "grandparent", "uncle", "cousin-0" }, Pov.TracePathBetween("kid-a", "cousin-0", cousins));
    }
}