AnalyzeJSONUnitTests.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using analyzeJSON;
  4. using Newtonsoft.Json.Linq;
  5. using Xunit;
  6. namespace analyzeJSONTests
  7. {
  8. public record TestCase(string Input, string ExpectedOutput);
  9. public class AnalyzeJSONUnitTests
  10. {
  11. [Fact]
  12. public void Traverse_EmptyJObject()
  13. {
  14. var test = new AnalyzeJSON(new JObject());
  15. var result = test.Traverse((token) => Console.WriteLine(token));
  16. Assert.False(result.Success);
  17. Assert.NotEmpty(result.Message);
  18. }
  19. [Fact]
  20. public void Traverse_NullAction()
  21. {
  22. var jObject = new JObject
  23. {
  24. { "test", new JObject() }
  25. };
  26. var test = new AnalyzeJSON(jObject);
  27. var result = test.Traverse(action: null);
  28. Assert.False(result.Success);
  29. Assert.NotEmpty(result.Message);
  30. }
  31. [Fact]
  32. public void Traverse_AllGood()
  33. {
  34. var jObject = new JObject
  35. {
  36. { "test", new JObject() }
  37. };
  38. var test = new AnalyzeJSON(jObject);
  39. var result = test.Traverse((token) => Console.WriteLine(token));
  40. Assert.True(result.Success);
  41. Assert.Empty(result.Message);
  42. }
  43. [Fact]
  44. public void GetNameFromPath_NullTokenPath()
  45. {
  46. Assert.Empty(AnalyzeJSON.GetNameFromPath(null));
  47. }
  48. [Fact]
  49. public void GetNameFromPath_EmptyTokenPath()
  50. {
  51. Assert.Empty(AnalyzeJSON.GetNameFromPath(string.Empty));
  52. }
  53. [Fact]
  54. public void GetNameFromPath_DifferentCases()
  55. {
  56. var testCases = new List<TestCase>
  57. {
  58. new TestCase("abc", "abc"),
  59. new TestCase("abc.def", "def"),
  60. new TestCase("abc.", string.Empty),
  61. new TestCase("abc.def.", string.Empty),
  62. new TestCase("abc.def.ghi[0]", "ghi"),
  63. };
  64. foreach (var testCase in testCases)
  65. Assert.Equal(testCase.ExpectedOutput, AnalyzeJSON.GetNameFromPath(testCase.Input));
  66. }
  67. [Fact]
  68. public void Traverse_EmptyJObject_WithActions()
  69. {
  70. var test = new AnalyzeJSON(new JObject());
  71. var result = test.Traverse(new List<Action<JToken>>{ (token) => Console.WriteLine(token) });
  72. Assert.False(result.Success);
  73. Assert.NotEmpty(result.Message);
  74. }
  75. [Fact]
  76. public void Traverse_NullActions()
  77. {
  78. var jObject = new JObject
  79. {
  80. { "test", new JObject() }
  81. };
  82. var test = new AnalyzeJSON(jObject);
  83. var result = test.Traverse(actions: null);
  84. Assert.False(result.Success);
  85. Assert.NotEmpty(result.Message);
  86. }
  87. [Fact]
  88. public void Traverse_EmptyActions()
  89. {
  90. var jObject = new JObject
  91. {
  92. { "test", new JObject() }
  93. };
  94. var test = new AnalyzeJSON(jObject);
  95. var result = test.Traverse(new List<Action<JToken>>());
  96. Assert.False(result.Success);
  97. Assert.NotEmpty(result.Message);
  98. }
  99. }
  100. }