LazyOfTTMetadataTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel.Composition.Factories;
  7. using System.ComponentModel.Composition.Hosting;
  8. using System.ComponentModel.Composition.Primitives;
  9. using System.UnitTesting;
  10. using Microsoft.VisualStudio.TestTools.UnitTesting;
  11. namespace System
  12. {
  13. [TestClass]
  14. public class LazyOfTMetadataTests
  15. {
  16. public class MetadataView
  17. {
  18. }
  19. [TestMethod]
  20. public void Constructor1_MetadataViewSet()
  21. {
  22. MetadataView metadataView = new MetadataView();
  23. var export = new Lazy<string, MetadataView>(() => "Value", metadataView, false);
  24. Assert.AreEqual(metadataView, export.Metadata);
  25. }
  26. [TestMethod]
  27. public void Constructor1_MetadataViewSetToNull()
  28. {
  29. MetadataView metadataView = new MetadataView();
  30. var export = new Lazy<string, MetadataView>(() => "Value", null, false);
  31. Assert.IsNull(export.Metadata);
  32. }
  33. [TestMethod]
  34. public void Constructor1_NullAsExportedValueGetterArgument_ShouldThrowArgumentNull()
  35. {
  36. ExceptionAssert.ThrowsArgument<ArgumentNullException>("valueFactory", () =>
  37. {
  38. new Lazy<string, MetadataView>((Func<string>)null, new MetadataView(), false);
  39. });
  40. }
  41. [TestMethod]
  42. public void Constructor1_FuncReturningAStringAsExportedValueGetter_ShouldBeReturnedByGetExportedValue()
  43. {
  44. var export = new Lazy<string, MetadataView>(() => "Value", new MetadataView(), false);
  45. Assert.AreEqual("Value", export.Value);
  46. }
  47. [TestMethod]
  48. public void Constructor1_FuncReturningNullAsExportedValueGetter_ShouldBeReturnedByGetExportedValue()
  49. {
  50. var export = new Lazy<string, MetadataView>(() => null, new MetadataView(), false);
  51. Assert.IsNull(export.Value);
  52. }
  53. [TestMethod]
  54. public void Value_ShouldCacheExportedValueGetter()
  55. {
  56. int count = 0;
  57. var export = new Lazy<int, MetadataView>(() =>
  58. {
  59. count++;
  60. return count;
  61. }, new MetadataView(), false);
  62. Assert.AreEqual(1, export.Value);
  63. Assert.AreEqual(1, export.Value);
  64. Assert.AreEqual(1, export.Value);
  65. }
  66. [TestMethod]
  67. public void Constructor2_MetadataSet()
  68. {
  69. MetadataView metadataView = new MetadataView();
  70. var export = new Lazy<object, MetadataView>(metadataView, false);
  71. Assert.AreSame(metadataView, export.Metadata);
  72. Assert.IsNotNull(export.Value);
  73. }
  74. #if CLR40
  75. [TestMethod]
  76. public void Constructor3_MetadataSet()
  77. {
  78. MetadataView metadataView = new MetadataView();
  79. var export = new Lazy<object, MetadataView>(metadataView, true);
  80. Assert.AreSame(metadataView, export.Metadata);
  81. Assert.IsNotNull(export.Value);
  82. }
  83. [TestMethod]
  84. public void Constructor4_MetadataSet()
  85. {
  86. MetadataView metadataView = new MetadataView();
  87. var export = new Lazy<string, MetadataView>(() => "Value",
  88. metadataView, true);
  89. Assert.AreSame(metadataView, export.Metadata);
  90. Assert.IsNotNull(export.Value);
  91. }
  92. #endif
  93. }
  94. }