|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using FluentAssertions; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace dotnet_json.Tests |
| 8 | +{ |
| 9 | + public class IntegrationTests : IDisposable |
| 10 | + { |
| 11 | + private string _tmpDir; |
| 12 | + |
| 13 | + public IntegrationTests() |
| 14 | + { |
| 15 | + _tmpDir = Path.Join(Path.GetTempPath(), Path.GetTempFileName()); |
| 16 | + Directory.CreateDirectory(_tmpDir); |
| 17 | + } |
| 18 | + |
| 19 | + public void Dispose() |
| 20 | + { |
| 21 | + Directory.Delete(_tmpDir, true); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public async Task Set() |
| 26 | + { |
| 27 | + await File.WriteAllTextAsync(Path.Join(_tmpDir, "set.json"), @"{ ""key"": ""value"" }"); |
| 28 | + |
| 29 | + await Program.Main(new[] |
| 30 | + { |
| 31 | + "set", |
| 32 | + Path.Join(_tmpDir, "set.json"), |
| 33 | + "path:to:0:key", |
| 34 | + "value", |
| 35 | + }); |
| 36 | + |
| 37 | + var content = await File.ReadAllTextAsync(Path.Join(_tmpDir, "set.json")); |
| 38 | + content.Should().Be(@"{ |
| 39 | + ""key"": ""value"", |
| 40 | + ""path"": { |
| 41 | + ""to"": [ |
| 42 | + { |
| 43 | + ""key"": ""value"" |
| 44 | + } |
| 45 | + ] |
| 46 | + } |
| 47 | +}"); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public async Task Remove() |
| 52 | + { |
| 53 | + await File.WriteAllTextAsync(Path.Join(_tmpDir, "remove.json"), @"{ ""key"": ""value"", ""path"": { ""to"": [ { ""key"": ""value"" } ] } }"); |
| 54 | + |
| 55 | + await Program.Main(new[] |
| 56 | + { |
| 57 | + "remove", |
| 58 | + Path.Join(_tmpDir, "remove.json"), |
| 59 | + "path:to:0:key", |
| 60 | + }); |
| 61 | + |
| 62 | + var content = await File.ReadAllTextAsync(Path.Join(_tmpDir, "remove.json")); |
| 63 | + content.Should().Be(@"{ |
| 64 | + ""key"": ""value"", |
| 65 | + ""path"": { |
| 66 | + ""to"": [ |
| 67 | + {} |
| 68 | + ] |
| 69 | + } |
| 70 | +}"); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public async Task Merge() |
| 75 | + { |
| 76 | + await File.WriteAllTextAsync(Path.Join(_tmpDir, "a.json"), "{}"); |
| 77 | + await File.WriteAllTextAsync(Path.Join(_tmpDir, "b.json"), @"{ ""b"": { ""key"": ""value"" } }"); |
| 78 | + await File.WriteAllTextAsync(Path.Join(_tmpDir, "c.json"), @"{ ""c"": [ 1 ] }"); |
| 79 | + |
| 80 | + await Program.Main(new[] |
| 81 | + { |
| 82 | + "merge", |
| 83 | + Path.Join(_tmpDir, "a.json"), |
| 84 | + Path.Join(_tmpDir, "b.json"), |
| 85 | + Path.Join(_tmpDir, "c.json"), |
| 86 | + }); |
| 87 | + |
| 88 | + var content = await File.ReadAllTextAsync(Path.Join(_tmpDir, "a.json")); |
| 89 | + content.Should().Be(@"{ |
| 90 | + ""b"": { |
| 91 | + ""key"": ""value"" |
| 92 | + }, |
| 93 | + ""c"": [ |
| 94 | + 1 |
| 95 | + ] |
| 96 | +}"); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments