Files
Terminal.Gui/UnitTests/Configuration/JsonConverterTests.cs
dodexahedron 92a601254e Fixes #3170. Color improvements (#3204)
* Reduces indentation by remove the namespace braces.

* Reduces indentation and removes unused using.

* Ensures clear selection if it isn't selecting.

* Turn on nullability context for TryParse and update usages as needed.

* Use IsNullOrWhiteSpace, which includes IsNullOrEmpty

All-whitespace values are also illegal, so may as well handle that here too

* Respect the nullable here

* It's a struct

* Use byte.MaxValue and add remark

* Just use the bytes directly

* Must respect endianness

* Add uint constructor so consumers don't have to do unchecked math

* Completely re-work parsing and implement ISpanParsable<Color>

All parsing is now almost-0 allocation, and is significantly faster than before

* Extension methods required by new code

* Use standard Math.Clamp method here

* Add some new unit tests for TryParse

* De-duplicate code and handle more cases

* Enable nullability context for the file

* Go ahead and enable the language features and analysis

* Implicit usings remove a lot of boilerplate usings.

* Add these to the dictionary to shut spell check up

* Make this thing a record struct and a union, and update constructors

This commit won't build. I'm just breaking out changes a little bit.

* Some additional XmlDoc standardizing

* Make FindClosestColor and CalculateColorDistance use the vector for SIMD

* Add a TryFormat method for support of I*Formattable

* Add an interface for support of custom formatting of Colors

* Pass by in reference

* Parse string delegates to Parse span

* Parse now does all the work

* Remove the old new code from TryParse

* Some new cast operators

* Add IFormattable.ToString implementation

* Add the rest of the code for Color in its current (unfinished) state

* Move that interface to its own file

* Add ColorParseException class

* Move Attribute to its own file, too.

* Re-implement these operators as explicit methods

* Get rid of fuzzy equality operators and update tests to use the named methods that replace them

* Add an explicit test case for ToString with null format string and explicitly specified Invariant culture

* Fix byte orders for hex format to be standard ARGB

* Prove that ToString and Parse can round-trip values

* Unroll this test into parameterized cases

* Fix a couple of comments to match byte order

* Update R# dictionary to match correct byte orders

* Remove stray comment

* Separate all types in this to their own files

* Convert this one to use the handy extension

* Add test for Argb property

* Add a file nesting rule to make some incoming file changes display nicely

* Move constructor tests to their own file and add some new tests

* Add implicit cast from uint

* More constructor tests

* Since this is now a record, the equality operators are compiler generated

Still spot-checking a few arbitrary values for completeness

* Override ToString to delegate to Attribute

* Simplify and clean up ToString. Delegate to ColorScheme

* Update the test to match new output

* These should be fields, really. It's a value type.

* Add some type checks for change control

* Allow unsafe and turn on implicit usings

* Make this one better

* Rename tests and remove redundant checks (the type checks already guarantee field consistency)

* Reorganize a bit

* Make these test 16 random values

* Existing operator tests converted

* That one is now redundant with both of the other tests that check all the named colors

* Move this to type checks and simplify a little bit

* These lambdas can be static

* Move operators to another file.

* Add global using for System.Text because it's EVERYWHERE

* Reorganization of Color and some related types.

Updated usages to reflect changes

* Update tests to reflect changes in Terminal.Gui

* Add missing keyword

* Add entry to dictionary

* Add dotsettings for Terminal.Gui

Only specifies language level

* Commit unsaved changes for usings here

* Implement last remaining TryFormat method

* A little cleanup/formality

* Sorting rules

Sort methods by name and interface they implement

* Sort code

* Match namespace for tests

None!

* Unroll ordinal check and reorganize.

* Sort before writing new tests

* These got reversed...

* Add test to prove explicit cast to Vector3 works properly

* Sort these too

* Add test for uint->Color implicit operator

* Add test for Color->uint implicit operator

* Correct this test name and re-order

* Add test for implicit conversion from Vector3 to Color

* Add test for implicit conversion from Vector4 to Color

* These also got reversed, like with Vector3

* Add test for implicit conversion from Color to Vector4

* Add test for GetHashCode

* Make sure these are all under the same namespace

* Remove a now-redundant test

* Reorganize formatting and parsing tests to another type part

* Tests moved back to Terminal.Gui.DrawingTests namespace as before.

* Add tests for the constructor taking 3 or 4 integers and sort

* Cleanup

 - Renamed some tests
 - Make a test even clearer
 - Removed redundant code
 - Got rid of unused parameter in Constructor_WithColorName_AllChannelsCorrect

* That needs to be from the reverse map

Not broken - just was sub-optimal due to my error.

* Enable nullability context in this file

Not sure how it got removed but whatev

* Respect nullability context in this file now that it's on 🤦‍♂️

* Add tests for expected exceptions with whitespace or null values

* Add test for parameterless constructor

* A couple more places for reference passing and some SkipLocalsInit attributes.

* Some XmlDoc corrections to reflect the final implementation

* Remove namespace qualifier

* Can't use these because of lambdas :(

* Removed a collection that never ended up being needed.

* Add bracing, newline, and modifier style rules

* Add spacing rules inside parens/brackets

* This was still under the Terminal.Gui.Drawing namespace. Revert that.

* Applied updated formatting settings and addressed XmlDoc reviews in #3204

* More places where spaces got added in dependent code.

Also a couple of null checks fixed to not use the equality operator

* More dependent code format fixes

* Finished re-formatting modified code that got spaces added everywhere

* Visual studio didn't actually write this file to disk til I closed out of VS...

Grr

* Delete the ReSharper settings files from this branch.

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2024-01-24 09:58:02 -07:00

331 lines
10 KiB
C#

using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
namespace Terminal.Gui.ConfigurationTests;
public class ColorJsonConverterTests {
[Theory]
[InlineData ("Black", Color.Black)]
[InlineData ("Blue", Color.Blue)]
[InlineData ("BrightBlue", Color.BrightBlue)]
[InlineData ("BrightCyan", Color.BrightCyan)]
[InlineData ("BrightGreen", Color.BrightGreen)]
[InlineData ("BrightMagenta", Color.BrightMagenta)]
[InlineData ("BrightRed", Color.BrightRed)]
[InlineData ("BrightYellow", Color.BrightYellow)]
[InlineData ("Yellow", Color.Yellow)]
[InlineData ("Cyan", Color.Cyan)]
[InlineData ("DarkGray", Color.DarkGray)]
[InlineData ("Gray", Color.Gray)]
[InlineData ("Green", Color.Green)]
[InlineData ("Magenta", Color.Magenta)]
[InlineData ("Red", Color.Red)]
[InlineData ("White", Color.White)]
public void TestColorDeserializationFromHumanReadableColorNames (string colorName, ColorName expectedColor)
{
// Arrange
string json = $"\"{colorName}\"";
// Act
var actualColor = JsonSerializer.Deserialize<Color> (json, ConfigurationManagerTests._jsonOptions);
// Assert
Assert.Equal (new Color (expectedColor), actualColor);
}
[Theory]
[InlineData (ColorName.Black, "Black")]
[InlineData (ColorName.Blue, "Blue")]
[InlineData (ColorName.Green, "Green")]
[InlineData (ColorName.Cyan, "Cyan")]
[InlineData (ColorName.Gray, "Gray")]
[InlineData (ColorName.Red, "Red")]
[InlineData (ColorName.Magenta, "Magenta")]
[InlineData (ColorName.Yellow, "Yellow")]
[InlineData (ColorName.DarkGray, "DarkGray")]
[InlineData (ColorName.BrightBlue, "BrightBlue")]
[InlineData (ColorName.BrightGreen, "BrightGreen")]
[InlineData (ColorName.BrightCyan, "BrightCyan")]
[InlineData (ColorName.BrightRed, "BrightRed")]
[InlineData (ColorName.BrightMagenta, "BrightMagenta")]
[InlineData (ColorName.BrightYellow, "BrightYellow")]
[InlineData (ColorName.White, "White")]
public void SerializesEnumValuesAsStrings (ColorName colorName, string expectedJson)
{
var converter = new ColorJsonConverter ();
var options = new JsonSerializerOptions { Converters = { converter } };
string serialized = JsonSerializer.Serialize<Color> (new Color (colorName), options);
Assert.Equal ($"\"{expectedJson}\"", serialized);
}
[Fact]
public void TestSerializeColor_Black ()
{
// Arrange
string expectedJson = "\"Black\"";
// Act
string json = JsonSerializer.Serialize<Color> (new Color (Color.Black), new JsonSerializerOptions {
Converters = { new ColorJsonConverter () }
});
// Assert
Assert.Equal (expectedJson, json);
}
[Fact]
public void TestSerializeColor_BrightRed ()
{
// Arrange
string expectedJson = "\"BrightRed\"";
// Act
string json = JsonSerializer.Serialize<Color> (new Color (Color.BrightRed), new JsonSerializerOptions {
Converters = { new ColorJsonConverter () }
});
// Assert
Assert.Equal (expectedJson, json);
}
[Fact]
public void TestDeserializeColor_Black ()
{
// Arrange
string json = "\"Black\"";
var expectedColor = new Color (ColorName.Black);
// Act
var color = JsonSerializer.Deserialize<Color> (json, new JsonSerializerOptions {
Converters = { new ColorJsonConverter () }
});
// Assert
Assert.Equal (expectedColor, color);
}
[Fact]
public void TestDeserializeColor_BrightRed ()
{
// Arrange
string json = "\"BrightRed\"";
var expectedColor = new Color (ColorName.BrightRed);
// Act
var color = JsonSerializer.Deserialize<Color> (json, new JsonSerializerOptions {
Converters = { new ColorJsonConverter () }
});
// Assert
Assert.Equal (expectedColor, color);
}
[Theory]
[InlineData (0, 0, 0, "\"#000000\"")]
[InlineData (0, 0, 1, "\"#000001\"")]
public void SerializesToHexCode (int r, int g, int b, string expected)
{
// Arrange
// Act
string actual = JsonSerializer.Serialize (new Color (r, g, b), new JsonSerializerOptions {
Converters = { new ColorJsonConverter () }
});
//Assert
Assert.Equal (expected, actual);
}
[Theory]
[InlineData ("\"#000000\"", 0, 0, 0)]
public void DeserializesFromHexCode (string hexCode, int r, int g, int b)
{
// Arrange
var expected = new Color (r, g, b);
// Act
var actual = JsonSerializer.Deserialize<Color> (hexCode, new JsonSerializerOptions {
Converters = { new ColorJsonConverter () }
});
//Assert
Assert.Equal (expected, actual);
}
[Theory]
[InlineData ("\"rgb(0,0,0)\"", 0, 0, 0)]
public void DeserializesFromRgb (string rgb, int r, int g, int b)
{
// Arrange
var expected = new Color (r, g, b);
// Act
var actual = JsonSerializer.Deserialize<Color> (rgb, new JsonSerializerOptions {
Converters = { new ColorJsonConverter () }
});
//Assert
Assert.Equal (expected, actual);
}
}
public class AttributeJsonConverterTests {
[Fact]
public void TestDeserialize ()
{
// Test deserializing from human-readable color names
string json = "{\"Foreground\":\"Blue\",\"Background\":\"Green\"}";
var attribute = JsonSerializer.Deserialize<Attribute> (json, ConfigurationManagerTests._jsonOptions);
Assert.Equal (Color.Blue, attribute.Foreground.GetClosestNamedColor ());
Assert.Equal (Color.Green, attribute.Background.GetClosestNamedColor ());
// Test deserializing from RGB values
json = "{\"Foreground\":\"rgb(255,0,0)\",\"Background\":\"rgb(0,255,0)\"}";
attribute = JsonSerializer.Deserialize<Attribute> (json, ConfigurationManagerTests._jsonOptions);
Assert.Equal (Color.Red, attribute.Foreground.GetClosestNamedColor ());
Assert.Equal (Color.BrightGreen, attribute.Background.GetClosestNamedColor ());
}
[Fact, AutoInitShutdown]
public void TestSerialize ()
{
// Test serializing to human-readable color names
var attribute = new Attribute (Color.Blue, Color.Green);
string json = JsonSerializer.Serialize<Attribute> (attribute, ConfigurationManagerTests._jsonOptions);
Assert.Equal ("{\"Foreground\":\"Blue\",\"Background\":\"Green\"}", json);
}
}
public class ColorSchemeJsonConverterTests {
//string json = @"
// {
// ""ColorSchemes"": {
// ""Base"": {
// ""normal"": {
// ""foreground"": ""White"",
// ""background"": ""Blue""
// },
// ""focus"": {
// ""foreground"": ""Black"",
// ""background"": ""Gray""
// },
// ""hotNormal"": {
// ""foreground"": ""BrightCyan"",
// ""background"": ""Blue""
// },
// ""hotFocus"": {
// ""foreground"": ""BrightBlue"",
// ""background"": ""Gray""
// },
// ""disabled"": {
// ""foreground"": ""DarkGray"",
// ""background"": ""Blue""
// }
// }
// }
// }";
[Fact, AutoInitShutdown]
public void TestColorSchemesSerialization ()
{
// Arrange
var expectedColorScheme = new ColorScheme {
Normal = new Attribute (Color.White, Color.Blue),
Focus = new Attribute (Color.Black, Color.Gray),
HotNormal = new Attribute (Color.BrightCyan, Color.Blue),
HotFocus = new Attribute (Color.BrightBlue, Color.Gray),
Disabled = new Attribute (Color.DarkGray, Color.Blue)
};
string serializedColorScheme = JsonSerializer.Serialize<ColorScheme> (expectedColorScheme, ConfigurationManagerTests._jsonOptions);
// Act
var actualColorScheme = JsonSerializer.Deserialize<ColorScheme> (serializedColorScheme, ConfigurationManagerTests._jsonOptions);
// Assert
Assert.Equal (expectedColorScheme, actualColorScheme);
}
}
public class KeyCodeJsonConverterTests {
[Theory]
[InlineData (KeyCode.A, "A")]
[InlineData (KeyCode.A | KeyCode.ShiftMask, "A, ShiftMask")]
[InlineData (KeyCode.A | KeyCode.CtrlMask, "A, CtrlMask")]
[InlineData (KeyCode.A | KeyCode.AltMask | KeyCode.CtrlMask, "A, CtrlMask, AltMask")]
[InlineData ((KeyCode)'a' | KeyCode.AltMask | KeyCode.CtrlMask, "Space, A, CtrlMask, AltMask")]
[InlineData ((KeyCode)'a' | KeyCode.ShiftMask, "Space, A, ShiftMask")]
[InlineData (KeyCode.Delete | KeyCode.AltMask | KeyCode.CtrlMask, "Delete, CtrlMask, AltMask")]
[InlineData (KeyCode.D4, "D4")]
[InlineData (KeyCode.Esc, "Esc")]
public void TestKeyRoundTripConversion (KeyCode key, string expectedStringTo)
{
// Arrange
var options = new JsonSerializerOptions ();
options.Converters.Add (new KeyCodeJsonConverter ());
// Act
string json = JsonSerializer.Serialize (key, options);
var deserializedKey = JsonSerializer.Deserialize<KeyCode> (json, options);
// Assert
Assert.Equal (expectedStringTo, deserializedKey.ToString ());
}
}
public class KeyJsonConverterTests {
[Theory]
[InlineData (KeyCode.A, "\"a\"")]
[InlineData ((KeyCode)'â', "\"â\"")]
[InlineData (KeyCode.A | KeyCode.ShiftMask, "\"A\"")]
[InlineData (KeyCode.A | KeyCode.CtrlMask, "\"Ctrl+A\"")]
[InlineData (KeyCode.A | KeyCode.AltMask | KeyCode.CtrlMask, "\"Ctrl+Alt+A\"")]
[InlineData ((KeyCode)'a' | KeyCode.AltMask | KeyCode.CtrlMask, "\"Ctrl+Alt+A\"")]
[InlineData ((KeyCode)'a' | KeyCode.ShiftMask, "\"A\"")]
[InlineData (KeyCode.Delete | KeyCode.AltMask | KeyCode.CtrlMask, "\"Ctrl+Alt+Delete\"")]
[InlineData (KeyCode.D4, "\"4\"")]
[InlineData (KeyCode.Esc, "\"Esc\"")]
public void TestKey_Serialize (KeyCode key, string expected)
{
// Arrange
var options = new JsonSerializerOptions ();
options.Converters.Add (new KeyJsonConverter ());
options.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
// Act
string json = JsonSerializer.Serialize ((Key)key, options);
// Assert
Assert.Equal (expected, json);
}
[Theory]
[InlineData (KeyCode.A, "a")]
[InlineData (KeyCode.A | KeyCode.ShiftMask, "A")]
[InlineData (KeyCode.A | KeyCode.CtrlMask, "Ctrl+A")]
[InlineData (KeyCode.A | KeyCode.AltMask | KeyCode.CtrlMask, "Ctrl+Alt+A")]
[InlineData ((KeyCode)'a' | KeyCode.AltMask | KeyCode.CtrlMask, "Ctrl+Alt+A")]
[InlineData ((KeyCode)'a' | KeyCode.ShiftMask, "A")]
[InlineData (KeyCode.Delete | KeyCode.AltMask | KeyCode.CtrlMask, "Ctrl+Alt+Delete")]
[InlineData (KeyCode.D4, "4")]
[InlineData (KeyCode.Esc, "Esc")]
public void TestKeyRoundTripConversion (KeyCode key, string expectedStringTo)
{
// Arrange
var options = new JsonSerializerOptions ();
options.Converters.Add (new KeyJsonConverter ());
var encoderSettings = new TextEncoderSettings ();
encoderSettings.AllowCharacters ('+', '-');
encoderSettings.AllowRange (UnicodeRanges.BasicLatin);
options.Encoder = JavaScriptEncoder.Create (encoderSettings);
// Act
string json = JsonSerializer.Serialize ((Key)key, options);
var deserializedKey = JsonSerializer.Deserialize<Key> (json, options);
// Assert
Assert.Equal (expectedStringTo, deserializedKey.ToString ());
}
}