Merge pull request #3 from LuisM000/master

Character and Sentences options added
This commit is contained in:
Javier Suárez Ruiz 2019-01-29 20:14:19 +01:00 коммит произвёл GitHub
Родитель 449ff389b2 a3ad7add28
Коммит a97a8c6430
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 76 добавлений и 13 удалений

Просмотреть файл

@ -23,5 +23,27 @@ namespace VS4Mac.TextGenerator.Tests
Assert.AreEqual(2, words.Length);
}
[Test]
public void GenerateCharactersTest()
{
var textGeneratorService = new TextGeneratorService();
var words = textGeneratorService.GenerateCharacters(5);
Assert.AreEqual(1, words.Length);
Assert.AreEqual(5, words[0].Length);
}
[Test]
public void FirstGenerateCharactersAreNotNewLineOrReturnTest()
{
var textGeneratorService = new TextGeneratorService();
var words = textGeneratorService.GenerateCharacters(3);
var characters = words[0];
Assert.That(characters[0], Is.Not.EqualTo('\n').And.Not.EqualTo('\r'));
Assert.That(characters[1], Is.Not.EqualTo('\n').And.Not.EqualTo('\r'));
Assert.That(characters[2], Is.Not.EqualTo('\n').And.Not.EqualTo('\r'));
}
}
}

Просмотреть файл

@ -1,4 +1,5 @@
using NLipsum.Core;
using System.Linq;
using NLipsum.Core;
namespace VS4Mac.TextGenerator.Services
{
@ -20,7 +21,9 @@ namespace VS4Mac.TextGenerator.Services
public string[] GenerateCharacters(int numberOfChars)
{
string[] words = _generator.GenerateCharacters(numberOfChars);
//First three characters are \n or \r
var words = _generator.GenerateCharacters(numberOfChars + 3);
words[0] = words[0].Remove(0, 3);
return words;
}

Просмотреть файл

@ -16,6 +16,10 @@ namespace VS4Mac.TextGenerator.Views
HBox _buttonBox;
Button _generateButton;
HBox _optionsBox;
Label _optionsLabel;
ComboBox _generatorOptionsComboBox;
TextGeneratorService _textGeneratorService;
public GenerateTextDialog()
@ -33,8 +37,8 @@ namespace VS4Mac.TextGenerator.Views
_mainBox = new VBox
{
HeightRequest = 50,
WidthRequest = 200
HeightRequest = 100,
WidthRequest = 235
};
_numberBox = new HBox();
@ -52,17 +56,31 @@ namespace VS4Mac.TextGenerator.Views
LabelColor = Styles.BaseSelectionTextColor,
WidthRequest = 48
};
_optionsBox = new HBox();
_optionsLabel = new Label("Generator option:");
_generatorOptionsComboBox = new ComboBox();
_generatorOptionsComboBox.Items.Add("Characters");
_generatorOptionsComboBox.Items.Add("Words");
_generatorOptionsComboBox.Items.Add("Sentences");
_generatorOptionsComboBox.SelectedIndex = 1;
}
void BuildGui()
{
_optionsBox.PackStart(_optionsLabel);
_optionsBox.PackEnd(_generatorOptionsComboBox);
_numberBox.PackStart(_numberLabel, true);
_numberBox.PackEnd(_numberEntry, false);
_buttonBox.PackEnd(_generateButton);
_mainBox.PackStart(_optionsBox);
_mainBox.PackStart(_numberBox);
_mainBox.PackEnd(_buttonBox);
_numberEntry.SetFocus();
Content = _mainBox;
Resizable = false;
}
@ -71,6 +89,12 @@ namespace VS4Mac.TextGenerator.Views
{
_numberEntry.Changed += OnNumberEntryChanged;
_generateButton.Clicked += OnGenerateClicked;
_generatorOptionsComboBox.SelectionChanged += OnGeneratorOptionsChanged;
}
void OnGeneratorOptionsChanged(object sender, EventArgs e)
{
_numberLabel.Text = $"Introduce number of {_generatorOptionsComboBox.SelectedItem.ToString().ToLower()}:";
}
void OnNumberEntryChanged(object sender, EventArgs e)
@ -86,8 +110,22 @@ namespace VS4Mac.TextGenerator.Views
try
{
var editor = IdeApp.Workbench.ActiveDocument.Editor;
int.TryParse(_numberEntry.Text, out int numberOfWords);
var words = _textGeneratorService.GenerateWords(numberOfWords);
int.TryParse(_numberEntry.Text, out int numberOfElements);
string[] words = null;
switch (_generatorOptionsComboBox.SelectedIndex)
{
case 0:
words = _textGeneratorService.GenerateCharacters(numberOfElements);
break;
case 1:
words = _textGeneratorService.GenerateWords(numberOfElements);
break;
case 2:
words = _textGeneratorService.GenerateSentences(numberOfElements);
break;
}
var result = string.Join(" ", words);
editor.InsertAtCaret(result);