Enums

Specification

Enums are created as instances of EnumType.

Creating enum

Direction enum


        [Fact]
        public void Define_enum()
        {
            /* Given */
            /* When */
            var Enum = new EnumType(
                "Direction",
                new EnumValues
                {
                    "NORTH",
                    "EAST",
                    "SOUTH",
                    "WEST"
                });

            /* Then */
            Assert.True(Enum.Contains("NORTH"));
            Assert.True(Enum.Contains("EAST"));
            Assert.True(Enum.Contains("SOUTH"));
            Assert.True(Enum.Contains("WEST"));
        }

Serialization

From input value

        [Theory]
        [InlineData("success", "SUCCESS")]
        [InlineData("FAILURE", "FAILURE")]
        [InlineData("Inconclusive", "INCONCLUSIVE")]
        public void ParseValue(object input, string expected)
        {
            /* Given */
            var Enum = new EnumType("TestResult", new EnumValues
            {
                ["SUCCESS"] = null,
                ["FAILURE"] = null,
                ["INCONCLUSIVE"] = null
            });

            /* When */
            var actual = Enum.ParseValue(input);

            /* Then */
            Assert.Equal(expected, actual);
        }

From AST node value


        [Theory]
        [InlineData("success", "SUCCESS")]
        [InlineData("FAILURE", "FAILURE")]
        [InlineData("Inconclusive", "INCONCLUSIVE")]
        public void ParseLiteral(string input, string expected)
        {
            /* Given */
            var astValue = new GraphQLScalarValue(ASTNodeKind.EnumValue)
            {
                Value = input
            };

            var Enum = new EnumType("TestResult", new EnumValues
            {
                ["SUCCESS"] = null,
                ["FAILURE"] = null,
                ["INCONCLUSIVE"] = null
            });

            /* When */
            var actual = Enum.ParseLiteral(astValue);

            /* Then */
            Assert.Equal(expected, actual);
        }

Serialize


        [Theory]
        [InlineData("success", "SUCCESS")]
        [InlineData("FAILURE", "FAILURE")]
        [InlineData("Inconclusive", "INCONCLUSIVE")]
        public void Serialize(object input, string expected)
        {
            /* Given */
            var Enum = new EnumType("TestResult", new EnumValues
            {
                ["SUCCESS"] = null,
                ["FAILURE"] = null,
                ["INCONCLUSIVE"] = null
            });

            /* When */
            var actual = Enum.Serialize(input);

            /* Then */
            Assert.Equal(expected, actual);
        }