Skip to content

Commit ff24d41

Browse files
committed
Add tests for get command
1 parent 2f3a1db commit ff24d41

File tree

4 files changed

+169
-26
lines changed

4 files changed

+169
-26
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.CommandLine;
3+
using System.CommandLine.IO;
4+
using System.IO;
5+
using System.Threading.Tasks;
6+
using dotnet_json.Commands;
7+
using FluentAssertions;
8+
using Xunit;
9+
10+
namespace dotnet_json.Tests.Commands
11+
{
12+
public class GetCommandTests : IDisposable
13+
{
14+
private readonly string _tmpDir;
15+
16+
public GetCommandTests()
17+
{
18+
_tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
19+
Directory.CreateDirectory(_tmpDir);
20+
}
21+
22+
public void Dispose()
23+
{
24+
Directory.Delete(_tmpDir, true);
25+
}
26+
27+
[Theory]
28+
[InlineData(@"{""key"": ""value""}", "key", "value")]
29+
[InlineData(@"{""key"": 3.14}", "key", "3.14")]
30+
[InlineData(@"{""key"": null}", "key", "null")]
31+
[InlineData(@"{""key"": true}", "key", "true")]
32+
public async Task ReturnsCorrectValue(string json, string key, string expected)
33+
{
34+
await File.WriteAllTextAsync(Path.Join(_tmpDir, "a.json"), json);
35+
36+
var (exitCode, console) = await RunCommand(Path.Join(_tmpDir, "a.json"), key);
37+
38+
exitCode.Should().Be(0);
39+
console.Error.ToString().Should().BeEmpty();
40+
console.Out.ToString().Should().Be($"{expected}\n");
41+
}
42+
43+
[Fact]
44+
public async Task ReturnsErrorWhenKeyDoesntExist()
45+
{
46+
await File.WriteAllTextAsync(Path.Join(_tmpDir, "a.json"), @"{""key"": ""value""}");
47+
48+
var (exitCode, console) = await RunCommand(Path.Join(_tmpDir, "a.json"), "value");
49+
50+
exitCode.Should().Be(1);
51+
console.Error.ToString().Should().Contain("Key 'value' does not exist in the json");
52+
}
53+
54+
[Fact]
55+
public async Task ReturnsJsonValueWhenKeyIsComplexObject()
56+
{
57+
await File.WriteAllTextAsync(Path.Join(_tmpDir, "a.json"), @"{""nested"": {""key"": ""value""}}");
58+
59+
var (exitCode, console) = await RunCommand(Path.Join(_tmpDir, "a.json"), "nested");
60+
61+
exitCode.Should().Be(0);
62+
console.Error.ToString().Should().BeEmpty();
63+
console.Out.ToString().Should().Be("{\n \"key\": \"value\"\n}\n");
64+
}
65+
66+
[Fact]
67+
public async Task ReturnsErrorWhenKeyIsComplexObjectWithExact()
68+
{
69+
await File.WriteAllTextAsync(Path.Join(_tmpDir, "a.json"), @"{""nested"": {""key"": ""value""}}");
70+
71+
var (exitCode, console) = await RunCommand(Path.Join(_tmpDir, "a.json"), "nested", "-e");
72+
73+
exitCode.Should().Be(1);
74+
console.Error.ToString().Should().Contain("x");
75+
console.Out.ToString().Should().BeEmpty();
76+
}
77+
78+
private async Task<(int exitCode, IConsole console)> RunCommand(params string[] arguments)
79+
{
80+
var command = new GetCommand();
81+
82+
var console = new TestConsole();
83+
84+
var exitCode = await command.InvokeAsync(arguments, console);
85+
86+
return (exitCode, console);
87+
}
88+
}
89+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.CommandLine;
3+
using System.CommandLine.IO;
4+
using System.IO;
5+
using System.Threading.Tasks;
6+
using dotnet_json.Commands;
7+
using FluentAssertions;
8+
using Xunit;
9+
10+
namespace dotnet_json.Tests.Commands
11+
{
12+
public class MergeCommandTests : IDisposable
13+
{
14+
private readonly string _tmpDir;
15+
16+
public MergeCommandTests()
17+
{
18+
_tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
19+
Directory.CreateDirectory(_tmpDir);
20+
}
21+
22+
public void Dispose()
23+
{
24+
Directory.Delete(_tmpDir, true);
25+
}
26+
27+
[Fact]
28+
public async Task DoesNotLeaveTraceOfPreviousJsonInFile()
29+
{
30+
await File.WriteAllTextAsync(Path.Join(_tmpDir, "a.json"), @"{
31+
// This file uses comments
32+
""b"": {
33+
// To have more lines of JSON
34+
// then the resulting file
35+
""key"": ""value""
36+
}
37+
// So to test that it does not leave behind
38+
// data from the previous file and it still
39+
// is a valid JSON file after merge
40+
}");
41+
await File.WriteAllTextAsync(Path.Join(_tmpDir, "b.json"), @"{ ""a"": 1 }");
42+
43+
var (exitCode, console) = await RunCommand(
44+
Path.Join(_tmpDir, "a.json"),
45+
Path.Join(_tmpDir, "b.json"));
46+
47+
exitCode.Should().Be(0);
48+
49+
var content = await File.ReadAllTextAsync(Path.Join(_tmpDir, "a.json"));
50+
content.Should().Be(@"{
51+
""b"": {
52+
""key"": ""value""
53+
},
54+
""a"": 1
55+
}");
56+
}
57+
58+
private async Task<(int exitCode, IConsole console)> RunCommand(params string[] args)
59+
{
60+
var command = new MergeCommand();
61+
62+
var console = new TestConsole();
63+
64+
var exitCode = await command.InvokeAsync(args, console);
65+
66+
return (exitCode, console);
67+
}
68+
}
69+
}

dotnet-json.Tests/IntegrationTests.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,37 +107,21 @@ public async Task Merge()
107107
}
108108

109109
[Fact]
110-
public async Task Merge_DoesNotLeaveTraceOfPreviousJsonInFile()
110+
public async Task Get()
111111
{
112-
await File.WriteAllTextAsync(Path.Join(_tmpDir, "a.json"), @"{
113-
// This file uses comments
114-
""b"": {
115-
// To have more lines of JSON
116-
// then the resulting file
117-
""key"": ""value""
118-
}
119-
// So to test that it does not leave behind
120-
// data from the previous file and it still
121-
// is a valid JSON file after merge
122-
}");
123-
await File.WriteAllTextAsync(Path.Join(_tmpDir, "b.json"), @"{ ""a"": 1 }");
112+
await File.WriteAllTextAsync(Path.Join(_tmpDir, "a.json"), @"{""key"": ""value""}");
124113

125114
var (exitCode, console) = await RunCommand(new[]
126115
{
127-
"merge",
116+
"get",
128117
Path.Join(_tmpDir, "a.json"),
129-
Path.Join(_tmpDir, "b.json"),
118+
"key",
130119
});
131120

132121
exitCode.Should().Be(0);
133122

134-
var content = await File.ReadAllTextAsync(Path.Join(_tmpDir, "a.json"));
135-
content.Should().Be(@"{
136-
""b"": {
137-
""key"": ""value""
138-
},
139-
""a"": 1
140-
}");
123+
console.Error.ToString().Should().BeEmpty();
124+
console.Out.ToString().Should().Be("value\n");
141125
}
142126

143127
private async Task<(int ExitCode, IConsole Console)> RunCommand(string[] args)

dotnet-json/Commands/GetCommand.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.CommandLine;
3+
using System.CommandLine.IO;
34
using System.Threading.Tasks;
45
using dotnet_json.Core;
56
using Newtonsoft.Json.Linq;
@@ -31,17 +32,17 @@ protected override async Task<int> ExecuteAsync()
3132
var result = document[key];
3233
if (result == null)
3334
{
34-
Console.Error.WriteLine($"Key '{key}' does not exist in the json");
35+
Context!.Console.Error.WriteLine($"Key '{key}' does not exist in the json");
3536
return 1;
3637
}
3738

3839
if (Context!.ParseResult.ValueForOption(Exact) && !(result is JValue))
3940
{
40-
Console.Error.WriteLine($"Value for key '{key}' is a complex object.");
41+
Context!.Console.Error.WriteLine($"Value for key '{key}' is a complex object.");
4142
return 1;
4243
}
4344

44-
Console.WriteLine(ToString(result));
45+
Context!.Console.Out.WriteLine(ToString(result));
4546
return 0;
4647
}
4748

@@ -51,7 +52,7 @@ protected override async Task<int> ExecuteAsync()
5152
JValue value when value.Value is null => "null",
5253
JValue value => ToString(value.Value!),
5354
bool b => b.ToString().ToLowerInvariant(),
54-
_ => obj.ToString() ?? "null",
55+
_ => obj.ToString() ?? "",
5556
};
5657
}
5758
}

0 commit comments

Comments
 (0)