AddGrammar() public method

public AddGrammar ( GrammarModel grammar ) : void
grammar GrammarModel
return void
コード例 #1
0
        public override GrammarModel ApplyOverrides(GrammarModel grammar)
        {
            if (!(grammar is FixtureModel))
            {
                return(this);
            }

            var model = new FixtureModel(key)
            {
                implementation = implementation
            };
            var over = grammar.As <FixtureModel>();

            model.title = over.title.IsNotEmpty() ? over.title : title;

            foreach (var existing in _grammars)
            {
                var overridden = over.FindGrammar(existing.key);
                var combined   = overridden == null ? existing : existing.ApplyOverrides(overridden);
                combined.errors =
                    (existing.errors ?? new GrammarError[0]).Concat((overridden?.errors ?? new GrammarError[0]))
                    .ToArray();

                model.AddGrammar(combined);
            }

            foreach (var specified in over.grammars.Where(x => !grammars.Any(_ => _.key == x.key)))
            {
                specified.IsMissing = true;
                model.AddGrammar(specified);
            }

            return(model);
        }
コード例 #2
0
        public void copies_grammars()
        {
            const string fixtureKey = "a key";

            var lib = new FixtureLibrary();
            var original = new FixtureModel(fixtureKey);
            lib.Models[fixtureKey] = original;

            original.AddGrammar(new Sentence { key = "sentence", format = "a format"});

            var overrides = new FixtureLibrary();
            var overriden = new FixtureModel(fixtureKey);
            overrides.Models[fixtureKey] = overriden;

            var result = lib.ApplyOverrides(overrides);

            result.Models.Count.ShouldBe(1);

            var fixture = result.Models[fixtureKey];
            ReferenceEquals(fixture, overrides.Models[fixtureKey]).ShouldBeFalse();
            fixture.key.ShouldBe(fixtureKey);

            fixture.grammars.Length.ShouldBe(1);

            var sentence = fixture.grammars[0] as Sentence;
            sentence.key.ShouldBe("sentence");
            sentence.format.ShouldBe("a format");
        }
コード例 #3
0
        public override GrammarModel ApplyOverrides(GrammarModel grammar)
        {
            var model = new FixtureModel(key);

            model.implementation = implementation;

            var over = grammar as FixtureModel;

            if (over == null)
            {
                model.title    = title;
                model.grammars = grammars.Select(x => x.ApplyOverrides(null)).ToArray();
                return(model);
            }

            model.title    = over.title.IsNotEmpty() ? over.title : title;
            model.grammars = grammars.Select(g =>
            {
                var match = over.grammars.FirstOrDefault(x => x.key == g.key);
                return(g.ApplyOverrides(match));
            }).ToArray();

            var keys    = model.grammars.Select(x => x.key).ToList();
            var missing = over.grammars.Where(x => !keys.Contains(x.key));

            missing.Each(x =>
            {
                model.AddGrammar(x.ApplyOverrides(null));
            });

            return(model);
        }
コード例 #4
0
        public void can_mark_the_missing_grammars()
        {
            var system = new FixtureModel("something");
            system.AddGrammar(new Sentence {key = "Go1", format = "Go One"});

            var overrides = new FixtureModel("something");
            overrides.AddGrammar(new Sentence { key = "Go2", format = "Go Two" });

            var combined = system.ApplyOverrides(overrides).As<FixtureModel>();

            combined.FindGrammar("Go1").IsMissing.ShouldBeFalse();
            combined.FindGrammar("Go2").IsMissing.ShouldBeTrue();
        }
コード例 #5
0
        public override GrammarModel ApplyOverrides(GrammarModel grammar)
        {
            if (!(grammar is FixtureModel)) return this;

            var model = new FixtureModel(key) {implementation = implementation};
            var over = grammar.As<FixtureModel>();

            model.title = over.title.IsNotEmpty() ? over.title : title;

            foreach (var existing in _grammars)
            {
                var overridden = over.FindGrammar(existing.key);
                var combined = overridden == null ? existing : existing.ApplyOverrides(overridden);
                combined.errors =
                    (existing.errors ?? new GrammarError[0]).Concat((overridden?.errors ?? new GrammarError[0]))
                        .ToArray();

                model.AddGrammar(combined);
            }

            foreach (var specified in over.grammars.Where(x => !grammars.Any(_ => _.key == x.key)))
            {
                specified.IsMissing = true;
                model.AddGrammar(specified);
            }

            return model;
        }