Add new exercises for csharp

This commit is contained in:
Daniel Siepmann 2024-03-26 07:32:35 +01:00
parent fc27e7631b
commit dd395435af
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
14 changed files with 816 additions and 0 deletions

View file

@ -0,0 +1,26 @@
{
"authors": [
"ErikSchierboom"
],
"contributors": [
"yzAlvin"
],
"files": {
"solution": [
"AnnalynsInfiltration.cs"
],
"test": [
"AnnalynsInfiltrationTests.cs"
],
"exemplar": [
".meta/Exemplar.cs"
],
"invalidator": [
"AnnalynsInfiltration.csproj"
]
},
"forked_from": [
"fsharp/annalyns-infiltration"
],
"blurb": "Learn about booleans while helping Annalyn rescue her friend."
}

View file

@ -0,0 +1,24 @@
using System;
static class QuestLogic
{
public static bool CanFastAttack(bool knightIsAwake)
{
throw new NotImplementedException("Please implement the (static) QuestLogic.CanFastAttack() method");
}
public static bool CanSpy(bool knightIsAwake, bool archerIsAwake, bool prisonerIsAwake)
{
throw new NotImplementedException("Please implement the (static) QuestLogic.CanSpy() method");
}
public static bool CanSignalPrisoner(bool archerIsAwake, bool prisonerIsAwake)
{
throw new NotImplementedException("Please implement the (static) QuestLogic.CanSignalPrisoner() method");
}
public static bool CanFreePrisoner(bool knightIsAwake, bool archerIsAwake, bool prisonerIsAwake, bool petDogIsPresent)
{
throw new NotImplementedException("Please implement the (static) QuestLogic.CanFreePrisoner() method");
}
}

View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,313 @@
using Xunit;
using Exercism.Tests;
public class AnnalynsInfiltrationTests
{
[Fact]
[Task(1)]
public void Cannot_execute_fast_attack_if_knight_is_awake()
{
var knightIsAwake = true;
Assert.False(QuestLogic.CanFastAttack(knightIsAwake));
}
[Fact]
[Task(1)]
public void Can_execute_fast_attack_if_knight_is_sleeping()
{
var knightIsAwake = false;
Assert.True(QuestLogic.CanFastAttack(knightIsAwake));
}
[Fact]
[Task(2)]
public void Cannot_spy_if_everyone_is_sleeping()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = false;
Assert.False(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_everyone_but_knight_is_sleeping()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = false;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_everyone_but_archer_is_sleeping()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = false;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_everyone_but_prisoner_is_sleeping()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = true;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_only_knight_is_sleeping()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = true;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_only_archer_is_sleeping()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = true;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_only_prisoner_is_sleeping()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = false;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_everyone_is_awake()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = true;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(3)]
public void Can_signal_prisoner_if_archer_is_sleeping_and_prisoner_is_awake()
{
var archerIsAwake = false;
var prisonerIsAwake = true;
Assert.True(QuestLogic.CanSignalPrisoner(archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(3)]
public void Cannot_signal_prisoner_if_archer_is_awake_and_prisoner_is_sleeping()
{
var archerIsAwake = true;
var prisonerIsAwake = false;
Assert.False(QuestLogic.CanSignalPrisoner(archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(3)]
public void Cannot_signal_prisoner_if_archer_and_prisoner_are_both_sleeping()
{
var archerIsAwake = false;
var prisonerIsAwake = false;
Assert.False(QuestLogic.CanSignalPrisoner(archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(3)]
public void Cannot_signal_prisoner_if_archer_and_prisoner_are_both_awake()
{
var archerIsAwake = true;
var prisonerIsAwake = true;
Assert.False(QuestLogic.CanSignalPrisoner(archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_everyone_is_awake_and_pet_dog_is_present()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = true;
var petDogIsPresent = true;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_everyone_is_awake_and_pet_dog_is_absent()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = true;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Can_free_prisoner_if_everyone_is_asleep_and_pet_dog_is_present()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = false;
var petDogIsPresent = true;
Assert.True(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_everyone_is_asleep_and_pet_dog_is_absent()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = false;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Can_free_prisoner_if_only_prisoner_is_awake_and_pet_dog_is_present()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = true;
var petDogIsPresent = true;
Assert.True(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Can_free_prisoner_if_only_prisoner_is_awake_and_pet_dog_is_absent()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = true;
var petDogIsPresent = false;
Assert.True(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_archer_is_awake_and_pet_dog_is_present()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = false;
var petDogIsPresent = true;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_archer_is_awake_and_pet_dog_is_absent()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = false;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Can_free_prisoner_if_only_knight_is_awake_and_pet_dog_is_present()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = false;
var petDogIsPresent = true;
Assert.True(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_knight_is_awake_and_pet_dog_is_absent()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = false;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_knight_is_asleep_and_pet_dog_is_present()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = true;
var petDogIsPresent = true;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_knight_is_asleep_and_pet_dog_is_absent()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = true;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Can_free_prisoner_if_only_archer_is_asleep_and_pet_dog_is_present()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = true;
var petDogIsPresent = true;
Assert.True(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_archer_is_asleep_and_pet_dog_is_absent()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = true;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_prisoner_is_asleep_and_pet_dog_is_present()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = false;
var petDogIsPresent = true;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_prisoner_is_asleep_and_pet_dog_is_absent()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = false;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
}

View file

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.org/docs/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit AnnalynsInfiltration.cs` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [C# track's documentation](https://exercism.org/docs/tracks/csharp)
- The [C# track's programming category on the forum](https://forum.exercism.org/c/programming/csharp)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [/r/csharp](https://www.reddit.com/r/csharp) is the C# subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/c%23) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

View file

@ -0,0 +1,12 @@
# Hints
## General
- There are three [boolean operators][operators] to work with boolean values.
- Multiple operators can be combined in a single expression.
## 1. Check if a fast attack can be made
- The [boolean operators][operators] can also be applied to boolean parameters.
[operators]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators

View file

@ -0,0 +1,93 @@
# Annalyn's Infiltration
Welcome to Annalyn's Infiltration on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
## Booleans
Booleans in C# are represented by the `bool` type, which values can be either `true` or `false`.
C# supports three boolean operators: `!` (NOT), `&&` (AND), and `||` (OR).
## Instructions
In this exercise, you'll be implementing the quest logic for a new RPG game a friend is developing.
The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog. Unfortunately, disaster strikes, as her best friend was kidnapped while searching for berries in the forest.
Annalyn will try to find and free her best friend, optionally taking her dog with her on this quest.
After some time spent following her best friend's trail, she finds the camp in which her best friend is imprisoned. It turns out there are two kidnappers: a mighty knight and a cunning archer.
Having found the kidnappers, Annalyn considers which of the following actions she can engage in:
- _Fast attack_: a fast attack can be made if the knight is sleeping, as it takes time for him to get his armor on, so he will be vulnerable.
- _Spy_: the group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.
- _Signal prisoner_: the prisoner can be signalled using bird sounds if the prisoner is awake and the archer is sleeping, as archers are trained in bird signaling so they could intercept the message.
- _Free prisoner_: Annalyn can try sneaking into the camp to free the prisoner.
This is a risky thing to do and can only succeed in one of two ways:
- If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep.
The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape.
- If Annalyn does not have her dog then she and the prisoner must be very sneaky!
Annalyn can free the prisoner if the prisoner is awake and the knight and archer are both sleeping, but if the prisoner is sleeping they can't be rescued: the prisoner would be startled by Annalyn's sudden appearance and wake up the knight and archer.
You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters found in the forest and whether Annalyn's pet dog is present or not.
## 1. Check if a fast attack can be made
Implement the (_static_) `QuestLogic.CanFastAttack()` method that takes a boolean value that indicates if the knight is awake. This method returns `true` if a fast attack can be made based on the state of the knight. Otherwise, returns `false`:
```csharp
var knightIsAwake = true;
QuestLogic.CanFastAttack(knightIsAwake);
// => false
```
## 2. Check if the group can be spied upon
Implement the (_static_) `QuestLogic.CanSpy()` method that takes three boolean values, indicating if the knight, archer and the prisoner, respectively, are awake. The method returns `true` if the group can be spied upon, based on the state of the three characters. Otherwise, returns `false`:
```csharp
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = false;
QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake);
// => true
```
## 3. Check if the prisoner can be signalled
Implement the (_static_) `QuestLogic.CanSignalPrisoner()` method that takes two boolean values, indicating if the archer and the prisoner, respectively, are awake. The method returns `true` if the prisoner can be signalled, based on the state of the two characters. Otherwise, returns `false`:
```csharp
var archerIsAwake = false;
var prisonerIsAwake = true;
QuestLogic.CanSignalPrisoner(archerIsAwake, prisonerIsAwake);
// => true
```
## 4. Check if the prisoner can be freed
Implement the (_static_) `QuestLogic.CanFreePrisoner()` method that takes four boolean values. The first three parameters indicate if the knight, archer and the prisoner, respectively, are awake. The last parameter indicates if Annalyn's pet dog is present. The method returns `true` if the prisoner can be freed based on the state of the three characters and Annalyn's pet dog presence. Otherwise, it returns `false`:
```csharp
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = false;
var petDogIsPresent = false;
QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent);
// => false
```
## Source
### Created by
- @ErikSchierboom
### Contributed to by
- @yzAlvin

View file

@ -0,0 +1,23 @@
{
"authors": [
"ErikSchierboom"
],
"contributors": [
"yzAlvin"
],
"files": {
"solution": [
"LogLevels.cs"
],
"test": [
"LogLevelsTests.cs"
],
"exemplar": [
".meta/Exemplar.cs"
],
"invalidator": [
"LogLevels.csproj"
]
},
"blurb": "Learn about strings by processing logs."
}

39
csharp/log-levels/HELP.md Normal file
View file

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.org/docs/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit LogLevels.cs` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [C# track's documentation](https://exercism.org/docs/tracks/csharp)
- The [C# track's programming category on the forum](https://forum.exercism.org/c/programming/csharp)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [/r/csharp](https://www.reddit.com/r/csharp) is the C# subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/c%23) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

View file

@ -0,0 +1,27 @@
# Hints
## General
- The [csharp.net strings tutorial][tutorial-csharp.net-strings] has a nice introduction to C# `string`s.
- The `string` class has many useful [built-in methods][docs-string-methods].
## 1. Get message from a log line
- Different options to search for text in a string are explored in [this tutorial][tutorial-docs.microsoft.com-search-text-in-string].
- Removing white space is [built-in][tutorial-docs.microsoft.com-trim-white-space].
## 2. Get log level from a log line
- Changing a `string`'s casing is explored in [this tutorial][tutorial-docs.microsoft.com-changing-case].
## 3. Reformat a log line
- There are several ways to [concatenate strings][tutorial-docs.microsoft.com-concatenate-strings], but the preferred one is usually [string interpolation][tutorial-csharp.net-string-interpolation].
[docs-string-methods]: https://docs.microsoft.com/en-us/dotnet/api/system.string
[tutorial-docs.microsoft.com-search-text-in-string]: https://docs.microsoft.com/en-us/dotnet/csharp/how-to/search-strings#where-does-the-sought-text-occur-in-a-string
[tutorial-docs.microsoft.com-trim-white-space]: https://docs.microsoft.com/en-us/dotnet/csharp/how-to/modify-string-contents#trim-white-space
[tutorial-docs.microsoft.com-changing-case]: https://docs.microsoft.com/en-us/dotnet/standard/base-types/changing-case
[tutorial-docs.microsoft.com-concatenate-strings]: https://docs.microsoft.com/en-us/dotnet/csharp/how-to/concatenate-multiple-strings
[tutorial-csharp.net-strings]: https://csharp.net-tutorials.com/data-types/strings/
[tutorial-csharp.net-string-interpolation]: https://csharp.net-tutorials.com/operators/the-string-interpolation-operator/

View file

@ -0,0 +1,19 @@
using System;
static class LogLine
{
public static string Message(string logLine)
{
throw new NotImplementedException("Please implement the (static) LogLine.Message() method");
}
public static string LogLevel(string logLine)
{
throw new NotImplementedException("Please implement the (static) LogLine.LogLevel() method");
}
public static string Reformat(string logLine)
{
throw new NotImplementedException("Please implement the (static) LogLine.Reformat() method");
}
}

View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,82 @@
using Xunit;
using Exercism.Tests;
public class LogLevelsTests
{
[Fact]
[Task(1)]
public void Error_message()
{
Assert.Equal("Stack overflow", LogLine.Message("[ERROR]: Stack overflow"));
}
[Fact]
[Task(1)]
public void Warning_message()
{
Assert.Equal("Disk almost full", LogLine.Message("[WARNING]: Disk almost full"));
}
[Fact]
[Task(1)]
public void Info_message()
{
Assert.Equal("File moved", LogLine.Message("[INFO]: File moved"));
}
[Fact]
[Task(1)]
public void Message_with_leading_and_trailing_white_space()
{
Assert.Equal("Timezone not set", LogLine.Message("[WARNING]: \tTimezone not set \r\n"));
}
[Fact]
[Task(2)]
public void Error_log_level()
{
Assert.Equal("error", LogLine.LogLevel("[ERROR]: Disk full"));
}
[Fact]
[Task(2)]
public void Warning_log_level()
{
Assert.Equal("warning", LogLine.LogLevel("[WARNING]: Unsafe password"));
}
[Fact]
[Task(2)]
public void Info_log_level()
{
Assert.Equal("info", LogLine.LogLevel("[INFO]: Timezone changed"));
}
[Fact]
[Task(3)]
public void Error_reformat()
{
Assert.Equal("Segmentation fault (error)", LogLine.Reformat("[ERROR]: Segmentation fault"));
}
[Fact]
[Task(3)]
public void Warning_reformat()
{
Assert.Equal("Decreased performance (warning)", LogLine.Reformat("[WARNING]: Decreased performance"));
}
[Fact]
[Task(3)]
public void Info_reformat()
{
Assert.Equal("Disk defragmented (info)", LogLine.Reformat("[INFO]: Disk defragmented"));
}
[Fact]
[Task(3)]
public void Reformat_with_leading_and_trailing_white_space()
{
Assert.Equal("Corrupt disk (error)", LogLine.Reformat("[ERROR]: \t Corrupt disk\t \t \r\n"));
}
}

View file

@ -0,0 +1,91 @@
# Log Levels
Welcome to Log Levels on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
## Strings
A `string` in C# is an object that represents immutable text as a sequence of Unicode characters (letters, digits, punctuation, etc.). Double quotes are used to define a `string` instance:
```csharp
string fruit = "Apple";
```
Strings are manipulated by calling the string's methods. Once a string has been constructed, its value can never change. Any methods that appear to modify a string will actually return a new string.
Multiple strings can be concatenated (added) together. The simplest way to achieve this is by using the `+` operator.
```csharp
string name = "Jane";
"Hello " + name + "!";
// => "Hello Jane!"
```
For any string formatting more complex than simple concatenation, string interpolation is preferred. To use interpolation in a string, prefix it with the dollar (`$`) symbol. Then you can use curly braces (`{}`) to access any variables inside your string.
```csharp
string name = "Jane";
$"Hello {name}!";
// => "Hello Jane!"
```
## Instructions
In this exercise you'll be processing log-lines.
Each log line is a string formatted as follows: `"[<LEVEL>]: <MESSAGE>"`.
There are three different log levels:
- `INFO`
- `WARNING`
- `ERROR`
You have three tasks, each of which will take a log line and ask you to do something with it.
## 1. Get message from a log line
Implement the (_static_) `LogLine.Message()` method to return a log line's message:
```csharp
LogLine.Message("[ERROR]: Invalid operation")
// => "Invalid operation"
```
Any leading or trailing white space should be removed:
```csharp
LogLine.Message("[WARNING]: Disk almost full\r\n")
// => "Disk almost full"
```
## 2. Get log level from a log line
Implement the (_static_) `LogLine.LogLevel()` method to return a log line's log level, which should be returned in lowercase:
```csharp
LogLine.LogLevel("[ERROR]: Invalid operation")
// => "error"
```
## 3. Reformat a log line
Implement the (_static_) `LogLine.Reformat()` method that reformats the log line, putting the message first and the log level after it in parentheses:
```csharp
LogLine.Reformat("[INFO]: Operation completed")
// => "Operation completed (info)"
```
## Source
### Created by
- @ErikSchierboom
### Contributed to by
- @yzAlvin