Esempio n. 1
0
        public void NonSerialized([Values(BehaviorMode.RewrittenBare, BehaviorMode.RewrittenPretty)] BehaviorMode mode)
        {
            var ephemeral = Dec.Database.Create <NonSerializedDec>("TestDec");

            ephemeral.serializedValue    = 35;
            ephemeral.nonSerializedValue = 45;

            var    writer = new Dec.Composer();
            string data   = writer.ComposeXml(mode == BehaviorMode.RewrittenPretty);

            Assert.IsTrue(data.Contains("serializedValue"));
            Assert.IsFalse(data.Contains("nonSerializedValue"));
        }
Esempio n. 2
0
        public void Enum([Values(BehaviorMode.RewrittenBare, BehaviorMode.RewrittenPretty)] BehaviorMode mode)
        {
            var enums = Dec.Database.Create <EnumContainerDec>("TestDec");

            enums.alph = EnumContainerDec.Enum.Alpha;
            enums.bet  = EnumContainerDec.Enum.Beta;
            enums.gam  = EnumContainerDec.Enum.Gamma;

            var    writer = new Dec.Composer();
            string data   = writer.ComposeXml(mode == BehaviorMode.RewrittenPretty);

            Assert.IsTrue(data.Contains("Alpha"));
            Assert.IsTrue(data.Contains("Beta"));
            Assert.IsTrue(data.Contains("Gamma"));

            Assert.IsFalse(data.Contains("__value"));
        }
Esempio n. 3
0
        public void DoBehavior(BehaviorMode mode,
                               bool rewrite_expectWriteErrors     = false,
                               bool rewrite_expectParseErrors     = false,
                               bool validation_expectWriteErrors  = false,
                               Assembly[] validation_assemblies   = null,
                               Func <string, bool> errorValidator = null)
        {
            if (mode == BehaviorMode.Bare)
            {
                // we actually have nothing to do here, we're good
            }
            else if (mode == BehaviorMode.RewrittenBare || mode == BehaviorMode.RewrittenPretty)
            {
                string data = null;
                void RunComposer()
                {
                    var composer = new Dec.Composer();

                    data = composer.ComposeXml(mode == BehaviorMode.RewrittenPretty);
                }

                if (rewrite_expectWriteErrors)
                {
                    ExpectErrors(() => RunComposer(), errorValidator: errorValidator);
                }
                else
                {
                    RunComposer();
                }

                Dec.Database.Clear();

                // This is a janky hack; it resets the type caches so we also generate errors again properly.
                Dec.Config.UsingNamespaces = Dec.Config.UsingNamespaces;

                void RunParser()
                {
                    var parser = new Dec.Parser();

                    parser.AddString(data);
                    parser.Finish();
                }

                if (rewrite_expectParseErrors)
                {
                    ExpectErrors(() => RunParser(), errorValidator: errorValidator);
                }
                else
                {
                    RunParser();
                }
            }
            else if (mode == BehaviorMode.Validation)
            {
                string code = null;
                void RunComposer()
                {
                    var composer = new Dec.Composer();

                    code = composer.ComposeValidation();
                }

                if (validation_expectWriteErrors)
                {
                    ExpectErrors(() => RunComposer(), errorValidator: errorValidator);
                }
                else
                {
                    RunComposer();
                }

                var assemblies = new Assembly[] { this.GetType().Assembly };
                if (validation_assemblies != null)
                {
                    assemblies = assemblies.Concat(validation_assemblies).ToArray();
                }

                CompileAndRun($"public static void Test() {{\n{code}\n}}", assemblies, "Test", null);
            }
            else if (mode == BehaviorMode.Null)
            {
                void RunComposer()
                {
                    var composer = new Dec.Composer();

                    composer.ComposeNull();
                }

                if (rewrite_expectWriteErrors)
                {
                    // We don't really insist on an error, but we tolerate one.
                    ExpectErrors(() =>
                    {
                        RunComposer();
                        handledError = true; // good enough, just continue
                    }, errorValidator: errorValidator);
                }
                else
                {
                    RunComposer();
                }

                // may as well just stop here
                Assert.Pass();
            }
            else
            {
                Assert.IsTrue(false, "Bad case for behavior mode!");
            }
        }