Repository navigation
Expand file tree
/
Copy pathDocumentFixture.cs
More file actions
66 lines (60 loc) · 2.23 KB
/
Copy pathDocumentFixture.cs
File metadata and controls
66 lines (60 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using Assent;
using JavaPropertiesParser.Expressions;
using JavaPropertiesParser.Tests.TestUtils;
using JavaPropertiesParser.Utils;
using NUnit.Framework;
namespace JavaPropertiesParser.Tests
{
public class DocumentFixture
{
[Test]
public void CanReplaceValuesWhilePreservingFormatting()
{
var keyValues = new Dictionary<string, string>
{
{ "KEY1", "The new value for key\n1" },
{ "key\\4", "The new value for key 4" },
{ "key5", "Key 5's\n\n\nnewvalue" },
{ "notarealkey", "Should not be found" },
{ "", "New value for the empty key" },
{ "key15", "" }
};
ITopLevelExpression Mutator(ITopLevelExpression expr)
{
switch (expr)
{
case null:
throw new ArgumentNullException(nameof(expr));
case KeyValuePairExpression pair:
var key = pair.Key?.Text?.LogicalValue ?? "";
if (keyValues.TryGetValue(key, out var value))
{
return new KeyValuePairExpression(
pair.Key,
pair.Separator,
new ValueExpression(
new StringValue(
value,
Encode.Value(value)
)
)
);
}
else
{
return expr;
}
default:
return expr;
}
}
var input = ResourceUtils.ReadEmbeddedResource("multiple-pairs-and-comments.properties");
var parsed = Parser.Parse(input);
var updated = parsed.Mutate(Mutator);
var actual = updated.ToString();
this.Assent(actual, TestEnvironment.AssentConfiguration);
}
}
}