Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions .github/workflows/dotnet-desktop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
name: .NET Tests & Auto-commit

on:
pull_request:
types: [opened, synchronize, reopened]
push:
branches-ignore: [main]
workflow_dispatch:

permissions:
contents: write
pull-requests: write
issues: read

jobs:

check-target:
runs-on: ubuntu-latest
outputs:
pr_valid: ${{ steps.check.outputs.pr_valid }}
steps:
- name: 🚫 Проверка PR в main
id: check
shell: bash
run: |
PR_BASE="${{ github.event.pull_request.base.ref }}"
echo "Base branch: $PR_BASE"
if [ "$PR_BASE" = "main" ]; then
echo "❌ Нельзя отправлять работу в main ветку."
echo "pr_valid=false" >> $GITHUB_OUTPUT
exit 1
else
echo "✅ PR направлен в '$PR_BASE', можно запускать тесты."
echo "pr_valid=true" >> $GITHUB_OUTPUT
fi

tests:
name: Run test
runs-on: windows-latest
needs: check-target
if: |
(github.event_name == 'pull_request' && needs.check-target.outputs.pr_valid == 'true') ||
github.event_name == 'push' ||
github.event_name == 'workflow_dispatch'
strategy:
matrix:
configuration: [Debug]

steps:
- name: Checkout code safely
uses: actions/checkout@v4
with:
repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
ref: ${{ github.event.pull_request.head.sha || github.ref }}
fetch-depth: 0

- name: Install .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.0.x
9.0.x

- name: Setup MSBuild.exe
uses: microsoft/setup-msbuild@v2

- name: Restore
run: dotnet restore Lab9/Lab9.sln

- name: Build
run: dotnet build Lab9/Lab9.sln --configuration ${{ matrix.configuration }} --no-restore

- name: Run tests (league cascade)
id: cascade
shell: pwsh
env:
CFG: ${{ matrix.configuration }}
run: |
$testProjectPath = "Lab9Test/Lab9Test.csproj"
$testDir = "Lab9Test"

Write-Host "Building test project..."
dotnet build $testProjectPath -c $Env:CFG --no-restore

$tfm = (dotnet list $testProjectPath framework --framework net9.0 | Select-Object -Last 1).Trim()
if (-not $tfm) { $tfm = "net9.0" }

$dll = Join-Path $PWD "$testDir\bin\$Env:CFG\$tfm\Lab9Test.dll"

Write-Host "Ожидаемая DLL: $dll"

if (-not (Test-Path $dll)) {
Write-Error "DLL не найдена: $dll"
Write-Host "`nСодержимое bin папки:"
Get-ChildItem "$testDir\bin" -Recurse -ErrorAction SilentlyContinue |
Select-Object FullName | Out-Host
exit 1
}

Write-Host "DLL успешно найдена`n"

$leagues = @{
"Purple" = @("Task1","Task2","Task3","Task4")
"Blue" = @("Task1","Task2","Task3","Task4")
"Green" = @("Task1","Task2","Task3","Task4")
"White" = @("Task1","Task2","Task3","Task4")
}

foreach ($leagueName in $leagues.Keys) {
Write-Host "Лига: $leagueName"
$allPassed = $true

foreach ($cls in $leagues[$leagueName]) {
$filter = "FullyQualifiedName~$leagueName.$cls"
Write-Host " Запуск: $leagueName.$cls"

dotnet vstest $dll --TestCaseFilter:"$filter" --Logger:"trx;LogFileName=test-$leagueName-$cls.trx"

if ($LASTEXITCODE -ne 0) {
Write-Host " ❌ $leagueName.$cls не прошёл" -ForegroundColor Red
$allPassed = $false
} else {
Write-Host " ✅ $leagueName.$cls прошёл" -ForegroundColor Green
}
}

if ($allPassed) {
Write-Host "✅ Все тесты лиги $leagueName прошли! Останавливаем каскад." -ForegroundColor Green
exit 0
} else {
Write-Host "⚠️ Лига $leagueName не прошла полностью. Переходим к следующей..." -ForegroundColor Yellow
}
}

Write-Host "❌ Ни одна лига полностью не прошла." -ForegroundColor Red
exit 1

- name: Upload TRX
if: always()
uses: actions/upload-artifact@v4
with:
name: trx-${{ matrix.configuration }}
path: '**/test-*.trx'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/bin
**/obj
17 changes: 17 additions & 0 deletions Lab9/Purple/Purple.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
namespace Lab9.Purple
{
public abstract class Purple
{
public string Input { get; private set; }

protected Purple(string text)
{
Input = text;
}

public abstract void Review();

public virtual void ChangeText(string text)
{
Input = text;
Review();
}
}
}
43 changes: 43 additions & 0 deletions Lab9/Purple/Task1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Text;

namespace Lab9.Purple;

public class Task1 : Purple
{
public string Output { get; private set; }

public Task1(string text) : base(text)
{
Output = string.Empty;
}

public override void Review()
{
Output = string.Join(" ", Input.Split().Select(Reverse));
}

private static string Reverse(string text)
{
if (string.IsNullOrEmpty(text)) return text;

if (text.Any(char.IsDigit)) return text;

int start = 0;
int end = text.Length-1;

while (start <= end && !char.IsLetter(text[start])) start++;
while (end >= start && !char.IsLetter(text[end])) end--;

if (start > end) return text;

var sb = new StringBuilder(text.Length);
sb.Append(text, 0, start);
sb.Append(new string(text[start..(end+1)]
.Reverse()
.ToArray()));
sb.Append(text, end+1, text.Length-end-1);
return sb.ToString();
}

public override string ToString() => Output ?? string.Empty;
}
64 changes: 64 additions & 0 deletions Lab9/Purple/Task2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Text;

namespace Lab9.Purple;

public class Task2 : Purple
{
public string[] Output { get; private set; }

public Task2(string text) : base(text)
{
Output = Array.Empty<string>();
}

public override void Review()
{
if (string.IsNullOrEmpty(Input)) return;

var words = Input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
string[] tempLines = new string[words.Length];

int lineCount = 0, start = 0, len = 0;

for (int i = 0; i < words.Length; i++)
{
if (len == 0)
len = words[i].Length;
else if (len + 1 +words[i].Length <= 50)
len += 1 + words[i].Length;
else
{
tempLines[lineCount++] = JustifyLine(words, start, i-1, 50);
start = i;
len = words[i].Length;
}
}

if (len > 0)
tempLines[lineCount++] = JustifyLine(words, start, words.Length - 1, 50);

Output = tempLines.Take(lineCount).ToArray();
}

private static string JustifyLine(string[] words, int start, int end, int targetWidth)
{
if (start == end) return words[start];

var lineWords = words.Skip(start).Take(end-start+1).ToArray();
int lettersCount = lineWords.Sum(w => w.Length);

int totalSpacesNeeded = targetWidth - lettersCount;
int gapsCount = lineWords.Length-1;
int spacesPerGap = totalSpacesNeeded / gapsCount;
int extraSpaces = totalSpacesNeeded % gapsCount;

string result = string.Empty;
for (int i = 0; i < gapsCount; i++)
result += lineWords[i] + new string(' ', spacesPerGap + (i < extraSpaces ? 1 : 0));
result += lineWords.Last();

return result;
}

public override string ToString() => Output != null ? string.Join(Environment.NewLine, Output) : string.Empty;
}
44 changes: 44 additions & 0 deletions Lab9/Purple/Task3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace Lab9.Purple;

public class Task3 : Purple
{
public string Output { get; private set;}
public (string, char)[] Codes { get; private set;}

public Task3(string text) : base(text)
{
Output = string.Empty;
Codes = Array.Empty<(string, char)>();
}

public override void Review()
{
if (string.IsNullOrEmpty(Input)) return;

var topPairs = Enumerable.Range(0, Input.Length-1)
.Where(i => char.IsLetter(Input[i]) && char.IsLetter(Input[i+1]))
.GroupBy(i => Input.Substring(i, 2))
.OrderByDescending(g => g.Count())
.ThenBy(g => g.Min())
.Select(g => g.Key)
.Take(5)
.ToArray();

var usedChars = Input.Distinct().ToArray();
var avialableChars = Enumerable.Range(32, 95)
.Select(i => (char)i)
.Except(usedChars)
.Take(topPairs.Length)
.ToArray();

Codes = topPairs.Zip(avialableChars, (pair, c) => (pair, c)).ToArray();

string result = Input;
foreach (var code in Codes)
result = result.Replace(code.Item1, code.Item2.ToString());

Output = result;
}

public override string ToString() => Output ?? string.Empty;
}
31 changes: 31 additions & 0 deletions Lab9/Purple/Task4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Lab9.Purple;

public class Task4 : Purple
{
public string Output { get; private set; }
public (string, char)[] Codes { get; private set;}

public Task4(string text, (string, char)[] codes) : base(text)
{
Codes = codes;
Output = string.Empty;
}

public override void Review()
{
if (string.IsNullOrEmpty(Input))
{
Output = Input ?? string.Empty;
return;
}

string result = Input;

foreach (var code in Codes)
result = result.Replace(code.Item2.ToString(), code.Item1);

Output = result;
}

public override string ToString() => Output ?? string.Empty;
}
Loading
Loading