From 81918b1e5ff58755d1b41b8b768d0947349689d6 Mon Sep 17 00:00:00 2001 From: Alan Hemmings Date: Fri, 24 Jan 2020 01:02:51 +0000 Subject: [PATCH] fix test --- backlog.md | 4 +-- change-log.md | 6 ++++ src/Konsole.Tests/FormTests/WriteShould.cs | 38 ++++++++++++++++++++++ src/Konsole.Tests/Internal/Person.cs | 9 ++++- src/Konsole/Forms/FieldReader.cs | 26 ++++++++++++--- 5 files changed, 75 insertions(+), 8 deletions(-) diff --git a/backlog.md b/backlog.md index 0ac0aaa..cf85b73 100644 --- a/backlog.md +++ b/backlog.md @@ -1,10 +1,9 @@ # Roadmap -- update dotnetcore workflow to use sln, to simplify build. +- Add support to fields to Form.Write #44 ## Busy next -- Add support to fields to Form.Write #44 - simple input, ReadLine() - create proper highspeedwriter interface that's used by the private highspeed writer, or App? so that the interface is real and not fakes. - write Mac HighSpeedWriter and test locally @@ -36,6 +35,7 @@ - do pull request and squash commits after a nice cleanup so that my commits can be easier to read. - OpenBox - list view :D +- move .source.md files to seperate folder. (mdsnippets) - fix bug - when creating an inline window from an new ConcurrentWriter() IConsole the colors are wrong? See spike "paul". - NB! Set scrollable region (using VT100 commands!) - so that you can have input region at the bottom, and "app" at the top? diff --git a/change-log.md b/change-log.md index 89424fc..cd4156e 100644 --- a/change-log.md +++ b/change-log.md @@ -2,6 +2,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) +## [6.2.0] + +### Added + +- Form Write now supports fields as well as properties. + ## [6.1.0] ### Added diff --git a/src/Konsole.Tests/FormTests/WriteShould.cs b/src/Konsole.Tests/FormTests/WriteShould.cs index 699c4b3..e01d381 100644 --- a/src/Konsole.Tests/FormTests/WriteShould.cs +++ b/src/Konsole.Tests/FormTests/WriteShould.cs @@ -7,7 +7,45 @@ namespace Konsole.Tests.FormTests { public class WriteShould { + internal class Cat + { + public Cat(int age, string breed, string name) + { + Breed = breed; + Age = age; + Name = name; + } + public static string StarSign = "LEO"; + public int Age; + public string Breed { get; set; } + private string Breeder { get; set; } = "private"; + public string Name; + public int NumberOfKittens = 3; + } + [Test] + public void print_public_properties_then_fields() + { + var console = new MockConsole(80, 20); + var form = new Form(console); + var cat = new Cat(10, "Tabby", "Fred"); + console.WriteLine("line1"); + form.Write(cat); + console.WriteLine("line2"); + var expected = new[] + { + "line1", + " ┌──────────────────────────────────── Cat ────────────────────────────────────┐", + " │ Breed : Tabby │", + " │ Age : 10 │", + " │ Name : Fred │", + " │ Number Of Kittens : 3 │", + " └─────────────────────────────────────────────────────────────────────────────┘", + "line2" + }; + + console.BufferWrittenTrimmed.Should().BeEquivalentTo(expected); + } [Test] public void show_the_form_inline_at_the_next_line_below_current_cursor_position_and_update_cursor() { diff --git a/src/Konsole.Tests/Internal/Person.cs b/src/Konsole.Tests/Internal/Person.cs index 005c177..5ce7b84 100644 --- a/src/Konsole.Tests/Internal/Person.cs +++ b/src/Konsole.Tests/Internal/Person.cs @@ -1,12 +1,19 @@ namespace Konsole.Tests.TestClasses { - public class Person + internal class Person { public Person() { Address = new Address(); } + public static string StarSign = "LEO"; + + public static decimal Height + { + get { return 10.5M; } + } + public string FirstName { get; set; } public string LastName { get; set; } public string AFieldWithAMuchLongerName { get; set; } diff --git a/src/Konsole/Forms/FieldReader.cs b/src/Konsole/Forms/FieldReader.cs index 2638b8e..8005dd0 100644 --- a/src/Konsole/Forms/FieldReader.cs +++ b/src/Konsole/Forms/FieldReader.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; namespace Konsole.Forms @@ -39,9 +40,12 @@ public FieldReader(object @object) typeof (DateTime) }.Concat(NumericTypes).ToArray(); + /// + /// reads the simple non generic public fields and properties that are numbers, strings, dateTime and booleans. + /// public FieldList ReadFieldList() { - var fields = readFields(); + var fields = readPropertiesAndFields(); int width = fields.Any() ? fields.Max(f => f.Caption.Length) : 10; var fieldlist = new FieldList(fields.ToArray(), width); return fieldlist; @@ -60,14 +64,16 @@ public static bool IsNumericType(Type type) } - private IEnumerable readFields() + private IEnumerable readPropertiesAndFields() { - var properties = _type.GetProperties(); + var fields = _type.GetFields( BindingFlags.Public | BindingFlags.Instance); + var supportedFields = fields.Where(f => SupportedTypes.Contains(NonGenericType(f.FieldType))); + var properties = _type.GetProperties(BindingFlags.Public | BindingFlags.Instance); var supportedProps = properties .Where(f => SupportedTypes.Contains(NonGenericType(f.PropertyType))); - var fields = supportedProps + var _props = supportedProps .Select(f => new Field( ParseFieldType(f.PropertyType), f.Name, @@ -75,7 +81,17 @@ private IEnumerable readFields() IsNullable(f.PropertyType), f.GetValue(_object) )); - return fields; + var _fields = supportedFields + .Select(f => new Field( + ParseFieldType(f.FieldType), + f.Name, + ToCaption(f.Name), + IsNullable(f.FieldType), + f.GetValue(_object) + )); + + var fieldsAndProps = _props.Concat(_fields); + return fieldsAndProps; } public static FieldType ParseFieldType(Type type)