// Copyright (c) 2007 Omer Rauchwerger (a.k.a rauchy) (omer@rauchy.net)
// All rights reserved.
//
// This file is part of Regionerate.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
using System;
using System.Collections.Generic;
using BestBrains.System;
using NUnit.Framework;
using Rauchy.Regionerate.ServiceLayer.Components.Tools;
using Rauchy.Regionerate.Shared.Components;
using Rauchy.Regionerate.Shared.Components.Rendering;
using Rhino.Mocks;
namespace Rauchy.Regionerate.ServiceLayer.Components.Tests
{
///
/// Tests the class.
///
[ TestFixture ]
public sealed class RegionUnpackerTests
{
#region Data Members (2)
private MockRepository _mockRepository;
private RegionUnpacker _regionUnpacker;
#endregion Data Members
#region Initialization (1)
[ SetUp ]
public void SetUp()
{
_mockRepository = new MockRepository();
// Create an "easy" symbol, one that will accept any string.
Symbol easySymbol = CreateEasySymbol();
// Instanciate a RegionUnpacker with the easy symbol.
_regionUnpacker = new RegionUnpacker( easySymbol, null );
}
#endregion Initialization
#region Finalization (1)
[ TearDown ]
public void TearDown()
{
_mockRepository.VerifyAll();
}
#endregion Finalization
#region Tests (13)
///
/// #region Pattern1
/// #region Pattern2
/// Code
/// #endregion
/// #endregion
///
[ Test ]
public void HandlesMultiplePatterns()
{
// Simulate the Unpack element (create a CodeLayout.UnpackOptions instance directly)
IList regionPatterns = new List();
regionPatterns.Add( "Pattern1" );
regionPatterns.Add( "Pattern2" );
CodeLayout.UnpackOptions unpackOptions = new CodeLayout.UnpackOptions( regionPatterns );
string code = CommentReader.GetElement( "code" );
// Unpack the code.
_regionUnpacker = new RegionUnpacker( null, unpackOptions );
string actual = _regionUnpacker.Unpack( code );
// Verify results.
Assert.AreEqual( "Code", actual.Trim() );
}
[ Test ]
[ ExpectedException( typeof ( ArgumentNullException ) ) ]
public void ShouldNotAcceptNullString()
{
using ( _mockRepository.Playback() )
{
_regionUnpacker.Unpack( null );
}
}
///
/// #region [abc] MyRegion
/// #endregion [abc]
///
[ Test ]
public void ShouldNotUnpackARegionWithDifferentSymbol()
{
Symbol mockedSymbol = _mockRepository.CreateMock();
using ( _mockRepository.Record() )
{
Expect.Call( mockedSymbol.IsEmbeddedIn( "#region [abc] MyRegion" ) ).Return( false );
Expect.Call( mockedSymbol.IsEmbeddedIn( "#endregion [abc]" ) ).Return( false );
}
string code = CommentReader.GetElement( "code" ).Trim();
_regionUnpacker = new RegionUnpacker( mockedSymbol, null );
using ( _mockRepository.Playback() )
{
string actual = _regionUnpacker.Unpack( code ).Trim();
Assert.AreEqual( code, actual );
}
}
/// public IDisposable Foo()
/// {
/// }
[ Test ]
public void ShouldNotUnpackNonRegionLinesThatMatchThePattern()
{
// Simulate the Unpack element (create a CodeLayout.UnpackOptions instance directly)
IList regionPatterns = new List();
regionPatterns.Add( "IDisposable" );
CodeLayout.UnpackOptions unpackOptions = new CodeLayout.UnpackOptions( regionPatterns );
string code = CommentReader.GetElement( "code" );
// Unpack the code.
_regionUnpacker = new RegionUnpacker( null, unpackOptions );
string actual = _regionUnpacker.Unpack( code );
// Verify results.
Assert.AreEqual( code.Trim(), actual.Trim() );
}
/// // [rgn] Region
[ Test ]
public void ShouldRemoveCommentRegion()
{
Symbol mockedSymbol = _mockRepository.CreateMock();
using ( _mockRepository.Record() )
{
Expect.Call( mockedSymbol.IsEmbeddedIn( "// [rgn] Region" ) ).Return( true );
}
string code = CommentReader.GetElement( "code" );
_regionUnpacker = new RegionUnpacker( mockedSymbol, null );
using ( _mockRepository.Playback() )
{
string actual = _regionUnpacker.Unpack( code ).Trim();
Assert.IsEmpty( actual );
}
}
///
/// #region [rgn] Region1
/// Code #1
/// #endregion [rgn]
///
/// #region [rgn] Region2
/// Code #2
/// #endregion [rgn]
///
/// Code #1
///
/// Code #2
[ Test ]
public void ShouldRemoveMultipleRegions()
{
Symbol mockedSymbol = _mockRepository.CreateMock();
using ( _mockRepository.Record() )
{
Expect.Call( mockedSymbol.IsEmbeddedIn( "#region [rgn] Region1" ) ).Return( true );
Expect.Call( mockedSymbol.IsEmbeddedIn( "Code #1" ) ).Return( false );
Expect.Call( mockedSymbol.IsEmbeddedIn( "#endregion [rgn]" ) ).Return( true );
Expect.Call( mockedSymbol.IsEmbeddedIn( "" ) ).Return( false );
Expect.Call( mockedSymbol.IsEmbeddedIn( "#region [rgn] Region2" ) ).Return( true );
Expect.Call( mockedSymbol.IsEmbeddedIn( "Code #2" ) ).Return( false );
Expect.Call( mockedSymbol.IsEmbeddedIn( "#endregion [rgn]" ) ).Return( true );
}
string code = CommentReader.GetElement( "code" ).Trim();
string expected = CommentReader.GetElement( "expected" );
_regionUnpacker = new RegionUnpacker( mockedSymbol, null );
using ( _mockRepository.Playback() )
{
string actual = _regionUnpacker.Unpack( code ).Trim();
Assert.AreEqual( expected, actual );
}
}
/// #region Outer Region
/// Outer region code
/// #region Inner Region
/// Inner region code
/// #endregion Inner Region
/// #endregion Outer Region
/// Outer region code
/// Inner region code
[ Test ]
public void ShouldRemoveTwoNestedRegions()
{
// Set up a mocked symbol that accepts the outer & inner region declarations.
Symbol mockedSymbol = _mockRepository.CreateMock();
using ( _mockRepository.Record() )
{
Expect.Call( mockedSymbol.IsEmbeddedIn( "#region Outer Region" ) ).Return( true );
Expect.Call( mockedSymbol.IsEmbeddedIn( "Outer region code" ) ).Return( false );
Expect.Call( mockedSymbol.IsEmbeddedIn( "#region Inner Region" ) ).Return( true );
Expect.Call( mockedSymbol.IsEmbeddedIn( "Inner region code" ) ).Return( false );
Expect.Call( mockedSymbol.IsEmbeddedIn( "#endregion Inner Region" ) ).Return( true );
Expect.Call( mockedSymbol.IsEmbeddedIn( "#endregion Outer Region" ) ).Return( true );
}
// Inject the RegionUnpacker with the mocked symbol and let her rip.
_regionUnpacker = new RegionUnpacker( mockedSymbol, null );
string code = CommentReader.GetElement( "code" );
string expected = CommentReader.GetElement( "expected" );
using ( _mockRepository.Playback() )
{
string actual = _regionUnpacker.Unpack( code ).Trim();
// Verify results
Assert.AreEqual( expected, actual );
}
}
[ Test ]
public void ShouldReturnEmptyOnEmpty()
{
string actual = _regionUnpacker.Unpack( string.Empty );
Assert.IsEmpty( actual );
}
///
/// #region [rgn] MyRegion
/// #endregion [rgn]
///
[ Test ]
public void ShouldUnpackASingleEmptyRegion()
{
Symbol mockedSymbol = _mockRepository.CreateMock();
using ( _mockRepository.Record() )
{
Expect.Call( mockedSymbol.IsEmbeddedIn( "#region [rgn] MyRegion" ) ).Return( true );
Expect.Call( mockedSymbol.IsEmbeddedIn( "#endregion [rgn]" ) ).Return( true );
}
string code = CommentReader.GetElement( "code" ).Trim();
_regionUnpacker = new RegionUnpacker( mockedSymbol, null );
using ( _mockRepository.Playback() )
{
string actual = _regionUnpacker.Unpack( code ).Trim();
Assert.IsEmpty( actual );
}
}
///
/// #region [rgn] MyRegion
/// This is my code
/// #endregion [rgn]
///
/// This is my code
[ Test ]
public void ShouldUnpackASingleRegion()
{
Symbol mockedSymbol = _mockRepository.CreateMock();
using ( _mockRepository.Record() )
{
Expect.Call( mockedSymbol.IsEmbeddedIn( "#region [rgn] MyRegion" ) ).Return( true );
Expect.Call( mockedSymbol.IsEmbeddedIn( "This is my code" ) ).Return( false );
Expect.Call( mockedSymbol.IsEmbeddedIn( "#endregion [rgn]" ) ).Return( true );
}
string code = CommentReader.GetElement( "code" ).Trim();
string expected = CommentReader.GetElement( "expected" );
_regionUnpacker = new RegionUnpacker( mockedSymbol, null );
using ( _mockRepository.Playback() )
{
string actual = _regionUnpacker.Unpack( code ).Trim();
Assert.AreEqual( expected, actual );
}
}
///
/// #region [rgn] MyRegion
/// #endregion [rgn]
///
[ Test ]
public void ShouldUnpackASingleTabbedEmptyRegion()
{
Symbol mockedSymbol = _mockRepository.CreateMock();
using ( _mockRepository.Record() )
{
Expect.Call( mockedSymbol.IsEmbeddedIn( "#region [rgn] MyRegion" ) ).Return( true );
Expect.Call( mockedSymbol.IsEmbeddedIn( "#endregion [rgn]" ) ).Return( true );
}
string code = CommentReader.GetElement( "code" ).Trim();
_regionUnpacker = new RegionUnpacker( mockedSymbol, null );
using ( _mockRepository.Playback() )
{
string actual = _regionUnpacker.Unpack( code ).Trim();
Assert.IsEmpty( actual );
}
}
///
/// Makes sure that region blocks (which are not symboled, meaning are not created by Regionerate)
/// can be removed using the Unpack element of the Code Layout.
///
///
/// In this test, we simulate an Unpack element that orders to
/// unpack any regions that contain the word "IDisposable" in them.
///
/// #region IDisposable
/// public void Dispose() {}
/// #endregion
[ Test ]
public void ShouldUnpackByPattern()
{
// Simulate the Unpack element (create a CodeLayout.UnpackOptions instance directly)
IList regionPatterns = new List();
regionPatterns.Add( "IDisposable" );
CodeLayout.UnpackOptions unpackOptions = new CodeLayout.UnpackOptions( regionPatterns );
string code = CommentReader.GetElement( "code" );
// Unpack the code.
_regionUnpacker = new RegionUnpacker( null, unpackOptions );
string actual = _regionUnpacker.Unpack( code );
// Verify results.
Assert.AreEqual( "public void Dispose() {}", actual.Trim() );
}
///
/// Makes sure that outer region blocks (which are not symboled, meaning are not created by Regionerate)
/// can be removed using the Unpack element of the Code Layout.
///
///
/// In this test, we simulate an Unpack element that orders to
/// unpack any regions that contain the word "IDisposable" in them. Inside the IDisposable region,
/// we place an IComparable region, which should not be removed.
///
/// #region IDisposable
/// public void Dispose() {};
/// #region IComparable
/// void Foo() {};
/// #endregion
/// #endregion
/// public void Dispose() {};
/// #region IComparable
/// void Foo() {};
/// #endregion
[ Test ]
public void ShouldUnpackOuterRegionsByPattern()
{
// Simulate the Unpack element (create a CodeLayout.UnpackOptions instance directly)
IList regionPatterns = new List();
regionPatterns.Add( "IDisposable" );
CodeLayout.UnpackOptions unpackOptions = new CodeLayout.UnpackOptions( regionPatterns );
string code = CommentReader.GetElement( "code" );
// Unpack the code.
_regionUnpacker = new RegionUnpacker( null, unpackOptions );
string actual = _regionUnpacker.Unpack( code );
// Verify results.
string expected = CommentReader.GetElement( "expected" );
Assert.AreEqual( expected, actual.Trim() );
}
#endregion Tests
#region Helper Methods (1)
///
/// Creates an "easy" symbol, one that will accept any string.
///
///
private Symbol CreateEasySymbol()
{
Symbol easySymbol = _mockRepository.CreateMock();
using ( _mockRepository.Record() ) {}
return easySymbol;
}
#endregion Helper Methods
}
}