// ----------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. // ----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.ComponentModel.Composition.Factories; using System.ComponentModel.Composition.Hosting; using System.ComponentModel.Composition.Primitives; using System.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace System { [TestClass] public class LazyOfTMetadataTests { public class MetadataView { } [TestMethod] public void Constructor1_MetadataViewSet() { MetadataView metadataView = new MetadataView(); var export = new Lazy(() => "Value", metadataView, false); Assert.AreEqual(metadataView, export.Metadata); } [TestMethod] public void Constructor1_MetadataViewSetToNull() { MetadataView metadataView = new MetadataView(); var export = new Lazy(() => "Value", null, false); Assert.IsNull(export.Metadata); } [TestMethod] public void Constructor1_NullAsExportedValueGetterArgument_ShouldThrowArgumentNull() { ExceptionAssert.ThrowsArgument("valueFactory", () => { new Lazy((Func)null, new MetadataView(), false); }); } [TestMethod] public void Constructor1_FuncReturningAStringAsExportedValueGetter_ShouldBeReturnedByGetExportedValue() { var export = new Lazy(() => "Value", new MetadataView(), false); Assert.AreEqual("Value", export.Value); } [TestMethod] public void Constructor1_FuncReturningNullAsExportedValueGetter_ShouldBeReturnedByGetExportedValue() { var export = new Lazy(() => null, new MetadataView(), false); Assert.IsNull(export.Value); } [TestMethod] public void Value_ShouldCacheExportedValueGetter() { int count = 0; var export = new Lazy(() => { count++; return count; }, new MetadataView(), false); Assert.AreEqual(1, export.Value); Assert.AreEqual(1, export.Value); Assert.AreEqual(1, export.Value); } [TestMethod] public void Constructor2_MetadataSet() { MetadataView metadataView = new MetadataView(); var export = new Lazy(metadataView, false); Assert.AreSame(metadataView, export.Metadata); Assert.IsNotNull(export.Value); } #if CLR40 [TestMethod] public void Constructor3_MetadataSet() { MetadataView metadataView = new MetadataView(); var export = new Lazy(metadataView, true); Assert.AreSame(metadataView, export.Metadata); Assert.IsNotNull(export.Value); } [TestMethod] public void Constructor4_MetadataSet() { MetadataView metadataView = new MetadataView(); var export = new Lazy(() => "Value", metadataView, true); Assert.AreSame(metadataView, export.Metadata); Assert.IsNotNull(export.Value); } #endif } }