public void GivenTokenAndLastTemplateMatches_WhenGetMeasurements_MeasurementsReturned_Test()
        {
            var m1 = new Measurement();

            var template       = new CollectionContentTemplate();
            var innerTemplate1 = Substitute.For <IContentTemplate>();

            innerTemplate1.GetMeasurements(null).ReturnsForAnyArgs(t => Enumerable.Empty <Measurement>());
            var innerTemplate2 = Substitute.For <IContentTemplate>();

            innerTemplate2.GetMeasurements(null).ReturnsForAnyArgs(t => new[] { m1 });

            template.RegisterTemplate(innerTemplate1)
            .RegisterTemplate(innerTemplate2);

            var token = JToken.FromObject(new object());

            var measurements = template.GetMeasurements(token);

            Assert.Collection(measurements, m =>
            {
                Assert.Equal(m1, m);
            });

            innerTemplate1.Received(1).GetMeasurements(Arg.Is <JToken>(t => t == token));
            innerTemplate2.Received(1).GetMeasurements(Arg.Is <JToken>(t => t == token));
        }
Ejemplo n.º 2
0
        protected override IContentTemplate BuildCollectionTemplate(JArray templateCollection, ICollection <TemplateError> errors)
        {
            EnsureArg.IsNotNull(templateCollection, nameof(templateCollection));
            EnsureArg.IsNotNull(errors, nameof(errors));

            var collectionTemplate = new CollectionContentTemplate();

            foreach (var token in templateCollection)
            {
                try
                {
                    var container       = token.ToObject <TemplateContainer>();
                    var createdTemplate = TemplateFactories.Evaluate(container);
                    collectionTemplate.RegisterTemplate(createdTemplate);
                }
                catch (InvalidTemplateException ex)
                {
                    errors.Add(new TemplateError(ex.Message));
                }
                catch (JsonSerializationException ex)
                {
                    errors.Add(new TemplateError(ex.Message));
                }
            }

            return(collectionTemplate);
        }
        public void GivenTokenAndNoTemplates_WhenGetMeasurements_EmptyCollectionReturned_Test()
        {
            var template = new CollectionContentTemplate();
            var token    = JToken.FromObject(new object());

            var measurements = template.GetMeasurements(token);

            Assert.Empty(measurements);
        }
Ejemplo n.º 4
0
        protected override IContentTemplate BuildCollectionTemplate(JArray templateCollection)
        {
            var template = new CollectionContentTemplate();

            foreach (var token in templateCollection)
            {
                var container       = token.ToObject <TemplateContainer>();
                var createdTemplate = TemplateFactories.Evaluate(container);
                template.RegisterTemplate(createdTemplate);
            }

            return(template);
        }
        public void GivenTokenAndAllTemplatesMatchAndMultipleResultsGeneratedPerTemplate_WhenGetMeasurements_MeasurementsReturned_Test()
        {
            var m1 = new Measurement();
            var m2 = new Measurement();
            var m3 = new Measurement();

            var template       = new CollectionContentTemplate();
            var innerTemplate1 = Substitute.For <IContentTemplate>();

            innerTemplate1.GetMeasurements(null).ReturnsForAnyArgs(t => new[] { m1 });
            var innerTemplate2 = Substitute.For <IContentTemplate>();

            innerTemplate2.GetMeasurements(null).ReturnsForAnyArgs(t => new[] { m2, m3 });

            template.RegisterTemplate(innerTemplate1)
            .RegisterTemplate(innerTemplate2);

            var token = JToken.FromObject(new object());

            var measurements = template.GetMeasurements(token);

            Assert.Collection(
                measurements,
                m =>
            {
                Assert.Equal(m1, m);
            },
                m =>
            {
                Assert.Equal(m2, m);
            },
                m =>
            {
                Assert.Equal(m3, m);
            });

            innerTemplate1.Received(1).GetMeasurements(Arg.Is <JToken>(t => t == token));
            innerTemplate2.Received(1).GetMeasurements(Arg.Is <JToken>(t => t == token));
        }
        public void GivenTokenAndNoMatchingTemplates_WhenGetMeasurements_EmptyCollectionReturned_Test()
        {
            var template       = new CollectionContentTemplate();
            var innerTemplate1 = Substitute.For <IContentTemplate>();

            innerTemplate1.GetMeasurements(null).ReturnsForAnyArgs(t => Enumerable.Empty <Measurement>());
            var innerTemplate2 = Substitute.For <IContentTemplate>();

            innerTemplate2.GetMeasurements(null).ReturnsForAnyArgs(t => Enumerable.Empty <Measurement>());

            template.RegisterTemplate(innerTemplate1)
            .RegisterTemplate(innerTemplate2);

            var token = JToken.FromObject(new object());

            var measurements = template.GetMeasurements(token);

            Assert.Empty(measurements);

            innerTemplate1.Received(1).GetMeasurements(Arg.Is <JToken>(t => t == token));
            innerTemplate2.Received(1).GetMeasurements(Arg.Is <JToken>(t => t == token));
        }
        protected override IContentTemplate BuildCollectionTemplate(JArray templateCollection, ICollection <TemplateError> errors)
        {
            EnsureArg.IsNotNull(templateCollection, nameof(templateCollection));
            EnsureArg.IsNotNull(errors, nameof(errors));

            var collectionTemplate = new CollectionContentTemplate();

            foreach (var token in templateCollection)
            {
                try
                {
                    var container       = token.ToObject <TemplateContainer>();
                    var createdTemplate = TemplateFactories.Evaluate(container);
                    collectionTemplate.RegisterTemplate(createdTemplate);
                }
                catch (InvalidTemplateException ex)
                {
                    errors.Add(new TemplateError(ex.Message, ex.GetLineInfo));
                }
                catch (JsonSerializationException ex)
                {
                    errors.Add(new TemplateError(ex.Message, new LineInfo()
                    {
                        LineNumber = ex.LineNumber, LinePosition = ex.LinePosition
                    }));
                }
                catch (AggregateException ex)
                {
                    foreach (var error in ex.ConvertExceptionToTemplateErrors())
                    {
                        errors.Add(error);
                    }
                }
            }

            return(collectionTemplate);
        }