AnalyzeJSONUnitTests.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. jObject.Add("test", new JObject());
  24. var test = new AnalyzeJSON(jObject);
  25. var result = test.Traverse(null);
  26. Assert.False(result.Success);
  27. Assert.NotEmpty(result.Message);
  28. }
  29. [Fact]
  30. public void Traverse_AllGood()
  31. {
  32. var jObject = new JObject();
  33. jObject.Add("test", new JObject());
  34. var test = new AnalyzeJSON(jObject);
  35. var result = test.Traverse((token) => Console.WriteLine(token));
  36. Assert.True(result.Success);
  37. Assert.Empty(result.Message);
  38. }
  39. [Fact]
  40. public void GetNameFromPath_NullTokenPath()
  41. {
  42. Assert.Empty(AnalyzeJSON.GetNameFromPath(null));
  43. }
  44. [Fact]
  45. public void GetNameFromPath_EmptyTokenPath()
  46. {
  47. Assert.Empty(AnalyzeJSON.GetNameFromPath(string.Empty));
  48. }
  49. [Fact]
  50. public void GetNameFromPath_DifferentCases()
  51. {
  52. var testCases = new List<TestCase>
  53. {
  54. new TestCase("abc", "abc"),
  55. new TestCase("abc.def", "def"),
  56. new TestCase("abc.", string.Empty),
  57. new TestCase("abc.def.", string.Empty),
  58. };
  59. foreach (var testCase in testCases)
  60. Assert.Equal(testCase.ExpectedOutput, AnalyzeJSON.GetNameFromPath(testCase.Input));
  61. }
  62. }
  63. }