From 45d7a6583d1b8342393c142cb2aad28904cf8d20 Mon Sep 17 00:00:00 2001 From: Joel Hendrix Date: Fri, 23 Jan 2015 08:09:59 -0800 Subject: [PATCH] Refactored resource writer test code per feedback. Removed hand-rolled checks for exception type in favor of xunit's Assert.Throws method. It makes the code easier to read and uncovered a bug in test ExceptionforResWriter05 that is now fixed. --- .../tests/ResourceWriterUnitTest.cs | 163 ++++++------------ 1 file changed, 54 insertions(+), 109 deletions(-) diff --git a/src/System.Resources.ResourceWriter/tests/ResourceWriterUnitTest.cs b/src/System.Resources.ResourceWriter/tests/ResourceWriterUnitTest.cs index 9ca22f86f8be..4777aa64cc4b 100644 --- a/src/System.Resources.ResourceWriter/tests/ResourceWriterUnitTest.cs +++ b/src/System.Resources.ResourceWriter/tests/ResourceWriterUnitTest.cs @@ -24,160 +24,105 @@ public class ResourceWriterTests [Fact] public static void ExceptionforResWriter01() { - MemoryStream ms2 = null; - - try - { - var rw = new ResourceWriter(ms2); - } - catch (Exception Ex) - { - if (Ex.GetType() != typeof(ArgumentNullException)) + Assert.Throws(() => { - Assert.True(false, string.Format("Expected {0} but got {1}", typeof(ArgumentNullException).ToString(), Ex.GetType().ToString())); - } - } + MemoryStream ms2 = null; + var rw = new ResourceWriter(ms2); + }); } [Fact] public static void ExceptionforResWriter02() { - byte[] buffer = new byte[_RefBuffer.Length]; - using (var ms2 = new MemoryStream(buffer, false)) - { - try + Assert.Throws(() => { - var rw = new ResourceWriter(ms2); - } - catch (Exception Ex) - { - if (Ex.GetType() != typeof(ArgumentException)) + byte[] buffer = new byte[_RefBuffer.Length]; + using (var ms2 = new MemoryStream(buffer, false)) { - Assert.True(false, string.Format("Expected {0} but got {1}", typeof(ArgumentException).ToString(), Ex.GetType().ToString())); + var rw = new ResourceWriter(ms2); } - } - } + }); } [Fact] public static void ExceptionforResWriter03() { - byte[] buffer = new byte[_RefBuffer.Length]; - using (var ms2 = new MemoryStream(buffer, true)) - { - var rw1 = new ResourceWriter(ms2); - try + Assert.Throws(() => { - rw1.AddResource(null, "args"); - } - catch (Exception Ex) - { - if (Ex.GetType() != typeof(ArgumentNullException)) - { - Assert.True(false, string.Format("Expected {0} but got {1}", typeof(ArgumentNullException).ToString(), Ex.GetType().ToString())); - } - } - finally - { - try - { - rw1.Dispose(); - } - catch (Exception Ex) + byte[] buffer = new byte[_RefBuffer.Length]; + using (var ms2 = new MemoryStream(buffer, true)) { - if (Ex.GetType() != typeof(System.ArgumentOutOfRangeException)) + var rw1 = new ResourceWriter(ms2); + try { - Assert.True(false, string.Format("Expected {0} but got {1}", typeof(System.ArgumentOutOfRangeException).ToString(), Ex.GetType().ToString())); + rw1.AddResource(null, "args"); + } + finally + { + Assert.Throws(() => + { + rw1.Dispose(); + }); } } - } - } + }); } [Fact] public static void ExceptionforResWriter04() { - byte[] buffer = new byte[_RefBuffer.Length]; - using (var ms2 = new MemoryStream(buffer, true)) - { - var rw1 = new ResourceWriter(ms2); - try + Assert.Throws(() => { - rw1.AddResource("key1", "args"); - rw1.AddResource("key1", "args"); - } - catch (Exception Ex) - { - if (Ex.GetType() != typeof(System.ArgumentException)) + byte[] buffer = new byte[_RefBuffer.Length]; + using (var ms2 = new MemoryStream(buffer, true)) { - Assert.True(false, string.Format("Expected {0} but got {1}", typeof(System.ArgumentException).ToString(), Ex.GetType().ToString())); + using (var rw1 = new ResourceWriter(ms2)) + { + rw1.AddResource("key1", "args"); + rw1.AddResource("key1", "args"); + } } - } - finally - { - rw1.Dispose(); - } - } + }); } [Fact] public static void ExceptionforResWriter05() { - byte[] buffer = new byte[_RefBuffer.Length]; - using (var ms2 = new MemoryStream(buffer, true)) - { - var rw1 = new ResourceWriter(ms2); - try - { - rw1.AddResource("key2", "args"); - } - catch (Exception Ex) + Assert.Throws(() => { - if (Ex.GetType() != typeof(InvalidOperationException)) + byte[] buffer = new byte[_RefBuffer.Length]; + using (var ms2 = new MemoryStream(buffer, true)) { - Assert.True(false, string.Format("Expected {0} but got {1}", typeof(InvalidOperationException).ToString(), Ex.GetType().ToString())); + var rw1 = new ResourceWriter(ms2); + rw1.AddResource("key2", "args"); + rw1.Dispose(); + rw1.AddResource("key2", "args"); } - } - finally - { - rw1.Dispose(); - } - } + }); } [Fact] public static void ExceptionforResWriter06() { - byte[] buffer = new byte[_RefBuffer.Length]; - using (var ms2 = new MemoryStream(buffer, true)) - { - var rw1 = new ResourceWriter(ms2); - try - { - rw1.Generate(); - } - catch (Exception Ex) - { - if (Ex.GetType() != typeof(ArgumentOutOfRangeException)) - { - Assert.True(false, string.Format("Expected {0} but got {1}", typeof(ArgumentOutOfRangeException).ToString(), Ex.GetType().ToString())); - } - } - finally + Assert.Throws(() => { - try - { - rw1.Dispose(); - } - catch (Exception Ex) + byte[] buffer = new byte[_RefBuffer.Length]; + using (var ms2 = new MemoryStream(buffer, true)) { - if (Ex.GetType() != typeof(System.NotSupportedException)) + var rw1 = new ResourceWriter(ms2); + try { - Assert.True(false, string.Format("Expected {0} but got {1}", typeof(System.NotSupportedException).ToString(), Ex.GetType().ToString())); + rw1.Generate(); + } + finally + { + Assert.Throws(() => + { + rw1.Dispose(); + }); } } - } - } + }); } [Fact]