Skip to content

Commit 98fdbe5

Browse files
committed
created a unit test
using moq for unit test. added some sample json and static classes that are using that json as a string. update post and comment so no out of memory excecption when a sub comment is a more thing.
1 parent 5d0c3d9 commit 98fdbe5

File tree

10 files changed

+611
-9
lines changed

10 files changed

+611
-9
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ TestResults/
1111
*.vsp
1212

1313
# StyleCop files
14-
StyleCop.Cache
14+
StyleCop.Cache
15+
.vs/config/applicationhost.config
16+
/.vs
17+
/packages

RedditSharp/Things/Comment.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public Comment PopulateComments(IEnumerator<Thing> things)
4848
Thing first = things.Current;
4949
Dictionary<string, Tuple<Comment, List<Comment>>> comments = new Dictionary<string, Tuple<Comment, List<Comment>>>();
5050
comments[this.FullName] = Tuple.Create<Comment, List<Comment>>(this, new List<Comment>());
51-
things.MoveNext();
52-
while (first is Comment || first is More)
51+
52+
while (things.MoveNext() && (first is Comment || first is More) )
5353
{
5454
first = things.Current;
5555
if (first is Comment)
@@ -80,9 +80,10 @@ public Comment PopulateComments(IEnumerator<Thing> things)
8080
break;
8181
}
8282
}
83-
things.MoveNext();
83+
//things.MoveNext();
8484

8585
}
86+
8687
foreach (KeyValuePair<string, Tuple<Comment, List<Comment>>> kvp in comments)
8788
{
8889
kvp.Value.Item1.Comments = kvp.Value.Item2.ToArray();

RedditSharp/Things/Post.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ public IEnumerable<Comment> EnumerateComments()
411411
More moreComments = null;
412412
foreach (var comment in postJson)
413413
{
414-
//TODO Thing.Parse()
415414
Comment newComment = new Comment().Init(Reddit, comment, WebAgent, this);
416415
if (newComment.Kind == "more")
417416
{
@@ -440,14 +439,12 @@ public IEnumerable<Comment> EnumerateComments()
440439
if (things.Current is More)
441440
{
442441
More more = (More)things.Current;
442+
if(more.ParentId == currentThing.Id) break;
443443
things = more.Things().GetEnumerator();
444444
things.MoveNext();
445445
}
446446
}
447447
}
448-
449-
450-
451448
}
452449
}
453450
}

UnitTesting/MoreUnitTest.cs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Moq;
4+
using RedditSharp.Things;
5+
using RedditSharp;
6+
using System.Net;
7+
using UnitTesting.TestData;
8+
using System.IO;
9+
using Newtonsoft.Json.Linq;
10+
using System.Linq;
11+
namespace UnitTesting
12+
{
13+
/// <summary>
14+
/// Summary description for MoreUnitTest
15+
/// </summary>
16+
[TestClass]
17+
public class MoreUnitTest
18+
{
19+
20+
private TestContext testContextInstance;
21+
22+
/// <summary>
23+
///Gets or sets the test context which provides
24+
///information about and functionality for the current test run.
25+
///</summary>
26+
public TestContext TestContext
27+
{
28+
get
29+
{
30+
return testContextInstance;
31+
}
32+
set
33+
{
34+
testContextInstance = value;
35+
}
36+
}
37+
38+
#region Additional test attributes
39+
//
40+
// You can use the following additional attributes as you write your tests:
41+
//
42+
// Use ClassInitialize to run code before running the first test in the class
43+
// [ClassInitialize()]
44+
// public static void MyClassInitialize(TestContext testContext) { }
45+
//
46+
// Use ClassCleanup to run code after all tests in a class have run
47+
// [ClassCleanup()]
48+
// public static void MyClassCleanup() { }
49+
//
50+
// Use TestInitialize to run code before running each test
51+
// [TestInitialize()]
52+
// public void MyTestInitialize() { }
53+
//
54+
// Use TestCleanup to run code after each test has run
55+
// [TestCleanup()]
56+
// public void MyTestCleanup() { }
57+
//
58+
#endregion
59+
60+
[TestMethod]
61+
public void EnumerateComments()
62+
{
63+
Mock<WebAgent> mockWebAgent = new Mock<WebAgent>(MockBehavior.Strict);
64+
Mock<HttpWebRequest> mockRequest = new Mock<HttpWebRequest>(MockBehavior.Strict);
65+
Mock<HttpWebRequest> mockRequestMore1 = new Mock<HttpWebRequest>(MockBehavior.Strict);
66+
67+
Mock<WebResponse> mockResponse = new Mock<WebResponse>(MockBehavior.Strict);
68+
Mock<WebResponse> mockResponseMore1 = new Mock<WebResponse>(MockBehavior.Strict);
69+
70+
mockRequest.Setup(mr =>
71+
mr.GetResponse()
72+
).Returns(mockResponse.Object);
73+
74+
mockRequestMore1.Setup(mr =>
75+
mr.GetResponse()
76+
).Returns(mockResponseMore1.Object);
77+
78+
79+
MemoryStream ms1 = new MemoryStream();
80+
MemoryStream ms2 = new MemoryStream();
81+
82+
mockResponse.Setup(mr => mr.GetResponseStream()).Returns(ms1);
83+
mockResponseMore1.Setup(mr => mr.GetResponseStream()).Returns(ms2);
84+
85+
mockWebAgent.Setup(wa => wa.CreateGet("/comments/post.json")).Returns(mockRequest.Object );
86+
mockWebAgent.Setup(wa => wa.CreateGet("/api/morechildren.json?link_id=post&children=3,3-1,3-1-more,4,5,5-1,5-1-1,5-1-more&api_type=json")).Returns(mockRequestMore1.Object);
87+
88+
89+
mockWebAgent.Setup(wa => wa.GetResponseString(It.Is<Stream>(x=>x==ms1))).Returns(JsonGetComments.JsonComments());
90+
mockWebAgent.Setup(wa => wa.GetResponseString(It.Is<Stream>(x => x == ms2))).Returns(More_3_4_5.GetMore_3_4_5());
91+
92+
Mock<Reddit> mockReddit = new Mock<Reddit>(MockBehavior.Strict);
93+
JToken jObject = JsonGetComments.GetComments()[0]["data"]["children"].First;
94+
95+
Reddit r = mockReddit.Object;
96+
WebAgent waz = mockWebAgent.Object;
97+
98+
99+
Post p = new Post().Init(mockReddit.Object, jObject, mockWebAgent.Object);
100+
101+
string[] fullNames = {"t1_1","t1_2","t1_3","t1_4","t1_5" };
102+
Comment[] comments = p.EnumerateComments().ToArray();
103+
Assert.AreEqual(fullNames.Count(), comments.Count());
104+
105+
foreach (var element in
106+
comments.Zip(fullNames,
107+
(c,fn)=>new { comment = c, fullName = fn }))
108+
{
109+
Assert.AreEqual(element.comment.FullName, element.fullName);
110+
}
111+
112+
Assert.AreEqual(comments[0].Comments.Count,1);
113+
114+
115+
116+
}
117+
}
118+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Newtonsoft.Json.Linq;
7+
8+
namespace UnitTesting.TestData
9+
{
10+
public static class JsonGetComments
11+
{
12+
public static JArray GetComments()
13+
{
14+
15+
return JArray.Parse(JsonComments());
16+
}
17+
public static String JsonComments()
18+
{
19+
#region return
20+
return @"
21+
[
22+
{
23+
24+
""kind"": ""Listing"",
25+
""data"": {
26+
""modhash"": """",
27+
""children"": [
28+
{
29+
""kind"": ""t3"",
30+
""data"": {
31+
""author"": ""anauthor"",
32+
""name"": ""t3_post"",
33+
""id"": ""post""
34+
}
35+
}
36+
],
37+
""after"": null,
38+
""before"": null
39+
}
40+
},
41+
{
42+
43+
""kind"": ""Listing"",
44+
""data"": {
45+
46+
""modhash"": """",
47+
48+
""children"": [
49+
50+
{
51+
""kind"": ""t1"",
52+
""data"": {
53+
""author"": ""somone"",
54+
""id"": ""1"",
55+
""name"": ""t1_1"",
56+
""link_id"": ""0"",
57+
""replies"": {
58+
""kind"": ""Listing"",
59+
""data"": {
60+
""modhash"": """",
61+
""children"": [
62+
{
63+
""kind"": ""t1"",
64+
""data"": {
65+
""author"": ""someperson"",
66+
""id"": ""1-1"",
67+
""name"": ""t1_1-1"",
68+
""link_id"": ""0"",
69+
""replies"": {
70+
""kind"": ""Listing"",
71+
""data"": {
72+
""modhash"": """",
73+
""children"": [
74+
{
75+
""kind"": ""t1"",
76+
""data"":{
77+
""author"": ""someotherperson"",
78+
""id"": ""1-1-1"",
79+
""name"": ""t1_1-1-1"",
80+
""link_id"": ""0"",
81+
""replies"": {
82+
""kind"": ""Listing"",
83+
""data"": {
84+
""modhash"": """",
85+
""children"": [
86+
{
87+
""kind"": ""more"",
88+
""data"": {
89+
""count"": ""4"",
90+
""parent_id"": ""post"",
91+
""children"": [ ""1-1-1-1"", ""1-1-1-1-1"", ""1-1-1-2"", ""1-1-1-2-more"" ],
92+
""id"": ""1-1-1-more"",
93+
""name"": ""t1_1-1-1-more""
94+
}
95+
}
96+
]
97+
}
98+
}
99+
}
100+
}
101+
]
102+
}
103+
}
104+
}
105+
}
106+
]
107+
}
108+
}
109+
}
110+
},
111+
112+
{
113+
""kind"": ""t1"",
114+
""data"": {
115+
""author"": ""user"",
116+
""id"": ""2"",
117+
""name"": ""t1_2"",
118+
""link_id"": ""0"",
119+
""replies"": {
120+
""kind"": ""Listing"",
121+
""data"": {
122+
""modhash"": """",
123+
""children"": [
124+
{
125+
""kind"": ""more"",
126+
""data"": {
127+
""count"": 1,
128+
""parent_id"": ""t1_2"",
129+
""children"": [ ""2-1"" ]
130+
},
131+
""id"": ""2-more"",
132+
""name"": ""t1_2-more""
133+
}
134+
]
135+
}
136+
}
137+
138+
}
139+
},
140+
{
141+
""kind"": ""more"",
142+
""data"": {
143+
""count"": 3,
144+
""parent_id"": ""post"",
145+
""children"": [
146+
""3"",""3-1"",""3-1-more"",
147+
""4"",
148+
""5"",""5-1"",""5-1-1"",""5-1-more""
149+
]
150+
},
151+
""id"": ""1-more"",
152+
""name"": ""t1_1-more""
153+
}
154+
]
155+
156+
}
157+
}
158+
159+
160+
]
161+
162+
";
163+
#endregion
164+
}
165+
166+
}
167+
}

0 commit comments

Comments
 (0)