Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 6 additions & 8 deletions TagCloud/Colors/Factories/WordColorizerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ public class WordColorizerFactory(IIndex<string, IWordColorizerCreator> creators
{
public Result<IWordColorizer> Create(TagCloudOptions options)
{
return Result.Of(() =>
{
if (string.IsNullOrWhiteSpace(options.ColorizerName))
return Result.Fail<IWordColorizer>("Colorizer name not specified");
if (string.IsNullOrWhiteSpace(options.ColorizerName))
return Result.Fail<IWordColorizer>("Colorizer name not specified");

if (!creators.TryGetValue(options.ColorizerName, out var creator))
return Result.Fail<IWordColorizer>($"Colorizer {options.ColorizerName} not found");
if (!creators.TryGetValue(options.ColorizerName, out var creator))
return Result.Fail<IWordColorizer>($"Colorizer {options.ColorizerName} not found");

return creator.Create(options);
}).Then(x => x);
return creator.Create(options)
.Then(Result.Ok);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А зачем этот .Then(Result.Ok) появился?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И пробегись по остальным местам где была такая же конструкция .Then(x => x)

}
}
1 change: 1 addition & 0 deletions TagCloud/ResultModel/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static Result<T> Ok<T>(T value)
{
return new Result<T>(null, value);
}

public static Result<None> Ok()
Comment on lines +35 to +37

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

немного форматирование

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Поправил

{
return Ok<None>(null);
Expand Down
80 changes: 80 additions & 0 deletions TagCloudTests/ResultTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using FluentAssertions;
using TagCloud.ResultModel;

namespace TagCloudTests;

public class ResultTests
{
[Test]
public void Ok_ShouldBeSuccessAndContainValue_WhenValueIsProvided()
{
var result = Result.Ok(52);

result.IsSuccess.Should().BeTrue();
result.GetValueOrThrow().Should().Be(52);
}

[Test]
public void Fail_ShouldNotBeSuccessAndContainError_WhenErrorIsProvided()
{
var result = Result.Fail<int>("some error");

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be("some error");
}

[Test]
public void GetValueOrThrow_ShouldThrow_WhenFail()
{
var result = Result.Fail<int>("error");

Action action = () => result.GetValueOrThrow();

action.Should().Throw<InvalidOperationException>();
}

[Test]
public void Then_ShouldTransformValue_WhenSuccess()
{
var result = Result.Ok(3)
.Then(x => x * 3);

result.IsSuccess.Should().BeTrue();
result.GetValueOrThrow().Should().Be(9);
}

[Test]
public void Then_ShouldNotCallNextAction_WhenFail()
{
var isCalled = false;

var result = Result.Fail<int>("error")
.Then(x =>
{
isCalled = true;
return x * 2;
});

result.IsSuccess.Should().BeFalse();
isCalled.Should().BeFalse();
}

[Test]
public void Of_ShouldCatchExceptionAndReturnFail_WhenExceptionIsInside()
{
var result = Result.Of<int>(() => throw new Exception("error"));

result.IsSuccess.Should().BeFalse();
result.Error.Should().Contain("error");
}

[Test]
public void RefineError_ShouldPrefixErrorMessage()
{
var result = Result.Fail<int>("error")
.RefineError("refined error");

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be("refined error. error");
}
}