Skip to content

Commit

Permalink
Add unused feature to sort json arrays in unit test string comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed May 15, 2024
1 parent cbb8b16 commit c01a7ab
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tests/Imageflow.Test/TestJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public void TestAllJob()
"""{"security":null,"framewise":{"graph":{"edges":[{"from":0,"to":1,"kind":"input"},{"from":1,"to":2,"kind":"input"},{"from":2,"to":3,"kind":"input"},{"from":3,"to":4,"kind":"input"},{"from":4,"to":5,"kind":"input"},{"from":5,"to":6,"kind":"input"},{"from":6,"to":7,"kind":"input"},{"from":7,"to":8,"kind":"input"},{"from":8,"to":9,"kind":"input"},{"from":9,"to":10,"kind":"input"},{"from":10,"to":11,"kind":"input"},{"from":11,"to":12,"kind":"input"},{"from":12,"to":13,"kind":"input"},{"from":13,"to":14,"kind":"input"},{"from":14,"to":15,"kind":"input"},{"from":15,"to":16,"kind":"input"},{"from":16,"to":17,"kind":"input"},{"from":17,"to":18,"kind":"input"},{"from":18,"to":19,"kind":"input"},{"from":19,"to":20,"kind":"input"},{"from":20,"to":21,"kind":"input"},{"from":21,"to":22,"kind":"input"},{"from":22,"to":23,"kind":"input"},{"from":23,"to":24,"kind":"input"},{"from":24,"to":25,"kind":"input"},{"from":25,"to":26,"kind":"input"},{"from":26,"to":27,"kind":"input"},{"from":27,"to":28,"kind":"input"},{"from":28,"to":29,"kind":"input"}],"nodes":{"0":{"decode":{"io_id":0}},"1":{"flip_v":null},"2":{"flip_h":null},"3":{"rotate_90":null},"4":{"rotate_180":null},"5":{"rotate_270":null},"6":{"transpose":null},"7":{"crop_whitespace":{"threshold":80,"percent_padding":0.5}},"8":{"resample_2d":{"w":30,"h":20,"hints":null}},"9":{"crop":{"x1":0,"y1":0,"x2":10,"y2":10}},"10":{"region":{"x1":-5,"y1":-5,"x2":10,"y2":10,"background_color":{"black":null}}},"11":{"region_percent":{"x1":-10.0,"y1":-10.0,"x2":110.0,"y2":110.0,"background_color":{"transparent":null}}},"12":{"color_filter_srgb":{"brightness":-1.0}},"13":{"color_filter_srgb":{"contrast":1.0}},"14":{"color_filter_srgb":{"saturation":1.0}},"15":{"white_balance_histogram_area_threshold_srgb":{"threshold":80}},"16":{"color_filter_srgb":"invert"},"17":{"color_filter_srgb":"sepia"},"18":{"color_filter_srgb":"grayscale_bt709"},"19":{"color_filter_srgb":"grayscale_flat"},"20":{"color_filter_srgb":"grayscale_ntsc"},"21":{"color_filter_srgb":"grayscale_ry"},"22":{"expand_canvas":{"left":5,"top":5,"right":5,"bottom":5,"color":{"srgb":{"hex":"ffeecc"}}}},"23":{"fill_rect":{"x1":2,"y1":2,"x2":8,"y2":8,"color":{"black":null}}},"24":{"command_string":{"kind":"ir4","value":"width=10&height=10&mode=crop"}},"25":{"round_image_corners":{"radius":{"percentage":100.0},"background_color":{"black":null}}},"26":{"round_image_corners":{"radius":{"pixels":1},"background_color":{"transparent":null}}},"27":{"constrain":{"mode":"within","w":5,"h":5}},"28":{"watermark":{"io_id":1,"gravity":{"percentage":{"x":90.0,"y":90.0}},"fit_box":{"image_margins":{"left":1,"top":1,"right":1,"bottom":1}},"fit_mode":"within","min_canvas_width":1,"min_canvas_height":1,"opacity":0.5,"hints":{"sharpen_percent":15.0,"down_filter":null,"up_filter":null,"scaling_colorspace":null,"resample_when":null,"sharpen_when":"always"}}},"29":{"encode":{"io_id":2,"preset":{"mozjpeg":{"quality":80,"progressive":true,"matte":null}}}}}}}}""";
// parse and reformat both before comparing

var expectedJson = SortPropertiesRecursive(JsonNode.Parse(expected))!.ToString();
var actualJson = SortPropertiesRecursive(JsonNode.Parse(jsonStr))!.ToString();
var expectedJson = SortPropertiesRecursive(JsonNode.Parse(expected), false)!.ToString();
var actualJson = SortPropertiesRecursive(JsonNode.Parse(jsonStr), false)!.ToString();
try
{
Assert.Equal(expectedJson, actualJson);
Expand Down Expand Up @@ -105,14 +105,14 @@ public void TestAllJob()
// }
}

private static JsonNode? SortPropertiesRecursive(JsonNode? n)
private static JsonNode? SortPropertiesRecursive(JsonNode? n, bool sortArrays = true)
{
if (n is JsonObject o)
{
var sorted = new SortedDictionary<string, JsonNode?>();
foreach (var pair in o)
{
var v = SortPropertiesRecursive(pair.Value);
var v = SortPropertiesRecursive(pair.Value, sortArrays);
if (v != null) sorted.Add(pair.Key, v);
}

Expand All @@ -124,9 +124,14 @@ public void TestAllJob()
var sorted = new List<JsonNode?>();
foreach (var value in a)
{
var v = SortPropertiesRecursive(value);
var v = SortPropertiesRecursive(value, sortArrays);
if (v != null) sorted.Add(v);
}
if (sortArrays)
{
// sort by the stringified value
sorted.Sort((x, y) => x!.ToString()!.CompareTo(y!.ToString()!));
}

return new JsonArray(sorted.ToArray());
}
Expand Down

0 comments on commit c01a7ab

Please sign in to comment.