AnalyzeJSONUnitTests.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(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. }
  68. }