Skip to content

Commit af6e2e8

Browse files
committed
server ticket, tests, working
1 parent 62073f0 commit af6e2e8

File tree

7 files changed

+138
-102
lines changed

7 files changed

+138
-102
lines changed

src/courses/Linq/Slides/02-ReadNumbersExcercise.cs

Lines changed: 13 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using System.Runtime.InteropServices;
6-
using System.Security.Cryptography.X509Certificates;
75
using NUnit.Framework;
86

97
namespace uLearn.Courses.Linq.Slides
@@ -29,80 +27,28 @@ Нужно прочитать числа из файла в массив. Реш
2927
Решение этой задачи будет использоваться следующим образом:
3028
*/
3129

32-
public int U(string s)
33-
{
34-
return 7;
35-
}
36-
37-
38-
3930
[Sample]
4031
public void ParseNumber_Sample()
4132
{
4233
int[] numbers = ParseNumbers(File.ReadLines("numbers.txt"));
4334
}
4435

45-
46-
public class AQ
47-
{
48-
public void W()
49-
{
50-
51-
}
52-
53-
public void E()
54-
{
55-
56-
}
57-
58-
59-
60-
public class QQQ
61-
{
62-
[Exercise(SingleStatement = true)]
63-
[Hint("`int.Parse` преобразует строку в целое число.")]
64-
public int[] ParseNumbers2(IEnumerable<string> lines)
65-
{
66-
return lines
67-
.Where(line => line != "")
68-
.Select(int.Parse)
69-
.ToArray();
70-
/*uncomment
71-
return lines
72-
.Where(...)
73-
.Select(...)
74-
...
75-
*/
76-
}
77-
78-
public void W()
79-
{
80-
}
81-
}
82-
}
83-
84-
public void Trash()
36+
[Exercise(SingleStatement = true)]
37+
[Hint("`int.Parse` преобразует строку в целое число.")]
38+
public int[] ParseNumbers(IEnumerable<string> lines)
8539
{
86-
var a = 0;
87-
var b = 1;
40+
return lines
41+
.Where(line => line != "")
42+
.Select(int.Parse)
43+
.ToArray();
44+
/*uncomment
45+
return lines
46+
.Where(...)
47+
.Select(...)
48+
...
49+
*/
8850
}
8951

90-
//[Exercise(SingleStatement = true)]
91-
//[Hint("`int.Parse` преобразует строку в целое число.")]
92-
//public int[] ParseNumbers(IEnumerable<string> lines)
93-
//{
94-
// return lines
95-
// .Where(line => line != "")
96-
// .Select(int.Parse)
97-
// .ToArray();
98-
// /*uncomment
99-
// return lines
100-
// .Where(...)
101-
// .Select(...)
102-
// ...
103-
// */
104-
//}
105-
10652

10753

10854
[Test]

src/uLearn.Web/Views/Course/Slide.cshtml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,11 @@
2020
}
2121

2222
@{ var slide = Model.Slide as ExerciseSlide; }
23-
@if (slide != null)
24-
{
25-
<textarea class='code code-sample'>@slide.WithoutAttribut</textarea>
26-
<textarea class='code code-sample'>@slide.Head</textarea>
27-
}
2823
@if (slide != null)
2924
{
3025
<p>Допишите решение в окне ниже:</p>
3126
<textarea class='code code-exercise'>@slide.Exercise.Text</textarea>
32-
27+
3328
<text>Ожидаемый вывод на консоль:</text>
3429
<pre><code>@slide.ExpectedOutput</code></pre>
3530
@Html.Partial("_ExerciseHints", slide.HintsHtml)

src/uLearn/CSharp/SlideParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ private static Slide ParseSyntaxTree(SyntaxTree tree)
2323
walker.Visit(tree.GetRoot());
2424
if (walker.Exercise == null)
2525
return new Slide(walker.Blocks);
26-
walker.CleanWithoutAttributes();
27-
return new ExerciseSlide(walker.Blocks, walker.Exercise, walker.ExpectedOutput, walker.Hints, walker.WithoutAttributs, walker.Head);
26+
walker.CreateSolution();
27+
return new ExerciseSlide(walker.Blocks, walker.Exercise, walker.ExpectedOutput, walker.Hints, walker.Solution);
2828
}
2929
}
3030
}

src/uLearn/CSharp/SlideWalker.cs

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Linq;
44
using System.Runtime.Remoting.Services;
55
using System.Text;
6+
using System.Text.RegularExpressions;
7+
using System.Xml.Linq;
68
using Microsoft.CodeAnalysis;
79
using Microsoft.CodeAnalysis.CSharp;
810
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -16,9 +18,12 @@ public class SlideWalker : CSharpSyntaxWalker
1618
public SlideBlock Exercise { get; private set; }
1719
public string ExpectedOutput { get; private set; }
1820
public readonly List<string> Hints = new List<string>();
19-
public List<string> WithoutAttributs = new List<string>();
21+
private List<string> _withoutAttributes = new List<string>();
22+
private readonly List<string> _withAttributes = new List<string>();
23+
private string WithExersiceAttribute { get; set; }
2024
public MethodDeclarationSyntax ExerciseNode;
21-
public string Head;
25+
public string InitialDataForSolution;
26+
public SolutionForTesting Solution { get; private set; }
2227

2328
public readonly List<MemberDeclarationSyntax> samples = new List<MemberDeclarationSyntax>();
2429

@@ -30,25 +35,21 @@ public override void Visit(SyntaxNode node)
3035
{
3136
base.Visit(node);
3237
var usings = node.DescendantNodes().Where(x => x is UsingDirectiveSyntax);
33-
Head = "";
34-
foreach (var u in usings)
35-
{
36-
Head += u;
37-
}
38-
Head += "namespace u \n{\n public class S\n {\n }}";
39-
"/^**^*/"
38+
InitialDataForSolution = "";
39+
InitialDataForSolution = usings.Aggregate("", (x, y) => x + y.ToString());
40+
InitialDataForSolution += "namespace u \n{\n public class S\n {\n";
4041
}
4142

4243
public override void VisitClassDeclaration(ClassDeclarationSyntax node)
4344
{
4445
base.VisitClassDeclaration(node);
4546
if (node.AttributeLists.Count == 0)
4647
{
47-
foreach (var nod in WithoutAttributs.ToList())
48+
foreach (var nod in _withoutAttributes.Where(x => node.ToString().Contains(x)).ToList())
4849
{
49-
WithoutAttributs.Remove(nod);
50+
_withoutAttributes.Remove(nod);
5051
}
51-
WithoutAttributs.Add(node.ToString());
52+
_withoutAttributes.Add("\t\t" + node);
5253
}
5354

5455
if (node.HasAttribute<SampleAttribute>())
@@ -63,7 +64,11 @@ public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
6364
base.VisitMethodDeclaration(node);
6465
if (node.AttributeLists.Count == 0)
6566
{
66-
WithoutAttributs.Add(node.ToString());
67+
_withoutAttributes.Add("\t\t" + node);
68+
}
69+
else
70+
{
71+
_withAttributes.Add(node.ToString());
6772
}
6873

6974
if (node.HasAttribute<SampleAttribute>())
@@ -73,6 +78,8 @@ public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
7378
}
7479
else if (node.HasAttribute<ExerciseAttribute>())
7580
{
81+
_withoutAttributes.Add("\t\t" + node);
82+
WithExersiceAttribute = node.ToString();
7683
ExerciseNode = node;
7784
Exercise = SlideBlock.FromCode(GetExerciseCode(node));
7885
Hints.AddRange(node.GetAttributes<HintAttribute>().Select(attr => attr.GetArgument()));
@@ -117,7 +124,6 @@ private IEnumerable<string> FilterSpecialComments(IEnumerable<string> lines)
117124
}
118125
}
119126

120-
121127
public override void VisitTrivia(SyntaxTrivia trivia)
122128
{
123129
base.VisitTrivia(trivia);
@@ -152,10 +158,60 @@ public static SlideBlock ExtractMarkDownFromComment(SyntaxTrivia comment)
152158

153159
public void CleanWithoutAttributes()
154160
{
155-
if (Exercise == null)
156-
return;
157-
//WithoutAttributs =
158-
// WithoutAttributs.Select(x => x.Replace(WithoutAttributs, Exercise.Text.Split('\n').First() + "\n{\n}\n")).ToList();
161+
_withoutAttributes = _withoutAttributes
162+
.Select(x => x.Replace(WithExersiceAttribute, Exercise.Text
163+
.Split('\n')
164+
.First()))
165+
.Select(RemoveBlocksFromSolution)
166+
.ToList();
167+
}
168+
169+
private string RemoveBlocksFromSolution(string arg)
170+
{
171+
return _withAttributes.Aggregate(arg, (current, block) => current.Replace(block, ""));
172+
}
173+
174+
public void CreateSolution()
175+
{
176+
CleanWithoutAttributes();
177+
var withoutAttribute = _withoutAttributes.Aggregate("", (current, v) => current + (v + "\n"));
178+
withoutAttribute = CleanFromComments(withoutAttribute);
179+
withoutAttribute = withoutAttribute
180+
.Split('\n')
181+
.Where(x => x.Length > 2)
182+
.Select(x => x.Substring(2))
183+
.Aggregate("", (current, v) => current + (v + "\n"));
184+
//there is alignment for tabs
185+
var indexForInsert = withoutAttribute.IndexOf(Exercise.Text.Split('\n').First(), StringComparison.Ordinal);
186+
var sb = new StringBuilder();
187+
for (var i = indexForInsert - 1; i >= 0 && withoutAttribute[i] == '\t'; i--)
188+
sb.Append('\t');
189+
var tabs = sb.ToString();
190+
for (var i = indexForInsert; i < withoutAttribute.Length; i++)
191+
if (withoutAttribute[i] == '\n')
192+
{
193+
withoutAttribute = withoutAttribute.Insert(i, "\n" + tabs + "{\n" + tabs + "}\n");
194+
break;
195+
}
196+
Solution = new SolutionForTesting(InitialDataForSolution, withoutAttribute, indexForInsert);
197+
}
198+
199+
200+
201+
public string CleanFromComments(string content)
202+
{
203+
var s = new StringBuilder();
204+
var isOpen = false;
205+
for (var i = 0; i < content.Length; i++)
206+
{
207+
if (content[i] == '/' && content[i + 1] == '*')
208+
isOpen = true;
209+
if (!isOpen) s.Append(content[i]);
210+
if (content[i] != '*' || content[i + 1] != '/') continue;
211+
i++;
212+
isOpen = false;
213+
}
214+
return s.ToString();
159215
}
160216
}
161217
}

src/uLearn/ExerciseSlide.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,16 @@ public class ExerciseSlide : Slide
77
{
88
public SlideBlock Exercise { get; private set; }
99
public string ExpectedOutput { get; private set; }
10-
public string Head { get; private set; }
11-
public string WithoutAttribut { get; private set; }
10+
public SolutionForTesting Solution { get; private set; }
1211
public string[] HintsHtml { get; private set; }
1312

1413
public ExerciseSlide(IEnumerable<SlideBlock> blocks, SlideBlock exercise, string expectedOutput,
15-
IEnumerable<string> hints, IEnumerable<string> withoutAttribut, string head)
14+
IEnumerable<string> hints, SolutionForTesting solution)
1615
: base(blocks)
1716
{
1817
Exercise = exercise;
1918
ExpectedOutput = expectedOutput;
20-
Head = head;
21-
WithoutAttribut = "";
22-
foreach (var v in withoutAttribut)
23-
{
24-
WithoutAttribut += v + "\n";
25-
}
19+
Solution = solution;
2620
HintsHtml = hints.Select(Md.ToHtml).ToArray();
2721
}
2822

src/uLearn/SolutionForTesting.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace uLearn
8+
{
9+
public class SolutionForTesting
10+
{
11+
private string Usings { get; set; }
12+
public string Content { get; private set; }
13+
private int IndexForInsert { get; set; }
14+
15+
public SolutionForTesting(string usings, string content, int indexForInsert)
16+
{
17+
Usings = usings;
18+
Content = content.Trim();
19+
IndexForInsert = indexForInsert;
20+
}
21+
22+
public string BuildSolution(string usersExercise)
23+
{
24+
var countOfNLines = 0;
25+
for (var i = IndexForInsert; i < Content.Length; i++) //inserting of user exercise
26+
{
27+
if (Content[i] == '\n') countOfNLines++;
28+
if (countOfNLines == 2)
29+
{
30+
var a = Content.Remove(IndexForInsert, i - IndexForInsert);
31+
a = Usings + a.Insert(IndexForInsert, usersExercise) + "\n}";
32+
return a;
33+
}
34+
}
35+
return "error";
36+
}
37+
}
38+
}

src/uLearn/uLearn.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,17 @@
8383
<Compile Include="SampleAttribute.cs" />
8484
<Compile Include="Slide.cs" />
8585
<Compile Include="SlideBlock.cs" />
86+
<Compile Include="SolutionForTesting.cs" />
8687
<Compile Include="StringExtensions.cs" />
8788
<Compile Include="StringExtensions_SplitToLines_should.cs" />
8889
<Compile Include="CSharp\SyntaxExtensions.cs" />
8990
<Compile Include="TestExerciseStaff.cs" />
91+
<Compile Include="tests\HelloWorld.cs">
92+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
93+
</Compile>
94+
<Compile Include="tests\SelectWhereToArray.cs">
95+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
96+
</Compile>
9097
<Compile Include="tests\ReadNumbersExcercise.cs">
9198
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
9299
</Compile>

0 commit comments

Comments
 (0)