From ac410a0599d7ed85e652fbfc14c7f808e6990456 Mon Sep 17 00:00:00 2001 From: yunus Date: Wed, 4 Oct 2023 01:23:10 +0300 Subject: [PATCH] Fix #19 and updated the unit test and examples. --- AudioExample/MainWindow.cs | 4 +-- Example/Program.cs | 4 +-- MorseSharp/Converter/TextMorseConverter.cs | 15 ++++---- MorseSharp/MorseRepo/IMorseConverter.cs | 4 +-- MorseTest/Languages.cs | 40 +++++++++++----------- MorseTest/TextMorseConverter.cs | 16 ++++----- 6 files changed, 42 insertions(+), 41 deletions(-) diff --git a/AudioExample/MainWindow.cs b/AudioExample/MainWindow.cs index afbb2a9..e39b0d7 100644 --- a/AudioExample/MainWindow.cs +++ b/AudioExample/MainWindow.cs @@ -38,7 +38,7 @@ private async void ToAudioBtn_Click(object sender, EventArgs e) //Update the richtextbox text to morse dash and dots. - MorseTxt.Text = await textConverter.ConvertTextToMorse(MessageMorseTxt.Text); + MorseTxt.Text = textConverter.ConvertTextToMorse(MessageMorseTxt.Text); //Enable the play button to play PlayBtn.Enabled = true; @@ -82,7 +82,7 @@ private async void ToMorseKurdish_Click(object sender, EventArgs e) //Update the richtextbox text to morse dash and dots. - MorseTxt.Text = await textConverter.ConvertTextToMorse(MessageMorseTxt.Text); + MorseTxt.Text = textConverter.ConvertTextToMorse(MessageMorseTxt.Text); //Enable the play button to play PlayBtn.Enabled = true; diff --git a/Example/Program.cs b/Example/Program.cs index 2fbf68e..2ec4f12 100644 --- a/Example/Program.cs +++ b/Example/Program.cs @@ -10,8 +10,8 @@ try { - morse = await Converter.ConvertTextToMorse("The quick brown fox jumps over the lazy dog"); - text = await Converter.ConvertMorseToText("_ .... . / __._ .._ .. _._. _._ / _... ._. ___ .__ _. / .._. ___ _.._ / .___ .._ __ .__. ... / ___ ..._ . ._. / _ .... . / ._.. ._ __.. _.__ / _.. ___ __."); + morse = Converter.ConvertTextToMorse("The quick brown fox jumps over the lazy dog"); + text = Converter.ConvertMorseToText("_ .... . / __._ .._ .. _._. _._ / _... ._. ___ .__ _. / .._. ___ _.._ / .___ .._ __ .__. ... / ___ ..._ . ._. / _ .... . / ._.. ._ __.. _.__ / _.. ___ __."); } catch (Exception ex) { diff --git a/MorseSharp/Converter/TextMorseConverter.cs b/MorseSharp/Converter/TextMorseConverter.cs index 3b0408f..2cdfe6f 100644 --- a/MorseSharp/Converter/TextMorseConverter.cs +++ b/MorseSharp/Converter/TextMorseConverter.cs @@ -32,15 +32,16 @@ public TextMorseConverter(Language Language) /// of the morse result. /// Throws if a character doesn't presented. /// Throws if the string text was null. - public Task ConvertTextToMorse(string Text) + public string ConvertTextToMorse(string Text) { strBuilder!.Clear(); - if ((language & nonLatin) == 0) - Text = Text.ToUpper(); - if (Text is not null) { + if ((language & nonLatin) == 0) + Text = Text.ToUpper(); + + for (int i = 0; i < Text.Length; i++) { if (morseChar.ContainsKey(Text[i])) @@ -55,7 +56,7 @@ public Task ConvertTextToMorse(string Text) } } - return Task.Run(() => strBuilder.ToString()); + return strBuilder.ToString(); } throw new ArgumentNullException(nameof(Text)); @@ -68,7 +69,7 @@ public Task ConvertTextToMorse(string Text) /// of the converted morse. /// Throws if the morse letter was not presented. /// Throw if Morse param was null. - public Task ConvertMorseToText(string Morse) + public string ConvertMorseToText(string Morse) { strBuilder!.Clear(); @@ -94,7 +95,7 @@ public Task ConvertMorseToText(string Morse) } } - return Task.Run(() => strBuilder.ToString()); + return strBuilder.ToString(); } throw new ArgumentNullException(nameof(Morse)); } diff --git a/MorseSharp/MorseRepo/IMorseConverter.cs b/MorseSharp/MorseRepo/IMorseConverter.cs index b3265d9..cad78ea 100644 --- a/MorseSharp/MorseRepo/IMorseConverter.cs +++ b/MorseSharp/MorseRepo/IMorseConverter.cs @@ -10,12 +10,12 @@ public interface IMorseConverter /// /// The to convert to morse code. /// of the morse result. - Task ConvertTextToMorse(string Text); + string ConvertTextToMorse(string Text); /// /// Converts a morse message to english sentence /// /// The of the morse message. /// of the converted morse. - Task ConvertMorseToText(string Morse); + string ConvertMorseToText(string Morse); } } diff --git a/MorseTest/Languages.cs b/MorseTest/Languages.cs index a388926..5768024 100644 --- a/MorseTest/Languages.cs +++ b/MorseTest/Languages.cs @@ -10,103 +10,103 @@ namespace MorseTest public class Languages { [Fact] - public async void EnglishToMorse() + public void EnglishToMorse() { TextMorseConverter Converter = new TextMorseConverter(Language.English); - var morse = await Converter.ConvertTextToMorse("The quick brown fox jumps over the lazy dog"); + var morse = Converter.ConvertTextToMorse("The quick brown fox jumps over the lazy dog"); Assert.Equal("_ .... . / __._ .._ .. _._. _._ / _... ._. ___ .__ _. / .._. ___ _.._ / .___ .._ __ .__. ... / ___ ..._ . ._. / _ .... . / ._.. ._ __.. _.__ / _.. ___ __. ", morse); } [Fact] - public async void KurdishToMorse() + public void KurdishToMorse() { TextMorseConverter Converter = new TextMorseConverter(Language.Kurdish); - var morse = await Converter.ConvertTextToMorse("کۆژین و ڤیان چوونە بۆ باغەکە ئاوی ساردیان دا بە خرینگ و عەگ و قوڵینگەکان ئینجا پەروازەکانیان فڕاند دواتریش هەموو حاجیلەکانیان چنی"); + var morse = Converter.ConvertTextToMorse("کۆژین و ڤیان چوونە بۆ باغەکە ئاوی ساردیان دا بە خرینگ و عەگ و قوڵینگەکان ئینجا پەروازەکانیان فڕاند دواتریش هەموو حاجیلەکانیان چنی"); Assert.Equal("_.... ._._ __. .. _. / .__ / .._.. .. ._ _. / ___. .__ .__ _. . / _... ._._ / _... ._ ..__ . _.... . / .._.. ._ .__ .. / ... ._ _._ _.. .. ._ _. / _.. ._ / _... . / _.._ _._ .. _. __._ / .__ / ___ . __._ / .__ / ...___ .__ ..._ .. _. __._ . _.... ._ _. / .._.. .. _. .___ ._ / .__. . _._ .__ ._ __.. . _.... ._ _. .. ._ _. / .._. ._. ._ _. _.. / _.. .__ ._ _ _._ .. ____ / _._. . __ .__ .__ / .... ._ .___ .. ._.. . _.... ._ _. .. ._ _. / ___. _. .. ", morse); } [Fact] - public async void KurdishLatinToMorse() + public void KurdishLatinToMorse() { TextMorseConverter Converter = new TextMorseConverter(Language.KurdishLatin); - var morse = await Converter.ConvertTextToMorse("Cem vî Fekoyê pîs zêdetir ji çar gulên xweşik hebûn"); + var morse = Converter.ConvertTextToMorse("Cem vî Fekoyê pîs zêdetir ji çar gulên xweşik hebûn"); Assert.Equal(".___ . __ / .._.. .. / .._. . _.... ._._ ..__ .._ / .__. .. ... / __.. .._ _.. . _ .._.. _._ / __. .._.. / ___. ._ _._ / __._ .__ ._.. .._ _. / _.._ ___ . ____ .._.. _.... / _._. . _... .__.__ _. ", morse); } [Fact] - public async void ArabicToMorse() + public void ArabicToMorse() { TextMorseConverter Converter = new TextMorseConverter(Language.Arabic); - var morse = await Converter.ConvertTextToMorse("ابجد هوز حطي كلمن سعفص قرشت ثخذ ضظغ"); + var morse = Converter.ConvertTextToMorse("ابجد هوز حطي كلمن سعفص قرشت ثخذ ضظغ"); Assert.Equal("._ _... .___ _.. / .._.. .__ ___. / .... .._ .. / _._ ._.. __ _. / ... ._._ .._. _.._ / __._ ._. ____ _ / _._. ___ __.. / ..._ _.__ __. ", morse); } [Fact] - public async void DeutschToMorse() + public void DeutschToMorse() { TextMorseConverter Converter = new TextMorseConverter(Language.Deutsch); - var morse = await Converter.ConvertTextToMorse("Victor jagt zwölf Boxkämpfer quer über den groẞen Sylter Deich"); + var morse = Converter.ConvertTextToMorse("Victor jagt zwölf Boxkämpfer quer über den groẞen Sylter Deich"); Assert.Equal("..._ .. _._. _ ___ _._ / .___ ._ __. _ / __.. .__ ___. ._.. .._. / _... ___ _.._ _._ ._._ __ .__. .._. . _._ / __._ .._ . _._ / ..__ _... . _._ / _.. . _. / __. _._ ___ ...... . _. / ... _.__ ._.. _ . _._ / _.. . .. _._. .... ", morse); } [Fact] - public async void EspanolToMorse() + public void EspanolToMorse() { TextMorseConverter Converter = new TextMorseConverter(Language.Espanol); - var morse = await Converter.ConvertTextToMorse("El jefe buscó el éxtasis en un imprevisto baño de whisky y gozó como un duque"); + var morse = Converter.ConvertTextToMorse("El jefe buscó el éxtasis en un imprevisto baño de whisky y gozó como un duque"); Assert.Equal(". ._.. / .___ . .._. . / _... .._ ... _._. ___. / . ._.. / .._.. _.._ _ ._ ... .. ... / . _. / .._ _. / .. __ .__. ._. . ..._ .. ... _ ___ / _... ._ __.__ ___ / _.. . / .__ .... .. ... _._ _.__ / _.__ / __. ___ __.. ___. / _._. ___ __ ___ / .._ _. / _.. .._ __._ .._ . ", morse); } [Fact] - public async void FrancaisToMorse() + public void FrancaisToMorse() { TextMorseConverter Converter = new TextMorseConverter(Language.Francais); - var morse = await Converter.ConvertTextToMorse("Portez ce vieux whisky au juge blond qui fume"); + var morse = Converter.ConvertTextToMorse("Portez ce vieux whisky au juge blond qui fume"); Assert.Equal(".__. ___ ._. _ . __.. / _._. . / ..._ .. . .._ _.._ / .__ .... .. ... _._ _.__ / ._ .._ / .___ .._ __. . / _... ._.. ___ _. _.. / __._ .._ .. / .._. .._ __ . ", morse); } [Fact] - public async void ItalianoToMorse() + public void ItalianoToMorse() { TextMorseConverter Converter = new TextMorseConverter(Language.Italiano); - var morse = await Converter.ConvertTextToMorse("Pranzo d'acqua fa volti sghembi"); + var morse = Converter.ConvertTextToMorse("Pranzo d'acqua fa volti sghembi"); Assert.Equal(".__. ._. ._ _. __.. ___ / _.. .____. ._ _._. __._ .._ ._ / .._. ._ / ..._ ___ ._.. _ .. / ... __. .... . __ _... .. ", morse); } [Fact] - public async void JapaneseToMorse() + public void JapaneseToMorse() { TextMorseConverter Converter = new TextMorseConverter(Language.Japanese); - var morse = await Converter.ConvertTextToMorse("アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン"); + var morse = Converter.ConvertTextToMorse("アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン"); Assert.Equal("__.__ ._ .._ _.___ ._... ._.. _._.. ..._ _.__ ____ _._._ __._. ___._ .___. ___. _. .._. .__. ._.__ .._.. ._. _._. .... __._ ..__ _... __.._ __.. . _.. _.._ .._._ _ _..._ _.._. .__ _..__ __ ... __. _.__. ___ ._._ _._ .___ ._._. ", morse); } [Fact] - public async void PortuguesToMorse() + public void PortuguesToMorse() { TextMorseConverter Converter = new TextMorseConverter(Language.Portugues); - var morse = await Converter.ConvertTextToMorse("Um pequeno jabuti xereta viu dez cegonhas felizes"); + var morse = Converter.ConvertTextToMorse("Um pequeno jabuti xereta viu dez cegonhas felizes"); Assert.Equal(".._ __ / .__. . __._ .._ . _. ___ / .___ ._ _... .._ _ .. / _.._ . ._. . _ ._ / ..._ .. .._ / _.. . __.. / _._. . __. ___ _. .... ._ ... / .._. . ._.. .. __.. . ... ", morse); diff --git a/MorseTest/TextMorseConverter.cs b/MorseTest/TextMorseConverter.cs index cc99d78..6574768 100644 --- a/MorseTest/TextMorseConverter.cs +++ b/MorseTest/TextMorseConverter.cs @@ -5,10 +5,10 @@ namespace MorseTest public class TextMorseConverterTest { [Fact] - public async void ConvertMorseWithValidString() + public void ConvertMorseWithValidString() { TextMorseConverter converter = new TextMorseConverter(Language.English); - var morse = await converter.ConvertTextToMorse("Hi There"); + var morse = converter.ConvertTextToMorse("Hi There"); Assert.True(morse.Length > 0); } @@ -16,32 +16,32 @@ public async void ConvertMorseWithValidString() public void ConvertMorseWithNullString() { TextMorseConverter converter = new TextMorseConverter(Language.English); - Assert.ThrowsAsync(async () => await converter.ConvertTextToMorse(null)); + Assert.Throws(() => converter.ConvertTextToMorse(null)); } [Fact] public void ConvertMorseWithInvalidCharacter() { var converter = new TextMorseConverter(Language.Kurdish); - Assert.ThrowsAsync(async () => await converter.ConvertTextToMorse("Hi~")); + Assert.Throws(() => converter.ConvertTextToMorse("Hi~")); } [Fact] - public async void ConvertTextWithValidMorse() + public void ConvertTextWithValidMorse() { TextMorseConverter converter = new TextMorseConverter(Language.Kurdish); - var morse = await converter.ConvertMorseToText("... ..._ ._ .__"); + var morse = converter.ConvertMorseToText("... ..._ ._ .__"); Assert.True(morse.Length > 0); } [Fact] public void ConvertTextWithNullMorse() { TextMorseConverter converter = new TextMorseConverter(Language.Kurdish); - Assert.ThrowsAsync(async () => await converter.ConvertMorseToText(null)); + Assert.Throws(() => converter.ConvertMorseToText(null)); } [Fact] public void ConvertTextWithInvalidMorse() { TextMorseConverter converter = new TextMorseConverter(Language.Kurdish); - Assert.ThrowsAsync(async () => await converter.ConvertMorseToText("........")); + Assert.Throws(() => converter.ConvertMorseToText("........")); }