AnalyzeJSONUnitTests.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 GetNameFromPath_NullTokenPath()
  31. {
  32. Assert.Empty(AnalyzeJSON.GetNameFromPath(null));
  33. }
  34. [Fact]
  35. public void GetNameFromPath_EmptyTokenPath()
  36. {
  37. Assert.Empty(AnalyzeJSON.GetNameFromPath(string.Empty));
  38. }
  39. [Fact]
  40. public void GetNameFromPath_DifferentCases()
  41. {
  42. var testCases = new List<TestCase>
  43. {
  44. new TestCase("abc", "abc"),
  45. new TestCase("abc.def", "def"),
  46. new TestCase("abc.", string.Empty),
  47. new TestCase("abc.def.", string.Empty),
  48. };
  49. foreach (var testCase in testCases)
  50. Assert.Equal(testCase.ExpectedOutput, AnalyzeJSON.GetNameFromPath(testCase.Input));
  51. }
  52. }
  53. }