AnalyzeJSONUnitTests.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. };
  63. foreach (var testCase in testCases)
  64. Assert.Equal(testCase.ExpectedOutput, AnalyzeJSON.GetNameFromPath(testCase.Input));
  65. }
  66. }
  67. }