Esempio n. 1
0
        private static string ConvertName(string originalName, NameConvention convention)
        {
            var name = originalName;

            switch (convention)
            {
            case NameConvention.Lowercase:
                name = originalName.ToLowerInvariant();
                break;

            case NameConvention.Uppercase:
                name = originalName.ToUpperInvariant();
                break;

            case NameConvention.SplitCase:
                name = Regex.Replace(originalName, "(?<=[a-z])([A-Z])", "_$1", RegexOptions.Compiled);
                break;

            case NameConvention.SplitCaseLower:
                name = Regex.Replace(originalName, "(?<=[a-z])([A-Z])", "_$1", RegexOptions.Compiled).ToLowerInvariant();
                break;

            case NameConvention.SplitCaseUpper:
                name = Regex.Replace(originalName, "(?<=[a-z])([A-Z])", "_$1", RegexOptions.Compiled).ToUpperInvariant();
                break;
            }

            return(name);
        }
Esempio n. 2
0
        public static string ToCase(string name, NameConvention nameConvention)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(name);
            }
            if (name.Contains("-"))
            {
                name = new Regex(@"\-([a-z]?)", RegexOptions.IgnoreCase)
                       .Replace(name, m => m.Groups[1].Value.ToUpper());
            }

            switch (nameConvention)
            {
            case NameConvention.CamelCase:
                return($"{name.Substring(0, 1).ToLower()}{name.Substring(1)}");

            case NameConvention.PascalCase:
                return($"{name.Substring(0, 1).ToUpper()}{name.Substring(1)}");

            default:
                return(name);
            }
        }
Esempio n. 3
0
        public void SerializaionUtilizeNamingConventions()
        {
            var convention = A.Fake<INamingConvention>();
            A.CallTo(() => convention.Apply(A<string>._)).ReturnsLazily((string x) => x);
            var obj = new NameConvention { FirstTest = "1", SecondTest = "2" };

            var serializer = new Serializer(namingConvention: convention);
            serializer.Serialize(new StringWriter(), obj);

            A.CallTo(() => convention.Apply("FirstTest")).MustHaveHappened();
            A.CallTo(() => convention.Apply("SecondTest")).MustHaveHappened();
        }
Esempio n. 4
0
        public void RoundtripAlias()
        {
            var writer = new StringWriter();
            var input = new NameConvention { AliasTest = "Fourth" };

            Serializer.Serialize(writer, input, input.GetType());
            var text = writer.ToString();

            // Todo: use RegEx once FluentAssertions 2.2 is released
            text.TrimEnd('\r', '\n').Should().Be("fourthTest: Fourth");

            var output = Deserializer.Deserialize<NameConvention>(UsingReaderFor(text));

            output.AliasTest.Should().Be(input.AliasTest);
        }
        public void RoundtripAliasOverride()
        {
            var writer = new StringWriter();
            var input = new NameConvention { AliasTest = "Fourth" };

            var overrides = new YamlAttributeOverrides();
            var attribute = new YamlMemberAttribute();
            attribute.Alias = "fourthOverride";
            overrides.Add(typeof(NameConvention), "AliasTest", attribute);
            var serializer = new Serializer(overrides: overrides);
            
            serializer.Serialize(writer, input, input.GetType());
            var text = writer.ToString();

            // Todo: use RegEx once FluentAssertions 2.2 is released
            text.TrimEnd('\r', '\n').Should().Be("fourthOverride: Fourth");

            var deserializer = new Deserializer(overrides: overrides);
            var output = deserializer.Deserialize<NameConvention>(UsingReaderFor(text));

            output.AliasTest.Should().Be(input.AliasTest);
        }